<?php 
require 'db.php';
 
$data = json_decode(file_get_contents('php://input'), true);


$rid = $data['deli_code'];
$fromdate = $data['from_date'];
$todate = $data['to_date'];

if ($rid == '' || $fromdate == '' || $todate == '')
{
$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went wrong  try again !");
}
else 
{
	$pok = array();
	$sales_total = 0;
	
// 	 $riderdata = $con->query("select * from assist_settings where deli_code='$rid'")->fetch_assoc();
// 	 $pok['total_reject_order'] = $riderdata['rejected_orders'];
// 	 $pok['total_complete_order'] = $riderdata['completed_orders'];
// 	 $pok['total_order'] = $riderdata['assigned_orders'];
	 
// 	 $r_count = $riderdata['rejected_orders'];
	
	 $o_id = $con->query("select count(order_id) as oid from orders where rid='$rid' and status='completed' and order_date BETWEEN '$fromdate' AND '$todate'")->fetch_assoc();
     if($o_id['oid'] == '')
	 {
		 $o_count = 0;
	 }
	 else 
	 {
		 $o_count = $o_id['oid'];
	 }
	 
	 $n_id = $con->query("select count(order_id) as oid from orders where rid='$rid' and status='cancelled' and order_date BETWEEN '$fromdate' AND '$todate'")->fetch_assoc();
     if($n_id['oid'] == '')
	 {
		 $n_count = 0;
	 }
	 else 
	 {
		 $n_count = $n_id['oid'];
	 }
	 
	 $pok['total_reject_order'] = $n_count;
	 $pok['total_complete_order'] = $o_count;
	 $pok['total_order'] = $o_count + $n_count;
	 
	  $sale = $con->query("select sum(total) as total_earn, sum(distance) as total_distance  from orders where rid='$rid' and status='completed' and o_status = '4' and order_date BETWEEN '$fromdate' AND '$todate'")->fetch_assoc();
     if($sale['total_earn'] == 0)
     {
         $total_dis = 0;
    	 $sales_total = 0;
     }
     else 
     {
         $total_dis = $sale['total_distance'];
    	 $sales_total = $sale['total_earn'];
     }
     
     $pok['total_distance'] = $total_dis;
     $pok['total_sale'] = number_format((float)$sales_total, 2, '.', '');
     
     $total_earn = 0.00;
     $sels = $con->query("select distance from orders where rid='$rid' and status='completed' and o_status = '4' and order_date BETWEEN '$fromdate' AND '$todate'");
    while($rows = $sels->fetch_assoc())
    {
        
        $distance = $rows['distance'];
        if($distance < 3)
        {
            $total_earn = $total_earn + 15;
        }
        else if($distance >= 3 && $distance < 4)
        {
            $total_earn = $total_earn + 16.50;
        }
        else if($distance >= 4 && $distance < 5)
        {
            $total_earn = $total_earn + 24;
        }
        else if($distance >= 5 && $distance < 6)
        {
            $total_earn = $total_earn + 30;
        }
        else if($distance >= 6 && $distance < 7)
        {
            $total_earn = $total_earn + 37.80;
        }
        else if($distance >= 7 && $distance < 8)
        {
            $total_earn = $total_earn + 46.90;
        }
        else if($distance >= 8 && $distance < 9)
        {
            $total_earn = $total_earn + 56;
        }
        else if($distance >= 9 && $distance < 10)
        {
            $total_earn = $total_earn + 65.70;
        }
        else if($distance >= 10 && $distance < 11)
        {
            $total_earn = $total_earn + 75;
        }
    }
        
    $pok['total_earning'] = number_format((float)$total_earn, 2, '.', '');
    
     $pay_d = $con->query("select count(p_method) as pay_method, sum(total) as pay_total from orders where rid='$rid' and status='completed' and p_method='Pay on Delivery' and order_date BETWEEN '$fromdate' AND '$todate'")->fetch_assoc();
     if($pay_d['pay_method'] == 0)
	 {
		 $pay = 0;
		 $pay_total = 0;
	 }
	 else 
	 {
		 $pay = $pay_d['pay_method'];
		 $pay_total = $pay_d['pay_total'];
	 }
	 
	 $pok['total_cod_order'] = $pay;
	 $pok['total_cod_amount'] = number_format((float)$pay_total, 2, '.', '');
	 
	 $upi_d = $con->query("select count(p_method) as pay_method,sum(total) as pay_total from orders where rid='$rid' and status='completed' and p_method='Online Payment' and order_date BETWEEN '$fromdate' AND '$todate'")->fetch_assoc();
     if($upi_d['pay_method'] == 0)
	 {
		 $upi = 0;
		 $upi_total = 0;
	 }
	 else 
	 {
		 $upi = $upi_d['pay_method'];
		 $upi_total = $upi_d['pay_total'];
	 }
	 
	 $pok['total_upi_order'] = $upi;
	 $pok['total_upi_amount'] = number_format((float)$upi_total, 2, '.', '');
	 
	 $returnArr = array("assist_data"=>$pok,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Status Get Successfully!");    
}
echo json_encode($returnArr);
mysqli_close($con);
?>
