<?php
$message  = array();
$Act      = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$fromdate = isset($_REQUEST['fromdate']) ? $_REQUEST['fromdate'] : $date;
$todate   = isset($_REQUEST['todate']) ? $_REQUEST['todate'] : $date;
$sup_name2= isset($_REQUEST['sup_name2']) ? $_REQUEST['sup_name2'] :"SEMUA";
#header
$inch_id  		= isset($_REQUEST['inch_id']) ? $_REQUEST['inch_id'] :'';
$trcid 			= isset($_REQUEST['trcid']) ? $_REQUEST['trcid'] :'';			
$supcustname	= isset($_REQUEST['supcustname']) ? $_REQUEST['supcustname'] :'';
$invno			= isset($_REQUEST['invno']) ? $_REQUEST['invno'] :'';
$invdate   		= isset($_REQUEST['invdate']) ? $_REQUEST['invdate'] :'';
$fakturno	    = isset($_REQUEST['fakturno']) ? $_REQUEST['fakturno'] :'';
$doctype		= isset($_REQUEST['doctype']) ? $_REQUEST['doctype'] :'';
$docno			= isset($_REQUEST['docno']) ? $_REQUEST['docno'] :'';
$docdate		= isset($_REQUEST['docdate']) ? $_REQUEST['docdate'] :'';
$npwpno			= isset($_REQUEST['npwpno']) ? $_REQUEST['npwpno'] :'';
$currency  		= isset($_REQUEST['currency']) ? $_REQUEST['currency'] :'';
$kurs			= isset($_REQUEST['kurs']) ? $_REQUEST['kurs'] :0;
$jurnal_no		= isset($_REQUEST['jurnal_no']) ? $_REQUEST['jurnal_no'] :'';
$jurnal_date	= isset($_REQUEST['jurnal_date']) ? $_REQUEST['jurnal_date'] :$date;
$nilaiidr 		= isset($_REQUEST['nilaiidr']) ? $_REQUEST['nilaiidr'] :'';
$nilaiusd 		= isset($_REQUEST['nilaiusd']) ? $_REQUEST['nilaiusd'] :'';
$tdebit			= isset($_REQUEST['tdebit']) ? $_REQUEST['tdebit'] :'';
$tdebit_usd		= isset($_REQUEST['tdebit_usd']) ? $_REQUEST['tdebit_usd'] :'';
$ttotal			= isset($_REQUEST['ttotal']) ? $_REQUEST['ttotal'] :'';
$jurnal_ptype	= isset($_REQUEST['jurnal_ptype']) ? $_REQUEST['jurnal_ptype'] :'';
#detail		
$Act_Det  			= isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$id_det				= isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth			= isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
$jurnald_acctno     = isset($_REQUEST['acctno']) ? $_REQUEST['acctno'] : '';
$jurnald_desc   	= isset($_REQUEST['acctdesc']) ? $_REQUEST['acctdesc'] : '';
$acctamount		 	= isset($_REQUEST['acctamount']) ? $_REQUEST['acctamount'] : '';
$jurnald_status     = isset($_REQUEST['acctstatus']) ? $_REQUEST['acctstatus'] : '';
if($currency=="IDR"){
	if(!empty($kurs)){
		$jurnald_amount_usd = $acctamount / $kurs;
	}else{
		$jurnald_amount_usd = 0;
	}	
	$jurnald_amount     = $acctamount;
}else{
	$jurnald_amount_usd = $acctamount;
	$jurnald_amount     = $acctamount * $kurs;
}

