<?php 
include "../config.php";
$customer=$_POST['customer'];
$from_date = $_POST['fromdate'];
$to_date = $_POST['todate'];
if(empty($from_date)&&empty($to_date)){
$sales_sql = "select date,(ifnull((select tripno from ticket where ticket.ticket = purchasedetails.ticket && status!='deleted'),'-'))as tripno,customer,salescode as entrycode,totalamount,('Sales')as process,productname,netqty,('-') as collectionby,('-')as cash,('-')as bank,('-')as collection,financialyear,remark from purchasedetails where customer='$customer'&& status!='deleted'
union
select date,('-')as tripno,customer,code as entrycode,('-')as totalamount,('Collection')as process,('-')as productname,('-')as netqty,paymentmethod as collectionby,amount as cash, otheramount as bank,(amount+otheramount)as collection,financialyear,remark from debitdetails where customer = '$customer'&& status!='deleted' order by date asc
";
$sales_data = mysqli_query($connect,$sales_sql);
while($sales_statement=mysqli_fetch_array($sales_data))
{
    $customer = $sales_statement['customer'];
    $date_from = $sales_statement['date'];
    $date_change = date_create($date_from);
    $date_format = date_format($date_change,"d/m/y");
$cus=mysqli_query($connect,"select name from customerdetails where code='$customer' && status !='deleted'");
while($cusshow = mysqli_fetch_array($cus))
{
   $customer_name = $cusshow['name'];
}
    $statement[]=(object)["customercode"=>$customer,"tripno"=>$sales_statement['tripno'],"customername"=>$customer_name,"salescode"=>$sales_statement['entrycode'],"process"=>$sales_statement['process'],"product"=>$sales_statement['productname'],"quantity"=>$sales_statement['netqty'],"salesamount"=>$sales_statement['totalamount'],"collectionby"=>$sales_statement['collectionby'],"cash"=>$sales_statement['cash'],"bank"=>$sales_statement['bank'],"collection"=>$sales_statement['collection'],"financialyear"=>$sales_statement['financialyear'],"remark"=>$sales_statement['remark'],"date"=>$date_format];
}
$opening_balance = mysqli_query($connect,"select purchaseopening from customerdetails where code='$customer'&& status!='deleted'");
$dis_opening = mysqli_fetch_assoc($opening_balance);
$opening_value = $dis_opening['purchaseopening'];
$purchase_balance = mysqli_query($connect,"select ifnull(sum(totalamount),0)as purchase from purchasedetails where customer='$customer'&& status!='deleted'");
$dis_purchase = mysqli_fetch_assoc($purchase_balance);
$purchase_value = $dis_purchase['purchase'];
$cash_balance = mysqli_query($connect,"select ifnull(sum(amount),0)as amount,ifnull(sum(otheramount),0)as otheramount,ifnull(sum(advance),0)as advance from debitdetails where customer='$customer'&& status!='deleted'");
$dis_cash = mysqli_fetch_assoc($cash_balance);
$cash_value = $dis_cash['amount'];
$other_value = $dis_cash['otheramount'];
$advance_value = $dis_cash['advance'];
$total_amount = $opening_value + $purchase_value;
$pending_amount = $total_amount-$cash_value-$other_value-$advance_value;
$mini_statement= (object)["name"=>$customer_name,"opening"=>$opening_value,"purchase"=>$purchase_value,"collection"=>$cash_value+$other_value,"advance"=>$advance_value,"pending"=>$pending_amount];
$final_output = (object)["statement"=>$statement,"ministatement"=>$mini_statement];
echo json_encode($final_output);
}
if(!empty($customer)&&!empty($from_date)&&!empty($to_date)){
       $sales_sql = "select date,customer,salescode as entrycode,totalamount,('Sales')as process,('-') as collectionby,('-')as cash,('-')as bank,('-')as advance,('-')as collection,financialyear,remark from purchasedetails where customer='$customer'&& status!='deleted'&& date between '$from_date' and '$to_date'
union
select date,customer,code as entrycode,('-')as totalamount,('Payment')as process,paymentmethod as collectionby,amount as cash, otheramount as bank,advance as advance,(amount+otheramount+advance)as collection,financialyear,remark from debitdetails where customer = '$customer'&& status!='deleted'&& date between '$from_date' and '$to_date' order by date asc
";
$sales_data = mysqli_query($connect,$sales_sql);
while($sales_statement=mysqli_fetch_array($sales_data))
{
    $customer = $sales_statement['customer'];
    $date_from = $sales_statement['date'];
    $date_change = date_create($date_from);
    $date_format = date_format($date_change,"d/m/y");
$cus=mysqli_query($connect,"select name from customerdetails where code='$customer' && status !='deleted'");
while($cusshow = mysqli_fetch_array($cus))
{
   $customer_name = $cusshow['name'];
}
    $statement[]=(object)["customercode"=>$customer,"customername"=>$customer_name,"salescode"=>$sales_statement['entrycode'],"salesamount"=>$sales_statement['totalamount'],"process"=>$sales_statement['process'],"collectionby"=>$sales_statement['collectionby'],"cash"=>$sales_statement['cash'],"bank"=>$sales_statement['bank'],"advance"=>$sales_statement['advance'],"collection"=>$sales_statement['collection'],"financialyear"=>$sales_statement['financialyear'],"remark"=>$sales_statement['remark'],"date"=>$date_format];
}
$opening_balance = mysqli_query($connect,"select purchaseopening from customerdetails where code='$customer'&& status!='deleted' ");
$dis_opening = mysqli_fetch_assoc($opening_balance);
$opening_value_date = $dis_opening['purchaseopening'];
$purchase_balance_date = mysqli_query($connect,"select ifnull(sum(totalamount),0)as purchase from purchasedetails where customer='$customer'&& status!='deleted'&& date<'$from_date'");
$dis_purchase_date = mysqli_fetch_assoc($purchase_balance_date);
$purchase_value_date = $dis_purchase_date['purchase'];
$cash_balance_date = mysqli_query($connect,"select ifnull(sum(amount),0)as amount,ifnull(sum(otheramount),0)as otheramount,ifnull(sum(advance),0)as advance from debitdetails where customer='$customer'&& status!='deleted'&& date<'$from_date'");
$dis_cash_date = mysqli_fetch_assoc($cash_balance_date);
$cash_value_date = $dis_cash_date['amount'];
$other_value_date = $dis_cash_date['otheramount'];
$advance_value_date = $dis_cash_date['advance'];
$total_amount_date = $opening_value_date + $purchase_value_date;
$pending_amount_date = $total_amount_date-$cash_value_date-$other_value_date-$advance_value_date;
$purchase_balance = mysqli_query($connect,"select ifnull(sum(totalamount),0)as purchase from purchasedetails where customer='$customer'&& status!='deleted' && date between '$from_date' and '$to_date'");
$dis_purchase = mysqli_fetch_assoc($purchase_balance);
$purchase_value = $dis_purchase['purchase'];
$cash_balance = mysqli_query($connect,"select ifnull(sum(amount),0)as amount,ifnull(sum(otheramount),0)as otheramount,ifnull(sum(advance),0)as advance from debitdetails where customer='$customer'&& status!='deleted' && date between '$from_date' and '$to_date'");
$dis_cash = mysqli_fetch_assoc($cash_balance);
$cash_value = $dis_cash['amount'];
$other_value = $dis_cash['otheramount'];
$advance_value = $dis_cash['advance'];
$total_amount = $pending_amount_date + $purchase_value;
$pending_amount = $total_amount-$cash_value-$other_value-$advance_value;
$mini_statement= (object)["name"=>$customer_name,"opening"=>$pending_amount_date,"purchase"=>$purchase_value,"collection"=>$cash_value+$other_value,"advance"=>$advance_value,"pending"=>$pending_amount];
$final_output = (object)["statement"=>$statement,"ministatement"=>$mini_statement];
echo json_encode($final_output);
}
?>