<?php
# Baca Variabel Form
$message = array();
$Act	 	 	= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det 	 	= isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$outh_id 	 	= isset($_REQUEST['outh_id']) ? $_REQUEST['outh_id'] : '';
$outh_sjdate	= isset($_REQUEST['outh_sjdate']) ? $_REQUEST['outh_sjdate'] : '';
$cust_id    	= isset($_REQUEST['cust_id']) ? $_REQUEST['cust_id'] : '';
$cust_name   	= isset($_REQUEST['cust_name']) ? $_REQUEST['cust_name'] : '';
$outh_invno  	= isset($_REQUEST['outh_invno']) ? $_REQUEST['outh_invno'] : '';
$outh_invdate	= isset($_REQUEST['outh_invdate']) ? $_REQUEST['outh_invdate'] : '';
$outh_dono		= isset($_REQUEST['outh_dono']) ? $_REQUEST['outh_dono'] : '';
$tujuan	= isset($_REQUEST['tujuan']) ? $_REQUEST['tujuan'] : '';
$outh_docid	 	= isset($_REQUEST['outh_docid']) ? $_REQUEST['outh_docid'] : '';
$outh_docdate	= isset($_REQUEST['outh_docdate']) ? $_REQUEST['outh_docdate'] : '';
$outh_docno		= isset($_REQUEST['outh_docno']) ? $_REQUEST['outh_docno'] : '';
$id_det			= isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth		= isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
$no_aju			= isset($_REQUEST['no_aju']) ? $_REQUEST['no_aju'] : '';
$tgl_aju		= isset($_REQUEST['tgl_aju']) ? $_REQUEST['tgl_aju'] : '';
$no_npe			= isset($_REQUEST['no_npe']) ? $_REQUEST['no_npe'] : '';
$tgl_npe		= isset($_REQUEST['tgl_npe']) ? $_REQUEST['tgl_npe'] : '';
$doc			= mysql_fetch_array(mysql_query("SELECT doc_type FROM document_type WHERE id='$outh_docid'"));
$cust			= mysql_fetch_array(mysql_query("SELECT cust_name FROM customer WHERE id='$cust_id'"));
$val_date       = get_validationdate($outh_docdate);
//if(empty($outh_id)){ 
	//$outh_sjno = get_sjno();
//}else{
	$outh_sjno = isset($_REQUEST['outh_sjno']) ? $_REQUEST['outh_sjno'] : '';
//}
$outd_prodid      = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$outd_qty   	  = isset($_REQUEST['outdqty']) ? $_REQUEST['outdqty'] : '';
$outd_price   	  = isset($_REQUEST['outdprice']) ? $_REQUEST['outdprice'] : '';
$outd_currency    = isset($_REQUEST['outh_currency']) ? $_REQUEST['outh_currency'] : '';
$outd_weight  	  = isset($_REQUEST['outdweight']) ? $_REQUEST['outdweight'] : '';
$outd_stock  	  = isset($_REQUEST['prodstock']) ? $_REQUEST['prodstock'] : '';
$po_no	 	 	  = isset($_REQUEST['po_no']) ? $_REQUEST['po_no'] : '';
$outd_price_rate  = isset($_REQUEST['n_rate']) ? $_REQUEST['n_rate'] : '';
$outd_amount	  = isset($_REQUEST['outdamount']) ? $_REQUEST['outdamount'] : '';
$outd_price_usd   = isset($_REQUEST['n_usd']) ? $_REQUEST['n_usd'] : '';
$cust_npwp		  = isset($_REQUEST['cust_npwp']) ? $_REQUEST['cust_npwp'] : '';
$pilih			= isset($_REQUEST['pilih']) ? $_REQUEST['pilih'] : '';
$pilih_reg		= isset($_REQUEST['pilih_reg']) ? $_REQUEST['pilih_reg'] : '';

