<?php
      if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
			case "transno":
              if(RetRstField("SELECT fnid FROM txtracking_hdr WHERE fttracking_code='{$_REQUEST['fttrans_no']}'")) {
                  ?>
                  alert('Kode aktivitas sudah dipakai...');
                  OnChangeDate_fdtrans_date();
            <?php
              }
              else {
                    ?>


                  submitbutton('Simpan','','','','');

            <?php
              }
            break;

				
            
        }
        
    }

    Getsecurity('Tracking');

	//KONDISI VIEW PERUSER ====================================================================================
    $ssql="select ftusername,ftuser_group_code from tluser where ftusername='{$_SESSION['username']}'";
    $rs=mysql_query($ssql) or die($ssql);
    $r=mysql_fetch_assoc($rs);
    
    if($r['ftuser_group_code']=='administrator'){
        $where="where a.ftstatus_customer=2";
    }else if($r['ftuser_group_code']!='administrator') {
        $where="LEFT JOIN tluser c ON a.ftusername = c.ftusername WHERE  a.ftstatus_customer=2 and c.ftusername='{$r['ftusername']}' or a.ftsupport_code = '{$r['ftusername']}'";
    }else {
        $where="";
    }
	
    $objAxpDB->ViewName("Daftar Tracking");
    $objAxpDB->ViewSQL("SELECT a.fnid, fttracking_code,a.fdtrans_date,g.ftproject_name,a.ftproject_category_name,a.ftket_customer,a.ftket_internal,e.fttrans_status_name,a.ftsupport_code,a.ftusername,a.fdproses_date,a.fdend_date,a.fdqc_date,a.fdselesai_date,f.fttrans_status_name AS status_kunci FROM txtracking_det a
	LEFT JOIN tstransaction_status e ON a.fnstatus = e.fntrans_status_code AND e.fntrans_group_code = 71 
    LEFT JOIN tstransaction_status f ON a.ftstatus_customer = f.fntrans_status_code AND f.fntrans_group_code = 72
	LEFT JOIN tlproject g ON a.ftproject_code = g.ftproject_code {$where} order by a.ftstatus_customer,a.fnstatus ASC,a.fdtrans_date ASC");
    $objAxpDB->ViewCaption("fnid,No Tracking,Tanggal,Nama Project,Category,Keterangan,Catatan,Status,Dilaporkan Oleh,Dikerjakan Oleh,Tanggal Mulai Proses,Tanggal Selesai Proses,Tanggal QC,Tanggal Selesai,Konfirmasi Customer");
	$objAxpDB->ViewPeriod(TRUE);
	//KONDISI VIEW PERUSER ====================================================================================
 
    
	
	
	if ($_REQUEST['id'] == 0) {
		
   //= HEADER FORM INPUT START ====================================================================================
/*     $objAxpDB->FormName("Project Tracking");
    $objAxpDB->FormSQL("SELECT ftproject_code FROM tlproject");
    $objAxpDB->FormCaption("Project");
    $objAxpDB->FormPosition("TL,TL"); */
	$objAxpDB->FormName("Project Tracking");
	$objAxpDB->FormSQL("SELECT  fttracking_code,ftproject_code,fdtrans_date,ftsupport_code,fntype FROM txtracking_hdr");
	$objAxpDB->FormCaption("No Tracking,Project,Tanggal,User,Type Input");
	$objAxpDB->SetStatus("id");
	$objAxpDB->FormPosition("TL,TL,TR,TR,HD");	
	//= HEADER FORM INPUT END ====================================================================================
	
/*     $objAxpDB->Lookup("ftproject_code"
			, "SELECT a.ftproject_code,a.ftproject_name FROM tlproject a "
			, "ftproject_code"
			, "ftproject_name", -1,true);

 */	
    $ssql3="select ftusername,ftuser_group_code from tluser where ftusername='{$_SESSION['username']}'";
    $rs3=mysql_query($ssql3) or die($ssql3);
    $r3=mysql_fetch_assoc($rs3);
    
    if($r['ftuser_group_code']=='administrator'){
		$or = "OR a.ftuser_project!='{$_SESSION['username']}' group by a.ftproject_code";
	}
	else{$or ="";}
		
	$objAxpDB->Lookup("ftproject_code"
			, "SELECT a.ftproject_code,b.ftproject_name FROM tlproject_user a left join tlproject b on a.ftproject_code=b.ftproject_code WHERE a.ftuser_project='{$_SESSION['username']}' {$or}"
			, "ftproject_code"
			, "ftproject_name", -1,true);				
	
	//= DETAIL FORM INPUT START ==================================================================================
    $ajaxGrid="ajax_do_grid0('tracking.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftproject_code='+ document.getElementById('ftproject_code').value);";
    $objAxpDB->FormScript(1,"onChange=\"{$ajaxGrid}\"");
	
	
	
	
	
	$ssql="SELECT fttracking_code FROM txtracking_hdr WHERE fnid='{$_REQUEST['id']}'";
	$rs=mysql_query($ssql);
	$r=mysql_fetch_assoc($rs);
	$objAxpDB->GridSQL(0, "SELECT fnindex,ftket_customer,ftket_internal,fnstatus,ftusername FROM txtracking_det WHERE fttracking_code='{$r['fttracking_code']}' #");
	$objAxpDB->GridCaption(0, "No.,Keterangan,Catatan,Status,Dikerjakan Oleh");
		

			$objAxpDB->Lookup("ftsupport_code"
			, "SELECT ftusername, ftfirst_name FROM tluser where ftusername = '{$_SESSION['username']}'"
			, "ftusername", "ftfirst_name", 0,true);
		
/* 			$objAxpDB->Lookup("ftusername"
			, "SELECT ftusername, ftfirst_name FROM tluser where ftuser_group_code='support'"
			, "ftusername"
			, "ftfirst_name", 0,true); */
			$objAxpDB->Lookup("fnstatus", 
			"SELECT fntrans_status_code,fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 71", 
			"fntrans_status_code", 
			"fttrans_status_name", 0,true);
			$objAxpDB->Lookup("ftstatus_customer", 
			"SELECT fntrans_status_code,fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 72", 
			"fntrans_status_code", 
			"fttrans_status_name", 0,true);		
		$objAxpDB->GridAllowAddNew(0, false);
		$objAxpDB->GridAllowDelete(0, false);
		$objAxpDB->GridAutoAdd(0, false);
	}
	else
		{
		// FORM EDIT ============================================================================
		$ssqlp="SELECT ftproject_code,ftimg_bugs FROM  txtracking_det WHERE fnid='{$_REQUEST['id']}'";
		$rsp=mysql_query($ssqlp) or die($ssqlp);
		$rp=mysql_fetch_assoc($rsp);
		
		
		$ssqlx="SELECT COUNT(b.`ftproject_code`) AS jd FROM  tlproject_category b WHERE ftproject_code='{$rp['ftproject_code']}'";
		$rsx=mysql_query($ssqlx) or die($ssqlx);
		$rx=mysql_fetch_assoc($rsx);
		if($rx['jd']!=0){
			$categorysql=',ftproject_category_name';
			$categorycaption=',Category';
			$categoryTL=',TL';
		}else{
			/* aktifkan ini untuk kondisi edit form apabila ada category hidden
 			$categorysql='';
			$categorycaption='';
			$categoryTL='';		 */
			$categorysql=',ftproject_category_name';
			$categorycaption=',Category';
			$categoryTL=',TL';			
		}
		
		
		
		$objAxpDB->FormName("Tracking");
		$objAxpDB->FormSQL("SELECT fnid,fttracking_code,fdtrans_date,ftproject_code {$categorysql},ftket_customer,ftket_internal,fnstatus,ftsupport_code,ftusername,fdproses_date,fdend_date,fdqc_date,fdselesai_date,ftstatus_customer,ftimg_bugs FROM txtracking_det");
		$objAxpDB->FormCaption("ID,No. Tracking,Tanggal,Project {$categorycaption},Keterangan,Catatan,Status,Dilaporkan Oleh,Dikerjakan Oleh,Tanggal Mulai Proses,Tanggal Selesai Proses,Tanggal QC,Tanggal Selesai,Konfirmasi Customer,gambar");
		
		$objAxpDB->FormPosition("HD,TL,TL{$categoryTL},TL,TL,TL,TL,TR,TR,TR,TR,TR,TR,TR,TR");
		
			if($rp['ftimg_bugs']=="" or $rp['ftimg_bugs']==null){
 					$strAutoNumber0="   
											 
											document.getElementById('DivCtl_ftimg_bugs').innerHTML=\"<table colspacing=0 colpadding=0><tr><td><img src='employee_photo/"."blank".".jpg' width='180' height='200'></td></tr><tr><td></td></tr></table>\";

											 "; 												 
			}
			else{
/* 					$strAutoNumber0="   
											 
											 document.getElementById('DivCtl_ftimg_bugs').innerHTML=\"<table colspacing=0 colpadding=0><tr><td><img src='images/img_bugs/{$rp['ftimg_bugs']}' width='180' height='200'></td></tr><tr><td></td></tr></table>\";

											 ";	
 */
					$strAutoNumber0="   
											 
											 document.getElementById('DivCtl_ftimg_bugs').innerHTML=\"<table colspacing=0 colpadding=0><tr><td><a href='images/img_bugs/{$rp['ftimg_bugs']}' title='image bugs' data-gallery=''><img width='220px' src='images/img_bugs/{$rp['ftimg_bugs']}'></a><input type='hidden' name='ftimg_bugs' value='{$rp['ftimg_bugs']}'><div id='blueimp-gallery' class='blueimp-gallery'><div class='slides'></div><h3 class='title'></h3><a class='prev'>‹</a><a class='next'>›</a><a class='close'>×</a><a class='play-pause'></a><ol class='indicator'></ol></div></td></tr><tr><td></td></tr></table>\";

											 ";	

											 }								 
								 
/* 	                         
 */							
            $objAxpDB->FormJS($strAutoNumber0);
			
			$objAxpDB->Lookup("ftsupport_code"
			, "SELECT ftusername, ftfirst_name FROM tluser "
			, "ftusername"
			, "ftfirst_name", 0,true);
			
			$objAxpDB->Lookup("ftproject_code"
			, "SELECT ftproject_code, ftproject_name FROM tlproject"
			, "ftproject_code"
			, "ftproject_name", -1,true);		
			
			$objAxpDB->Lookup("ftusername"
			, "SELECT ftusername, ftfirst_name FROM tluser where ftuser_group_code='support'"
			, "ftusername"
			, "ftfirst_name", -1,true);
			
			$objAxpDB->Lookup("fnstatus", 
			"SELECT fntrans_status_code,fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 71", 
			"fntrans_status_code", 
			"fttrans_status_name", 1,true);
			$objAxpDB->Lookup("ftstatus_customer", 
			"SELECT fntrans_status_code,fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 72", 
			"fntrans_status_code", 
			"fttrans_status_name", 0,true);
		}
	

    if(!$_REQUEST['id']) {
        $strAutoNumber1=" ajax_do('query2.php?code=2&ftbranch_code={$_SESSION['strbranch']}&ftmodule_code=tr01&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=fttracking_code&no_trans=fttracking_code&table_name=txtracking_det');";
        $AutoNumber0="function OnChangeDate_fdtrans_date(){
                {$strAutoNumber1}
            }
            document.getElementById('ftsupport_code').value='{$_SESSION['username']}';
            OnChangeDate_fdtrans_date();";
        $objAxpDB->FormJS($AutoNumber0);
    }	
    $objAxpDB->SetStatus("id");
    
 

    if ($objAxpDB->GetStatus("id") == ""){
		//kondisi peruser
		$ssql="select ftusername,ftuser_group_code from tluser where ftusername='{$_SESSION['username']}'";
		$rs=mysql_query($ssql) or die($ssql);
		$r=mysql_fetch_assoc($rs);
		
			$objAxpDB->ButtonShow("cetak_daftar , tutup");

   } elseif ($objAxpDB->GetStatus("id") == 0) {
            //$objAxpDB->GridAllowAddNew(0,true);
            //$objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->FormLocked("1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1");
            $objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->ButtonShow("reset, simpan, tutup");
    }
    elseif ($objAxpDB->GetStatus("fnstatus") == 0) {
			

						$objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
						$objAxpDB->ButtonShow("tutup");						
			
				
			
    }

    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();