<?php
$message 	 	= array();
$Act         	= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$nomor          = isset($_REQUEST['nomor']) ? $_REQUEST['nomor'] : '';
$no_bpb   		= isset($_REQUEST['no_bpb']) ? $_REQUEST['no_bpb'] : '';
$tgl_bpb   		= isset($_REQUEST['tgl_bpb']) ? $_REQUEST['tgl_bpb'] : '';
$pemasok 		= isset($_REQUEST['pemasok']) ? $_REQUEST['pemasok'] : '';
$no_invoice     = isset($_REQUEST['no_invoice']) ? $_REQUEST['no_invoice'] : '';
$tgl_invoice 	= isset($_REQUEST['tgl_invoice']) ? $_REQUEST['tgl_invoice'] : '';
$no_do 		    = isset($_REQUEST['no_do']) ? $_REQUEST['no_do'] : '';
$catatan 		= isset($_REQUEST['catatan']) ? $_REQUEST['catatan'] : '';
$jenis_dokpab 	= isset($_REQUEST['jenis_dokpab']) ? $_REQUEST['jenis_dokpab'] : '';
$no_dokpab 		= isset($_REQUEST['no_dokpab']) ? $_REQUEST['no_dokpab'] : '';
$tgl_dokpab 	= isset($_REQUEST['tgl_dokpab']) ? $_REQUEST['tgl_dokpab'] : '';
$pilih			= isset($_REQUEST['pilih']) ? $_REQUEST['pilih'] : '';
$pilih_reg		= isset($_REQUEST['pilih_reg']) ? $_REQUEST['pilih_reg'] : '';
$start_date		= isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : get_startdate();
$end_date		= isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : get_enddate();

$new_nomor 		= get_nomorinc(1);
$new_nobpb 		= get_nobpbinc(1);
//echo "$new_nomor==>$new_nobpb";
$Act_Det 	 	= isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$inch_id 	 	= isset($_REQUEST['inch_id']) ? $_REQUEST['inch_id'] : '';
$inch_bpbdate	= isset($_REQUEST['inch_bpbdate']) ? $_REQUEST['inch_bpbdate'] : '';
$sup_id			= isset($_REQUEST['sup_id']) ? $_REQUEST['sup_id'] : '';
$sup_name		= isset($_REQUEST['sup_name']) ? $_REQUEST['sup_name'] : '';
$inch_invno  	= isset($_REQUEST['inch_invno']) ? $_REQUEST['inch_invno'] : '';
$inch_invdate	= isset($_REQUEST['inch_invdate']) ? $_REQUEST['inch_invdate'] : '';
$inch_dono		= isset($_REQUEST['inch_dono']) ? $_REQUEST['inch_dono'] : '';
$inch_docid	 	= isset($_REQUEST['inch_docid']) ? $_REQUEST['inch_docid'] : '';
$tujuan	= isset($_REQUEST['tujuan']) ? $_REQUEST['tujuan'] : '';
$inch_docdate	= isset($_REQUEST['inch_docdate']) ? $_REQUEST['inch_docdate'] : '';
$inch_docno		= isset($_REQUEST['inch_docno']) ? $_REQUEST['inch_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'] : '';
$doc			= mysql_fetch_array(mysql_query("SELECT doc_type FROM document_type WHERE id='$inch_docid'"));
$sup			= mysql_fetch_array(mysql_query("SELECT sup_name FROM supplier WHERE id='$sup_id'"));
$val_date       = get_validationdate($inch_bpbdate);
$po_no	 	 	= isset($_REQUEST['po_no']) ? $_REQUEST['po_no'] : '';
$pilih			= isset($_REQUEST['pilih']) ? $_REQUEST['pilih'] : '';
/*if(empty($inch_id)){ 
	$inch_bpbno = get_bpbno();
}else{*/
	$inch_bpbno	= isset($_REQUEST['inch_bpbno']) ? $_REQUEST['inch_bpbno'] : get_bpbno();
