<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
    
$startdate = mysqli_real_escape_string($con, trim($request->value->start_date));
$enddate = mysqli_real_escape_string($con, trim($request->value->end_date));

$shop_id = mysqli_real_escape_string($con, trim($request->value->shop_id));
$shop_code = mysqli_real_escape_string($con, trim($request->value->shop_code));

include 'token_check.php';

if($token_check)
{
    $date = str_replace('/', '-', $startdate);
    $new_start_date = date('Y-m-d', strtotime($date));
    $date1 = str_replace('/', '-', $enddate);
    $new_end_date = date('Y-m-d', strtotime($date1));
    
    $i=0;
    $sels = $con->query("select order_id,datesloat,item_total,d_charge,tax_amount,discount_amount,shop_distance,total,p_method from orders where status = 'completed' and shop_id='$shop_id' and shop_code='$shop_code' and order_date BETWEEN '".$new_start_date."' AND '".$new_end_date."'");
    while($rows = $sels->fetch_assoc())
    {
    	$p['s_no'] = $i+1;
    	$p['order_id'] = $rows['order_id'];
    	$p['d_date'] = $rows['datesloat'];
    	$p['item_total'] = $rows['item_total'];
    	$p['d_charge'] = $rows['d_charge'];
    	$p['tax_amount'] = $rows['tax_amount'];
    	$p['discount_amount'] = $rows['discount_amount'];
    	$p['total'] = $rows['total'];
    	$p['p_method'] = $rows['p_method'];
    	$p['distance'] = floatval($rows['shop_distance']);
	    $cat =  $con->query("select per_km from admin where id='1'");
    	if($row = $cat->fetch_assoc())
    	{
    	    $perkm = $row['per_km'];
    	}
        $p['dis_amount'] = floatval($rows['shop_distance'] * $perkm);
    	$cp[] = $p;
		$i++;
    }
    $rows = array('data'=>$cp,'status'=>true);
    echo json_encode($rows);
}
else
{
    $rows = array('status'=>false,'message' => 'Data Error');
    echo json_encode($rows);
}
}


?>