<?php 
include "../config.php";
$customer_id=mysqli_query($connect,"select code,name from customerdetails where status!='deleted'");
while($customer_code = mysqli_fetch_array($customer_id))
{
    $customer = $customer_code['code'];
    $customer_name = $customer_code['name'];
$opening_balance = mysqli_query($connect,"select salesopening from customerdetails where code='$customer'&& status!='deleted'");
$dis_opening = mysqli_fetch_assoc($opening_balance);
$opening_value = $dis_opening['salesopening'];
$sales_balance = mysqli_query($connect,"select ifnull(sum(totalamount),0)as sales from second_transaction where customer='$customer'&& status!='deleted'");
$dis_sales = mysqli_fetch_assoc($sales_balance);
$sales_value = $dis_sales['sales'];
$sales_balance_est = mysqli_query($connect,"select ifnull(sum(totalamount),0)as sales from second_transaction_est where customer='$customer'&& status!='deleted'");
$dis_sales_est = mysqli_fetch_assoc($sales_balance_est);
$sales_value_est = $dis_sales_est['sales'];
$cash_balance = mysqli_query($connect,"select ifnull(sum(amount),0)as amount,ifnull(sum(otheramount),0)as otheramount from creditdetails where customer='$customer'&& status!='deleted'");
$dis_cash = mysqli_fetch_assoc($cash_balance);
$cash_value = $dis_cash['amount'];
$other_value = $dis_cash['otheramount'];
$total_amount = $opening_value + $sales_value + $sales_value_est;
$pending_amount = $total_amount-$cash_value-$other_value;
$output[] = (object)["name"=>$customer_name,"opening"=>$opening_value,"sales"=>$sales_value + $sales_value_est,"collection"=>$cash_value+$other_value,"pending"=>$pending_amount];
}
echo json_encode($output);
?>
