<?php 
include "../config.php";
$customer = $_POST['customer'];
$load = $_POST['loadtype'];
//echo $customer;
if ($customer != '')
{
$sql = "select blockdate,purchaseopening,salesopening,creditlimit from customerdetails where code='$customer' && status!='deleted'";
$conn=mysqli_query($connect,$sql);
while($row=mysqli_fetch_array($conn))
{
      
            $block_date= $row['blockdate'];
            if($block_date!="-"){
             $date_change = date_create($block_date);
             $date_format = date_format($date_change,"d-m-Y");
            }
              $purchase_opening = $row['purchaseopening'];
              $sales_opening = $row['salesopening'];
              $credit_value = $row['creditlimit'];
}             
if($load == "Load"){
    $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'];
    $advance_amount = mysqli_query($connect,"select ifnull(sum(advance),0)as advance from advance where customer='$customer'&& status!='deleted'");
    $advance_dis = mysqli_fetch_assoc($advance_amount);
    $total_advance_dis = $advance_dis['advance'];
    $advance_total = $advance_dis['advance']-$dis_cash['advance'];
        $limit =$advance_total-$purchase_opening-$purchase_value+$dis_cash['amount']+$dis_cash['otheramount']+$dis_cash['advance'];
        $output = $block_date."||".$limit."||".$total_advance_dis;
}
if($load == "Empty"){
    $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'];
        $net_amount = mysqli_query($connect,"select ifnull(sum(totalamount),0)as totalamt from second_transaction where customer='$customer' && status!='deleted'");
        $net_amt = mysqli_fetch_assoc($net_amount);
        $total_amount = $net_amt['totalamt'];
        $net_amount_est = mysqli_query($connect,"select ifnull(sum(totalamount),0)as totalamt from second_transaction_est where customer='$customer' && status!='deleted'");
        $net_amt_est = mysqli_fetch_assoc($net_amount_est);
        $total_amount_est = $net_amt_est['totalamt'];
        $limit = $credit_value == '-' ? '-' : $credit_value-$sales_opening-$total_amount-$total_amount_est+$cash_value+$other_value;
        $output = $block_date."||".$limit;
}
if($load == "Return"){
        $string_hyphen = '-';
        $output = $string_hyphen."||".$string_hyphen;
}

echo  $output;
}
else
{
$block = "-";
$limit = 0;
$output = $block."||".$limit;
 echo $output;
}

?>
