<?php
include('../config/database.php');
date_default_timezone_set('Asia/Calcutta');
$request 		= file_get_contents('php://input');
$input 			= json_decode($request);
$shop_id	    = $input->{'shop_id'};
$order_total	= $input->{'order_total'};
$productList    = $input->{'productList'};
$count			= count($productList);
$sql_3			= 0;
$sql            = mysqli_query($cn,"select max(order_id) from `tbl_order`");
$sqlOrderData   = mysqli_fetch_array($sql);

$sqlRet         = mysqli_query($cn,"SELECT * FROM `tbl_retailer` where `id`='".$shop_id."'");
$sqlRetData     = mysqli_fetch_array($sqlRet);
		
$maxOrderId     = $sqlOrderData['max(order_id)'];	
if($maxOrderId==NULL){
	$order_id = 1;}
else{
	$order_id =$sqlOrderData['max(order_id)']+1;
}
for($i=0;$i<$count;$i++)
{
	$product	= $productList[$i];
	$product_id	= $product->product_id;
	$sku		= $product->sku;
	$unit    	= $product->unit;
	$price		= $product->price;
	$qty		= $product->qty;
	$total		= $product->total;	
	if($qty>0)
	{
		$sqlOrder = mysqli_query($cn,"INSERT INTO `tbl_order` set `order_date`=Now(), `shop_id`='".$shop_id."', `state`='".$sqlRetData['state']."', `district`='".$sqlRetData['district']."', `beat`='".$sqlRetData['beat']."', `order_id`='".$order_id."', `product_id`='".$product_id."', `sku`='".$sku."', `unit`='".$unit."', `price`='".$price."', `qty`='".$qty."', `total`='".$total."',`order_total`='".$order_total."', `date`=Now()");
	}
}
if($sqlOrder)
{
	$arr 			     = array("status"=>"true", 'msg'=>"Order Saved Successfully.");    	
}
else
{
	$arr 				 = array("status"=>"false", 'msg'=>"Some thing when Wrong. Try Again.");
}
echo json_encode($arr);
mysqli_close($cn);
?>