<?php
# Baca Variabel Form
$message = array();
$fromdate	 = $start_date;
$todate      = $end_date;
$Act	 	 = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det 	 = isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$wo_id 	 	 = isset($_REQUEST['wo_id']) ? $_REQUEST['wo_id'] : '';
$wo_date 	 = isset($_REQUEST['wo_date']) ? $_REQUEST['wo_date'] : '';
$wo_lotno 	 = isset($_REQUEST['wo_lotno']) ? $_REQUEST['wo_lotno'] : '';
$wo_prodid 	 = isset($_REQUEST['wo_prodid']) ? $_REQUEST['wo_prodid'] : ''; 
$wo_deptid	 = isset($_REQUEST['wo_deptid']) ? $_REQUEST['wo_deptid'] : '';
$wo_pic  	 = isset($_REQUEST['wo_pic']) ? $_REQUEST['wo_pic'] : '';
$wo_desc  	 = isset($_REQUEST['wo_desc']) ? $_REQUEST['wo_desc'] : '';
$wo_qty  	 = isset($_REQUEST['wo_qty']) ? $_REQUEST['wo_qty'] : '';
$id_det		 = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth	 = isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
$id_temp	 = isset($_REQUEST['id_temp']) ? $_REQUEST['id_temp'] : '';
$bom_id	 	 = isset($_REQUEST['bom_id']) ? $_REQUEST['bom_id'] : '';
$wo_price 	 = isset($_REQUEST['wo_price']) ? $_REQUEST['wo_price'] : '';
$wo_prodname = isset($_REQUEST['wo_prodname']) ? $_REQUEST['wo_prodname'] : '';
$val_date = get_validationdate($wo_date);
$tahun       = substr($wo_date,0,4);
$bulan		 = substr($wo_date,5,2);
if(empty($wo_id)){
	$wo_no	 = get_wo($bulan,$tahun);
}else{
	$wo_no	 = isset($_REQUEST['wo_no']) ? $_REQUEST['wo_no'] : '';
}

if($Act =="GET_BOM"){
	$bom = mysql_fetch_array(mysql_query("SELECT id FROM bom WHERE bomh_prodid='$wo_prodid' AND bomh_level='FG' AND bomh_status=10"));
	$bom_id = $bom['id'];
	#reload
	require_once "add_workorderfg.php";
}
if($Act =="SAVE"){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_workorderfg.php";
		exit;
	}elseif(empty($wo_prodid) || $wo_prodid==0){
		echo "<font color='red'>Kode barang jadi atau wip tidak boleh kosong</font><br>";
		#reload
		require_once "add_workorderfg.php";
		exit;
	}else{
		if(count($message)==0){	
			if(empty($wo_id)){
				$sts=0;
				foreach($id_det as $i => $id){
					$prod_code	= $_REQUEST['prod_code'][$id];
					$wod_status	= $_REQUEST['wod_status'][$id];
					if($wod_status=="KURANG"){
						echo "<font color='red'>kode barang :$prod_code stocknya kurang.</font><br>";
						$sts++;					
					}
				}
				if($sts>0){
					#reload
					require_once "add_workorderfg.php";
					exit;
				}
				$qrySave=mysql_query("INSERT INTO workorder SET wo_no='$wo_no', wo_date='$wo_date', wo_lotno='$wo_lotno', wo_bomid='$bom_id',
								  wo_prodid='$wo_prodid', wo_deptid='$wo_deptid', wo_pic='$wo_pic', wo_qty='$wo_qty', wo_level='Level FG',
								  wo_desc='$wo_desc', wo_inputby='$user_id',wo_inputdate='$datetime', wo_status=20, wo_price='$wo_price'") or die ("Gagal query".mysql_error());
				if($qrySave){
					$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM workorder"));
					$wo_id = $data->id;
					foreach($id_det as $i => $id){
						$wod_prodid	= $_REQUEST['wod_prodid'][$id];
						$wod_qty	= $_REQUEST['wod_qty'][$id];
						$wod_price_satuan	= $_REQUEST['wod_price_satuan'][$id];
						mysql_query("INSERT INTO workorder_detail SET wod_prodid='$wod_prodid', wod_qty='$wod_qty', wod_hid='$wo_id', wod_price_satuan='$wod_price_satuan' ");
					}					
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi workorder No. $wo_no', user_date='$date', user_time='$time'");	
				}
			}else{
				$qrySave=mysql_query("UPDATE workorder SET wo_no='$wo_no', wo_date='$wo_date', wo_lotno='$wo_lotno', wo_qty='$wo_qty', wo_pic='$wo_pic', wo_desc='$wo_desc', wo_price='$wo_price', wo_status=20 WHERE id='$wo_id'") 
				or die ("Gagal query".mysql_error());
				if($qrySave){
					foreach($id_det as $i => $id){
						$wod_prodid	= $_REQUEST['wod_prodid'][$id];
						$wod_qty	= $_REQUEST['wod_qty'][$id];
						$wod_price_satuan	= $_REQUEST['wod_price_satuan'][$id];
						mysql_query("UPDATE workorder_detail SET wod_qty='$wod_qty', wod_price_satuan='$wod_price_satuan' WHERE wod_hid='$wo_id' AND wod_prodid='$wod_prodid'");
					}
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah transaksi workorder No. $wo_no', user_date='$date', user_time='$time'");
				}							  
			}				
			if($qrySave){
				$message[] = "Data Sukses tersimpan !";	
				$status = "OK";
			}
		}	
	}
}
if($Act =="DETAIL"){
	$dd = mysql_fetch_object(mysql_query("SELECT a.*, CONCAT_WS(' - ', b.prod_code, b.prod_name) codename FROM workorder a 
	      INNER JOIN product b ON b.id=a.wo_prodid WHERE a.id='$wo_id'"));		
	$wo_id 		= $dd->id;			
	$wo_no		= $dd->wo_no;
	$wo_date	= $dd->wo_date;
	$wo_lotno 	= $dd->wo_lotno;
	$wo_prodid	= $dd->wo_prodid;
	$wo_deptid	= $dd->wo_deptid;
	$wo_pic   	= $dd->wo_pic;
	$wo_desc	= $dd->wo_desc;
	$wo_qty		= $dd->wo_qty;
	$bom_id		= $dd->wo_bomid;
	$wo_prodname = $dd->codename;
}
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE workorder SET wo_status=100 WHERE id='$wo_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus transaksi workorder No. $wo_no', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}
}
if($Act == "POSTING"){
	#Update Status
	$qd = mysql_query("UPDATE workorder SET wo_status=20 WHERE id='$wo_id'");
	#Insert History User
	$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting transaksi workorder No. $wo_no', user_date='$date', user_time='$time'");
	$message[] = "Data Sukses diposting !";	
	$status = "OK";	
}
if($Act == "ADD" || $Act == "DETAIL"){
	require_once "add_workorderfg.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Work-OrderFG'>";
}
if (! count($message)==0 ){
	?>
		<script language="javascript">
			var pesan ='<?php 
							$Num=0;
							foreach ($message as $indeks=>$pesan_tampil) { 
								$Num++;
								echo $Num.'. '.$pesan_tampil.'\n';
							}
						?>';
			alert(pesan);
		</script>
	<?php  
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Work-OrderFG'>";	
		exit;
	}
}
?>
