<?php
session_start();
include_once "../../include/config.php";
include_once "../../function/inc_function.php";

$datetime 	= get_datetime();
$date		= get_date();
$time		= get_time();
$user_id  	= $_SESSION['USER_ID'];
# Baca Variabel Form
$message = array();
$Act  			= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$doch_id		= isset($_REQUEST['doch_id']) ? $_REQUEST['doch_id'] : '';
$docd_id		= isset($_REQUEST['docd_id']) ? $_REQUEST['docd_id'] : '';
$doch_regno		= isset($_REQUEST['doch_regno']) ? $_REQUEST['doch_regno'] : get_nodocument();
$doch_date		= isset($_REQUEST['doch_date']) ? $_REQUEST['doch_date'] : $date;
$doch_custid	= isset($_REQUEST['doch_custid']) ? $_REQUEST['doch_custid'] : '';
$nm_penerima	= isset($_REQUEST['nm_penerima']) ? $_REQUEST['nm_penerima'] : '';
$doch_invno		= isset($_REQUEST['doch_invno']) ? $_REQUEST['doch_invno'] : '';
$doch_dono	    = isset($_REQUEST['doch_dono']) ? $_REQUEST['doch_dono'] : '';
$doch_doctype   = isset($_REQUEST['doch_doctype']) ? $_REQUEST['doch_doctype'] : '';
$doch_docno		= isset($_REQUEST['doch_docno']) ? $_REQUEST['doch_docno'] : '';
$doch_docdate	= isset($_REQUEST['doch_docdate']) ? $_REQUEST['doch_docdate'] : $date;
$doch_attn		= isset($_REQUEST['doch_attn']) ? $_REQUEST['doch_attn'] : '';
$doch_idtypeout = isset($_REQUEST['doch_idtypeout']) ? $_REQUEST['doch_idtypeout'] : 0;
$doch_desc		= isset($_REQUEST['doch_desc']) ? $_REQUEST['doch_desc'] : '';
$doch_idpayment	= isset($_REQUEST['doch_idpayment']) ? $_REQUEST['doch_idpayment'] : 0;
$doch_subtotal	= isset($_REQUEST['doch_subtotal']) ? $_REQUEST['doch_subtotal'] : '';
$doch_discount	= isset($_REQUEST['doch_discount']) ? $_REQUEST['doch_discount'] : 0;
$doch_ppn		= isset($_REQUEST['doch_ppn']) ? $_REQUEST['doch_ppn'] : 0;
$doch_grandtotal = isset($_REQUEST['doch_grandtotal']) ? $_REQUEST['doch_grandtotal'] : 0;
$doch_idttd		= isset($_REQUEST['doch_idttd']) ? $_REQUEST['doch_idttd'] : 0;
$doch_idjabatan	= isset($_REQUEST['doch_idjabatan']) ? $_REQUEST['doch_idjabatan'] : 0;
if($Act =="SAVE"){
	if(count($message)==0){	
		if(empty($doch_id)){
			$qrySave=mysql_query("INSERT INTO document SET doch_regno='$doch_regno', doch_date='$date', doch_custid='$doch_custid', doch_invno='$doch_invno',
			 				  doch_dono='$doch_dono', doch_inputby='$user_id', doch_inputdate='$datetime', doch_status=10, doch_docdate='$doch_docdate', 
							  doch_docno='$doch_docno', doch_doctype='$doch_doctype', nm_penerima='$nm_penerima', doch_attn='$doch_attn', doch_idtypeout='$doch_idtypeout',
							  doch_desc='$doch_desc', doch_idpayment='$doch_idpayment', doch_subtotal='$doch_subtotal', doch_discount='$doch_discount', doch_ppn='$doch_ppn',
							  doch_grandtotal='$doch_grandtotal', doch_idttd='$doch_idttd', doch_idjabatan='$doch_idjabatan'") or die ("Gagal query".mysql_error());
			if($qrySave){
				$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM document"));
				$doch_id = $data->id;
				$query = mysql_query("SELECT * FROM document_temp WHERE docd_userid='$user_id'");
				while($dt=mysql_fetch_object($query)){
					$qrySave=mysql_query("INSERT INTO document_detail SET docd_hid='$doch_id', docd_prodid='$dt->docd_prodid', docd_qty='$dt->docd_qty', 
										  docd_price='$dt->docd_price', docd_currency='$dt->docd_currency', docd_weight='$dt->docd_weight', docd_wunit='$dt->docd_wunit',
										  docd_buyerpo='$dt->docd_buyerpo', docd_total='$dt->docd_total', docd_gross='$dt->docd_gross', docd_box='$dt->docd_box', 
										  docd_pallet='$dt->docd_pallet'") or die ("Gagal query".mysql_error());
				}
				$qd = mysql_query("DELETE FROM document_temp WHERE docd_userid='$user_id'");
			}	
			#Insert History User
			$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi  plan pengeluaran No. $doch_regno', 
			         user_date='$date', user_time='$time'");
		}elseif(!empty($doch_id)){
			$qrySave=mysql_query("UPDATE document SET doch_regno='$doch_regno', doch_date='$date', doch_custid='$doch_custid', doch_invno='$doch_invno',
			 				  doch_dono='$doch_dono', doch_inputby='$user_id', doch_docdate='$doch_docdate', doch_docno='$doch_docno', doch_doctype='$doch_doctype',
							  nm_penerima='$nm_penerima', doch_attn='$doch_attn', doch_idtypeout='$doch_idtypeout', doch_desc='$doch_desc', doch_idpayment='$doch_idpayment', 
							  doch_subtotal='$doch_subtotal', doch_discount='$doch_discount', doch_ppn='$doch_ppn', doch_grandtotal='$doch_grandtotal', doch_idttd='$doch_idttd', 
							  doch_idjabatan='$doch_idjabatan' WHERE id='$doch_id'") or die ("Gagal query".mysql_error());
			/*if($qrySave){
			#Update Detail
				foreach($id_det as $i => $id)
				{
					$docd_qty		= $_REQUEST['docd_qty'][$id];
					$docd_price		= $_REQUEST['docd_price'][$id];
					$docd_weight	= $_REQUEST['docd_weight'][$id];
					mysql_query("UPDATE document_detail SET docd_qty='$docd_qty', docd_price='$docd_price', docd_weight='$docd_weight' WHERE id='$id' ");
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah transaksi pengeluaran No. $doch_sjno', 
				         user_date='$date', user_time='$time'");
			}*/							  
		}				
		if($qrySave){
			$message[] = "Data Sukses tersimpan";	
			$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';
							}
						?>';
			var status ='<?=$status;?>';			
			alert(pesan);
			//if(status == "SIMPAN"){
				//openwindow5('<?=$doch_id;?>');
			//}else{
				//self.history.back();
			//}
		</script>			
	<?php  
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Dokumen'>";	
		?>
			<script language="javascript">
				window.opener.onbeforeunload = null;        
				window.opener.location.reload(true);
				window.close();
			</script>
		<?php
		exit;
	}
}	
?>