<?php
# Baca Variabel Form
$message 	 = array();
$Act		 = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$py_id		 = isset($_REQUEST['py_id']) ? $_REQUEST['py_id'] : '';
$py_tanggal  = isset($_REQUEST['py_tanggal']) ? $_REQUEST['py_tanggal'] : '';
$py_tipe	 = isset($_REQUEST['py_tipe']) ? $_REQUEST['py_tipe'] : '';
$py_nmbarang = isset($_REQUEST['py_nmbarang']) ? $_REQUEST['py_nmbarang'] : '';
$py_stbarang = isset($_REQUEST['py_stbarang']) ? $_REQUEST['py_stbarang'] : '';
$py_umur  	 = isset($_REQUEST['py_umur']) ? $_REQUEST['py_umur'] : '';
$py_nilai  	 = isset($_REQUEST['py_nilai']) ? $_REQUEST['py_nilai'] : '';
$pyd_tanggal = isset($_REQUEST['pyd_tanggal']) ? $_REQUEST['pyd_tanggal'] : '';
$pyd_nilai 	 = isset($_REQUEST['pyd_nilai']) ? $_REQUEST['pyd_nilai'] : '';
$py_jumlah 	 = isset($_REQUEST['py_jumlah']) ? $_REQUEST['py_jumlah'] : '';
$py_tarif 	 = isset($_REQUEST['py_tarif']) ? $_REQUEST['py_tarif'] : '';
$pyd_tanggal	= isset($_REQUEST['pyd_tanggal']) ? $_REQUEST['pyd_tanggal'] : $date;
#Proses simpan dan edit
if ($Act == "SAVE") {
	if (count($message) == 0) {
		if (empty($py_id)) {
			$qrySave = mysql_query("INSERT INTO penyusutan SET py_tanggal='$py_tanggal',py_tipe='$py_tipe',py_nmbarang='$py_nmbarang', py_stbarang='$py_stbarang',
					 py_jumlah='$py_jumlah', py_tarif='$py_tarif', py_umur='$py_umur', py_nilai='$py_nilai', py_status=10, py_inputby='$user_id', 
					 py_inputdate=now()") or die("Gagal query" . mysql_error());

			#Aktivitas User
			$useractSave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan data master penyusutan - $py_tanggal :$py_nmbarang', 
									   user_date='$date', user_time='$time'") or die("Gagal query" . mysql_error());
		} else {
			$qrySave = mysql_query("UPDATE penyusutan SET py_tanggal='$py_tanggal',py_tipe='$py_tipe',py_nmbarang='$py_nmbarang', py_stbarang='$py_stbarang',
					 py_umur='$py_umur', py_nilai='$py_nilai', py_jumlah='$py_jumlah', py_tarif='$py_tarif' WHERE id='$py_id'") or die("Gagal query" . mysql_error());
			#Aktivitas User
			$useractSave = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah data master penyusutan - $py_tanggal :$py_nmbarang', 
									   user_date='$date', user_time='$time'") or die("Gagal query" . mysql_error());
		}
		if ($qrySave) {
			$message[] = "Data telah tersimpan.";
			$status	   = "OK";
		}
	}
}
if ($Act == "DETAIL") {
	$dq = mysql_fetch_array(mysql_query("SELECT * FROM penyusutan WHERE id='$py_id'"));
	$py_tanggal  = $dq['py_tanggal'];
	$py_tipe	 = $dq['py_tipe'];
	$py_nmbarang = $dq['py_nmbarang'];
	$py_stbarang = $dq['py_stbarang'];
	$py_umur  	 = $dq['py_umur'];
	$py_nilai  	 = $dq['py_nilai'];
	$py_jumlah 	 = $dq['py_jumlah'];
	$py_tarif  	 = $dq['py_tarif'];
}
if ($Act == "DELETE") {
	mysql_query("UPDATE penyusutan SET py_status=100 WHERE id='$py_id'");
	#Aktivitas User
	mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus data master penyusutan - $py_tanggal :$py_nmbarang', user_date='$date', user_time='$time'");
	$message[] = "Data telah dihapus.";
	$status	   = "OK";
}
if ($Act == "DEL_DET") {
	mysql_query("UPDATE penyusutan_detail SET pyd_status=100 WHERE pyd_tanggal='$pyd_tanggal'");
	#Aktivitas User
	mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus data penyusutan detail - $pyd_tanggal', user_date='$date', user_time='$time'");
	$message[] = "Data telah dihapus.";
	$status	   = "OK";
}
if ($Act == "PAY_ADD") {
	$dq = mysql_fetch_array(mysql_query("SELECT * FROM penyusutan WHERE id='$py_id'"));
	$py = mysql_fetch_array(mysql_query("SELECT SUM(pyd_nilai) susut FROM penyusutan_detail WHERE pyd_hid='$py_id' AND pyd_status=10"));
	$py_tanggal  = $dq['py_tanggal'];
	$py_tipe	 = $dq['py_tipe'];
	$py_nmbarang = $dq['py_nmbarang'];
	$py_stbarang = $dq['py_stbarang'];
	$py_umur  	 = $dq['py_umur'];
	$py_nilai  	 = number_format($dq['py_nilai'], 2, ',', '.');
	$bnilai		 = number_format($py['susut'], 2, ',', '.');
	$snilai		 = number_format(($dq['py_nilai'] - $py['susut']), 2, ',', '.');
}
if ($Act == "PAY_SAVE") {
	if (count($message) == 0) {
		$qrySave = mysql_query("INSERT INTO penyusutan_detail SET pyd_hid='$py_id', pyd_tanggal='$pyd_tanggal', pyd_nilai='$pyd_nilai', pyd_status=10, pyd_inputby='$user_id', pyd_inputdate=now()");
		#Aktivitas User
		mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan data penyusutan - $pyd_tanggal :$py_nmbarang', user_date='$date', user_time='$time'");
		if ($qrySave) {
			$message[] = "Data telah tersimpan.";
			$status	   = "OK";
		}
	}
}
if ($Act == "ADD" || $Act == "DETAIL") {
	require_once "add_penyusutan.php";
}
if ($Act == "PAY_ADD") {
	require_once "addp_penyusutan.php";
}
if ($Act == "PAY_ADDS") {
	$pilih2 = $_REQUEST['pilih2'];
	foreach ($pilih2 as $i => $id) {
		$pyd_nilai2		= isset($_REQUEST['pyd_nilai2'][$id]) ? $_REQUEST['pyd_nilai2'][$id] : '';
		$pyd_nmbarang2	= isset($_REQUEST['pyd_nmbarang2'][$id]) ? $_REQUEST['pyd_nmbarang2'][$id] : '';
		$vdate2			= isset($_REQUEST['vdate2'][$id]) ? $_REQUEST['vdate2'][$id] : '';
		#insert penyusutan detail
		if($vdate2=="OK"){
			mysql_query("INSERT INTO penyusutan_detail SET pyd_hid='$id', pyd_tanggal='$pyd_tanggal', pyd_nilai='$pyd_nilai2', pyd_status=10, 
			pyd_inputby='$user_id', pyd_keterangan='Penyusutan $pyd_nmbarang2 ', pyd_inputdate=now()");
			#insert aktivitas
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan data penyusutan - $pyd_nmbarang2 :$pyd_nilai2', 
			user_date='$date', user_time='$time'");
		}
	}
	$message[] = "Data sukses disusutkan";
	$status = "OK";
}
if ($Act == "AUT") {
	require_once "adda_penyusutan.php";
}
if ($Act == "AUTS") {
	$py_incdid = $_REQUEST['py_incdid'];
	foreach ($py_incdid as $i => $id) {
		$py_tipe		= isset($_REQUEST['py_tipe'][$id]) ? $_REQUEST['py_tipe'][$id] : '';
		$py_kdbarang	= isset($_REQUEST['py_kdbarang'][$id]) ? $_REQUEST['py_kdbarang'][$id] : '';
		$py_nmbarang	= isset($_REQUEST['py_nmbarang'][$id]) ? $_REQUEST['py_nmbarang'][$id] : '';
		$py_stbarang	= isset($_REQUEST['py_stbarang'][$id]) ? $_REQUEST['py_stbarang'][$id] : '';
		$py_jumlah		= isset($_REQUEST['py_jumlah'][$id]) ? $_REQUEST['py_jumlah'][$id] : '';
		$py_umur		= isset($_REQUEST['py_umur'][$id]) ? $_REQUEST['py_umur'][$id] : '';
		$py_tarif		= isset($_REQUEST['py_tarif'][$id]) ? $_REQUEST['py_tarif'][$id] : '';
		$py_nilai		= isset($_REQUEST['py_nilai'][$id]) ? $_REQUEST['py_nilai'][$id] : '';
		if ($py_tipe <> 'BLANK') {
			#Update status hutang
			mysql_query("INSERT INTO penyusutan SET py_tanggal='" . date('Y-m-d') . "',py_tipe='$py_tipe',py_kdbarang='$py_kdbarang', py_nmbarang='$py_nmbarang', 
			py_stbarang='$py_stbarang', py_jumlah='$py_jumlah', py_tarif='$py_tarif', py_umur='$py_umur', py_nilai='$py_nilai', py_status=10, py_incdid='$id',
			py_inputby='$user_id', py_inputdate=now()");
			#insert aktivitas
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan data master penyusutan - $py_nmbarang :$py_jumlah', 
			user_date='$date', user_time='$time'");
		}
	}
	$message[] = "Data sukses disimpan";
	$status = "OK";
}
if ($Act == "CLOSE") {
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Penyusutan'>";
}
# Popup pesan
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-Penyusutan'>";
	}
}
?>