<?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'];
$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, 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 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 return_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 ('-')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'];
}
$sno+=1;
$adjustment = number_format(($statement_report['netqtyori']-$statement_report['netqty']),2,'.','');
$adjustment_kg =number_format(( $adjustment * $statement_report['unitvalue']),2,'.','');
    $load_out[]=(object)["Sno"=>$sno,"ticket"=>$statement_report['ticket'],"tripno"=>$trip_no,"customer"=>$statement_report['customer'],"customername"=>$customer_name,"productname"=>$product_name,"vehicleno"=>$statement_report['vehicleno'],"chakka"=>$statement_report['chakka'],  "delivery"=>$statement_report['delivery'],"entrydate"=>$entry_date,"entrytime"=>$entry_time,"emptyweight"=>$statement_report['emptyweight'],"exitdate"=>$statement_report['date'],"exittime"=>$statement_report['time'],"loadweight"=>$statement_report['loadweight'], "netweight"=>$statement_report['netweight'],"capacity"=>$statement_report['cft'],"netqty"=>$statement_report['netqty'], "price"=>$statement_report['price'],
    "totalamount"=>$statement_report['totalamount'],"adjustment"=>$adjustment,"adjkg"=>$adjustment_kg,"editby"=>$statement_report['editby'],"remark"=>$statement_report['remark']];
}
//$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);
?>
