<?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['posdetail'];
$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 //
	$pr_autoid 		= $pos[$i]['pr_autoid'];
	$outlet_autoid 		= $pos[$i]['outlet'];
	$transno 			= $pos[$i]['pr_transno'];
	$transdate 			= $pos[$i]['pr_transdate'];
	$supp_name			= $conn->real_escape_string($pos[$i]['supp_name']);
	$member_regno		= $conn->real_escape_string($pos[$i]['member_regno']);
	$total 				= $pos[$i]['total'];
	$status 			= $pos[$i]['status'];
	$user_entry	      	= $pos[$i]['user_entry'];
	$date_entry	      	= $pos[$i]['date_entry'];



	// detail //
	$pm_autoid 		= $pos[$i]['pm_autoid'];
	$pm_name 		= $conn->real_escape_string($pos[$i]['pm_name']);
	$qty 		= $pos[$i]['qty'];	
	$price 		= $pos[$i]['price'];

	


$sqldetail  = "INSERT INTO temp_pr
				(pr_autoid,pr_transno, pr_transdate, outlet_autoid,pr_gtotal,supp_name,member_regno,
				pm_autoid,pm_name,prd_qty,prd_price,
				pr_userentry,pr_dateentry,pr_status,api_batch_no) 
				VALUES 
				('$pr_autoid',
				'$transno', '$transdate', '$outlet_autoid','$total','$supp_name', '$member_regno', 
				'$pm_autoid','$pm_name','$qty','$price',
				'$user_entry','$date_entry','$status','$batch_no')";
				$conn->query($sqldetail) or die($sqldetail);

}

$jml = 0;
$sqln = "SELECT * FROM temp_pr WHERE api_batch_no = '$batch_no' AND outlet_autoid = '$outlet_id' GROUP BY pr_transno, pr_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_pr WHERE api_batch_no = '$batch_no' AND outlet_autoid = '$outlet_id' GROUP BY pr_transno, pr_transdate, outlet_autoid";
$dcon = $conn->query($sql) or die($sql);
while($rs = $dcon->fetch_assoc()){
		// header //
	$pr_autoid 			= $rs['pr_autoid'];
	$outlet_autoid 		= $rs['outlet_autoid'];
	$transno 			= $rs['pr_transno'];
	$transdate 			= $rs['pr_transdate'];
	$supp_name		= $conn->real_escape_string($rs['supp_name']);
	$member_regno		= $conn->real_escape_string($rs['member_regno']);
	$total 				= $rs['pr_gtotal'];
	$status 			= $rs['pr_status'];
	$user_entry	      	=$rs['pr_userentry'];
	$date_entry	      	=$rs['pr_dateentry'];


	$conn->query("update setupautoid set pr = pr + 1");
	
	$sql_autoid = "SELECT * FROM setupautoid";
	$dconx = $conn->query($sql_autoid) or die($sql_autoid);
	$rsx = $dconx->fetch_array();
	$new_autoid = $rsx['pr'];
	
	$sql_chk = "SELECT COUNT(*) as num FROM prheader WHERE ref_autoid = '$pr_autoid' AND outlet_autoid = '$outlet_autoid'";
	$dcon_chk = $conn->query($sql_chk) or die($sql_chk);
	$rschk = $dcon_chk->fetch_array();
	$checker = $rschk['num'];
	
	if($checker==0){
	$sql_header = "INSERT INTO prheader 
				(pr_autoid,pr_transno, pr_transdate, outlet_autoid,pr_gtotal, supp_name, member_regno, 
				pr_userentry,pr_dateentry,pr_status,ref_autoid) 
				VALUES 
				('$new_autoid',
				'$transno', '$transdate', '$outlet_autoid','$total', '$supp_name', '$member_regno',
				'$user_entry','$date_entry','$status','$pr_autoid')";
	$conn->query($sql_header) or die($sql_header);


	$sqld = "SELECT * FROM temp_pr WHERE pr_transno = '$transno' AND pr_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()){
		$pm_autoid 		= $rsd['pm_autoid'];
		$pm_name 		= $conn->real_escape_string($rsd['pm_name']);
		$qty 		= $rsd['prd_qty'];	
		$price 		= $rsd['prd_price']; // update start //

		
		
		$sql_detail = "INSERT INTO prdetail (pr_autoid,pm_autoid,pm_name,prd_price,prd_qty,ref_autoid) 
					VALUES ('$new_autoid','$pm_autoid','$pm_name','$price','$qty','$pr_autoid')";
		$conn->query($sql_detail) or die($sql_detail);
		
		$itt_autoid = 102;
		$itt_desc 	= "Stok Masuk - Pembelian - ".$supp_name;
		$sql_out = "INSERT INTO invlog 
					(log_refno,log_refdate,log_refautoid,pm_autoid,
					pm_name,log_inqty,whs_autoid,log_userentry,log_dateentry,log_reftype,
					itt_autoid,log_ums,itt_desc,log_price,log_avg_price,outlet_id,itt_code) 
					VALUES 
					('$transno','$transdate','$new_autoid','$pm_autoid',
					'$pm_name','$qty','$outlet_autoid','SYS',CURRENT_TIMESTAMP(),'PO','
					$itt_autoid','Kg', '$itt_desc','$price','$price','$outlet_autoid', 'P')";
										if($status==2){
					$conn->query($sql_out) or die($sql_out);
					}
		} // end while
} // end if checker
	
	
}

?>