<?php 
include "../config.php";
include "../financial_year_calculation.php";
$accountyear    = $financialYearStart."-".$financialYearEnd;
$year=date('Y');
$month =date('m');
$last_month=date("m",strtotime("-1 month"));
$product_sql="select ifnull(sum(netqty),0) as total,productname,product from second_transaction where status!='deleted'&& date between '$year-$month-01' and '$year-$month-30' group by product order by sum(netqty) desc limit 1";
$product_sale= mysqli_query($connect,$product_sql);
while($product_code = mysqli_fetch_array($product_sale)){
$sno=0;
  $product_name_cus=$product_code['productname'];
  $product_code_cus=$product_code['product'] ;
$customer_sale= mysqli_query($connect,"select ifnull(sum(netqty),0) as total,customer,(select customerdetails.name from customerdetails where customerdetails.code=second_transaction.customer && status!='deleted')as cusname from second_transaction where status!='deleted'&& date between '$year-$month-01' and '$year-$month-30' && product='$product_code_cus' group by customer order by sum(netqty) desc limit 5");
while($customer_code = mysqli_fetch_array($customer_sale)){
  $sno+=1;
  $customer_chk = $customer_code ['cusname']; 
  $product_count_salepro[]=(float)$customer_code ['total'];
  $output[]= (object)["customer"=>$customer_chk,"productname"=>$product_name_cus,"unit"=>$product_count_salepro];
  $product_count_salepro=[];
}
}
echo json_encode($output);
?>