<?php 
include "../config.php";
$customer= $_POST['customer'];
$product = $_POST['product'];
$vehicle = $_POST['vehicle'];
$mode_trans=$_POST['modetrans'];
//$time_from = empty($_POST['timefrom'])?'00:00:00':$_POST['timefrom'];
//$time_to = empty($_POST['timeto'])?'24:00:00':$_POST['timeto'];
//$ticket = $_POST['ticket'];
$date_minimum = mysqli_query($connect,"select min(id),date from ticket");
$date_min_val = mysqli_fetch_assoc($date_minimum);
$date_min_value = $date_min_val['date'];
$date_from = empty($_POST['fromd'])?$date_min_value:$_POST['fromd'];
$date_maximum = mysqli_query($connect,"select CURRENT_DATE() as maxdate");
$date_max_val = mysqli_fetch_assoc($date_maximum);
$date_max_value = $date_max_val['maxdate'];
 $date_to = empty($_POST['tod'])?$date_max_value:$_POST['tod'];
$sno = 0;
$sql_statement="
select salescode, ticket, chakka, vehicleno, customer, delivery, product, price, priceori, date, time, processtype, emptyweight, loadweight, unitvalue, netweight, netqty, netqtyori, cft, customertype, totalamount,modeof,remark, blockdate, editby, edittime, editdate, status, supplier, financialyear, tablename from second_transaction where status!='deleted' && product like '%$product%' && customer like '%$customer%' && modeof like '%$mode_trans%' && date between '$date_from' and '$date_to' && vehicleno like '%$vehicle%'
";
//echo $sql_statement;
$statement = mysqli_query($connect,$sql_statement);
while($statement_report=mysqli_fetch_array($statement)){
    $customer_code = $statement_report['customer'];
    $cus=mysqli_query($connect,"select name from customerdetails where status !='deleted' && code='$customer_code'");
while($cusshow = mysqli_fetch_array($cus))
{
    $customer_name = $cusshow['name'];
}
$product_code=$statement_report['product'];
$pro=mysqli_query($connect,"select productname from productdetail where status !='deleted' && productcode='$product_code'");
while($proshow = mysqli_fetch_array($pro))
{
   
    $product_name = $proshow['productname'];
}
$ticket_code = $statement_report['ticket'];
$ticket_trip=mysqli_query($connect,"select tripno,date,time from ticket where status !='deleted' && ticket='$ticket_code'");
while($tkt_show = mysqli_fetch_array($ticket_trip))
{
   
    $trip_no = $tkt_show['tripno'];
    $entry_date = $tkt_show['date'];
    $entry_time = $tkt_show['time'];
}
$sno+=1;
$adjustment = number_format(($statement_report['netqtyori']-$statement_report['netqty']),2,'.','');
$adjustment_kg =number_format(( $adjustment * $statement_report['unitvalue']),2,'.','');
    $statement_out[]=(object)["Sno"=>$sno,"invoicecode"=>$statement_report['salescode'],"invoicedate"=>$statement_report['date'],"tripno"=>$trip_no,"customername"=>$customer_name,"productname"=>$product_name,"capacity"=>$statement_report['cft'],"netqty"=>$statement_report['netqty'], "price"=>$statement_report['price'],
    "totalamount"=>$statement_report['totalamount'],"mode"=>$statement_report['modeof'],"delivery"=>$statement_report['delivery'],"remark"=>$statement_report['remark'],"employee"=>$statement_report['editby']];
}
$sql_mini_statement = "select product,ifnull(sum(netweight),0) as netweight,second_transaction.unitvalue from  second_transaction where status!='deleted' && product like '%$product%' && customer like '%$customer%' && modeof like '%$mode_trans%' && date between '$date_from' and '$date_to' && vehicleno like '%$vehicle%' Group by product";
$conn_sql_mini = mysqli_query($connect,$sql_mini_statement);
$st_sn_no = 0;
while($mini_statement_dis=mysqli_fetch_array($conn_sql_mini)){
    $st_sn_no +=1; 
    $product_codee=$mini_statement_dis['product'];
$prod=mysqli_query($connect,"select productname from productdetail where status !='deleted' && productcode='$product_codee'");
while($prodshow = mysqli_fetch_array($prod))
{
   
    $product_namee = $prodshow['productname'];
}
    $mini_statement_out[]=(object)['sno'=>$st_sn_no,"product"=>$product_namee,"netweight"=>$mini_statement_dis['netweight'].'KG',"tonn"=>($mini_statement_dis['netweight']/1000).'-TONN',"unit"=>number_format(($mini_statement_dis['netweight']/$mini_statement_dis['unitvalue']),2,'.','').'-UNIT']; 
}
$output_final = (object)["products"=>$statement_out,"Cummulative"=>$mini_statement_out];
//$output = (object)["customer"=>$customer,"product"=>$product,"vehicle"=>$vehicle,"timefrom"=>$time_from,"timeto"=>$time_to,"datefrom"=>$date_from,"dateto"=>$date_to,"ticket"=>$ticket];
echo json_encode($output_final);
?>
