<?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));
$mode = mysqli_real_escape_string($con, trim($request->value->mode));

$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));
    
    if($mode == 'daily')
    {
        $i=0;
        $sels = $con->query("select uid,p_method,total,datesloat,item_total,d_charge,tax_amount,discount_amount,coins_use from orders where status = 'completed' and order_date ='".$new_start_date."' and shop_id='$shop_id' and shop_code='$shop_code'");
        while($rows = $sels->fetch_assoc())
        {
            $cat =  $con->query("select name from user where id=".$rows['uid']." and shop_id='$shop_id' and shop_code='$shop_code'");
        	if($row = $cat->fetch_assoc())
        	{
        	    $p['name'] = $row['name'];
        	}
        	$p['s_no'] = $i+1;
        	$p['order_date'] = $rows['datesloat'];
        	$p['pay_mode'] = $rows['p_method'];
            $p['item_total'] = $rows['item_total'];
        	$p['d_charge'] = $rows['d_charge'];
        	$p['tax_amount'] = $rows['tax_amount'];
        	$p['coins_use'] = $rows['coins_use'];
        	$p['discount_amount'] = $rows['discount_amount'];
        	$p['amount'] = $rows['total'];
        	$cp[] = $p;
    		$i++;
        }
        $rows = array('data'=>$cp,'status'=>true);
        echo json_encode($rows);
    }
    else if($mode == 'weekly')
    {
        $i=0;
        $sels = $con->query("select uid,p_method,total,datesloat,item_total,d_charge,tax_amount,discount_amount,coins_use 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())
        {
            $cat =  $con->query("select name from user where id=".$rows['uid']." and shop_id='$shop_id' and shop_code='$shop_code'");
        	if($row = $cat->fetch_assoc())
        	{
        	    $p['name'] = $row['name'];
        	}
        	$p['s_no'] = $i+1;
        	$p['order_date'] = $rows['datesloat'];
        	$p['pay_mode'] = $rows['p_method'];
        	$p['item_total'] = $rows['item_total'];
        	$p['d_charge'] = $rows['d_charge'];
        	$p['tax_amount'] = $rows['tax_amount'];
        	$p['coins_use'] = $rows['coins_use'];
        	$p['discount_amount'] = $rows['discount_amount'];
        	$p['amount'] = $rows['total'];
        	$cp[] = $p;
    		$i++;
        }
        $rows = array('data'=>$cp,'status'=>true);
        echo json_encode($rows);
    }
    else if($mode == 'monthly')
    {
        $i=0;
        $sels = $con->query("select uid,p_method,total,datesloat,item_total,d_charge,tax_amount,discount_amount,coins_use 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())
        {
            $uid = $rows['uid'];
            $cat =  $con->query("select name from user where id='$uid' and shop_id='$shop_id' and shop_code='$shop_code'");
        	if($row = $cat->fetch_assoc())
        	{
        	    $p['name'] = $row['name'];
        	}
        	$p['s_no'] = $i+1;
        	$p['order_date'] = $rows['datesloat'];
        	$p['pay_mode'] = $rows['p_method'];
        	$p['item_total'] = $rows['item_total'];
        	$p['d_charge'] = $rows['d_charge'];
        	$p['tax_amount'] = $rows['tax_amount'];
        	$p['coins_use'] = $rows['coins_use'];
        	$p['discount_amount'] = $rows['discount_amount'];
        	$p['amount'] = $rows['total'];
        	$cp[] = $p;
        	$i++;
        }
        $rows = array('data'=>$cp,'status'=>true);
        echo json_encode($rows);
    }
    else if($mode == 'custom')
    {
        $i=0;
        $sels = $con->query("select uid,p_method,total,datesloat,item_total,d_charge,tax_amount,discount_amount,coins_use 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())
        {
            $cat =  $con->query("select name from user where id=".$rows['uid']." and shop_id='$shop_id' and shop_code='$shop_code'");
        	if($row = $cat->fetch_assoc())
        	{
        	    $p['name'] = $row['name'];
        	}
        	$p['s_no'] = $i+1;
        	$p['order_date'] = $rows['datesloat'];
        	$p['pay_mode'] = $rows['p_method'];
        	$p['item_total'] = $rows['item_total'];
        	$p['d_charge'] = $rows['d_charge'];
        	$p['tax_amount'] = $rows['tax_amount'];
        	$p['coins_use'] = $rows['coins_use'];
        	$p['discount_amount'] = $rows['discount_amount'];
        	$p['amount'] = $rows['total'];
        	$cp[] = $p;
    		$i++;
        }
        $rows = array('data'=>$cp,'status'=>true);
        echo json_encode($rows);
    }
    else
    {
        $rows = array('data'=>"Error",'status'=>false);
        echo json_encode($rows);
    }
}
else
{
    $rows = array('status'=>false,'message' => 'Data Error');
    echo json_encode($rows);
}
}


?>