<?php
include('../sys/phpcore.inc');
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/x-www-form-urlencoded");


$key 		= $conn->real_escape_string($_POST['key']);
$id 		= $conn->real_escape_string($_POST['id']);
$pos 		= $_POST['qdetail'];
$batch_no 	= date('ymdhis');



if($key!="-_mAsdS90121+0182*"){
		echo "INVALID";
		return false;
	}




	$outlet_id 		= $pos[0]['outlet'];

$row = count($pos);
for($i=0;$i<$row;$i++){
	// header //
	$jt_autoid 			= $pos[$i]['jt_autoid'];
	$outlet_autoid 		= $pos[$i]['outlet'];
	$transno 			= $pos[$i]['jt_transno'];
	$transdate 			= $pos[$i]['jt_transdate'];
	$jt_vcno 			= $pos[$i]['jt_vcno'];
	$jt_vcdate 			= $pos[$i]['jt_vcdate'];

	$jt_notes 			= $conn->real_escape_string($pos[$i]['jt_notes']);

	$status 			= $pos[$i]['status'];
	$user_entry	      	= $pos[$i]['user_entry'];
	$date_entry	      	= $pos[$i]['date_entry'];

	if($status=="Y"){$status = "N";}

	// detail //
	$coa_autoid 		= $pos[$i]['coa_autoid'];
	$coa_autoid2 		= $pos[$i]['coa_autoid2'];
	$jtd_debit			= $pos[$i]['jtd_debit'];
	$jtd_credit			= $pos[$i]['jtd_credit'];	
	$jtd_debit2			= $pos[$i]['jtd_debit2'];
	$jtd_credit2		= $pos[$i]['jtd_credit2'];	
	$jtd_notes			= $conn->real_escape_string($pos[$i]['jtd_notes']);

$sqldetail  = "INSERT INTO temp_jurnal
				(jt_autoid,jt_transno, jt_transdate, jt_vcno,jt_vcdate,jt_notes ,
				coa_autoid,coa_autoid2,jtd_debit,jtd_credit,jtd_debit2,jtd_credit2,jtd_notes,
				outlet_autoid,jt_userentry,jt_dateentry,jt_flagpost,api_batch_no) 
				VALUES 
				('$jt_autoid',
				'$transno', '$transdate','$jt_vcno','$jt_vcdate','$jt_notes',
				'$coa_autoid','$coa_autoid2','$jtd_debit','$jtd_credit','$jtd_debit2','$jtd_credit2','$jtd_notes',
				'$outlet_autoid','$user_entry','$date_entry','$status','$batch_no')";
				$conn->query($sqldetail) or die($sqldetail);

}


$jml = 0;
$sqln = "SELECT * FROM temp_jurnal WHERE api_batch_no = '$batch_no' AND outlet_autoid = '$outlet_id' GROUP BY jt_transno, jt_transdate";
$dcon = $conn->query($sqln) or die($sqln);
while($rsn = $dcon->fetch_assoc()){
$jml = $jml + 1;
}


echo  $jml;
	//echo "<br><h4>".$jml." Transaksi Pembelian Berhasil Dikirim</h4>";

$sql = "SELECT * FROM temp_jurnal WHERE api_batch_no = '$batch_no' AND outlet_autoid = '$outlet_id' GROUP BY jt_transno, jt_transdate";
$dcon = $conn->query($sql) or die($sql);
while($rs = $dcon->fetch_assoc()){
		// header //
	$jt_autoid 			= $rs['jt_autoid'];
	$outlet_autoid 		= $rs['outlet_autoid'];
	$transno 			= $rs['jt_transno'];
	$transdate 			= $rs['jt_transdate'];
	$jt_vcno 			= $rs['jt_vcno'];
	$jt_vcdate 			= $rs['jt_vcdate'];
	$jt_notes 			= $conn->real_escape_string($pos[$i]['jt_notes']);



	$status 			= $rs['jt_flagpost'];
	$user_entry	      	=$rs['jt_userentry'];
	$date_entry	      	=$rs['jt_dateentry'];

	if($status=="Y"){$status = "N";}

	$conn->query("update setupautoid set jt = jt + 1");
	
	$sql_autoid = "SELECT * FROM setupautoid";
	$dconx = $conn->query($sql_autoid) or die($sql_autoid);
	$rsx = $dconx->fetch_array();
	$new_autoid = $rsx['jt'];
	
	$sql_header = "INSERT INTO jtheader 
				(jt_autoid,jt_transno, jt_transdate, jt_vcno, jt_vcdate,jt_notes,jt_flagpost, jt_userentry,jt_dateentry,ref_autoid,
				outlet_autoid) 
				VALUES 
				('$new_autoid',
				'$transno', '$transdate', '$jt_vcno', '$jt_vcdate', '$jt_notes', '$status','$user_entry', '$date_entry','$jt_autoid',
				'$outlet_autoid')";
	$conn->query($sql_header) or die($sql_header);


	$sqld = "SELECT * FROM temp_jurnal WHERE jt_transno = '$transno' AND jt_transdate = '$transdate' AND api_batch_no = '$batch_no' AND outlet_autoid = '$outlet_id'";
	$dcond = $conn->query($sqld) or die($sqld);
	while($rsd = $dcond->fetch_assoc()){
		$jt_autoid 		= $rsd['jt_autoid'];
		$coa_autoid 		= $rsd['coa_autoid'];
		$coa_autoid2 		= $rsd['coa_autoid2'];
		$jtd_vcno			= $rsd['jt_vcno'];
		$jtd_vcdate			= $rsd['jt_vcdate'];		
		$jtd_notes			= $conn->real_escape_string($rsd['jtd_notes']);
		$jtd_debit			= $rsd['jtd_debit'];		
		$jtd_credit			= $rsd['jtd_credit'];	
		$jtd_debit2			= $rsd['jtd_debit2'];		
		$jtd_credit2		= $rsd['jtd_credit2'];		
		
		
		$sql_detail = "INSERT INTO jtdetail 
					(jt_autoid,coa_autoid,coa_autoid2,jtd_vcno,jtd_vcdate, jtd_debit, jtd_credit, jtd_debit2,jtd_credit2,jtd_notes) 
					VALUES 
					('$new_autoid','$coa_autoid','$coa_autoid2','$jtd_vcno','$jtd_vcdate','$jtd_debit','$jtd_credit',
					'$jtd_debit2','$jtd_credit2','$jtd_notes')";
		$conn->query($sql_detail) or die($sql_detail);
		
	} // end detail
	
	
}

?>