<?php 
include "../config.php";
$customer= $_POST['customer'];
$product = $_POST['product'];
$vehicle = $_POST['vehicle'];
$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'];
$sql_statement="select salescode, ticket, chakka, vehicleno, customer, delivery, product, price, priceori, date, time, processtype, emptyweight, loadweight, unitvalue, netweight, netqty, netqtyori, cft, customertype, totalamount, remark, blockdate, editby, edittime, editdate, status, supplier, financialyear, tablename from second_transaction where status!='deleted' && status !='cancel' && product like '%$product%' && customer like '%$customer%' && concat(date,time) between '$date_from$time_from' and '$date_to$time_to' && ticket like '%$ticket%' && vehicleno like '%$vehicle%' 
union
select salescode, ticket, chakka, vehicleno, customer, delivery, product, price, priceori, date, time, processtype, emptyweight, loadweight, unitvalue, netweight, netqty, netqtyori, cft, customertype, totalamount, remark, blockdate, editby, edittime, editdate, status, supplier, financialyear, tablename from second_transaction_est where status!='deleted' && status !='cancel' && product like '%$product%' && customer like '%$customer%' && concat(date,time) between '$date_from$time_from' and '$date_to$time_to' && ticket like '%$ticket%' && vehicleno like '%$vehicle%'
union
select salescode, ticket, chakka, vehicleno, customer, delivery, product, price, priceori, date, time, processtype, emptyweight, loadweight, unitvalue, netweight, netqty, netqtyori, cft, customertype, totalamount, remark, blockdate, editby, edittime, editdate, status, supplier, financialyear, tablename from purchasedetails where status!='deleted' && status !='cancel' && product like '%$product%' && customer like '%$customer%' && concat(date,time) between '$date_from$time_from' and '$date_to$time_to' && ticket like '%$ticket%' && vehicleno like '%$vehicle%'
union
select ('-')as salescode,ticket,chakka,vehicleno,customer,delivery,product,('-')as price,('-')as priceori,entrydate as date, entrytime as time,matentrytype as processtype,emptyweight,loadweight,('-')as unitvalue,netweight,quantity as netqty,quantityori as netqtyori,('-')as cft,('-')as customertype,('-')as totalamount,remark,('-')as blockdate,editby,edittime,editdate,status,('-')as supplier,financialyear,('-')as tablename from single_transaction where status!='deleted' && status !='cancel' && product like '%$product%' && customer like '%$customer%' && concat(entrydate,entrytime) between '$date_from$time_from' and '$date_to$time_to' && ticket like '%$ticket%' && 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'];
}
$adjustment = number_format(($statement_report['netqtyori']-$statement_report['netqty']),2,'.','');
$adjustment_kg =number_format(( $adjustment * $statement_report['unitvalue']),2,'.','');
    $load_out[]=(object)["salescode"=>$statement_report['salescode'], "ticket"=>$statement_report['ticket'], "chakka"=>$statement_report['chakka'], "vehicleno"=>$statement_report['vehicleno'],"productname"=>$product_name,"customername"=>$customer_name,"customer"=>$statement_report['customer'], "delivery"=>$statement_report['delivery'], "product"=>$statement_report['product'], "price"=>$statement_report['price'], "priceori"=>$statement_report['priceori'], "date"=>$statement_report['date'], "time"=>$statement_report['time'], "processtype"=>$statement_report['processtype'], "emptyweight"=>$statement_report['emptyweight'], "loadweight"=>$statement_report['loadweight'], "unitvalue"=>$statement_report['unitvalue'], "netweight"=>$statement_report['netweight'], "netqty"=>$statement_report['netqty'], "netqtyori"=>$statement_report['netqtyori'], "cft"=>$statement_report['cft'], "customertype"=>$statement_report['customertype'], "totalamount"=>$statement_report['totalamount'], "remark"=>$statement_report['remark'], "blockdate"=>$statement_report['blockdate'], "editby"=>$statement_report['editby'],"adjustment"=>$adjustment,"adjkg"=>$adjustment_kg,"tripno"=>$trip_no,"entrydate"=>$entry_date,"entrytime"=>$entry_time,"cft"=>$statement_report['cft']];
}
//$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($load_out);
?>
