<?php 
include "../config.php";
$month=array(1,2,3,4,5,6,7,8,9,10,11,12);
$month_name=array('jan', 'feb', 'mar', 'apr', 'may','jun', 'jul', 'aug', 'sep', 'oct','nov','dec');
$year=date('Y');
foreach($month as $id => $value)
{
    $opening_balance_date = mysqli_query($connect,"select ifnull(sum(salesopening),0) as salesopening from customerdetails where  status!='deleted'");
$dis_opening_date = mysqli_fetch_assoc($opening_balance_date);
$opening_value_date = $dis_opening_date['salesopening'];
    $month_value=sprintf('%02d', $month[$id]);
    $sales_balance_date= mysqli_query($connect,"select ifnull(sum(totalamount),0)as sales from second_transaction where status!='deleted' && date < '$year-$month_value-01'");
$dis_sales_date = mysqli_fetch_assoc($sales_balance_date);
$sales_value_date = $dis_sales_date['sales'];
$cash_balance_date = mysqli_query($connect,"select ifnull(sum(amount),0)as amount,ifnull(sum(otheramount),0)as otheramount from creditdetails where  status!='deleted' && date < '$year-$month_value-01'");
$dis_cash_date = mysqli_fetch_assoc($cash_balance_date);
$cash_value_date = $dis_cash_date['amount'];
$other_value_date = $dis_cash_date['otheramount'];
$total_amount_date = $opening_value_date + $sales_value_date;
$pending_amount_date = $total_amount_date-$cash_value_date-$other_value_date;
    $sales_sql="select ifnull(sum(totalamount),0) as total from second_transaction where status!='deleted'&& date between '$year-$month_value-01' and '$year-$month_value-30' ";
    //echo $sales_sql;
$sales_count= mysqli_query($connect,$sales_sql);
while($sale_weight = mysqli_fetch_array($sales_count)){
    $collection_sql="select ifnull(sum(amount+otheramount),0) as amount from creditdetails where status!='deleted'&& date between '$year-$month_value-01' and '$year-$month_value-30' ";
    //echo $sales_sql;
    $collection_sql_count= mysqli_query($connect,$collection_sql);

while($collection_sql_cash = mysqli_fetch_array($collection_sql_count)){
    $total_sales_year = $sale_weight['total'] + $pending_amount_date;
    $total_collection_year = $collection_sql_cash['amount'];
   $output[]=(object)["month"=>$month_name[$id],"opening"=>$pending_amount_date,"saleamount"=>$sale_weight['total'],"totalamount"=>$sale_weight['total']==0?0:$total_sales_year,"collection"=>$total_collection_year];
}
}
}
echo json_encode($output); 

?>
