<?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 //
	$so_autoid 		= $pos[$i]['so_autoid'];
	$outlet_autoid 		= $pos[$i]['outlet'];
	$transno 			= $pos[$i]['so_transno'];
	$transdate 			= $pos[$i]['so_transdate'];
	$cust_name			= $conn->real_escape_string($pos[$i]['cust_name']);
	$cust_autoid		= $conn->real_escape_string($pos[$i]['cust_autoid']);
	$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_so
				(so_autoid,so_transno, so_transdate, outlet_autoid,so_gtotal,cust_name,cust_autoid,
				pm_autoid,pm_name,sd_qty,sd_price,
				so_userentry,so_dateentry,so_status,api_batch_no) 
				VALUES 
				('$so_autoid',
				'$transno', '$transdate', '$outlet_autoid','$total','$cust_name', '$cust_autoid', 
				'$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_so WHERE api_batch_no = '$batch_no' AND outlet_autoid = '$outlet_id' GROUP BY so_transno, so_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_so WHERE api_batch_no = '$batch_no' AND outlet_autoid = '$outlet_id' GROUP BY so_transno, so_transdate";
$dcon = $conn->query($sql) or die($sql);
while($rs = $dcon->fetch_assoc()){
		// header //
	$so_autoid 			= $rs['so_autoid'];
	$outlet_autoid 		= $rs['outlet_autoid'];
	$transno 			= $rs['so_transno'];
	$transdate 			= $rs['so_transdate'];
	$cust_name		= $conn->real_escape_string($rs['cust_name']);
	$cust_autoid		= $conn->real_escape_string($rs['cust_autoid']);
	$total 				= $rs['so_gtotal'];
	$status 			= $rs['so_status'];
	$user_entry	      	= $rs['so_userentry'];
	$date_entry	      	= $rs['so_dateentry'];

	if($status!=99){
		$status = 0;
		}

	$conn->query("update setupautoid set so = so + 1");
	
	$sql_autoid = "SELECT * FROM setupautoid";
	$dconx = $conn->query($sql_autoid) or die($sql_autoid);
	$rsx = $dconx->fetch_array();
	$new_autoid = $rsx['so'];
	
	$sql_header = "INSERT INTO soheader 
				(so_autoid,so_transno, so_transdate, outlet_autoid,so_gtotal, cust_name, cust_autoid, 
				so_userentry,so_dateentry,so_status,ref_autoid) 
				VALUES 
				('$new_autoid',
				'$transno', '$transdate', '$outlet_autoid','$total', '$cust_name', '$cust_autoid',
				'$user_entry','$date_entry','$status','$so_autoid')";
	$conn->query($sql_header) or die($sql_header);


	$sqld = "SELECT * FROM temp_so WHERE so_transno = '$transno' AND so_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['sd_qty'];	
		$price 		= $rsd['sd_price']; // update start //

		
		
		$sql_detail = "INSERT INTO sodetail (so_autoid,pm_autoid,pm_name,sd_price,sd_qty,ref_autoid) 
					VALUES ('$new_autoid','$pm_autoid','$pm_name','$price','$qty','$so_autoid')";
		$conn->query($sql_detail) or die($sql_detail);
		
		$itt_autoid = 101;
		$itt_desc 	= "Stok Keluar - Penjualan - ".$cust_name;
		$sql_out = "INSERT INTO invlog 
					(log_refno,log_refdate,log_refautoid,pm_autoid,
					pm_name,log_outqty,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(),'SALES','
					$itt_autoid','Kg', '$itt_desc','$price','$price','$outlet_autoid', 'S')";
				//	$conn->query($sql_out) or die($sql_out);
}
	
	
}

?>