<?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,coins_use,deli_tip 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['coins_use'] = $rows['coins_use'];
    	$p['p_method'] = $rows['p_method'];
    	$p['deli_tip'] = $rows['deli_tip'];
    	$shop_dis = $rows['shop_distance'];
    // 	$p['distance'] = floatval($rows['shop_distance']);
	    $cat =  $con->query("select per_km from admin where id='1'");
    	if($row = $cat->fetch_assoc())
    	{
    	    $perkm = number_format($row['per_km'],2);
    	}
    	
    	if($shop_dis <= 1)
        {
            $shop_dis = 1;
        }
        else if($shop_dis <= 2)
        {
            $shop_dis = 2;
        }
        else if($shop_dis <= 3)
        {
            $shop_dis = 3;
        }
        else if($shop_dis <= 4)
        {
            $shop_dis = 4;
        }
        else if($shop_dis <= 5)
        {
            $shop_dis = 5;
        }
        else if($shop_dis <= 6)
        {
            $shop_dis = 6;
        }
        else if($shop_dis <= 7)
        {
            $shop_dis = 7;
        }
        else if($shop_dis <= 8)
        {
            $shop_dis = 8;
        }
        else if($shop_dis <= 9)
        {
            $shop_dis = 9;
        }
        else if($shop_dis <= 10)
        {
            $shop_dis = 10;
        }
        else if($shop_dis <= 11)
        {
            $shop_dis = 11;
        }
        else if($shop_dis <= 12)
        {
            $shop_dis = 12;
        }
        $p['distance'] = $shop_dis;
        $p['dis_amount'] = number_format($shop_dis * $perkm,2);
    	$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);
}
}


?>