#}
$incd_prodid    	= isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$incd_qty   		= isset($_REQUEST['incdqty']) ? $_REQUEST['incdqty'] : '';
$incd_price 		= isset($_REQUEST['incdprice']) ? $_REQUEST['incdprice'] : '';
$incd_currency  	= isset($_REQUEST['inch_currency']) ? $_REQUEST['inch_currency'] : '';
$incd_weight  		= isset($_REQUEST['incdweight']) ? $_REQUEST['incdweight'] : '';
$incd_price_rate  	= isset($_REQUEST['n_rate']) ? $_REQUEST['n_rate'] : '';
$incd_amount	    = isset($_REQUEST['incdamount']) ? $_REQUEST['incdamount'] : '';
$incd_price_usd  	= isset($_REQUEST['n_usd']) ? $_REQUEST['n_usd'] : '';
$sup_npwp		    = isset($_REQUEST['sup_npwp']) ? $_REQUEST['sup_npwp'] : '';
$tipe_dokumen	    = isset($_REQUEST['tipe_dokumen']) ? $_REQUEST['tipe_dokumen'] : '';
$harga_dokumen	    = isset($_REQUEST['harga_dokumen']) ? $_REQUEST['harga_dokumen'] : 0;
$no_urut	    	= isset($_REQUEST['incdnourut']) ? $_REQUEST['incdnourut'] : 0;
$incd_scno	    	= isset($_REQUEST['incdscno']) ? $_REQUEST['incdscno'] : 0;
if($Act =="SAVEM"){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "addm_pemasukan_bc40.php";
		exit;
	}elseif(empty($incd_price_rate) || $incd_price_rate==0){
		echo "<font color='red'>Kurs tanggal : $inch_docdate belum disetting</font><br>";
		#reload
		require_once "addm_pemasukan_bc40.php";
		exit;
	}else{
		if(count($message)==0){	
			if(empty($inch_id)){
				#cek qty remain < qty terima
				if($po_no!="10"){
					foreach($pilih as $i => $id){
						$incd_qty		= $_REQUEST['incd_qty2'][$id];
						$incd_rmn		= $_REQUEST['incd_rmn'][$id];
						$prod_name		= $_REQUEST['prod_name2'][$id];
						if($incd_qty > $incd_rmn){
							echo "<font color='red'>Barang :$prod_name QTY INPUT Lebih besar dari QTY REMAIN($incd_qty > $incd_rmn)</font><br>";
							#reload
							require_once "addm_pemasukan_bc40.php";
							exit;
						}
					}
				}
				$qrySave=mysql_query("INSERT INTO incoming SET inch_bpbno='$inch_bpbno', inch_bpbdate='$inch_bpbdate', inch_supid='$sup_id', po_no='$po_no',
								  inch_invno='$inch_invno', inch_invdate='$inch_invdate', inch_dono='$inch_dono', no_aju='$no_aju', tgl_aju='$tgl_aju',
								  tujuan='$tujuan', inch_docid='$inch_docid', inch_inputby='$user_id', sumber='MANUAL', inch_doctype='$doc[doc_type]',
								  inch_inputdate='$datetime', inch_status=15, inch_docdate='$inch_docdate', inch_docno='$inch_docno', nm_pemasok='$sup[sup_name]',
								  id_pemasok='$sup_npwp', inch_currency='$incd_currency', inch_rate='$incd_price_rate'") 
								  or die ("Gagal query".mysql_error());
				if($qrySave){
					if($po_no=="10"){
						$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM incoming"));
						$inch_id = $data->id;
						$query = mysql_query("SELECT * FROM incoming_temp WHERE incd_inputby='$user_id' AND incd_status=10 ORDER BY id ASC");
						while($dt=mysql_fetch_object($query)){
							mysql_query("INSERT INTO incoming_detail SET incd_hid='$inch_id', incd_prodid='$dt->incd_prodid', 
										 incd_qty='$dt->incd_qty', incd_price='$dt->incd_price', incd_currency='$dt->incd_currency', 
										 incd_weight='$dt->incd_weight', incd_wunit='$dt->incd_wunit',
										 incd_price_rate='$dt->incd_price_rate', incd_price_usd='$dt->incd_price_usd', 
										 incd_price_satuan='$dt->incd_price_satuan',no_urut='$dt->no_urut', incd_scno='$dt->incd_scno'");
						}
						mysql_query("DELETE FROM incoming_temp WHERE incd_inputby='$user_id' AND incd_status=10");
					}else{
						$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM incoming"));
						$inch_id = $data->id;
						foreach($pilih as $i => $id){
							$incd_prodid	= $_REQUEST['incd_prodid2'][$id];
							$incd_qty		= $_REQUEST['incd_qty2'][$id];
							$pod_price		= $_REQUEST['pod_price'][$id];
							if($incd_currency=="IDR"){
								$incd_price		= $incd_qty*$pod_price;
								$incd_price_usd  = 0;
							}else{
								$incd_price		= ($incd_qty*$pod_price)*$incd_price_rate;
								$incd_price_usd  = $incd_qty*$pod_price;
							}
							$incd_weight	= $_REQUEST['incd_weight2'][$id];
							$no_urut		= $_REQUEST['no_urut2'][$id];
							$incd_scno		= $_REQUEST['incd_scno2'][$id];
							if($incd_qty > 0){
								mysql_query("INSERT INTO incoming_detail SET incd_hid='$inch_id', incd_prodid='$incd_prodid', incd_price_usd='$incd_price_usd',
											 incd_qty='$incd_qty', incd_price='$incd_price', incd_currency='$incd_currency', incd_price_rate='$incd_price_rate',
											 incd_weight='$incd_weight', incd_wunit='KG', no_urut='$no_urut', incd_price_satuan='$pod_price', incd_scno='$incd_scno'");
							}
						}
					}	
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', 
									  user_act='Menambahkan transaksi pemasukan No. $inch_bpbno:$inch_docno', 
									  user_date='$date', user_time='$time'");
							  
			}elseif(!empty($inch_id) && ($Act_Det <> "ADD_DET")){
				$qrySave=mysql_query("UPDATE incoming SET inch_bpbno='$inch_bpbno', inch_bpbdate='$inch_bpbdate', inch_supid='$sup_id', inch_doctype='$doc[doc_type]', 
									  inch_invno='$inch_invno', inch_invdate='$inch_invdate', inch_dono='$inch_dono', no_aju='$no_aju', tgl_aju='$tgl_aju', 
									  tujuan='$tujuan', inch_docid='$inch_docid', inch_docdate='$inch_docdate', inch_docno='$inch_docno', nm_pemasok='$sup[sup_name]', 
									  id_pemasok='$sup_npwp', inch_currency='$incd_currency', inch_rate='$incd_price_rate', inch_status=15
									  WHERE id='$inch_id'") or die ("Gagal query".mysql_error());
				if($qrySave){
				#Update Detail
					foreach($id_det as $i => $id){
						$incd_qty		    = $_REQUEST['incd_qty'][$id];
						$incd_price_satuan	= $_REQUEST['incd_price_satuan'][$id];
						$incd_price		    = $_REQUEST['incd_price'][$id];
						$incd_weight	    = $_REQUEST['incd_weight'][$id];						
						$incd_currency		= $_REQUEST['incd_currency'][$id];
						//$incd_desc		= $_REQUEST['incd_desc'][$id];
						$incd_price_usd		= $_REQUEST['incd_price_usd'][$id];
						$no_urut			= $_REQUEST['no_urut3'][$id];
						$incd_scno			= $_REQUEST['incd_scno3'][$id];
						mysql_query("UPDATE incoming_detail SET incd_qty='$incd_qty', incd_qty_temp='$incd_qty', incd_price='$incd_price', incd_weight='$incd_weight',
						incd_price_satuan='$incd_price_satuan', incd_price_usd='$incd_price_usd', incd_currency='$incd_currency', no_urut='$no_urut', incd_scno='$incd_scno' WHERE id='$id' ");
					}		
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah transaksi pemasukan No. $inch_bpbno:$inch_docno', user_date='$date', 
					user_time='$time'");
				}							  
			}	
			if(!empty($po_no)){
				#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 IN(20,30) AND b.po_no='$po_no'"));
				$qin = mysql_fetch_array(mysql_query("SELECT SUM(a.incd_qty) qty_in FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
					   WHERE b.inch_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'");
				}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 =="DETAILM"){
	$qd = mysql_query("SELECT a.*,b.sup_name FROM incoming a
					   INNER JOIN supplier b ON b.id=a.inch_supid					   	   
					   WHERE a.inch_status <> 100 AND b.sup_status <> 100 
					   AND a.id='$inch_id'");
	$dd=mysql_fetch_array($qd);		
	$inch_id 		= $dd['id'];			
	$inch_bpbno		= $dd['inch_bpbno'];
	$inch_bpbdate	= $dd['inch_bpbdate'];
	$sup_id 		= $dd['inch_supid'];
	$sup_name		= $dd['sup_name'];
	$inch_invno		= $dd['inch_invno'];
	$inch_invdate   = $dd['inch_invdate'];
	$inch_dono	    = $dd['inch_dono'];
	$tujuan	= $dd['tujuan'];
	$inch_docno		= $dd['inch_docno'];
	$inch_docdate	= $dd['inch_docdate'];
	$no_aju			= $dd['no_aju'];
	$tgl_aju		= $dd['tgl_aju'];
	$po_no			= $dd['po_no'];
	$sup_npwp		= $dd['id_pemasok'];
	$incd_currency  = $dd['inch_currency'];
	$incd_price_rate= $dd['inch_rate'];
	#$tipe_dokumen	    = $dd['tipe_dokumen'];
	#$harga_dokumen	    = $dd['harga_dokumen'];
	if(isset($_REQUEST['flagdoc'])==1){
		$inch_docid		 = isset($_REQUEST['inch_docid']);
	}else{
		$inch_docid	    = $dd['inch_docid'];
	}	
}
if($Act == "DELETEM"){
	$qd = mysql_query("UPDATE incoming SET inch_status=100 WHERE id='$inch_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus transaksi pemasukan No. $inch_bpbno', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}
}
if($Act == "POSTINGM"){
	$flag =0;
	$q =mysql_query("SELECT a.incd_hid, b.inch_bpbdate, a.incd_prodid, a.incd_qty 
		             FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
		             WHERE a.incd_hid='$inch_id'");
	while($data=mysql_fetch_object($q)){
		$cekkat    = cek_kategori($data->incd_prodid);
		/*if($cekkat==1){ #bahan baku
			mysql_query("INSERT INTO stock SET stock_trid='$data->incd_hid', stock_date='$data->inch_bpbdate', stock_prodid='$data->incd_prodid', stock_qty='$data->incd_qty',   
			             stock_status=20, stock_loc=1");
		}elseif($cekkat==2){ #barang modal
			mysql_query("INSERT INTO stock SET stock_trid='$data->incd_hid', stock_date='$data->inch_bpbdate', stock_prodid='$data->incd_prodid', stock_qty='$data->incd_qty',   
			             stock_status=20, stock_loc=4");
		}elseif($cekkat==3){ #barang jadi
			mysql_query("INSERT INTO stock SET stock_trid='$data->incd_hid', stock_date='$data->inch_bpbdate', stock_prodid='$data->incd_prodid', stock_qty='$data->incd_qty',   
			             stock_status=20, stock_loc=3");
		}elseif($cekkat==4){ #barang scrap
			mysql_query("INSERT INTO stock SET stock_trid='$data->incd_hid', stock_date='$data->inch_bpbdate', stock_prodid='$data->incd_prodid', stock_qty='$data->incd_qty',   
			             stock_status=20, stock_loc=5");
		}elseif($cekkat==4){ #barang wip
			mysql_query("INSERT INTO stock SET stock_trid='$data->incd_hid', stock_date='$data->inch_bpbdate', stock_prodid='$data->incd_prodid', stock_qty='$data->incd_qty',   
			             stock_status=20, stock_loc=2");
		}*/
		$flag = 1;
	}
	if($flag==1){
		#Update status menjadi posting
		$qd = mysql_query("UPDATE incoming SET inch_status=15 WHERE id='$inch_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.incd_qty) qty_in FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
			   WHERE b.inch_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 pemasukan No. $inch_bpbno', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses diposting !";	
		$status = "OK";
	}else{
		$message[] = "Detail barang tidak ada !";	
		$status = "OK";
	}
}
if(($Act_Det == "ADD_DETM") && (empty($inch_id))){ 
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "addm_pemasukan_bc40.php";
		exit;
	}elseif(empty($incd_price_rate) || $incd_price_rate==0){
		echo "<font color='red'>Kurs tanggal : $inch_docdate belum disetting</font><br>";
		#reload
		require_once "addm_pemasukan_bc40.php";
		exit;
	}else{
		mysql_query("INSERT INTO incoming_temp SET incd_prodid='$incd_prodid', incd_qty='$incd_qty', incd_price='$incd_amount', 
		incd_currency='$incd_currency', incd_weight='$incd_weight', incd_wunit='Kg', incd_inputby='$user_id', incd_status=10,
		incd_price_rate='$incd_price_rate', incd_price_usd='$incd_price_usd', incd_price_satuan='$incd_price', no_urut='$no_urut',
		incd_scno='$incd_scno'");
	}
}elseif(($Act_Det == "ADD_DETM") && (!empty($inch_id))){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "addm_pemasukan_bc40.php";
		exit;
	}elseif(empty($incd_price_rate) || $incd_price_rate==0){
		echo "<font color='red'>Kurs tanggal : $inch_docdate belum disetting</font><br>";
		#reload
		require_once "addm_pemasukan_bc40.php";
		exit;
	}else{
		mysql_query("INSERT INTO incoming_detail SET incd_hid='$inch_id', incd_prodid='$incd_prodid', incd_qty='$incd_qty', incd_price='$incd_amount', 
		incd_currency='$incd_currency', incd_weight='$incd_weight', incd_wunit='Kg',
		incd_price_rate='$incd_price_rate', incd_price_usd='$incd_price_usd', incd_price_satuan='$incd_price', no_urut='$no_urut',
		incd_scno='$incd_scno'");
	}
}
if(($Act_Det == "DELETE_DETM") && (empty($inch_id))){
	mysql_query("DELETE FROM incoming_temp WHERE id='$id_deth'");
}elseif(($Act_Det == "DELETE_DETM") && (!empty($inch_id))){
	mysql_query("DELETE FROM incoming_detail WHERE id='$id_deth'");
}
#Proses
if($Act=="DETAIL"){
	$data = mysql_fetch_array(mysql_query("SELECT * FROM pemasukan WHERE nomor='$nomor'"));
	$nomor          = $data['nomor'];
	$no_bpb   		= $data['no_bpb'];
	$tgl_bpb   		= $data['tgl_bpb'];
	$pemasok 		= $data['pemasok'];
	$no_invoice     = $data['no_invoice'];
	$tgl_invoice 	= $data['tgl_invoice'];
	$no_do 		    = $data['no_do'];
	$catatan 		= $data['catatan'];
	$jenis_dokpab 	= $data['jenis_dokpab'];
	$no_dokpab 		= $data['no_dokpab'];
	$tgl_dokpab 	= $data['tgl_dokpab'];
	
	require "add_pemasukan_bc40.php";
}elseif($Act=="CANCEL" || $Act=="CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Pemasukan-BC40'>";	
	exit;
}elseif($Act=="PROCESS"){
	if(!empty($pilih)){	
		foreach ( $pilih as $i => $val)
		{
			$tgl_bpb2     = $_REQUEST['tgl_bpb2'][$val];
			$tgl_dokpab2  = $_REQUEST['tgl_dokpab2'][$val];
			#$tipe_dokumen  = $_REQUEST['tipe_dokumen'][$val];
			#$harga_dokumen  = $_REQUEST['harga_dokumen'][$val];
			if($tgl_bpb2=="0000-00-00"){
				$tglbpb = $tgl_dokpab2;
			}else{
				$tglbpb = $tgl_bpb2;
			}
			#Update status temp
			mysql_query("UPDATE log_incoming SET log_status=2, tgl_bpb='$tglbpb' WHERE id='$val' ");
		}
		require "add_pemasukan_bc40.php";
	}else{
		require "add_pemasukan_bc40.php";
	}	
}elseif($Act=="INV"){
	#Insert header pemasukan
	$qh=mysql_query("SELECT * FROM log_incoming WHERE log_status = 2 AND jenis_dokpab='$jenis_dokpab' GROUP BY id_header");	
	while($dh=mysql_fetch_array($qh)){
		$ary_nomor 		= get_nomorinc(1);
		$ary_nobpb 		= get_nobpbinc(1);		
		if(!empty($dh['no_bpb'])){
			$no_bpb = $dh['no_bpb'];
		}else{
			$no_bpb = $ary_nobpb;
		}
		#Insert Pemasukan Headar
		mysql_query("INSERT INTO incoming SET inch_bpbno='$ary_nobpb', inch_bpbdate='$dh[tgl_bpb]', inch_invno='$dh[no_invoice]', 
		             inch_invdate='$dh[tgl_invoice]', inch_dono='$dh[no_do]', inch_purpid=1, inch_docno='$dh[no_dokpab]', 
					 inch_docdate='$dh[tgl_dokpab]', inch_inputby='$user_id', inch_inputdate='$datetime', inch_status=12, inch_nomor=$dh[flag],
					 inch_doctype='$dh[jenis_dokpab]', nm_pemasok='".mysql_real_escape_string($dh['nm_pemasok'])."', no_aju='$dh[no_aju]', tgl_aju='$dh[tgl_aju]', no_sppb='$dh[no_sppb]', tgl_sppb='$dh[tgl_sppb]',                     no_hbl='$dh[no_hbl]', tgl_hbl='$dh[tgl_hbl]', vesel='$dh[vesel]', eta_jkt='$dh[eta_jkt]', 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]', pelabuhan='$dh[pelabuhan]', no_izin='$dh[no_izin]', tgl_izin='$dh[tgl_izin]', no_dokpab_asal='$dh[no_dokpab_asal]',
					 tgl_dokpab_asal='$dh[tgl_dokpab_asal]', jatuh_tempo='$dh[jatuh_tempo]', valuta='$dh[valuta]', status_dokpab='$dh[status_dokpab]', sumber='CIESA',
					 id_header='$dh[id_header]',id_pemasok='$dh[id_pemasok]', inch_currency='$dh[valuta]',tujuan='$dh[tujuan]', inch_rate='$dh[inch_rate]'");
		#Aktivitas User
		mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi pemasukan No. $ary_nobpb', 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.tarif,a.nilai_inv, a.jenis_dokpab, a.no_dokpab, a.tgl_dokpab, a.tgl_bpb, a.incd_scno
						  FROM log_incoming a 
						  INNER JOIN incoming b ON b.id_header=a.id_header					 
						  WHERE a.log_status = 2 AND b.inch_status=12 AND a.jenis_dokpab='$jenis_dokpab'");
		WHILE ($dd = mysql_fetch_array($qd))
		{
			$prd2 = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$dd[kode]' AND prod_cat<>100"));
			#Insert Detail
			if($dd['valuta'] =="IDR"){$unit_price=$dd['nilai']/$dd['jumlah'];}else{$unit_price=$dd['non_rupiah']/$dd['jumlah'];}
			mysql_query("INSERT INTO incoming_detail SET incd_hid='$dd[id]', incd_prodid='$prd2[id]', incd_qty='$dd[jumlah]', 
			             incd_currency='$dd[valuta]', incd_price='$dd[nilai]', incd_weight='$dd[berat]', incd_wunit='KG', no_urut='$dd[no_urut]', kode_hs='$dd[kode_hs]',
						 tarif='$dd[tarif]', nilai_inv='$dd[nilai_inv]', incd_scno='$dd[incd_scno]', incd_price_satuan='$unit_price'");
			
			#Stock
			//mysql_query("INSERT INTO stock SET stock_prodid='$prd2[id]', stock_qty='$dd[jumlah]', stock_loc=1");			 
		}
		$status = 2;
	}
	//exit;
	if($status == 2){	
		#update status log
		mysql_query("UPDATE log_incoming SET log_status =3 WHERE log_status =2 AND jenis_dokpab='$jenis_dokpab'");
		#update status
		mysql_query("UPDATE incoming SET inch_status=15 WHERE inch_status=12 AND inch_doctype='$jenis_dokpab'");
		$message[] = "Data Sukses dikirim.";	
		$status    = "OK";
	}		 
}elseif($Act=="REG"){
	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_incoming 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 "add_pemasukan_bc40.php";
	}else{
		require "add_pemasukan_bc40.php";
	}	
}elseif($Act=="DEL_TEMP"){
	#delete all temp
	mysql_query("UPDATE log_incoming SET log_status=5 WHERE log_status=1");
	$message[] = "Data Sukses dihapus.";	
	#$status = "OK";	
	require_once "add_pemasukan_bc40.php";
}elseif($Act=="DELDET_TEMP"){
	mysql_query("UPDATE log_incoming SET log_status=1 WHERE id='".$_REQUEST['id_temp']."' ");
	require "add_pemasukan_bc40.php";
}elseif(($Act=="INF") || ($Act=="GET") || ($Act=="TEMP") ){
	require "add_pemasukan_bc40.php";
}elseif($Act == "SEL_PO"){
	$dd=mysql_fetch_array(mysql_query("SELECT a.*,b.sup_name, b.sup_npwp FROM purchase_order a INNER JOIN supplier b ON b.id=a.po_idsupp WHERE a.po_no='$po_no' AND po_type='Purchasing'"));
	$po_no			= $dd['po_no'];
	$sup_id 		= $dd['po_idsupp'];
	$sup_name		= $dd['sup_name'];
	$incd_currency	= $dd['po_currency'];
	$sup_npwp		= $dd['sup_npwp'];
	
	require "addm_pemasukan_bc40.php";
}elseif($Act=="ADDM" || $Act=="DETAILM" || $Act_Det == "ADD_DETM" || $Act_Det == "DELETE_DETM" || $Act == "SEL_PO"){
	require "addm_pemasukan_bc40.php";
}
#Messages
if (! count($message)==0 ){
	?>
		<script language="javascript">
			var pesan ='<?php 
							$Num=0;
							foreach ($message as $indeks=>$pesan_tampil) { 
								$Num++;
								echo $Num.'. '.$pesan_tampil.'\n';
							}
						?>';
			alert(pesan);
		</script>
	<?php  
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Pemasukan-BC40'>";	
		exit;
	}else{
		echo "<meta http-equiv='refresh' content='0; url=?page=Proses-Pemasukan-BC40&Act=TEMP'>";
	}
}
?>