<?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'] : '';
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_bc33.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_bc33.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_bc33.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_bc33.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'")
												  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];
							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'");
							}			 
						}
					}
				}	
				#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_bc33.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];
						#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' 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'");
					}	
					#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'");
				#Cek FIFO 1
				$q1 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
				AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
				while($dt1=mysql_fetch_array($q1)){
					#exbc
					$exbc1=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
							b.prod_code, b.prod_name, b.prod_sat FROM
							(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
							(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
							(stock_price/stock_qty) price 
							FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
							UNION ALL
							SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
							(b.incd_price/b.incd_qty) price 
							FROM incoming a
							INNER JOIN incoming_detail b ON b.incd_hid=a.id
							WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
							FROM outgoing_dokasal a
							INNER JOIN outgoing b ON b.id=a.outh_hid
							WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
							INNER JOIN product b ON b.id=a.prodid
							WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
							WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt1[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
					if($exbc1['qty_bal'] >= $dt1['outd_qty']){
						$qty1   = $dt1['outd_qty'];
						$price1 = $qty1 * $exbc1['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt1[outd_prodid]', jumlah='$dt1[outd_qty]', 
									 jenis_dokpab_asal='$exbc1[doctype]', no_dokpab_asal='$exbc1[docno]', tgl_dokpab_asal='$exbc1[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt1[outd_prodid]' AND outd_hid='$outh_id'");			 
					}else{
						$qty1   = $dt1['outd_qty'] - $exbc1['qty_bal'];
						$price1 = $exbc1['qty_bal'] * $exbc1['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt1[outd_prodid]', jumlah='$exbc1[qty_bal]', 
									 jenis_dokpab_asal='$exbc1[doctype]', no_dokpab_asal='$exbc1[docno]', tgl_dokpab_asal='$exbc1[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='$qty1' WHERE outd_prodid='$dt1[outd_prodid]' AND outd_hid='$outh_id'");								
					}		
				}	
				#Cek FIFO 2
				$q2 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
				AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
				while($dt2=mysql_fetch_array($q2)){
					#exbc
					$exbc2=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
							b.prod_code, b.prod_name, b.prod_sat FROM
							(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
							(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
							(stock_price/stock_qty) price 
							FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
							UNION ALL
							SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
							(b.incd_price/b.incd_qty) price 
							FROM incoming a
							INNER JOIN incoming_detail b ON b.incd_hid=a.id
							WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
							FROM outgoing_dokasal a
							INNER JOIN outgoing b ON b.id=a.outh_hid
							WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
							INNER JOIN product b ON b.id=a.prodid
							WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
							WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt2[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
					if($exbc2['qty_bal'] >= $dt2['outd_qty']){
						$qty2   = $dt2['outd_qty'];
						$price2 = $qty2 * $exbc2['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt2[outd_prodid]', jumlah='$dt2[outd_qty]', 
									 jenis_dokpab_asal='$exbc2[doctype]', no_dokpab_asal='$exbc2[docno]', tgl_dokpab_asal='$exbc2[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt2[outd_prodid]' AND outd_hid='$outh_id'");			 
					}else{
						$qty2   = $dt2['outd_qty'] - $exbc2['qty_bal'];
						$price2 = $exbc2['qty_bal'] * $exbc2['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt2[outd_prodid]', jumlah='$exbc2[qty_bal]', 
									 jenis_dokpab_asal='$exbc2[doctype]', no_dokpab_asal='$exbc2[docno]', tgl_dokpab_asal='$exbc2[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='$qty2' WHERE outd_prodid='$dt2[outd_prodid]' AND outd_hid='$outh_id'");								
					}		
				}
				#Cek FIFO 3
				$q3 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
				AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
				while($dt3=mysql_fetch_array($q3)){
					#exbc
					$exbc3=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
							b.prod_code, b.prod_name, b.prod_sat FROM
							(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
							(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
							(stock_price/stock_qty) price 
							FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
							UNION ALL
							SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
							(b.incd_price/b.incd_qty) price 
							FROM incoming a
							INNER JOIN incoming_detail b ON b.incd_hid=a.id
							WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
							FROM outgoing_dokasal a
							INNER JOIN outgoing b ON b.id=a.outh_hid
							WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
							INNER JOIN product b ON b.id=a.prodid
							WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
							WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt3[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
					if($exbc3['qty_bal'] >= $dt3['outd_qty']){
						$qty3   = $dt3['outd_qty'];
						$price3 = $qty3 * $exbc3['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt3[outd_prodid]', jumlah='$dt3[outd_qty]', 
									 jenis_dokpab_asal='$exbc3[doctype]', no_dokpab_asal='$exbc3[docno]', tgl_dokpab_asal='$exbc3[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt3[outd_prodid]' AND outd_hid='$outh_id'");			 
					}else{
						$qty3   = $dt3['outd_qty'] - $exbc3['qty_bal'];
						$price3 = $exbc3['qty_bal'] * $exbc3['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt3[outd_prodid]', jumlah='$exbc3[qty_bal]', 
									 jenis_dokpab_asal='$exbc3[doctype]', no_dokpab_asal='$exbc3[docno]', tgl_dokpab_asal='$exbc3[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='$qty3' WHERE outd_prodid='$dt3[outd_prodid]' AND outd_hid='$outh_id'");								
					}		
				}
				#Cek FIFO 4
				$q4 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
				AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
				while($dt4=mysql_fetch_array($q4)){
					#exbc
					$exbc4=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
							b.prod_code, b.prod_name, b.prod_sat FROM
							(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
							(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
							(stock_price/stock_qty) price 
							FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
							UNION ALL
							SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
							(b.incd_price/b.incd_qty) price 
							FROM incoming a
							INNER JOIN incoming_detail b ON b.incd_hid=a.id
							WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
							FROM outgoing_dokasal a
							INNER JOIN outgoing b ON b.id=a.outh_hid
							WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
							INNER JOIN product b ON b.id=a.prodid
							WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
							WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt4[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
					if($exbc4['qty_bal'] >= $dt4['outd_qty']){
						$qty4   = $dt4['outd_qty'];
						$price4 = $qty4 * $exbc4['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt4[outd_prodid]', jumlah='$dt4[outd_qty]', 
									 jenis_dokpab_asal='$exbc4[doctype]', no_dokpab_asal='$exbc4[docno]', tgl_dokpab_asal='$exbc4[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt4[outd_prodid]' AND outd_hid='$outh_id'");			 
					}else{
						$qty4   = $dt4['outd_qty'] - $exbc4['qty_bal'];
						$price4 = $exbc4['qty_bal'] * $exbc4['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt4[outd_prodid]', jumlah='$exbc4[qty_bal]', 
									 jenis_dokpab_asal='$exbc4[doctype]', no_dokpab_asal='$exbc4[docno]', tgl_dokpab_asal='$exbc4[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='$qty4' WHERE outd_prodid='$dt4[outd_prodid]' AND outd_hid='$outh_id'");								
					}		
				}
				#Cek FIFO 5
				$q5 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
				AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
				while($dt5=mysql_fetch_array($q5)){
					#exbc
					$exbc5=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
							b.prod_code, b.prod_name, b.prod_sat FROM
							(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
							(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
							(stock_price/stock_qty) price 
							FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
							UNION ALL
							SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
							(b.incd_price/b.incd_qty) price 
							FROM incoming a
							INNER JOIN incoming_detail b ON b.incd_hid=a.id
							WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
							FROM outgoing_dokasal a
							INNER JOIN outgoing b ON b.id=a.outh_hid
							WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
							INNER JOIN product b ON b.id=a.prodid
							WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
							WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt5[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
					if($exbc5['qty_bal'] >= $dt5['outd_qty']){
						$qty5   = $dt5['outd_qty'];
						$price5 = $qty5 * $exbc5['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt5[outd_prodid]', jumlah='$dt5[outd_qty]', 
									 jenis_dokpab_asal='$exbc5[doctype]', no_dokpab_asal='$exbc5[docno]', tgl_dokpab_asal='$exbc5[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt5[outd_prodid]' AND outd_hid='$outh_id'");			 
					}else{
						$qty5   = $dt5['outd_qty'] - $exbc5['qty_bal'];
						$price5 = $exbc5['qty_bal'] * $exbc5['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt5[outd_prodid]', jumlah='$exbc5[qty_bal]', 
									 jenis_dokpab_asal='$exbc5[doctype]', no_dokpab_asal='$exbc5[docno]', tgl_dokpab_asal='$exbc5[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='$qty5' WHERE outd_prodid='$dt5[outd_prodid]' AND outd_hid='$outh_id'");								
					}		
				}	
				#Cek FIFO 6
				$q6 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
				AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
				while($dt6=mysql_fetch_array($q6)){
					#exbc
					$exbc6=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
							b.prod_code, b.prod_name, b.prod_sat FROM
							(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
							(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
							(stock_price/stock_qty) price 
							FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
							UNION ALL
							SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
							(b.incd_price/b.incd_qty) price 
							FROM incoming a
							INNER JOIN incoming_detail b ON b.incd_hid=a.id
							WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
							FROM outgoing_dokasal a
							INNER JOIN outgoing b ON b.id=a.outh_hid
							WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
							INNER JOIN product b ON b.id=a.prodid
							WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
							WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt6[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
					if($exbc6['qty_bal'] >= $dt6['outd_qty']){
						$qty6   = $dt6['outd_qty'];
						$price6 = $qty6 * $exbc6['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt6[outd_prodid]', jumlah='$dt6[outd_qty]', 
									 jenis_dokpab_asal='$exbc6[doctype]', no_dokpab_asal='$exbc6[docno]', tgl_dokpab_asal='$exbc6[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt6[outd_prodid]' AND outd_hid='$outh_id'");			 
					}else{
						$qty6   = $dt6['outd_qty'] - $exbc6['qty_bal'];
						$price6 = $exbc6['qty_bal'] * $exbc6['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt6[outd_prodid]', jumlah='$exbc6[qty_bal]', 
									 jenis_dokpab_asal='$exbc6[doctype]', no_dokpab_asal='$exbc6[docno]', tgl_dokpab_asal='$exbc6[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='$qty6' WHERE outd_prodid='$dt6[outd_prodid]' AND outd_hid='$outh_id'");								
					}		
				}
				#Cek FIFO 7
				$q7 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
				AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
				while($dt7=mysql_fetch_array($q7)){
					#exbc
					$exbc7=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
							b.prod_code, b.prod_name, b.prod_sat FROM
							(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
							(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
							(stock_price/stock_qty) price 
							FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
							UNION ALL
							SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
							(b.incd_price/b.incd_qty) price 
							FROM incoming a
							INNER JOIN incoming_detail b ON b.incd_hid=a.id
							WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
							FROM outgoing_dokasal a
							INNER JOIN outgoing b ON b.id=a.outh_hid
							WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
							INNER JOIN product b ON b.id=a.prodid
							WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
							WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt7[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
					if($exbc7['qty_bal'] >= $dt7['outd_qty']){
						$qty7   = $dt7['outd_qty'];
						$price7 = $qty7 * $exbc7['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt7[outd_prodid]', jumlah='$dt7[outd_qty]', 
									 jenis_dokpab_asal='$exbc7[doctype]', no_dokpab_asal='$exbc7[docno]', tgl_dokpab_asal='$exbc7[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt7[outd_prodid]' AND outd_hid='$outh_id'");			 
					}else{
						$qty7   = $dt7['outd_qty'] - $exbc7['qty_bal'];
						$price7 = $exbc7['qty_bal'] * $exbc7['pricein'];
						#insert detail
						mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt7[outd_prodid]', jumlah='$exbc7[qty_bal]', 
									 jenis_dokpab_asal='$exbc7[doctype]', no_dokpab_asal='$exbc7[docno]', tgl_dokpab_asal='$exbc7[docdate]',log_status=20");
						#update qty temp
						mysql_query("UPDATE outgoing_temp SET outd_qty='$qty7' WHERE outd_prodid='$dt7[outd_prodid]' AND outd_hid='$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'");
			#Cek FIFO 1
			$q1 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
			AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
			while($dt1=mysql_fetch_array($q1)){
				#exbc
				$exbc1=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
						b.prod_code, b.prod_name, b.prod_sat FROM
						(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
						(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
						(stock_price/stock_qty) price 
						FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
						UNION ALL
						SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
						(b.incd_price/b.incd_qty) price 
						FROM incoming a
						INNER JOIN incoming_detail b ON b.incd_hid=a.id
						WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
						FROM outgoing_dokasal a
						INNER JOIN outgoing b ON b.id=a.outh_hid
						WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
						INNER JOIN product b ON b.id=a.prodid
						WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
						WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt1[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
				if($exbc1['qty_bal'] >= $dt1['outd_qty']){
					$qty1   = $dt1['outd_qty'];
					$price1 = $qty1 * $exbc1['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt1[outd_prodid]', jumlah='$dt1[outd_qty]', 
								 jenis_dokpab_asal='$exbc1[doctype]', no_dokpab_asal='$exbc1[docno]', tgl_dokpab_asal='$exbc1[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt1[outd_prodid]' AND outd_hid='$outh_id'");			 
				}else{
					$qty1   = $dt1['outd_qty'] - $exbc1['qty_bal'];
					$price1 = $exbc1['qty_bal'] * $exbc1['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt1[outd_prodid]', jumlah='$exbc1[qty_bal]', 
								 jenis_dokpab_asal='$exbc1[doctype]', no_dokpab_asal='$exbc1[docno]', tgl_dokpab_asal='$exbc1[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='$qty1' WHERE outd_prodid='$dt1[outd_prodid]' AND outd_hid='$outh_id'");								
				}		
			}	
			#Cek FIFO 2
			$q2 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
			AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
			while($dt2=mysql_fetch_array($q2)){
				#exbc
				$exbc2=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
						b.prod_code, b.prod_name, b.prod_sat FROM
						(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
						(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
						(stock_price/stock_qty) price 
						FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
						UNION ALL
						SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
						(b.incd_price/b.incd_qty) price 
						FROM incoming a
						INNER JOIN incoming_detail b ON b.incd_hid=a.id
						WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
						FROM outgoing_dokasal a
						INNER JOIN outgoing b ON b.id=a.outh_hid
						WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
						INNER JOIN product b ON b.id=a.prodid
						WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
						WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt2[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
				if($exbc2['qty_bal'] >= $dt2['outd_qty']){
					$qty2   = $dt2['outd_qty'];
					$price2 = $qty2 * $exbc2['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt2[outd_prodid]', jumlah='$dt2[outd_qty]', 
								 jenis_dokpab_asal='$exbc2[doctype]', no_dokpab_asal='$exbc2[docno]', tgl_dokpab_asal='$exbc2[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt2[outd_prodid]' AND outd_hid='$outh_id'");			 
				}else{
					$qty2   = $dt2['outd_qty'] - $exbc2['qty_bal'];
					$price2 = $exbc2['qty_bal'] * $exbc2['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt2[outd_prodid]', jumlah='$exbc2[qty_bal]', 
								 jenis_dokpab_asal='$exbc2[doctype]', no_dokpab_asal='$exbc2[docno]', tgl_dokpab_asal='$exbc2[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='$qty2' WHERE outd_prodid='$dt2[outd_prodid]' AND outd_hid='$outh_id'");								
				}		
			}
			#Cek FIFO 3
			$q3 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
			AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
			while($dt3=mysql_fetch_array($q3)){
				#exbc
				$exbc3=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
						b.prod_code, b.prod_name, b.prod_sat FROM
						(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
						(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
						(stock_price/stock_qty) price 
						FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
						UNION ALL
						SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
						(b.incd_price/b.incd_qty) price 
						FROM incoming a
						INNER JOIN incoming_detail b ON b.incd_hid=a.id
						WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
						FROM outgoing_dokasal a
						INNER JOIN outgoing b ON b.id=a.outh_hid
						WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
						INNER JOIN product b ON b.id=a.prodid
						WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
						WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt3[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
				if($exbc3['qty_bal'] >= $dt3['outd_qty']){
					$qty3   = $dt3['outd_qty'];
					$price3 = $qty3 * $exbc3['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt3[outd_prodid]', jumlah='$dt3[outd_qty]', 
								 jenis_dokpab_asal='$exbc3[doctype]', no_dokpab_asal='$exbc3[docno]', tgl_dokpab_asal='$exbc3[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt3[outd_prodid]' AND outd_hid='$outh_id'");			 
				}else{
					$qty3   = $dt3['outd_qty'] - $exbc3['qty_bal'];
					$price3 = $exbc3['qty_bal'] * $exbc3['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt3[outd_prodid]', jumlah='$exbc3[qty_bal]', 
								 jenis_dokpab_asal='$exbc3[doctype]', no_dokpab_asal='$exbc3[docno]', tgl_dokpab_asal='$exbc3[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='$qty3' WHERE outd_prodid='$dt3[outd_prodid]' AND outd_hid='$outh_id'");								
				}		
			}
			#Cek FIFO 4
			$q4 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
			AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
			while($dt4=mysql_fetch_array($q4)){
				#exbc
				$exbc4=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
						b.prod_code, b.prod_name, b.prod_sat FROM
						(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
						(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
						(stock_price/stock_qty) price 
						FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
						UNION ALL
						SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
						(b.incd_price/b.incd_qty) price 
						FROM incoming a
						INNER JOIN incoming_detail b ON b.incd_hid=a.id
						WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
						FROM outgoing_dokasal a
						INNER JOIN outgoing b ON b.id=a.outh_hid
						WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
						INNER JOIN product b ON b.id=a.prodid
						WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
						WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt4[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
				if($exbc4['qty_bal'] >= $dt4['outd_qty']){
					$qty4   = $dt4['outd_qty'];
					$price4 = $qty4 * $exbc4['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt4[outd_prodid]', jumlah='$dt4[outd_qty]', 
								 jenis_dokpab_asal='$exbc4[doctype]', no_dokpab_asal='$exbc4[docno]', tgl_dokpab_asal='$exbc4[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt4[outd_prodid]' AND outd_hid='$outh_id'");			 
				}else{
					$qty4   = $dt4['outd_qty'] - $exbc4['qty_bal'];
					$price4 = $exbc4['qty_bal'] * $exbc4['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt4[outd_prodid]', jumlah='$exbc4[qty_bal]', 
								 jenis_dokpab_asal='$exbc4[doctype]', no_dokpab_asal='$exbc4[docno]', tgl_dokpab_asal='$exbc4[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='$qty4' WHERE outd_prodid='$dt4[outd_prodid]' AND outd_hid='$outh_id'");								
				}		
			}
			#Cek FIFO 5
			$q5 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
			AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
			while($dt5=mysql_fetch_array($q5)){
				#exbc
				$exbc5=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
						b.prod_code, b.prod_name, b.prod_sat FROM
						(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
						(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
						(stock_price/stock_qty) price 
						FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
						UNION ALL
						SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
						(b.incd_price/b.incd_qty) price 
						FROM incoming a
						INNER JOIN incoming_detail b ON b.incd_hid=a.id
						WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
						FROM outgoing_dokasal a
						INNER JOIN outgoing b ON b.id=a.outh_hid
						WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
						INNER JOIN product b ON b.id=a.prodid
						WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
						WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt5[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
				if($exbc5['qty_bal'] >= $dt5['outd_qty']){
					$qty5   = $dt5['outd_qty'];
					$price5 = $qty5 * $exbc5['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt5[outd_prodid]', jumlah='$dt5[outd_qty]', 
								 jenis_dokpab_asal='$exbc5[doctype]', no_dokpab_asal='$exbc5[docno]', tgl_dokpab_asal='$exbc5[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt5[outd_prodid]' AND outd_hid='$outh_id'");			 
				}else{
					$qty5   = $dt5['outd_qty'] - $exbc5['qty_bal'];
					$price5 = $exbc5['qty_bal'] * $exbc5['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt5[outd_prodid]', jumlah='$exbc5[qty_bal]', 
								 jenis_dokpab_asal='$exbc5[doctype]', no_dokpab_asal='$exbc5[docno]', tgl_dokpab_asal='$exbc5[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='$qty5' WHERE outd_prodid='$dt5[outd_prodid]' AND outd_hid='$outh_id'");								
				}		
			}	
			#Cek FIFO 6
			$q6 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
			AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
			while($dt6=mysql_fetch_array($q6)){
				#exbc
				$exbc6=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
						b.prod_code, b.prod_name, b.prod_sat FROM
						(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
						(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
						(stock_price/stock_qty) price 
						FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
						UNION ALL
						SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
						(b.incd_price/b.incd_qty) price 
						FROM incoming a
						INNER JOIN incoming_detail b ON b.incd_hid=a.id
						WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
						FROM outgoing_dokasal a
						INNER JOIN outgoing b ON b.id=a.outh_hid
						WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
						INNER JOIN product b ON b.id=a.prodid
						WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
						WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt6[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
				if($exbc6['qty_bal'] >= $dt6['outd_qty']){
					$qty6   = $dt6['outd_qty'];
					$price6 = $qty6 * $exbc6['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt6[outd_prodid]', jumlah='$dt6[outd_qty]', 
								 jenis_dokpab_asal='$exbc6[doctype]', no_dokpab_asal='$exbc6[docno]', tgl_dokpab_asal='$exbc6[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt6[outd_prodid]' AND outd_hid='$outh_id'");			 
				}else{
					$qty6   = $dt6['outd_qty'] - $exbc6['qty_bal'];
					$price6 = $exbc6['qty_bal'] * $exbc6['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt6[outd_prodid]', jumlah='$exbc6[qty_bal]', 
								 jenis_dokpab_asal='$exbc6[doctype]', no_dokpab_asal='$exbc6[docno]', tgl_dokpab_asal='$exbc6[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='$qty6' WHERE outd_prodid='$dt6[outd_prodid]' AND outd_hid='$outh_id'");								
				}		
			}
			#Cek FIFO 7
			$q7 = mysql_query("SELECT a.*, b.prod_cat FROM outgoing_temp a INNER JOIN product b ON b.id=a.outd_prodid WHERE a.outd_hid='$outh_id' 
			AND b.prod_cat IN(1,6,7) AND outd_qty > 0");
			while($dt7=mysql_fetch_array($q7)){
				#exbc
				$exbc7=mysql_fetch_array(mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) qty_bal, b.price pricein,
						b.prod_code, b.prod_name, b.prod_sat FROM
						(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
						(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, 
						(stock_price/stock_qty) price 
						FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc=1
						UNION ALL
						SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
						(b.incd_price/b.incd_qty) price 
						FROM incoming a
						INNER JOIN incoming_detail b ON b.incd_hid=a.id
						WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
						UNION ALL
						SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
						FROM outgoing_dokasal a
						INNER JOIN outgoing b ON b.id=a.outh_hid
						WHERE b.outh_status IN(11,20) AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date') a
						INNER JOIN product b ON b.id=a.prodid
						WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
						WHERE (b.inqty-b.outqty) > 0  AND b.prodid='$dt7[outd_prodid]' ORDER BY b.docdate ASC LIMIT 0,1;"));
				if($exbc7['qty_bal'] >= $dt7['outd_qty']){
					$qty7   = $dt7['outd_qty'];
					$price7 = $qty7 * $exbc7['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt7[outd_prodid]', jumlah='$dt7[outd_qty]', 
								 jenis_dokpab_asal='$exbc7[doctype]', no_dokpab_asal='$exbc7[docno]', tgl_dokpab_asal='$exbc7[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='0' WHERE outd_prodid='$dt7[outd_prodid]' AND outd_hid='$outh_id'");			 
				}else{
					$qty7   = $dt7['outd_qty'] - $exbc7['qty_bal'];
					$price7 = $exbc7['qty_bal'] * $exbc7['pricein'];
					#insert detail
					mysql_query("INSERT INTO outgoing_dokasal SET outh_hid='$outh_id', prodid='$dt7[outd_prodid]', jumlah='$exbc7[qty_bal]', 
								 jenis_dokpab_asal='$exbc7[doctype]', no_dokpab_asal='$exbc7[docno]', tgl_dokpab_asal='$exbc7[docdate]',log_status=20");
					#update qty temp
					mysql_query("UPDATE outgoing_temp SET outd_qty='$qty7' WHERE outd_prodid='$dt7[outd_prodid]' AND outd_hid='$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_bc33.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' ");
	}
}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_bc33.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_bc33.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'");
	}
}
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_bc33.php";
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET" || $Act == "SEL_PO"){
	require_once "add_pengeluaran_bc33.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Pengeluaran-BC33'>";
}
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-BC33'>";	
		exit;
	}
}	
?>