if($Act =="SAVE"){
	$pilih = $_REQUEST['pilih'];
	foreach($pilih as $i => $id){
		$inch_docno			= isset($_REQUEST['inch_docno'][$id]) ? $_REQUEST['inch_docno'][$id] : '';
		$inch_doctype		= isset($_REQUEST['inch_doctype'][$id]) ? $_REQUEST['inch_doctype'][$id] : '';
		$inch_paytype		= isset($_REQUEST['type'][$id]) ? $_REQUEST['type'][$id] : '';
		$inch_invno_acct    = isset($_REQUEST['inch_invno_acct'][$id]) ? $_REQUEST['inch_invno_acct'][$id] : '';
		$inch_invdate_acct  = isset($_REQUEST['inch_invdate_acct'][$id]) ? $_REQUEST['inch_invdate_acct'][$id] : '';
		$inch_debit1  		= isset($_REQUEST['inch_debit1'][$id]) ? $_REQUEST['inch_debit1'][$id] : '';
		$inch_debit2  		= isset($_REQUEST['inch_debit2'][$id]) ? $_REQUEST['inch_debit2'][$id] : '';
		$inch_debit3  		= isset($_REQUEST['inch_debit3'][$id]) ? $_REQUEST['inch_debit3'][$id] : '';
		$inch_debit4  		= isset($_REQUEST['inch_debit4'][$id]) ? $_REQUEST['inch_debit4'][$id] : '';
		$inch_debit5  		= isset($_REQUEST['inch_debit5'][$id]) ? $_REQUEST['inch_debit5'][$id] : 'BLANK';
		$inch_debit6  		= isset($_REQUEST['inch_debit6'][$id]) ? $_REQUEST['inch_debit6'][$id] : 'BLANK';
		$inch_debit7  		= isset($_REQUEST['inch_debit7'][$id]) ? $_REQUEST['inch_debit7'][$id] : 'BLANK';
		$inch_debit8  		= isset($_REQUEST['inch_debit8'][$id]) ? $_REQUEST['inch_debit8'][$id] : 'BLANK';
		$inch_debit9  		= isset($_REQUEST['inch_debit9'][$id]) ? $_REQUEST['inch_debit9'][$id] : 'BLANK';
		$inch_debit10  		= isset($_REQUEST['inch_debit10'][$id]) ? $_REQUEST['inch_debit10'][$id] : 'BLANK';
		$inch_damount1 		= isset($_REQUEST['inch_damount1'][$id]) ? $_REQUEST['inch_damount1'][$id] : 0;
		$inch_damount2 		= isset($_REQUEST['inch_damount2'][$id]) ? $_REQUEST['inch_damount2'][$id] : 0;
		$inch_damount3 		= isset($_REQUEST['inch_damount3'][$id]) ? $_REQUEST['inch_damount3'][$id] : 0;
		$inch_damount4 		= isset($_REQUEST['inch_damount4'][$id]) ? $_REQUEST['inch_damount4'][$id] : 0;		
		$inch_damount5 		= isset($_REQUEST['inch_damount5'][$id]) ? $_REQUEST['inch_damount5'][$id] : 0;
		$inch_damount6 		= isset($_REQUEST['inch_damount6'][$id]) ? $_REQUEST['inch_damount6'][$id] : 0;
		$inch_damount7 		= isset($_REQUEST['inch_damount7'][$id]) ? $_REQUEST['inch_damount7'][$id] : 0;
		$inch_damount8 		= isset($_REQUEST['inch_damount8'][$id]) ? $_REQUEST['inch_damount8'][$id] : 0;
		$inch_damount9 		= isset($_REQUEST['inch_damount9'][$id]) ? $_REQUEST['inch_damount9'][$id] : 0;
		$inch_damount10 	= isset($_REQUEST['inch_damount10'][$id]) ? $_REQUEST['inch_damount10'][$id] : 0;
		$inch_kredit1  		= isset($_REQUEST['inch_kredit1'][$id]) ? $_REQUEST['inch_kredit1'][$id] : '';
		$inch_kredit2 		= isset($_REQUEST['inch_kredit2'][$id]) ? $_REQUEST['inch_kredit2'][$id] : '';
		$inch_kredit3  		= isset($_REQUEST['inch_kredit3'][$id]) ? $_REQUEST['inch_kredit3'][$id] : '';
		$inch_kredit4  		= isset($_REQUEST['inch_kredit4'][$id]) ? $_REQUEST['inch_kredit4'][$id] : '';		
		$inch_kredit5  		= isset($_REQUEST['inch_kredit5'][$id]) ? $_REQUEST['inch_kredit5'][$id] : 'BLANK';
		$inch_kredit6  		= isset($_REQUEST['inch_kredit6'][$id]) ? $_REQUEST['inch_kredit6'][$id] : 'BLANK';
		$inch_kredit7  		= isset($_REQUEST['inch_kredit7'][$id]) ? $_REQUEST['inch_kredit7'][$id] : 'BLANK';
		$inch_kredit8  		= isset($_REQUEST['inch_kredit8'][$id]) ? $_REQUEST['inch_kredit8'][$id] : 'BLANK';
		$inch_kredit9  		= isset($_REQUEST['inch_kredit9'][$id]) ? $_REQUEST['inch_kredit9'][$id] : 'BLANK';
		$inch_kredit10 		= isset($_REQUEST['inch_kredit10'][$id]) ? $_REQUEST['inch_kredit10'][$id] : 'BLANK';		
		$inch_kamount1 		= isset($_REQUEST['inch_kamount1'][$id]) ? $_REQUEST['inch_kamount1'][$id] : 0;
		$inch_kamount2 		= isset($_REQUEST['inch_kamount2'][$id]) ? $_REQUEST['inch_kamount2'][$id] : 0;
		$inch_kamount3 		= isset($_REQUEST['inch_kamount3'][$id]) ? $_REQUEST['inch_kamount3'][$id] : 0;
		$inch_kamount4 		= isset($_REQUEST['inch_kamount4'][$id]) ? $_REQUEST['inch_kamount4'][$id] : 0;
		$inch_kamount5 		= isset($_REQUEST['inch_kamount5'][$id]) ? $_REQUEST['inch_kamount5'][$id] : 0;
		$inch_kamount6 		= isset($_REQUEST['inch_kamount6'][$id]) ? $_REQUEST['inch_kamount6'][$id] : 0;
		$inch_kamount7 		= isset($_REQUEST['inch_kamount4'][$id]) ? $_REQUEST['inch_kamount4'][$id] : 0;
		$inch_kamount8 		= isset($_REQUEST['inch_kamount8'][$id]) ? $_REQUEST['inch_kamount8'][$id] : 0;
		$inch_kamount9 		= isset($_REQUEST['inch_kamount9'][$id]) ? $_REQUEST['inch_kamount9'][$id] : 0;
		$inch_kamount10		= isset($_REQUEST['inch_kamount10'][$id]) ? $_REQUEST['inch_kamount10'][$id] : 0;
		$inch_jurnalno  	= isset($_REQUEST['inch_jurnalno'][$id]) ? $_REQUEST['inch_jurnalno'][$id] : '';
		$inch_jurnaldate  	= isset($_REQUEST['inch_jurnaldate'][$id]) ? $_REQUEST['inch_jurnaldate'][$id] : '';
		$inch_nilai		  	= isset($_REQUEST['inch_nilai'][$id]) ? $_REQUEST['inch_nilai'][$id] : '';
		$inch_nilai_nidr  	= isset($_REQUEST['inch_nilai_nidr'][$id]) ? $_REQUEST['inch_nilai_nidr'][$id] : '';
		$inch_currency  	= isset($_REQUEST['inch_currency'][$id]) ? $_REQUEST['inch_currency'][$id] : '';
		$sup_name		  	= isset($_REQUEST['sup_name'][$id]) ? $_REQUEST['sup_name'][$id] : '';
		$id_pemasok		  	= isset($_REQUEST['id_pemasok'][$id]) ? $_REQUEST['id_pemasok'][$id] : '';
		$inch_dono		  	= isset($_REQUEST['inch_dono'][$id]) ? $_REQUEST['inch_dono'][$id] : '';
		$inch_ajd_nidr	  	= 0; #isset($_REQUEST['inch_ajd_nidr'][$id]) ? $_REQUEST['inch_ajd_nidr'][$id] : '';
		$inch_ajd		  	= 0; #isset($_REQUEST['inch_ajd'][$id]) ? $_REQUEST['inch_ajd'][$id] : '';
		$inch_rate		  	= isset($_REQUEST['inch_rate'][$id]) ? $_REQUEST['inch_rate'][$id] : 0;
		$total				= $inch_damount1 + $inch_damount2 + $inch_damount3 + $inch_damount4 + $inch_damount5 +
							  $inch_damount6 + $inch_damount7 + $inch_damount8 + $inch_damount9 + $inch_damount10;
		$totalk				= $inch_kamount1 + $inch_kamount2 + $inch_kamount3 + $inch_kamount4 + $inch_kamount5 +
							  $inch_kamount6 + $inch_kamount7 + $inch_kamount8 + $inch_kamount9 + $inch_kamount10;						  
		if($inch_paytype != 'BLANK' && $inch_paytype != 'OT'){
			if($inch_currency=="IDR"){
				if(!empty($inch_rate)){
					$inch_nilai_nidr2 = $total/$inch_rate; 
				}else{
					$inch_nilai_nidr2 = 0;
				}
				$inch_nilai2		= $total;
			}else{
				$inch_nilai_nidr2   = $total;
				$inch_nilai2		= $total*$inch_rate;
			}
		}
		#detail item
		$prdcd="";
		$query = mysql_query("SELECT b.prod_code FROM incoming_detail a 
				 INNER JOIN product b ON b.id=a.incd_prodid WHERE a.incd_hid='$id' GROUP BY b.prod_code");
		while($data=mysql_fetch_object($query)){
			$prdcd 	= $prdcd."".$data->prod_code." | ";
		}
		$prd_cd = substr($prdcd,0,-2);
		#cek jurnal no
		$cjn = mysql_fetch_array(mysql_query("SELECT jurnal_no FROM jurnal WHERE jurnal_no='$inch_jurnalno'"));
		if(($inch_paytype != 'BLANK') && ($inch_paytype != 'OT') && ($total==$totalk) ){	
			if(empty($inch_jurnalno)){
				$message[] ="Data no dok : $inch_docno : $inch_doctype nomor jurnal kosong";
				$status="OK";
			}elseif(empty($inch_jurnaldate) || $inch_jurnaldate=="0000-00-00"){
				$message[] ="Data no dok : $inch_docno : $inch_doctype tanggal jurnal kosong";
				$status="OK";
			}elseif(substr($inch_jurnaldate,0,4) != $thn_acct){
				$message[] ="Data no dok : $inch_docno : $inch_doctype tanggal jurnal ($inch_jurnaldate) tidak sama dengan periode aktif ($thn_acct)";
				$status="OK";
			}elseif(!empty($cjn['jurnal_no'])){
				$message[] ="Data no dok : $inch_docno : $inch_jurnalno nomor jurnal sudah ada didatabase";
				$status="OK";
			}else{
				#Jurnal Header
				mysql_query("INSERT INTO jurnal SET jurnal_no='$inch_jurnalno', jurnal_date='$inch_jurnaldate', jurnal_iddept='10', jurnal_suppname='$sup_name', 
				jurnal_invno='$inch_invno_acct', jurnal_currency='$inch_currency', jurnal_amount='$inch_nilai2', jurnal_inputby='$user_id', jurnal_inputdate='$datetime', 
				jurnal_status=20, jurnal_amount_usd='$inch_nilai_nidr2', jurnal_idpemasok='$id_pemasok', jurnal_desc='Pembelian Barang dari No. Dok:$inch_doctype:$inch_docno Kode Barang:$prd_cd',
				jurnal_type='$inch_paytype', jurnal_trcid='$id', jurnal_menu='BCINC', jurnal_nofaktur='$inch_dono', jurnal_rate='$inch_rate', jurnal_doctype='$inch_doctype',
				jurnal_docno='$inch_docno'");
				if($inch_debit1 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnald_amount1 =$inch_damount1;
						if(!empty($inch_rate)){
							$jurnald_amount_usd1 =$inch_damount1/$inch_rate;
						}else{
							$jurnald_amount_usd1 =0;
						}	
					}else{
						$jurnald_amount1 =$inch_damount1*$inch_rate;
						$jurnald_amount_usd1 =$inch_damount1;
					}
					#jurnal detail debit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_debit1', jurnald_status=10,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnald_amount1', jurnald_amount_usd='$jurnald_amount_usd1',
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$id', jurnald_invno_acct='$inch_invno_acct'");
				}
				if($inch_debit2 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnald_amount2 =$inch_damount2;
						if(!empty($inch_rate)){
							$jurnald_amount_usd2 =$inch_damount2/$inch_rate;
						}else{
							$jurnald_amount_usd2 =0;
						}
					}else{
						$jurnald_amount2 =$inch_damount2*$inch_rate;
						$jurnald_amount_usd2 =$inch_damount2;
					}
					#jurnal detail debit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_debit2', jurnald_status=10,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnald_amount2', jurnald_amount_usd='$jurnald_amount_usd2',
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$id', jurnald_invno_acct='$inch_invno_acct'");
				}
				if($inch_debit3 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnald_amount3 =$inch_damount3;
						if(!empty($inch_rate)){
							$jurnald_amount_usd3 =$inch_damount3/$inch_rate;
						}else{
							$jurnald_amount_usd3 =0;
						}
					}else{
						$jurnald_amount3 =$inch_damount3*$inch_rate;
						$jurnald_amount_usd3 =$inch_damount3;
					}
					#jurnal detail debit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_debit3', jurnald_status=10,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnald_amount3', jurnald_amount_usd='$jurnald_amount_usd3',
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$id', jurnald_invno_acct='$inch_invno_acct'");
				}
				if($inch_debit4 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnald_amount4 =$inch_damount4;
						if(!empty($inch_rate)){
							$jurnald_amount_usd4 =$inch_damount4/$inch_rate;
						}else{
							$jurnald_amount_usd4 =0;
						}
					}else{
						$jurnald_amount4 =$inch_damount4*$inch_rate;
						$jurnald_amount_usd4 =$inch_damount4;
					}
					#jurnal detail debit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_debit4', jurnald_status=10,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnald_amount4', jurnald_amount_usd='$jurnald_amount_usd4',
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$id', jurnald_invno_acct='$inch_invno_acct'");
				}				
				if($inch_kredit1 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnaldamount1 =$inch_kamount1;
						if(!empty($inch_rate)){
							$jurnaldamountusd1 =$inch_kamount1/$inch_rate;
						}else{
							$jurnaldamountusd1 =0;
						}	
					}else{
						$jurnaldamount1 =$inch_kamount1*$inch_rate;
						$jurnaldamountusd1 =$inch_kamount1;
					}
					#jurnal detail kredit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_kredit1', jurnald_status=20,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnaldamount1', jurnald_amount_usd='$jurnaldamountusd1', 
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$id', jurnald_invno_acct='$inch_invno_acct'");
				}	
				if($inch_kredit2 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnaldamount2 =$inch_kamount2;
						if(!empty($inch_rate)){
							$jurnaldamountusd2 =$inch_kamount2/$inch_rate;
						}else{
							$jurnaldamountusd2 =0;
						}	
					}else{
						$jurnaldamount2 =$inch_kamount2*$inch_rate;
						$jurnaldamountusd2 =$inch_kamount2;
					}
					#jurnal detail kredit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_kredit2', jurnald_status=20,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnaldamount2', jurnald_amount_usd='$jurnaldamountusd2', 
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$id', jurnald_invno_acct='$inch_invno_acct'");
				}
				if($inch_kredit3 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnaldamount3 =$inch_kamount3;
						if(!empty($inch_rate)){
							$jurnaldamountusd3 =$inch_kamount3/$inch_rate;
						}else{
							$jurnaldamountusd3 =0;
						}	
					}else{
						$jurnaldamount3 =$inch_kamount3*$inch_rate;
						$jurnaldamountusd3 =$inch_kamount3;
					}
					#jurnal detail kredit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_kredit3', jurnald_status=20,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnaldamount3', jurnald_amount_usd='$jurnaldamountusd3', 
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$id', jurnald_invno_acct='$inch_invno_acct'");
				}	
				if($inch_kredit4 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnaldamount4 =$inch_kamount4;
						if(!empty($inch_rate)){
							$jurnaldamountusd4 =$inch_kamount4/$inch_rate;
						}else{
							$jurnaldamountusd4 =0;
						}	
					}else{
						$jurnaldamount4 =$inch_kamount4*$inch_rate;
						$jurnaldamountusd4 =$inch_kamount4;
					}
					#jurnal detail kredit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_kredit4', jurnald_status=20,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnaldamount4', jurnald_amount_usd='$jurnaldamountusd4', 
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$id', jurnald_invno_acct='$inch_invno_acct'");
				}					
				#Update status
				mysql_query("UPDATE incoming SET inch_paytype='$inch_paytype', inch_invno_acct='$inch_invno_acct', inch_invdate_acct='$inch_invdate_acct',
				inch_debit1='$inch_debit1', inch_debit2='$inch_debit2', inch_debit3='$inch_debit3', inch_debit4='$inch_debit4', inch_debit5='$inch_debit5',
				inch_debit6='$inch_debit6', inch_debit7='$inch_debit7', inch_debit8='$inch_debit8', inch_debit9='$inch_debit9', inch_debit10='$inch_debit10',			
				inch_damount1='$inch_damount1', inch_damount2='$inch_damount2', inch_damount3='$inch_damount3', inch_damount4='$inch_damount4', inch_damount5='$inch_damount5',
				inch_damount6='$inch_damount6', inch_damount7='$inch_damount7', inch_damount8='$inch_damount8', inch_damount9='$inch_damount9', inch_damount10='$inch_damount10',			
				inch_kredit1='$inch_kredit1', inch_kredit2='$inch_kredit2', inch_kredit3='$inch_kredit3', inch_kredit4='$inch_kredit4', inch_kredit5='$inch_kredit5',
				inch_kredit6='$inch_kredit6', inch_kredit7='$inch_kredit7', inch_kredit8='$inch_kredit8', inch_kredit9='$inch_kredit9', inch_kredit10='$inch_kredit10',			
				inch_kamount1='$inch_kamount1', inch_kamount2='$inch_kamount2', inch_kamount3='$inch_kamount3', inch_kamount4='$inch_kamount4', inch_kamount5='$inch_kamount5',
				inch_kamount6='$inch_kamount6', inch_kamount7='$inch_kamount7', inch_kamount8='$inch_kamount8', inch_kamount9='$inch_kamount9', inch_kamount10='$inch_kamount10',			
				inch_jurnalno='$inch_jurnalno', inch_jurnaldate='$inch_jurnaldate',
				id_pemasok='$id_pemasok', inch_ajd_nidr='$inch_ajd_nidr', inch_ajd='$inch_ajd', inch_rate='$inch_rate', inch_dono='$inch_dono' WHERE id='$id'");
				#insert aktivitas
				mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Transfer inbox-pemasukan Dokumen No. $inch_docno==>$inch_docno', 
				user_date='$date', user_time='$time'");
				#konfirmasi OK
				$message[] ="Data  no dok : $inch_docno : $inch_doctype sukses disimpan";
				$status="OK";
			}
		}elseif($inch_paytype == 'OT'){
			#Update status
			mysql_query("UPDATE incoming SET inch_paytype='$inch_paytype', inch_invno_acct='$inch_invno_acct', inch_invdate_acct='$inch_invdate_acct',
			inch_debit1='$inch_debit1', inch_debit2='$inch_debit2', inch_debit3='$inch_debit3', inch_debit4='$inch_debit4', inch_debit5='$inch_debit5',
			inch_debit6='$inch_debit6', inch_debit7='$inch_debit7', inch_debit8='$inch_debit8', inch_debit9='$inch_debit9', inch_debit10='$inch_debit10',			
			inch_damount1='$inch_damount1', inch_damount2='$inch_damount2', inch_damount3='$inch_damount3', inch_damount4='$inch_damount4', inch_damount5='$inch_damount5',
			inch_damount6='$inch_damount6', inch_damount7='$inch_damount7', inch_damount8='$inch_damount8', inch_damount9='$inch_damount9', inch_damount10='$inch_damount10',			
			inch_kredit1='$inch_kredit1', inch_kredit2='$inch_kredit2', inch_kredit3='$inch_kredit3', inch_kredit4='$inch_kredit4', inch_kredit5='$inch_kredit5',
			inch_kredit6='$inch_kredit6', inch_kredit7='$inch_kredit7', inch_kredit8='$inch_kredit8', inch_kredit9='$inch_kredit9', inch_kredit10='$inch_kredit10',			
			inch_kamount1='$inch_kamount1', inch_kamount2='$inch_kamount2', inch_kamount3='$inch_kamount3', inch_kamount4='$inch_kamount4', inch_kamount5='$inch_kamount5',
			inch_kamount6='$inch_kamount6', inch_kamount7='$inch_kamount7', inch_kamount8='$inch_kamount8', inch_kamount9='$inch_kamount9', inch_kamount10='$inch_kamount10',			
			inch_jurnalno='$inch_jurnalno', inch_jurnaldate='$inch_jurnaldate',
			id_pemasok='$id_pemasok', inch_ajd_nidr='$inch_ajd_nidr', inch_ajd='$inch_ajd', inch_rate='$inch_rate', inch_dono='$inch_dono' WHERE id='$id'");
			#insert aktivitas
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Transfer inbox-pemasukan Dokumen No. $inch_docno==>$inch_docno', 
			user_date='$date', user_time='$time'");
			#konfirmasi OK
			$message[] ="Data  no dok : $inch_docno : $inch_doctype sukses disimpan";
			$status="OK";
		}elseif($inch_paytype == 'BLANK'){
			echo "<meta http-equiv='refresh' content='0; url=?page=Data-Temp-Pemasukan&fromdate=$fromdate&todate=$todate&sup_name2=$sup_name2'>";
		}else{
			$message[] ="Data no dok : $inch_docno : $inch_doctype debit-kredit tidak balance ($total-$totalk)";
			$status="OK";
		}
	}		
}
if($Act =="SAVE2"){
	$pilih = $_REQUEST['pilih'];
	foreach($pilih as $i => $id){
		$inch_docno			= isset($_REQUEST['inch_docno'][$id]) ? $_REQUEST['inch_docno'][$id] : '';
		$inch_doctype		= isset($_REQUEST['inch_doctype'][$id]) ? $_REQUEST['inch_doctype'][$id] : '';
		$inch_paytype		= isset($_REQUEST['type'][$id]) ? $_REQUEST['type'][$id] : '';
		$inch_invno_acct    = isset($_REQUEST['inch_invno_acct'][$id]) ? $_REQUEST['inch_invno_acct'][$id] : '';
		$inch_invdate_acct  = isset($_REQUEST['inch_invdate_acct'][$id]) ? $_REQUEST['inch_invdate_acct'][$id] : '';
		$inch_debit1  		= isset($_REQUEST['inch_debit1'][$id]) ? $_REQUEST['inch_debit1'][$id] : '';
		$inch_debit2  		= isset($_REQUEST['inch_debit2'][$id]) ? $_REQUEST['inch_debit2'][$id] : '';
		$inch_debit3  		= isset($_REQUEST['inch_debit3'][$id]) ? $_REQUEST['inch_debit3'][$id] : '';
		$inch_debit4  		= isset($_REQUEST['inch_debit4'][$id]) ? $_REQUEST['inch_debit4'][$id] : '';
		$inch_debit5  		= isset($_REQUEST['inch_debit5'][$id]) ? $_REQUEST['inch_debit5'][$id] : 'BLANK';
		$inch_debit6  		= isset($_REQUEST['inch_debit6'][$id]) ? $_REQUEST['inch_debit6'][$id] : 'BLANK';
		$inch_debit7  		= isset($_REQUEST['inch_debit7'][$id]) ? $_REQUEST['inch_debit7'][$id] : 'BLANK';
		$inch_debit8  		= isset($_REQUEST['inch_debit8'][$id]) ? $_REQUEST['inch_debit8'][$id] : 'BLANK';
		$inch_debit9  		= isset($_REQUEST['inch_debit9'][$id]) ? $_REQUEST['inch_debit9'][$id] : 'BLANK';
		$inch_debit10  		= isset($_REQUEST['inch_debit10'][$id]) ? $_REQUEST['inch_debit10'][$id] : 'BLANK';
		$inch_damount1 		= isset($_REQUEST['inch_damount1'][$id]) ? $_REQUEST['inch_damount1'][$id] : 0;
		$inch_damount2 		= isset($_REQUEST['inch_damount2'][$id]) ? $_REQUEST['inch_damount2'][$id] : 0;
		$inch_damount3 		= isset($_REQUEST['inch_damount3'][$id]) ? $_REQUEST['inch_damount3'][$id] : 0;
		$inch_damount4 		= isset($_REQUEST['inch_damount4'][$id]) ? $_REQUEST['inch_damount4'][$id] : 0;		
		$inch_damount5 		= isset($_REQUEST['inch_damount5'][$id]) ? $_REQUEST['inch_damount5'][$id] : 0;
		$inch_damount6 		= isset($_REQUEST['inch_damount6'][$id]) ? $_REQUEST['inch_damount6'][$id] : 0;
		$inch_damount7 		= isset($_REQUEST['inch_damount7'][$id]) ? $_REQUEST['inch_damount7'][$id] : 0;
		$inch_damount8 		= isset($_REQUEST['inch_damount8'][$id]) ? $_REQUEST['inch_damount8'][$id] : 0;
		$inch_damount9 		= isset($_REQUEST['inch_damount9'][$id]) ? $_REQUEST['inch_damount9'][$id] : 0;
		$inch_damount10 	= isset($_REQUEST['inch_damount10'][$id]) ? $_REQUEST['inch_damount10'][$id] : 0;
		$inch_kredit1  		= isset($_REQUEST['inch_kredit1'][$id]) ? $_REQUEST['inch_kredit1'][$id] : '';
		$inch_kredit2 		= isset($_REQUEST['inch_kredit2'][$id]) ? $_REQUEST['inch_kredit2'][$id] : '';
		$inch_kredit3  		= isset($_REQUEST['inch_kredit3'][$id]) ? $_REQUEST['inch_kredit3'][$id] : '';
		$inch_kredit4  		= isset($_REQUEST['inch_kredit4'][$id]) ? $_REQUEST['inch_kredit4'][$id] : '';		
		$inch_kredit5  		= isset($_REQUEST['inch_kredit5'][$id]) ? $_REQUEST['inch_kredit5'][$id] : 'BLANK';
		$inch_kredit6  		= isset($_REQUEST['inch_kredit6'][$id]) ? $_REQUEST['inch_kredit6'][$id] : 'BLANK';
		$inch_kredit7  		= isset($_REQUEST['inch_kredit7'][$id]) ? $_REQUEST['inch_kredit7'][$id] : 'BLANK';
		$inch_kredit8  		= isset($_REQUEST['inch_kredit8'][$id]) ? $_REQUEST['inch_kredit8'][$id] : 'BLANK';
		$inch_kredit9  		= isset($_REQUEST['inch_kredit9'][$id]) ? $_REQUEST['inch_kredit9'][$id] : 'BLANK';
		$inch_kredit10 		= isset($_REQUEST['inch_kredit10'][$id]) ? $_REQUEST['inch_kredit10'][$id] : 'BLANK';		
		$inch_kamount1 		= isset($_REQUEST['inch_kamount1'][$id]) ? $_REQUEST['inch_kamount1'][$id] : 0;
		$inch_kamount2 		= isset($_REQUEST['inch_kamount2'][$id]) ? $_REQUEST['inch_kamount2'][$id] : 0;
		$inch_kamount3 		= isset($_REQUEST['inch_kamount3'][$id]) ? $_REQUEST['inch_kamount3'][$id] : 0;
		$inch_kamount4 		= isset($_REQUEST['inch_kamount4'][$id]) ? $_REQUEST['inch_kamount4'][$id] : 0;
		$inch_kamount5 		= isset($_REQUEST['inch_kamount5'][$id]) ? $_REQUEST['inch_kamount5'][$id] : 0;
		$inch_kamount6 		= isset($_REQUEST['inch_kamount6'][$id]) ? $_REQUEST['inch_kamount6'][$id] : 0;
		$inch_kamount7 		= isset($_REQUEST['inch_kamount4'][$id]) ? $_REQUEST['inch_kamount4'][$id] : 0;
		$inch_kamount8 		= isset($_REQUEST['inch_kamount8'][$id]) ? $_REQUEST['inch_kamount8'][$id] : 0;
		$inch_kamount9 		= isset($_REQUEST['inch_kamount9'][$id]) ? $_REQUEST['inch_kamount9'][$id] : 0;
		$inch_kamount10		= isset($_REQUEST['inch_kamount10'][$id]) ? $_REQUEST['inch_kamount10'][$id] : 0;
		$inch_jurnalno  	= isset($_REQUEST['inch_jurnalno'][$id]) ? $_REQUEST['inch_jurnalno'][$id] : '';
		$inch_jurnaldate  	= isset($_REQUEST['inch_jurnaldate'][$id]) ? $_REQUEST['inch_jurnaldate'][$id] : '';
		$inch_nilai		  	= isset($_REQUEST['inch_nilai'][$id]) ? $_REQUEST['inch_nilai'][$id] : '';
		$inch_nilai_nidr  	= isset($_REQUEST['inch_nilai_nidr'][$id]) ? $_REQUEST['inch_nilai_nidr'][$id] : '';
		$inch_currency  	= isset($_REQUEST['inch_currency'][$id]) ? $_REQUEST['inch_currency'][$id] : '';
		$inch_supid			= isset($_REQUEST['inch_supid'][$id]) ? $_REQUEST['inch_supid'][$id] : '';
		$sup				= mysql_fetch_array(mysql_query("SELECT sup_name, sup_npwp FROM supplier WHERE id='$inch_supid'"));		
		$sup_name		  	= $sup['sup_name'];
		$id_pemasok		  	= $sup['sup_npwp'];
		$inch_dono		  	= isset($_REQUEST['inch_dono'][$id]) ? $_REQUEST['inch_dono'][$id] : '';
		$inch_ajd_nidr	  	= 0; #isset($_REQUEST['inch_ajd_nidr'][$id]) ? $_REQUEST['inch_ajd_nidr'][$id] : '';
		$inch_ajd		  	= 0; #isset($_REQUEST['inch_ajd'][$id]) ? $_REQUEST['inch_ajd'][$id] : '';
		$inch_rate		  	= isset($_REQUEST['inch_rate'][$id]) ? $_REQUEST['inch_rate'][$id] : 0;
		$total				= $inch_damount1 + $inch_damount2 + $inch_damount3 + $inch_damount4 + $inch_damount5 +
							  $inch_damount6 + $inch_damount7 + $inch_damount8 + $inch_damount9 + $inch_damount10;
		$totalk				= $inch_kamount1 + $inch_kamount2 + $inch_kamount3 + $inch_kamount4 + $inch_kamount5 +
							  $inch_kamount6 + $inch_kamount7 + $inch_kamount8 + $inch_kamount9 + $inch_kamount10;						  
		if($inch_paytype != 'BLANK' && $inch_paytype != 'OT'){
			if($inch_currency=="IDR"){
				if(!empty($inch_rate)){
					$inch_nilai_nidr2   = $total/$inch_rate; 
				}else{
					$inch_nilai_nidr2   = 0;
				}	
				$inch_nilai2		= $total;
			}else{
				$inch_nilai_nidr2   = $total;
				$inch_nilai2		= $total*$inch_rate;
			}
		}
		#detail item
		$prdcd="";
		$query = mysql_query("SELECT b.prod_code FROM incoming_detail a 
				 INNER JOIN product b ON b.id=a.incd_prodid WHERE a.incd_hid='$id' GROUP BY b.prod_code");
		while($data=mysql_fetch_object($query)){
			$prdcd 	= $prdcd."".$data->prod_code." | ";
		}
		$prd_cd = substr($prdcd,0,-2);
		#cek jurnal no
		$cjn = mysql_fetch_array(mysql_query("SELECT jurnal_no FROM jurnal WHERE jurnal_no='$inch_jurnalno'"));
		if(($inch_paytype != 'BLANK') && ($inch_paytype != 'OT') && ($total==$totalk) ){	
			if(empty($inch_jurnalno)){
				$message[] ="Data no dok : $inch_docno : $inch_doctype nomor jurnal kosong";
				$status="OK";
			}elseif(empty($inch_jurnaldate) || $inch_jurnaldate=="0000-00-00"){
				$message[] ="Data no dok : $inch_docno : $inch_doctype tanggal jurnal kosong";
				$status="OK";
			}elseif(substr($inch_jurnaldate,0,4) != $thn_acct){
				$message[] ="Data no dok : $inch_docno : $inch_doctype tanggal jurnal ($inch_jurnaldate) tidak sama dengan periode aktif ($thn_acct)";
				$status="OK";
			}elseif(!empty($cjn['jurnal_no'])){
				$message[] ="Data no dok : $inch_docno : $inch_jurnalno nomor jurnal sudah ada didatabase";
				$status="OK";
			}else{
				#Jurnal Header
				mysql_query("INSERT INTO jurnal SET jurnal_no='$inch_jurnalno', jurnal_date='$inch_jurnaldate', jurnal_iddept='10', jurnal_suppname='$sup_name', 
				jurnal_invno='$inch_invno_acct', jurnal_currency='$inch_currency', jurnal_amount='$inch_nilai2', jurnal_inputby='$user_id', jurnal_inputdate='$datetime', 
				jurnal_status=20, jurnal_amount_usd='$inch_nilai_nidr2', jurnal_idpemasok='$id_pemasok', jurnal_desc='Pembelian Barang dari No. Dok: $inch_doctype:$inch_docno Kode Barang :$prd_cd',
				jurnal_type='$inch_paytype', jurnal_trcid='$inch_id', jurnal_menu='BCINC', jurnal_nofaktur='$inch_dono', jurnal_rate='$inch_rate', jurnal_doctype='$inch_doctype',
				jurnal_docno='$inch_docno', jurnal_idsupp='$inch_supid'");
				if($inch_debit1 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnald_amount1 =$inch_damount1;
						if(!empty($inch_rate)){
							$jurnald_amount_usd1 =$inch_damount1/$inch_rate;
						}else{
							$jurnald_amount_usd1 =0;
						}	
					}else{
						$jurnald_amount1 =$inch_damount1*$inch_rate;
						$jurnald_amount_usd1 =$inch_damount1;
					}
					#jurnal detail debit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_debit1', jurnald_status=10,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnald_amount1', jurnald_amount_usd='$jurnald_amount_usd1',
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$inch_id', jurnald_invno_acct='$inch_invno_acct'");
				}
				if($inch_debit2 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnald_amount2 =$inch_damount2;
						if(!empty($inch_rate)){
							$jurnald_amount_usd2 =$inch_damount2/$inch_rate;
						}else{
							$jurnald_amount_usd2 =0;
						}
					}else{
						$jurnald_amount2 =$inch_damount2*$inch_rate;
						$jurnald_amount_usd2 =$inch_damount2;
					}
					#jurnal detail debit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_debit2', jurnald_status=10,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnald_amount2', jurnald_amount_usd='$jurnald_amount_usd2',
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$inch_id', jurnald_invno_acct='$inch_invno_acct'");
				}
				if($inch_debit3 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnald_amount3 =$inch_damount3;
						if(!empty($inch_rate)){
							$jurnald_amount_usd3 =$inch_damount3/$inch_rate;
						}else{
							$jurnald_amount_usd3 =0;
						}	
					}else{
						$jurnald_amount3 =$inch_damount3*$inch_rate;
						$jurnald_amount_usd3 =$inch_damount3;
					}
					#jurnal detail debit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_debit3', jurnald_status=10,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnald_amount3', jurnald_amount_usd='$jurnald_amount_usd3',
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$inch_id', jurnald_invno_acct='$inch_invno_acct'");
				}
				if($inch_debit4 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnald_amount4 =$inch_damount4;
						if(!empty($inch_rate)){
							$jurnald_amount_usd4 =$inch_damount4/$inch_rate;
						}else{
							$jurnald_amount_usd4 =0;
						}
					}else{
						$jurnald_amount4 =$inch_damount4*$inch_rate;
						$jurnald_amount_usd4 =$inch_damount4;
					}
					#jurnal detail debit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_debit4', jurnald_status=10,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnald_amount4', jurnald_amount_usd='$jurnald_amount_usd4',
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$inch_id', jurnald_invno_acct='$inch_invno_acct'");
				}				
				if($inch_kredit1 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnaldamount1 =$inch_kamount1;
						if(!empty($inch_rate)){
							$jurnaldamountusd1 =$inch_kamount1/$inch_rate;
						}else{
							$jurnaldamountusd1 =0;
						}	
					}else{
						$jurnaldamount1 =$inch_kamount1*$inch_rate;
						$jurnaldamountusd1 =$inch_kamount1;
					}
					#jurnal detail kredit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_kredit1', jurnald_status=20,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnaldamount1', jurnald_amount_usd='$jurnaldamountusd1', 
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$inch_id', jurnald_invno_acct='$inch_invno_acct'");
				}	
				if($inch_kredit2 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnaldamount2 =$inch_kamount2;
						if(!empty($inch_rate)){
							$jurnaldamountusd2 =$inch_kamount2/$inch_rate;
						}else{
							$jurnaldamountusd2 =0;
						}
					}else{
						$jurnaldamount2 =$inch_kamount2*$inch_rate;
						$jurnaldamountusd2 =$inch_kamount2;
					}
					#jurnal detail kredit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_kredit2', jurnald_status=20,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnaldamount2', jurnald_amount_usd='$jurnaldamountusd2', 
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$inch_id', jurnald_invno_acct='$inch_invno_acct'");
				}
				if($inch_kredit3 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnaldamount3 =$inch_kamount3;
						if(!empty($inch_rate)){
							$jurnaldamountusd3 =$inch_kamount3/$inch_rate;
						}else{
							$jurnaldamountusd3 =0;
						}
					}else{
						$jurnaldamount3 =$inch_kamount3*$inch_rate;
						$jurnaldamountusd3 =$inch_kamount3;
					}
					#jurnal detail kredit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_kredit3', jurnald_status=20,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnaldamount3', jurnald_amount_usd='$jurnaldamountusd3', 
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$inch_id', jurnald_invno_acct='$inch_invno_acct'");
				}	
				if($inch_kredit4 <> 'BLANK'){
					if($inch_currency=="IDR"){
						$jurnaldamount4 =$inch_kamount4;
						if(!empty($inch_rate)){
							$jurnaldamountusd4 =$inch_kamount4/$inch_rate;
						}else{
							$jurnaldamountusd4 =0;
						}
					}else{
						$jurnaldamount4 =$inch_kamount4*$inch_rate;
						$jurnaldamountusd4 =$inch_kamount4;
					}
					#jurnal detail kredit
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$inch_jurnalno', jurnald_acctno='$inch_kredit4', jurnald_status=20,
					jurnald_desc='Pembelian barang dengan nomor dokumen : $inch_doctype - $inch_docno Kode Barang :$prd_cd', jurnald_amount='$jurnaldamount4', jurnald_amount_usd='$jurnaldamountusd4', 
					jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$inch_id', jurnald_invno_acct='$inch_invno_acct'");
				}				
				#Update status
				mysql_query("UPDATE incoming SET inch_paytype='$inch_paytype' WHERE id='$inch_id'");
				#insert aktivitas
				mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Transfer inbox-pemasukan Dokumen No. $inch_docno==>$inch_docno', 
				user_date='$date', user_time='$time'");
				#konfirmasi OK
				$message[] ="Data  no dok : $inch_docno : $inch_doctype sukses disimpan";
				$status="OK";
			}
		}elseif($inch_paytype == 'OT'){
			#Update status
			mysql_query("UPDATE incoming SET inch_paytype='$inch_paytype' WHERE id='$inch_id'");
			#insert aktivitas
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Transfer inbox-pemasukan Dokumen No. $inch_docno==>$inch_docno', 
			user_date='$date', user_time='$time'");
			#konfirmasi OK
			$message[] ="Data  no dok : $inch_docno : $inch_doctype sukses disimpan";
			$status="OK";
		}elseif($inch_paytype == 'BLANK'){
			echo "<meta http-equiv='refresh' content='0; url=?page=Data-Temp-Pemasukan&fromdate=$fromdate&todate=$todate&sup_name2=$sup_name2'>";
		}else{
			$message[] ="Data no dok : $inch_docno : $inch_doctype debit-kredit tidak balance ($total-$totalk)";
			$status="OK";
		}
	}		
}
if($Act =="DETAIL"){
	$dd=mysql_fetch_array(mysql_query("SELECT * FROM incoming WHERE id='$inch_id'"));		
	$trcid 			= $dd['id'];			
	$supcustname	= $dd['nm_pemasok'];
	$invno			= $dd['inch_invno'];
	$invdate   		= $dd['inch_invdate'];
	$fakturno	    = $dd['inch_dono'];
	$doctype		= $dd['inch_doctype'];
	$docno			= $dd['inch_docno'];
	$docdate		= $dd['inch_docdate'];
	$npwpno			= $dd['id_pemasok'];
	$currency  		= $dd['inch_currency'];
	$kurs			= 0;
	$jurnal_no		= "";
	$tdebit			= 0;
	$total			= 0;
	$cek = mysql_fetch_array(mysql_query("SELECT SUM(incd_price) nilai_idr, SUM(incd_price_usd) nilai_nidr FROM incoming_detail 
		WHERE incd_hid='$inch_id'"));	
		$nilaiidr = number_format($cek['nilai_idr'],0,'',',');
		$nilaiusd = number_format($cek['nilai_nidr'],2,'.',',');
}
if($Act_Det == "ADD_DET"){ 
	mysql_query("INSERT INTO jurnal_temp SET jurnald_acctno='$jurnald_acctno', jurnald_desc='".mysql_real_escape_string($jurnald_desc)."', jurnald_amount='$jurnald_amount', 
	jurnald_amount_usd='$jurnald_amount_usd', jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_status='$jurnald_status', jurnald_inchid='$trcid'");
}
if($Act_Det == "DELETE_DET"){
	mysql_query("DELETE FROM jurnal_temp WHERE id='$id_deth'");
}
if($Act_Det == "UPDATE_DET"){
	foreach($id_det as $i => $id){
		$j_acctno		= $_REQUEST['j_acctno'][$id];
		$jurnald_desc	= $_REQUEST['jurnald_desc'][$id];
		$debit_usd		= $_REQUEST['debit_usd'][$id];
		$debit			= $_REQUEST['debit'][$id];
		$kredit_usd		= $_REQUEST['kredit_usd'][$id];
		$kredit			= $_REQUEST['kredit'][$id];
		$jurnald_status = $_REQUEST['jurnald_status'][$id];
		if($id_deth==$id){
			if($jurnald_status==10){
				mysql_query("UPDATE jurnal_temp SET jurnald_acctno='$j_acctno', jurnald_desc='".mysql_real_escape_string($jurnald_desc)."', jurnald_amount='$debit', jurnald_amount_usd='$debit_usd' WHERE id='$id_deth' ");
			}else{
				mysql_query("UPDATE jurnal_temp SET jurnald_acctno='$j_acctno', jurnald_desc='".mysql_real_escape_string($jurnald_desc)."', jurnald_amount='$kredit', jurnald_amount_usd='$kredit_usd' WHERE id='$id_deth' ");
			}
		}
	}					
}
if($Act =="SAVEJ"){
	#cek jurnal no
	$cekno = mysql_fetch_array(mysql_query("SELECT jurnal_no FROM jurnal WHERE jurnal_no='$jurnal_no'"));
	if(!empty($cekno['jurnal_no']) && empty($jurnal_id)){
		$jrn = "NO";
	}else{
		$jrn = "OK";
	}
	if($jrn=="NO"){
		echo "<font color='red'>Nomor Jurnal sudah ada silakan isi nomor jurnal yang baru</font><br>";
		#reload
		require_once "add_temp_pemasukan.php";
		exit;
	}else{
		if($jurnal_ptype != 'BLANK'){	
			#Update status
			mysql_query("UPDATE incoming SET inch_paytype='$jurnal_ptype', inch_invno_acct='$invno', inch_invdate_acct='$invdate',
			inch_jurnalno='$jurnal_no', inch_jurnaldate='$jurnal_date', id_pemasok='$npwpno' WHERE id='$trcid'");
		}
		if($jurnal_ptype != 'BLANK' && $jurnal_ptype != 'OT'){	
			$qrySave=mysql_query("INSERT INTO jurnal SET jurnal_no='$jurnal_no', jurnal_date='$jurnal_date', jurnal_iddept='10', jurnal_suppname='$supcustname', 
					jurnal_invno='$invno', jurnal_currency='$currency', jurnal_amount='$tdebit', jurnal_inputby='$user_id', jurnal_inputdate='$datetime', 
					jurnal_status=20, jurnal_amount_usd='$tdebit_usd', jurnal_idpemasok='$npwpno', jurnal_desc='Pembelian Barang dari No. Dok: $doctype:$docno',
					jurnal_type='$jurnal_ptype', jurnal_trcid='$trcid ', jurnal_menu='BCINC', jurnal_nofaktur='$fakturno', jurnal_rate='$kurs', jurnal_doctype='$doctype',
					jurnal_docno='$docno'");
			if($qrySave){
				$query = mysql_query("SELECT * FROM jurnal_temp WHERE jurnald_inputby='$user_id' AND jurnald_status IN(10,20) ORDER BY id ASC");
				while($dt=mysql_fetch_object($query)){
					mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$jurnal_no', jurnald_acctno='$dt->jurnald_acctno', jurnald_status='$dt->jurnald_status',
								 jurnald_desc='".mysql_real_escape_string($dt->jurnald_desc)."', jurnald_amount='$dt->jurnald_amount', jurnald_amount_usd='$dt->jurnald_amount_usd', 
								 jurnald_inputby='$user_id', jurnald_inputdate='$datetime', jurnald_inchid='$dt->jurnald_inchid'");
				}
				mysql_query("DELETE FROM jurnal_temp WHERE jurnald_inputby='$user_id' AND jurnald_status IN(10,20)");
			}	
			#Insert History User
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi jurnal No. $jurnal_no', user_date='$date', user_time='$time'");
			if($qrySave){
				$message[] = "Data Sukses tersimpan";	
				$status = "OK";				
			}
		}
	}
	
}	
if($Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET" || $Act_Det == "UPDATE_DET"){
	require_once "add_temp_pemasukan.php";
}
if($Act == "DETAIL2"){
	require_once "add2_temp_pemasukan.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Temp-Pemasukan&fromdate=$fromdate&todate=$todate'>";
}
#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-Temp-Pemasukan&fromdate=$fromdate&todate=$todate&sup_name2=$sup_name2'>";	
		exit;
	}else{
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Temp-Pemasukan&fromdate=$fromdate&todate=$todate&sup_name2=$sup_name2'>";
	}
}
?>