$nomor_nota		  = isset($_REQUEST['nomor_nota']) ? $_REQUEST['nomor_nota'] : '';
$tanggal_nota	  = isset($_REQUEST['tanggal_nota']) ? $_REQUEST['tanggal_nota'] : '';
$house_bl		  = isset($_REQUEST['house_bl']) ? $_REQUEST['house_bl'] : '';
$master_bl		  = isset($_REQUEST['master_bl']) ? $_REQUEST['master_bl'] : '';
$nomor_manifes	  = isset($_REQUEST['nomor_manifes']) ? $_REQUEST['nomor_manifes'] : '';
$tanggal_manifes  = isset($_REQUEST['tanggal_manifes']) ? $_REQUEST['tanggal_manifes'] : '';
$pos			  = isset($_REQUEST['pos']) ? $_REQUEST['pos'] : '';
$pelabuhan		  = isset($_REQUEST['pelabuhan']) ? $_REQUEST['pelabuhan'] : '';
$harga_maklon	  = isset($_REQUEST['harga_maklon']) ? $_REQUEST['harga_maklon'] : '';
$no_kontainer	  = isset($_REQUEST['no_kontainer']) ? $_REQUEST['no_kontainer'] : '';
$jumlah_kemasan	  = isset($_REQUEST['jumlah_kemasan']) ? $_REQUEST['jumlah_kemasan'] : '';
$harga_fob		  = isset($_REQUEST['harga_fob']) ? $_REQUEST['harga_fob'] : '';
$nm_pembeli		  = isset($_REQUEST['nm_pembeli']) ? $_REQUEST['nm_pembeli'] : '';
$outh_scno		  = isset($_REQUEST['outh_scno']) ? $_REQUEST['outh_scno'] : '';
$outd_scno  	  = isset($_REQUEST['outdscno']) ? $_REQUEST['outdscno'] : '';
$bulan2    	 	  = substr($start_date,5,2);
$tahun2  	      = substr($start_date,0,4);
$no_urut  	  	  = isset($_REQUEST['outdnourut']) ? $_REQUEST['outdnourut'] : '';
$outh_keterangan  = isset($_REQUEST['outh_keterangan']) ? $_REQUEST['outh_keterangan'] : '';
$tipe_dokumen 	  = isset($_REQUEST['tipe_dokumen']) ? $_REQUEST['tipe_dokumen'] : '';
$dharga_fob		  = isset($_REQUEST['dharga_fob']) ? $_REQUEST['dharga_fob'] : 0;
$dharga_maklon	  = isset($_REQUEST['dharga_maklon']) ? $_REQUEST['dharga_maklon'] : 0;
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_pengeluaran_bc30.php";
		exit;
	}elseif(empty($outd_price_rate) || $outd_price_rate==0){
		echo "<font color='red'>Kurs tanggal : $outh_docdate belum disetting</font><br>";
		#reload
		require_once "add_pengeluaran_bc30.php";
		exit;
	}else{
		if(count($message)==0){	
			if(empty($outh_id)){
				#cek qty remain < qty terima
				if($po_no!="10"){
					foreach($pilih as $i => $id){
						$outd_qty		= $_REQUEST['outd_qty2'][$id];
						$outd_rmn		= $_REQUEST['outd_rmn'][$id];
						$prod_name		= $_REQUEST['prod_name2'][$id];
						$outd_qstk		= $_REQUEST['outd_qstk'][$id];
						if($outd_qty > $outd_rmn){
							echo "<font color='red'>Barang :$prod_name QTY INPUT Lebih besar dari QTY REMAIN($outd_qty > $outd_rmn)</font><br>";
							#reload
							require_once "add_pengeluaran_bc30.php";
							exit;
						}elseif($outd_qty > $outd_qstk){
							echo "<font color='red'>Barang :$prod_name QTY INPUT Lebih besar dari QTY STOCK($outd_qty > $outd_qstk)</font><br>";
							#reload
							require_once "add_pengeluaran_bc30.php";
							exit;
						}
					}
				}
				$qrySave=mysql_query("INSERT INTO outgoing SET outh_sjno='$outh_sjno', outh_sjdate='$outh_sjdate', outh_custid='$cust_id', 
								  outh_invno='$outh_invno', outh_invdate='$outh_invdate', outh_dono='$outh_dono', harga_fob='$harga_fob',
								  tujuan='$tujuan', outh_docid='$outh_docid', outh_inputby='$user_id', sumber='MANUAL',
								  outh_inputdate='$datetime', outh_status=20, outh_docdate='$outh_docdate', outh_docno='$outh_docno', 
								  no_aju='$no_aju', tgl_aju='$tgl_aju', no_npe='$no_npe', tgl_npe='$tgl_npe', outh_doctype='$doc[doc_type]', nm_penerima='$cust[cust_name]',
								  nomor_nota='$nomor_nota', tanggal_nota='$tanggal_nota', house_bl='$house_bl', master_bl='$master_bl', nomor_manifes='$nomor_manifes',
								  tanggal_manifes='$tanggal_manifes', pos='$pos', pelabuhan='$pelabuhan', harga_maklon='$harga_maklon', no_kontainer='$no_kontainer',
								  jumlah_kemasan='$jumlah_kemasan',id_penerima='$cust_npwp', outh_currency='$outd_currency', outh_rate='$outd_price_rate', po_no='$po_no', nm_pembeli='$nm_pembeli',
								  outh_keterangan='$outh_keterangan', tipe_dokumen='$tipe_dokumen'") 
								  or die ("Gagal query".mysql_error());
				if($qrySave){
					if($po_no=="10"){
						$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM outgoing"));
						$outh_id = $data->id;
						$query = mysql_query("SELECT * FROM outgoing_temp WHERE outd_inputby='$user_id' ORDER BY id ASC");
						while($dt=mysql_fetch_object($query)){
							$qrySave=mysql_query("INSERT INTO outgoing_detail SET outd_hid='$outh_id', outd_prodid='$dt->outd_prodid', 
												  outd_qty='$dt->outd_qty', outd_price='$dt->outd_price', outd_currency='$dt->outd_currency', 
												  outd_weight='$dt->outd_weight', outd_wunit='$dt->outd_wunit', outd_scno='$dt->outd_scno',
												  outd_price_rate='$dt->outd_price_rate', outd_price_usd='$dt->outd_price_usd', outd_price_satuan='$dt->outd_price_satuan',
												  no_urut='$dt->no_urut', harga_fob='$dt->harga_fob', harga_maklon='$dt->harga_maklon'")
												  or die ("Gagal query".mysql_error());
						}
						$qd = mysql_query("UPDATE outgoing_temp SET outd_hid='$outh_id' WHERE outd_inputby='$user_id'");
					}else{
						$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM outgoing"));
						$outh_id = $data->id;
						foreach($pilih as $i => $id){
							$outd_prodid	= $_REQUEST['outd_prodid2'][$id];
							$outd_qty		= $_REQUEST['outd_qty2'][$id];
							$pod_price		= $_REQUEST['pod_price'][$id];
							$outd_scno		= $_REQUEST['outd_scno2'][$id];							
							if($outd_currency=="IDR"){
								$outd_price		 = $outd_qty*$pod_price;
								$outd_price_usd  = 0;
							}else{
								$outd_price		 = ($outd_qty*$pod_price)*$outd_price_rate;
								$outd_price_usd  = $outd_qty*$pod_price;
							}
							$outd_weight	= $_REQUEST['outd_weight2'][$id];
							$no_urut		= $_REQUEST['no_urut2'][$id];
							$dharga_fob2	= $_REQUEST['dharga_fob2'][$id];
							$dharga_maklon2	= $_REQUEST['dharga_maklon2'][$id];
							if($outd_qty > 0){
								mysql_query("INSERT INTO outgoing_detail SET outd_hid='$outh_id', outd_prodid='$outd_prodid', outd_price_usd='$outd_price_usd',
											 outd_qty='$outd_qty', outd_price='$outd_price', outd_currency='$outd_currency', outd_price_rate='$outd_price_rate',
											 outd_weight='$outd_weight', outd_wunit='KG', no_urut='$no_urut', outd_price_satuan='$pod_price', outd_scno='$outd_scno',
											 harga_fob='$dharga_fob2', harga_maklon='$dharga_maklon2'");
							}			 
						}
					}
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi pengeluaran No. $outh_sjno:$outh_docno', 
						 user_date='$date', user_time='$time'");
			}elseif(!empty($outh_id) && ($Act_Det <> "ADD_DET")){
				#cek qty stock
				foreach($id_det as $i => $id){
					$outd_qstk2		= $_REQUEST['outd_qstk2'][$id];
					$outd_qty		= $_REQUEST['outd_qty'][$id];
					$outd_codename	= $_REQUEST['outd_codename'][$id];
					if($outd_qty > $outd_qstk2){
						echo "<font color='red'>Barang :$outd_codename QTY INPUT Lebih besar dari QTY STOCK($outd_qty > $outd_qstk2)</font><br>";
						#reload
						require_once "add_pengeluaran_bc30.php";
						exit;
					}
				}
				$qrySave=mysql_query("UPDATE outgoing SET outh_sjno='$outh_sjno', outh_sjdate='$outh_sjdate', outh_custid='$cust_id', 
									  outh_invno='$outh_invno', outh_invdate='$outh_invdate', outh_dono='$outh_dono', harga_fob='$harga_fob',
									  tujuan='$tujuan', outh_docid='$outh_docid', outh_docdate='$outh_docdate', outh_docno='$outh_docno',
									  no_aju='$no_aju', tgl_aju='$tgl_aju', no_npe='$no_npe', tgl_npe='$tgl_npe', outh_doctype='$doc[doc_type]', nm_penerima='$cust[cust_name]',
									  nomor_nota='$nomor_nota', tanggal_nota='$tanggal_nota', house_bl='$house_bl', master_bl='$master_bl', nomor_manifes='$nomor_manifes',
								      tanggal_manifes='$tanggal_manifes', pos='$pos', pelabuhan='$pelabuhan', harga_maklon='$harga_maklon', no_kontainer='$no_kontainer',
									  jumlah_kemasan='$jumlah_kemasan',id_penerima='$cust_npwp', outh_currency='$outd_currency', outh_rate='$outd_price_rate',outh_status=20, nm_pembeli='$nm_pembeli',
								      outh_keterangan='$outh_keterangan', tipe_dokumen='$tipe_dokumen' WHERE id='$outh_id'") or die ("Gagal query".mysql_error());
				if($qrySave){
				#Update Detail
					#delete temp
					mysql_query("DELETE FROM outgoing_temp WHERE outd_hid='$outh_id'");
					foreach($id_det as $i => $id){
						$outd_currency		= $_REQUEST['outd_currency'][$id];
						$outd_qty			= $_REQUEST['outd_qty'][$id];
						$outd_price_satuan	= $_REQUEST['outd_price_satuan'][$id];
						$outd_weight		= $_REQUEST['outd_weight'][$id];
						$outd_price_rate	= $_REQUEST['outd_price_rate'][$id];
						$outd_prodid		= $_REQUEST['outd_prodid'][$id];
						$outd_price			= $_REQUEST['outd_price'][$id];
						$outd_price_usd		= $_REQUEST['outd_price_usd'][$id];
						$outd_scno			= $_REQUEST['outd_scno'][$id];
						$no_urut			= $_REQUEST['no_urut'][$id];
						$dharga_fob3		= $_REQUEST['dharga_fob3'][$id];
						$dharga_maklon3		= $_REQUEST['dharga_maklon3'][$id];
						#detail
						mysql_query("UPDATE outgoing_detail SET outd_qty='$outd_qty', outd_price='$outd_price', outd_weight='$outd_weight',
						outd_price_usd='$outd_price_usd', outd_price_satuan='$outd_price_satuan', outd_currency='$outd_currency', outd_scno='$outd_scno', no_urut='$no_urut',
						harga_fob='$dharga_fob3', harga_maklon='$dharga_maklon3' WHERE id='$id' ");
						#insert temp
						mysql_query("INSERT INTO outgoing_temp SET outd_prodid='$outd_prodid', outd_qty='$outd_qty', outd_price='$outd_price', outd_currency='$outd_currency', 
						outd_hid='$outh_id', outd_weight='$outd_weight', outd_wunit='Kg', outd_price_rate='$outd_price_rate', outd_price_usd='$outd_price_usd', 
						outd_price_satuan='$outd_price_satuan', outd_scno='$outd_scno', no_urut='$no_urut', harga_fob='$dharga_fob3', harga_maklon='$dharga_maklon3'");
					}	
					#delete exbc
					mysql_query("DELETE FROM outgoing_dokasal WHERE outh_hid='$outh_id'");
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah transaksi pengeluaran No. $outh_sjno:$outh_docno', 
							 user_date='$date', user_time='$time'");
				}							  
			}	
			if (count($message)==0 ){
				#Update Status
				$qd = mysql_query("UPDATE outgoing SET outh_status=11 WHERE id='$outh_id'");
				#get Exbc
				get_outexbc($start_date,$end_date,$outh_id);
				#delete temp
				mysql_query("DELETE FROM outgoing_temp WHERE outd_hid='$outh_id'");
				#Update Status
				$qd = mysql_query("UPDATE outgoing SET outh_status=20 WHERE id='$outh_id'");
				#update status PO, jika sudah komplit qty PO.
				$qpo = mysql_fetch_array(mysql_query("SELECT SUM(a.pod_qty) qty_po FROM purchase_order_detail a INNER JOIN purchase_order b ON b.po_no=a.po_no
					   WHERE b.po_status=20 AND b.po_no='$po_no'"));
				$qin = mysql_fetch_array(mysql_query("SELECT SUM(a.outd_qty) qty_in FROM outgoing_detail a INNER JOIN outgoing b ON b.id=a.outd_hid
					   WHERE b.outh_status IN(20) AND b.po_no='$po_no'"));
				if($qpo['qty_po']==$qin['qty_in']){
					mysql_query("UPDATE purchase_order SET po_status=30 WHERE po_no='$po_no'");
				}else{
					mysql_query("UPDATE purchase_order SET po_status=20 WHERE po_no='$po_no'");
				}				
			}
			if($qrySave){
				$message[] = "Data Sukses tersimpan";	
				$status = "OK";
				
			}
		}	
	}
}		
if($Act =="DETAIL"){
	$qd = mysql_query("SELECT a.*,b.cust_name FROM outgoing a
					   INNER JOIN customer b ON b.id=a.outh_custid					   	   
					   WHERE a.outh_status <> 100 AND b.cust_status <> 100 
					   AND a.id='$outh_id'");
	$dd=mysql_fetch_object($qd);		
	$outh_id 		= $dd->id;			
	$outh_sjno		= $dd->outh_sjno;
	$outh_sjdate	= $dd->outh_sjdate;
	$cust_id 		= $dd->outh_custid;
	$cust_name		= $dd->cust_name;
	$outh_invno		= $dd->outh_invno;
	$outh_invdate   = $dd->outh_invdate;
	$outh_dono	    = $dd->outh_dono;
	$tujuan	= $dd->tujuan;
	$outh_docno		= $dd->outh_docno;
	$outh_docdate	= $dd->outh_docdate;
	$no_aju			= $dd->no_aju;
	$tgl_aju		= $dd->tgl_aju;
	$no_npe			= $dd->no_npe;
	$tgl_npe		= $dd->tgl_npe;
	$po_no			= $dd->po_no;
	$outd_currency  = $dd->outh_currency;
	$outd_price_rate= $dd->outh_rate;
	$cust_npwp		= $dd->id_penerima;
	if(isset($_REQUEST['flagdoc'])==1){
		$outh_docid		 = $_REQUEST['outh_docid'];
	}else{
		$outh_docid	    = $dd->outh_docid;
	}
	$nomor_nota		  = $dd->nomor_nota;
	$tanggal_nota	  = $dd->tanggal_nota;
	$house_bl		  = $dd->house_bl;
	$master_bl		  = $dd->master_bl;
	$nomor_manifes	  = $dd->nomor_manifes;
	$tanggal_manifes  = $dd->tanggal_manifes;
	$pos			  = $dd->pos;
	$pelabuhan		  = $dd->pelabuhan;
	$harga_maklon	  = $dd->harga_maklon;
	$no_kontainer	  = $dd->no_kontainer;
	$jumlah_kemasan   = $dd->jumlah_kemasan;
	$harga_fob        = $dd->harga_fob;
	$nm_pembeli		  = $dd->nm_pembeli;
	$outh_keterangan  = $dd->outh_keterangan;
	$tipe_dokumen	  = $dd->tipe_dokumen;
	}	
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE outgoing SET outh_status=100 WHERE id='$outh_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus transaksi pengeluaran No. $outh_sjno', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}
}	
if($Act == "POSTING"){
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "OK";
	}else{
		/*$qc = mysql_query("SELECT a.*, b.prod_code FROM transfer_temp a INNER JOIN product b ON b.id=a.trfd_prodid WHERE a.trfd_hid='$trf_id' 
			  ORDER BY a.trfd_prodid ASC");
		while($dtc=mysql_fetch_array($qc)){
			$stock = get_stocktrfbb($dtc['trfd_prodid'],$start_date,$end_date,$inv);
			if($dtc['trfd_qty'] > $stock){
				 $message[] = "Kode Barang :$dtc[prod_code] Jumlah pengeluaran lebih besar dari jumlah stock : $dtc[trfd_qty] > $stock";
				 $status = "OK";
			 }
		}*/
		if (count($message)==0 ){
			#Update Status
			$qd = mysql_query("UPDATE outgoing SET outh_status=11 WHERE id='$outh_id'");
			#get Exbc
			get_outexbc($start_date,$end_date,$outh_id);
			#delete temp
			mysql_query("DELETE FROM outgoing_temp WHERE outd_hid='$outh_id'");
			#Update Status
			$qd = mysql_query("UPDATE outgoing SET outh_status=20 WHERE id='$outh_id'");
			#update status PO, jika sudah komplit qty PO.
			$qpo = mysql_fetch_array(mysql_query("SELECT SUM(a.pod_qty) qty_po FROM purchase_order_detail a INNER JOIN purchase_order b ON b.po_no=a.po_no
				   WHERE b.po_status=20 AND b.po_no='$po_no'"));
			$qin = mysql_fetch_array(mysql_query("SELECT SUM(a.outd_qty) qty_in FROM outgoing_detail a INNER JOIN outgoing b ON b.id=a.outd_hid
				   WHERE b.outh_status IN(15,20) AND b.po_no='$po_no'"));
			if($qpo['qty_po']==$qin['qty_in']){
				mysql_query("UPDATE purchase_order SET po_status=30 WHERE po_no='$po_no'");
			}
			#Insert History User
			$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting transaksi pengeluaran No. $outh_sjno', user_date='$date', 
					 user_time='$time'");
			$message[] = "Data Sukses diposting !";	
			$status = "OK";	
		}
	}
}
if(($Act_Det == "ADD_DET") && (empty($outh_id))){ 
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_pengeluaran_bc30.php";
		exit;
	}elseif(empty($outd_price_rate) || $outd_price_rate==0){
		echo "<font color='red'>Kurs tanggal : $outh_docdate belum disetting</font><br>";
		#reload
		require_once "add_pengeluaran_bc30.php";
		exit;
	}else{
		mysql_query("INSERT INTO outgoing_temp SET outd_prodid='$outd_prodid', outd_qty='$outd_qty', outd_price='$outd_amount', outd_currency='$outd_currency', 
					 outd_weight='$outd_weight', outd_wunit='Kg',outd_inputby='$user_id', 
					 outd_price_rate='$outd_price_rate', outd_price_usd='$outd_price_usd', outd_price_satuan='$outd_price', outd_scno='$outd_scno',
					 no_urut='$no_urut', harga_fob='$dharga_fob', harga_maklon='$dharga_maklon'");
	}
}elseif(($Act_Det == "ADD_DET") && (!empty($outh_id))){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_pengeluaran_bc30.php";
		exit;
	}elseif(empty($outd_price_rate) || $outd_price_rate==0){
		echo "<font color='red'>Kurs tanggal : $outh_docdate belum disetting</font><br>";
		#reload
		require_once "add_pengeluaran_bc30.php";
		exit;
	}else{
		mysql_query("INSERT INTO outgoing_detail SET outd_hid='$outh_id', outd_prodid='$outd_prodid', outd_qty='$outd_qty', outd_price='$outd_amount', 
					 outd_currency='$outd_currency', outd_weight='$outd_weight', outd_wunit='Kg',
					 outd_price_rate='$outd_price_rate', outd_price_usd='$outd_price_usd', outd_price_satuan='$outd_price', outd_scno='$outd_scno',
					 no_urut='$no_urut', harga_fob='$dharga_fob', harga_maklon='$dharga_maklon'");
	}
}
if(($Act_Det == "DELETE_DET") && (empty($outh_id))){
	mysql_query("DELETE FROM outgoing_temp WHERE id='$id_deth'");
}elseif(($Act_Det == "DELETE_DET") && (!empty($outh_id))){
	mysql_query("DELETE FROM outgoing_detail WHERE id='$id_deth'");
}
if($Act == "SEL_PO"){
	$dd=mysql_fetch_array(mysql_query("SELECT a.*,b.cust_name, b.cust_npwp FROM purchase_order a INNER JOIN customer b ON b.id=a.po_idcust 
	WHERE a.po_no='$po_no' AND po_type='Sales'"));
	$po_no			= $dd['po_no'];
	$cust_id 		= $dd['po_idcust'];
	$cust_name		= $dd['cust_name'];
	$outd_currency	= $dd['po_currency'];
	$cust_npwp		= $dd['cust_npwp'];
	
	require "add_pengeluaran_bc30.php";
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET" || $Act == "SEL_PO"){
	require_once "add_pengeluaran_bc30.php";
}
if($Act == "CLOSE" || $Act == "CANCEL"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Pengeluaran-BC30'>";
}
if($Act=="UPL40" ){
	require "add40_pengeluaran_bc30.php";
}elseif($Act=="GET40" ){
	$data 	= new Spreadsheet_Excel_Reader($_FILES['userfile']['tmp_name']);
	#cek dokumen 
	$cekdok = 0; 
	$cek1	= $data->rowcount($sheet_index=0); #sheet Header
	for ($i=2; $i<=$cek1; $i++){
		$kode_dokumen 	= $data->val($i, 2, 0);
		if($kode_dokumen == "30"){
			$cekdok = 1;
		}
	}
	if($cekdok==1){
		$sheet7	= $data->rowcount($sheet_index=6); #sheet Barang
		for ($i=2; $i<=$sheet7; $i++){
			$no_aju	  			= $data->val($i, 1, 6);
			#tglaju
			$thnaju 			= substr($no_aju, 12,4);
			$blnaju 			= substr($no_aju, 16,2);
			$hriaju 			= substr($no_aju, 18,2);
			$tglaju2 			= "$thnaju-$blnaju-$hriaju";
			$no_urut  			= $data->val($i, 2, 6);
			$kode_hs  			= $data->val($i, 3, 6);
			$kode  	  			= $data->val($i, 4, 6);
			$nm_barang			= $data->val($i, 5, 6);
			$satuan				= $data->val($i, 10, 6);
			$jumlah				= $data->val($i, 11, 6);
			$kode_kemasan		= $data->val($i, 12, 6);
			$jumlah_kemasan		= $data->val($i, 13, 6);
			$berat				= $data->val($i, 20, 6);
			$non_rupiah			= $data->val($i, 29, 6);
			$cif_rupiah			= $data->val($i, 34, 6);
			$outh_rate			= $data->val($i, 28, 6);
			$tipe_dokumen		= $data->val($i, 67, 6);
			$harga_fob			= $data->val($i, 29, 6);
			$harga_maklon		= $data->val($i, 68, 6);
			$cek = mysql_fetch_array(mysql_query("SELECT id, log_status FROM log_outgoing WHERE no_aju='$no_aju' AND no_urut='$no_urut' AND jenis_dokpab='BC 3.0' AND log_status<>5"));
			if(empty($cek['id'])){
				mysql_query("INSERT INTO log_outgoing SET no_aju='$no_aju', tgl_aju='$tglaju2', no_urut='$no_urut', kode_hs='$kode_hs', kode='$kode', nm_barang='$nm_barang', satuan='$satuan', 
				jumlah='$jumlah', kode_kemasan='$kode_kemasan', jumlah_kemasan='$jumlah_kemasan', berat='$berat', non_rupiah='$non_rupiah', cif_rupiah='$cif_rupiah', outh_rate='$outh_rate', 
				jenis_dokpab='BC 3.0', log_status=86, log_user='$user_id', log_input='$datetime',tipe_dokumen='$tipe_dokumen',harga_fob='$harga_fob',harga_maklon='$harga_maklon'");
			}else{
				if($cek['log_status']==88){
					echo "<font color=red>Data Nomor Aju : $no_aju, no urut : $no_urut, sudah diupload</font><br>";	
				}	
			}
		}
		$sheet1	= $data->rowcount($sheet_index=0); #sheet Header
		for ($i=2; $i<=$sheet1; $i++){
			$no_aju	  	= $data->val($i, 1, 0);
			$valuta     = $data->val($i, 87, 0);
			$no_dokpab  = $data->val($i, 94, 0);
			$tgl_dokpab = date("Y-m-d",strtotime($data->val($i, 95, 0))); #$data->val($i, 95, 0);
			mysql_query("UPDATE log_outgoing SET no_dokpab='$no_dokpab', tgl_dokpab='$tgl_dokpab', tgl_sj='$tgl_dokpab', valuta='$valuta' WHERE no_aju='$no_aju' AND jenis_dokpab='BC 3.0' AND log_status=86");
		}
		$sheet2	= $data->rowcount($sheet_index=1); #sheet Etitas
		for ($i=2; $i<=$sheet2; $i++){
			$no_aju	  		= $data->val($i, 1, 1);
			$seri	    	= $data->val($i, 2, 1);
			$kode_entitas	= $data->val($i, 3, 1);
			$nm_penerima    = $data->val($i, 6, 1);
			if($kode_entitas==8){		
				mysql_query("UPDATE log_outgoing SET nm_penerima='$nm_penerima' WHERE no_aju='$no_aju' AND jenis_dokpab='BC 3.0' AND log_status=86");
			}
		}
		$sheet3	= $data->rowcount($sheet_index=2); #sheet Dokumen
		for ($i=2; $i<=$sheet3; $i++){
			$no_aju	  		= $data->val($i, 1, 2);
			$kode_dokumen	= $data->val($i, 3, 2);
			$no_dokumen	    = $data->val($i, 4, 2);
			$tgl_dokumen	= date("Y-m-d",strtotime($data->val($i, 5, 2)));
			if($kode_dokumen=="380"){		
				mysql_query("UPDATE log_outgoing SET no_invoice='$no_dokumen', tgl_invoice='$tgl_dokumen' WHERE no_aju='$no_aju' AND jenis_dokpab='BC 3.0' AND log_status=86");
			}
			if($kode_dokumen=="217"){		
				mysql_query("UPDATE log_outgoing SET no_do='$no_dokumen' WHERE no_aju='$no_aju' AND jenis_dokpab='BC 3.0' AND log_status=86");
			}					
		}
		$sheet5	= $data->rowcount($sheet_index=4); #sheet Kemasan
		for ($i=2; $i<=$sheet5; $i++){
			$no_aju	  		= $data->val($i, 1, 4);
			$kode_kemasan	= $data->val($i, 3, 4);
			$jumlah_kemasan = $data->val($i, 4, 4);
			mysql_query("UPDATE log_outgoing SET kode_kemasan='$kode_kemasan', jumlah_kemasan='$jumlah_kemasan' WHERE no_aju='$no_aju' AND jenis_dokpab='BC 3.0' AND log_status=86");
		}	
		$sheet6	= $data->rowcount($sheet_index=5); #sheet Kontainer
		for ($i=2; $i<=$sheet6; $i++){
			$no_aju	  		  = $data->val($i, 1, 5);
			$no_kontainer	  = $data->val($i, 3, 5);
			$ukuran_kontainer = $data->val($i, 4, 5);
			mysql_query("UPDATE log_outgoing SET no_kontainer='$no_kontainer', ukuran_kontainer='$ukuran_kontainer' WHERE no_aju='$no_aju' AND jenis_dokpab='BC 30' AND log_status=86");
		}	
	}else{
		echo "<font color=red><b>File excel bukan BC 3.0</b></font>";
	}
	require "add40_pengeluaran_bc30.php";
}elseif($Act=="TEMP40" ){	
	require "add40_pengeluaran_bc30.php";
}elseif($Act=="REG40"){
	if(!empty($pilih_reg)){	
		foreach ( $pilih_reg as $i => $val)
		{
			$kd_barang   = $_REQUEST['kd_barang'][$val];
			$nm_barang   = $_REQUEST['nm_barang'][$val];
			$satuan      = $_REQUEST['satuan'][$val];
			$kd_kategori = $_REQUEST['kd_kategori'][$val];
			#Insert master barang
			if($kd_kategori != "BLANK"){
				#Update kode branag ditemp
				mysql_query("UPDATE log_outgoing SET kode='$kd_barang', nm_barang='".mysql_real_escape_string($nm_barang)."', satuan='$satuan' WHERE id='$val'");
				#Insert ke master barang				
				mysql_query("INSERT INTO product SET prod_code='$kd_barang', prod_name='".mysql_real_escape_string($nm_barang)."', prod_sat='$satuan', prod_cat='$kd_kategori', prod_status=10");
				#Aktivitas user					
				mysql_query("INSERT INTO user_activity SET user_id='".$_SESSION['USER_ID']."', user_act='Menambahkan data barang - $kd_barang', 
				             user_date='$date', user_time='$time'");
			}
		}
		require "add40_pengeluaran_bc30.php";
	}else{
		require "add40_pengeluaran_bc30.php";
	}	
}elseif($Act=="DEL_TEMP40"){
	#delete all temp
	mysql_query("UPDATE log_outgoing SET log_status=5 WHERE log_status=86");
	#$status = "OK";	
	require_once "add40_pengeluaran_bc30.php";
}elseif($Act=="DELDET_TEMP40"){
	mysql_query("UPDATE log_outgoing SET log_status=86 WHERE id='".$_REQUEST['id_temp']."' ");
	require "add40_pengeluaran_bc30.php";
}elseif($Act=="PROCESS40"){
	if(!empty($pilih)){	
		foreach ( $pilih as $i => $val){
			#Update status temp
			mysql_query("UPDATE log_outgoing SET log_status=87 WHERE id='$val' ");
		}
		require "add40_pengeluaran_bc30.php";
	}else{
		require "add40_pengeluaran_bc30.php";
	}	
}elseif($Act=="INV40"){
	#Insert header pemasukan
	$qh=mysql_query("SELECT * FROM log_outgoing WHERE log_status =87 AND jenis_dokpab='BC 3.0' GROUP BY no_aju");	
	while($dh=mysql_fetch_array($qh)){
		$ary_nomor 		= get_nomorout(1);
		$ary_nosj 		= get_nosjout(1);
		if(!empty($dh['no_sj'])){
			$no_sj = $dh['no_sj'];
		}else{
			$no_sj = $ary_nosj;
		}
		#Insert Pemasukan Headar
		mysql_query("INSERT INTO outgoing SET outh_sjno='$no_sj', outh_sjdate='$dh[tgl_sj]', outh_invno='$dh[no_invoice]', outh_doctype='$dh[jenis_dokpab]',
		             outh_invdate='$dh[tgl_invoice]', outh_dono='$dh[no_do]', outh_docno='$dh[no_dokpab]', outh_docdate='$dh[tgl_dokpab]', nm_penerima='".mysql_real_escape_string($dh['nm_penerima'])."',
					 outh_status=87, outh_inputby='$user_id', outh_inputdate='$datetime', outh_nomor='$dh[flag]', no_aju='$dh[no_aju]', tgl_aju='$dh[tgl_aju]',
					 no_sppb='$dh[no_sppb]', tgl_sppb='$dh[tgl_sppb]', no_ppb='$dh[no_ppb]', tgl_ppb='$dh[tgl_ppb]', no_npe='$dh[no_npe]', tgl_npe='$dh[tgl_npe]',
					 no_jaminan='$dh[no_jaminan]', tgl_jaminan='$dh[tgl_jaminan]', jumlah_jaminan='$dh[jumlah_jaminan]', jenis_dokpab_asal='$dh[jenis_dokpab_asal]',
					 no_dokpab_asal='$dh[no_dokpab_asal]', tgl_dokpab_asal='$dh[tgl_dokpab_asal]', jatuh_tempo='$dh[jatuh_tempo]', no_kontainer='$dh[no_kontainer]',
					 ukuran_kontainer='$dh[ukuran_kontainer]', tipe_kontainer='$dh[tipe_kontainer]', jumlah_kemasan='$dh[jumlah_kemasan]', kode_kemasan='$dh[kode_kemasan]',
					 jenis_kemasan='$dh[jenis_kemasan]', valuta='$dh[valuta]', sumber='UPL40', status_dokpab='$dh[status_dokpab]', id_header='$dh[id_header]',
					 no_izin='$dh[no_izin]', tgl_izin='$dh[tgl_izin]', tujuan='$dh[tujuan]', no_kontrak='$dh[no_kontrak]', tgl_kontrak='$dh[tgl_kontrak]',
					 house_bl='$dh[house_bl]',master_bl='$dh[master_bl]', pos='$dh[pos]', nomor_manifes='$dh[nomor_manifes]', pelabuhan='$dh[pelabuhan]', 
					 tanggal_manifes='$dh[tanggal_manifes]', tipe_dokumen='$dh[tipe_dokumen]',outh_rate='$dh[outh_rate]'");
		#Aktivitas User
		mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi pengeluaran No. $no_sj', user_date='$date', user_time='$time'");
		$status = 1;
	}	
	//exit;
	if($status == 1){
		$qd = mysql_query("SELECT b.id,a.kode,a.jumlah,a.valuta,a.nilai,a.berat,a.no_urut,a.kode_hs, a.jenis_dokpab, a.no_dokpab, a.tgl_dokpab, a.tgl_sj, 
						  a.outd_scno, a.non_rupiah, a.cif_rupiah,a.harga_maklon,a.harga_fob
						  FROM log_outgoing a 
						  INNER JOIN outgoing b ON b.no_aju=a.no_aju AND a.no_dokpab=b.outh_docno					 
						  WHERE a.log_status=87 AND b.outh_status=87 AND a.jenis_dokpab='BC 3.0'");						  
		while($dd = mysql_fetch_array($qd)){
			if($dd['valuta'] =="IDR"){
				$unit_price=$dd['cif_rupiah']/$dd['jumlah'];
				$nilai = $dd['cif_rupiah'];
				$harga_maklon = $dd['harga_maklon'];
				$harga_fob = $dd['harga_fob'];
			}else{
				$unit_price=$dd['non_rupiah']/$dd['jumlah'];
				$nilai = $dd['non_rupiah'];
				$harga_maklon = $dd['harga_maklon'];
				$harga_fob = $dd['harga_fob'];
			}
			$prd2 = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$dd[kode]' AND prod_cat<>100"));
			#Insert Detail
			mysql_query("INSERT INTO outgoing_detail SET outd_hid='$dd[id]', outd_prodid='$prd2[id]', 
						 outd_qty='$dd[jumlah]', outd_price='$nilai', outd_currency='$dd[valuta]', harga_maklon='$harga_maklon', harga_fob='$harga_fob',
						 outd_weight='$dd[berat]', outd_wunit='KG', no_urut='$dd[no_urut]', kode_hs='$dd[kode_hs]', outd_price_usd='$dd[non_rupiah]', outd_price_satuan='$unit_price'");
			#insert temp
			mysql_query("INSERT INTO outgoing_temp SET outd_prodid='$prd2[id]', outd_qty='$dd[jumlah]', outd_price='$nilai', outd_currency='$dd[valuta]', 
						outd_hid='$dd[id]', outd_weight='$dd[berat]', outd_wunit='KG',  outd_price_usd='$dd[non_rupiah]'");			 
		}
		$status = 2;
	}
	//exit;
	if($status == 2){
		#cek header outgoing
		$qh = mysql_query("SELECT id, outh_sjno FROM outgoing WHERE outh_status=87 AND sumber='UPL40' AND outh_doctype='BC 3.0' AND outh_inputby='$user_id' ORDER BY id ASC");
		while($dh=mysql_fetch_array($qh)){
			#cek item ditemp
			if (count($message)==0 ){
				#Update Status
				#$qd = mysql_query("UPDATE outgoing SET outh_status=14 WHERE id='$dh[id]'");
				#get Exbc
				get_outexbc($start_date,$end_date,$dh['id']);					
			}
			#delete temp
			mysql_query("DELETE FROM outgoing_temp WHERE outd_hid='$dh[id]'");
			#Update Status
			$qd = mysql_query("UPDATE outgoing SET outh_status=15 WHERE id='$dh[id]'");
			#Insert History User
			$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting transaksi pengeluaran No. $dh[outh_sjno]', user_date='$date', 
					 user_time='$time'");
			#$status = "OK";
		}		
		$status = 3;
	}
	if($status == 3){	
		#update status log
		mysql_query("UPDATE log_outgoing SET log_status =88 WHERE log_status =87 AND jenis_dokpab='BC 3.0'");
		#update status
		mysql_query("UPDATE outgoing SET outh_status=20 WHERE outh_status=15 AND outh_doctype='BC 3.0' AND sumber='UPL40'");
		$message[] = "Data Sukses dikirim.";	
		$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('<?=$outh_id;?>');
			//}else{
				//self.history.back();
			//}
		</script>			
	<?php  
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Pengeluaran-BC30'>";	
		exit;
	}
}	
?>