<?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 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;
$output[] = (object)["name"=>$customer_name,"opening"=>$opening_value,"purchase"=>$purchase_value,"payment"=>$cash_value+$other_value+$advance_value,"pending"=>$pending_amount];

}
echo json_encode($output);

?>
