<?php
ob_start();
include("../connect.php");
$response=array();
if(isset($_POST['distributor_id']) && isset($_POST['transaction_date']))
{ 
    $id = $_POST['distributor_id']; 
	$date= $_POST['transaction_date']; 
	
	 $result=mysqli_query($con,"select * from tbl_transaction,tbl_master_customer where tbl_transaction.customer_id=tbl_master_customer.customer_id and  tbl_transaction.transaction_status='Pending' and tbl_master_customer.distributor_id='$id'");
	 $count=mysqli_num_rows($result);
	
	if($date=='')
	{
	$res = mysqli_query($con,"SELECT * FROM tbl_master_customer,tbl_transaction where 
	tbl_master_customer.customer_id=tbl_transaction.customer_id AND
	tbl_master_customer.distributor_id='$id'");
	}
	else
	{
	$res = mysqli_query($con,"SELECT * FROM tbl_master_customer,tbl_transaction where 
	tbl_master_customer.customer_id=tbl_transaction.customer_id AND
	tbl_master_customer.distributor_id='$id' and tbl_transaction.transaction_date='$date'");	
	}
	
	$row=mysqli_num_rows($res);
	if($row>0) 
	{	
      $categories=array();
       while($row=mysqli_fetch_array($res))
	   {  
		  $category['customer_name'] = $row['customer_name'];
		  $category['customer_phone'] = $row['customer_phone'];
		  $category['total_quantity'] = $row['total_quantity'];
		  $category['total_amount'] = $row['total_amount'];
		  $category['remark'] = $row['remark'];
		  $category['transaction_id'] = $row['transaction_id'];
		  $category['transaction_date'] = $row['transaction_date'];
		  $category['transaction_time'] = $row['transaction_time'];
		  $category['transaction_status'] = $row['transaction_status'];
		  $c_id=$row['transaction_id'];
		  
		  
		  $res1 = mysqli_query($con,"SELECT * FROM tbl_transaction_detail,tbl_master_product,tbl_transaction where tbl_transaction.transaction_id=tbl_transaction_detail.transaction_id AND
		  tbl_transaction_detail.product_id=tbl_master_product.product_id AND tbl_transaction_detail.transaction_id='$c_id'");
          $category['product']=array();
		   while($row1=mysqli_fetch_array($res1))
		   {
			$product['product_id'] = $row1['product_id'];
			$product['product_name'] = $row1['product_name'];
			$product['quantity'] = $row1['quantity'];
			$product['price'] = $row1['price'];
			$product['warranty'] = $row1['warranty'];
			$product['warranty_description'] = $row1['warranty_description'];

            $category['product'][]=$product;		
		   }
		   
		  $response['categories'][]=$category;	   
	   }

	   $response['success']=1;
	   $response['pending_order']=$count;
	   $response['details']="Order Detail Fetched Succesfully";  
	}
    else
	{
		$response['success']=0;
		$response['pending_order']=$count;
		$response['details']="Orders Not Available!!";
	} 
	$response['app_version']='1.1';
}
else
{	
	$response['success']=0;
	$response['pending_order']=$count;
	$response['details']="Required fields missing";
}
echo json_encode($response);
?> 