<?php
# Baca Variabel Form
$message = array();
$Act		= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$id			= isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
$flag		= isset($_REQUEST['flag']) ? $_REQUEST['flag'] : '';
$sumber		= isset($_REQUEST['sumber']) ? $_REQUEST['sumber'] : '';
$idheader	= isset($_REQUEST['idheader']) ? $_REQUEST['idheader'] : '';
$no_aju		= isset($_REQUEST['no_aju']) ? $_REQUEST['no_aju'] : '';
$trf_spbno	= isset($_REQUEST['trf_spbno']) ? $_REQUEST['trf_spbno'] : '';
$kv_no	    = isset($_REQUEST['kv_no']) ? $_REQUEST['kv_no'] : '';
$type		= isset($_REQUEST['type']) ? $_REQUEST['type'] : '';
$jenis_dokpab	= isset($_REQUEST['jenis_dokpab']) ? $_REQUEST['jenis_dokpab'] : '';
$no_dokpab		= isset($_REQUEST['no_dokpab']) ? $_REQUEST['no_dokpab'] : '';
$jtipe			= isset($_REQUEST['jtipe']) ? $_REQUEST['jtipe'] : '';

if ($Act == "UNPOST") {
	if (count($message) == 0) {
		if ($flag == 10) { #incoming
			if ($sumber == "CIESA") { #dari CIESA
				#Hapus Header
				mysql_query("DELETE FROM incoming WHERE id='$id'");
				#Hapus Detail
				mysql_query("DELETE FROM incoming_detail WHERE incd_hid='$id'");
				#Update log incoming
				mysql_query("UPDATE log_incoming SET log_status=1 WHERE id_header='$idheader'");
			} elseif ($sumber == "UPLOAD" || $sumber == "UPL40") { #dari Upload
				#Hapus Header
				mysql_query("DELETE FROM incoming WHERE id='$id'");
				#Hapus Detail
				mysql_query("DELETE FROM incoming_detail WHERE incd_hid='$id'");
				#Update log incoming
				if ($sumber == "UPLOAD") {
					mysql_query("UPDATE log_incoming SET log_status=1000 WHERE jenis_dokpab='$jenis_dokpab' AND no_dokpab='$no_dokpab' AND log_status=3000 AND no_aju='$no_aju'");
				} else {
					mysql_query("UPDATE log_incoming SET log_status=86 WHERE jenis_dokpab='$jenis_dokpab' AND no_dokpab='$no_dokpab' AND log_status=88 AND no_aju='$no_aju'");
				}
			} else { #Manual
				#Update status
				mysql_query("UPDATE incoming SET inch_status=10 WHERE id='$id'");
			}
		} else { #outgoing
			if ($sumber == "CIESA") { #dari CIESA
				#Hapus Header
				mysql_query("DELETE FROM outgoing WHERE id='$id'");
				#Hapus Detail
				mysql_query("DELETE FROM outgoing_detail WHERE outd_hid='$id'");
				#Update log incoming
				mysql_query("UPDATE log_outgoing SET log_status=1 WHERE id_header='$idheader'");
			} elseif ($sumber == "UPLOAD" || $sumber == "UPL40") { #dari Upload
				#Hapus Header
				mysql_query("DELETE FROM outgoing WHERE id='$id'");
				#Hapus Detail
				mysql_query("DELETE FROM outgoing_detail WHERE outd_hid='$id'");
				#Update log outgoing
				if ($sumber == "UPLOAD") {
					mysql_query("UPDATE log_outgoing SET log_status=1000 WHERE jenis_dokpab='$jenis_dokpab' AND no_dokpab='$no_dokpab' AND log_status=3000 AND no_aju='$no_aju'");
				} else {
					mysql_query("UPDATE log_outgoing SET log_status=86 WHERE jenis_dokpab='$jenis_dokpab' AND no_dokpab='$no_dokpab' AND log_status=88 AND no_aju='$no_aju'");
				}
			} else { #Manual
				#Update status
				mysql_query("UPDATE outgoing SET outh_status=10 WHERE id='$id'");
			}
		}
		$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting no aju : $no_aju', 
							  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		if ($qrySave) {
			$message[] = "Data sukses diunposting.";
			$status	   = "OK";
		}
	}
}
if ($Act == "UNPOST2") {
	if (count($message) == 0) {
		if ($type == 21) {
			#update status
			mysql_query("UPDATE transfer SET trf_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting no spb : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		} elseif ($type == 22) {
			#update status
			mysql_query("UPDATE workorder SET wo_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting no wo : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		} elseif ($type == 23) {
			#update status
			mysql_query("UPDATE finishgoods SET fgh_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting no lap barang jadi : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		} elseif ($type == 24) {
			#update status
			mysql_query("UPDATE scrap SET scrh_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting no lap barang scrap : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		} elseif ($type == 25) {
			#update status
			mysql_query("UPDATE konversi SET kv_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting konversi barang : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		} elseif ($type == 26) {
			#update status
			mysql_query("UPDATE jurnal SET jurnal_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting jurnal no : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		} elseif ($type == 27) {
			#update status
			#echo "UPDATE purchase_order SET po_status=10 WHERE id='$id'";
			mysql_query("UPDATE purchase_order SET po_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting po no : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		} elseif ($type == 28) {
			#update status
			mysql_query("UPDATE konversi2 SET kv_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting konversi barang : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data sukses diunposting.";
			$status	   = "OK";
		}
	}
}
if ($Act == "UNPOST3") {
	if (count($message) == 0) {
		if ($type == 27) {
			#update status
			#echo "UPDATE purchase_order SET po_status=10 WHERE id='$id'";
			mysql_query("UPDATE purchase_order SET po_status=30 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses completed po no : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data sukses disimpan.";
			$status	   = "OK";
		}
	}
}
if ($Act == "UNPOST5") {
	if (count($message) == 0) {
		if ($type == 29) {
			#update status
			#echo "UPDATE purchase_order SET po_status=10 WHERE id='$id'";
			mysql_query("UPDATE adjusment SET adjh_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting penyesuaian No : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data sukses disimpan.";
			$status	   = "OK";
		}
	}
}
if ($Act == "UNPOST30") {
	if (count($message) == 0) {
		if ($type == 30) {
			#update status
			#echo "UPDATE purchase_order SET po_status=10 WHERE id='$id'";
			mysql_query("UPDATE transfer_lokasi SET tlh_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting transfer antar lokasi No : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data sukses disimpan.";
			$status	   = "OK";
		}
	}
}
if ($Act == "UNPOST31") {
	if (count($message) == 0) {
		if ($type == 31) {
			#update status
			#echo "UPDATE purchase_order SET po_status=10 WHERE id='$id'";
			mysql_query("UPDATE workorder_nonbom SET wonb_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting Workorder Non BOM No : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data sukses disimpan.";
			$status	   = "OK";
		}
	}
}
if ($Act == "UNPOST32") {
	if (count($message) == 0) {
		if ($type == 32) {
			#update status
			#echo "UPDATE purchase_order SET po_status=10 WHERE id='$id'";
			mysql_query("UPDATE konversi_external SET kv_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting Konversi External No : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data sukses disimpan.";
			$status	   = "OK";
		}
	}
}
if ($Act == "UNPOST33") {
	if (count($message) == 0) {
		if ($type == 33) {
			#update status
			#echo "UPDATE purchase_order SET po_status=10 WHERE id='$id'";
			mysql_query("UPDATE permintaan_barang SET pb_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting Permintaan Barang No : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data sukses disimpan.";
			$status	   = "OK";
		}
	}
}
if ($Act == "UNPOST34") {
	if (count($message) == 0) {
		if ($type == 34) {
			#update status
			#echo "UPDATE purchase_order SET po_status=10 WHERE id='$id'";
			mysql_query("UPDATE stockopname SET stoh_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting closing & stockopname No : $trf_spbno', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data sukses disimpan.";
			$status	   = "OK";
		}
	}
}
if ($Act == "UNPOST48") {
	if (count($message) == 0) {
		if ($type == 48) {
			mysql_query("UPDATE konversi_barangjadi SET kv_status=10 WHERE id='$id'");
			#insert history
			$qrySave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan proses unposting konversi barang jadi No : $kv_no', 
								  user_date='$date', user_time='$time', user_status=100") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data sukses disimpan.";
			$status	   = "OK";
		}
	}
}
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-Unposting&type=$type'>";
	}
}
?>