<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['uid'] == '')
{
 $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");    
}
else
{

$shop_id =  $data['shop_id'];
$shop_code =  $data['shop_code'];
$distance =  $data['distance'];
$shopdistance =  $data['shop_distance'];
$latitude =  $data['latitude'];
$longitude =  $data['longitude'];
$order_id =  $data['order_id'];
$uid =  $data['uid'];
$datesloat = $data['date'];
$timesloat = $data['time'];
$oid ='#'.uniqid();
$pname = $data['pname'];
$status = 'pending';
$p_method = $data['p_method'];
$address_id = $data['address_id'];
$address = $data['address'];
$tax = $data['tax'];
$timestamp = date("Y-m-d");
$tid = $data['tid'];
$cook = $data['cooking'];
$itemtotal = $data['item_total'];
$tax_amount = $data['tax_amount'];
$discount_amount = $data['discount_amount'];
$couponcode = $data['coupon_code'];
$dcharge = $data['dcharge'];
$total = number_format((float)$data['total'], 2, '.', '');

$e= array();
$p = array();
$w=array();
$pp = array();
$q = array();
for($i=0;$i<count($pname);$i++)
{
$e[] = $pname[$i]['title'];
$p[] = $pname[$i]['pid'];
$w[] = $pname[$i]['weight'];
$pp[] = $pname[$i]['cost'];
$q[] = $pname[$i]['qty'];
}
$pname = implode('$;',$e);
$pid = implode('$;',$p);
$ptype = implode('$;',$w);
$pprice = implode('$;',$pp);
$qty = implode('$;',$q);


$sql_qu ="select * from orders where order_id = '$order_id'";
$status1 = $con->query($sql_qu)->num_rows;
if($status1 == 0)
{
    $sql = "insert into orders(`shop_id`,`shop_code`,`order_id`,`oid`,`uid`,`pname`,`pid`,`ptype`,`pprice`,`datesloat`,`timesloat`,`order_date`,`status`,`qty`,`total`,`p_method`,`shop_distance`,
    `address_id`,`address`,`tax`,`tid`,`cooking_ins`,`d_charge`,`item_total`,`discount_amount`,`tax_amount`,`coupon_code`,`distance`,`latitude`,`longitude`)
    values('$shop_id','$shop_code','$order_id','$oid','$uid','$pname','$pid','$ptype','$pprice','$datesloat','$timesloat','$timestamp','$status','$qty','$total','$p_method','$shopdistance',
    '$address_id','$address','$tax','$tid','$cook','$dcharge','$itemtotal','$discount_amount','$tax_amount','$couponcode','$distance','$latitude','$longitude')";
    $con->query($sql);
    $returnArr = array("ResponseCode"=>$sql,"Result"=>"true","ResponseMsg"=>"Order Placed Successfully!");
}
else
{
    $returnArr = array("ResponseCode"=>$status,"Result"=>"false","ResponseMsg"=>"Retry Place Order");
}

}

echo json_encode($returnArr);