<?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,modeof, 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,modeof, 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 ('-')as salescode,ticket,chakka,vehicleno,customer,('-')as modeof,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,tax from productdetail where status !='deleted' && productcode='$product_code'");
while($proshow = mysqli_fetch_array($pro))
{
   
    $product_name = $proshow['productname'];
	$gst_tax = $proshow['tax']+100;
	
	$rate=$statement_report['price'];
	$modeof=$statement_report['modeof'];
	
	$gst_unit=number_format((($rate*$gst_tax)/100),2,'.','');
	 $rate_unit=number_format(($rate+$gst_unit),2,'.','');
	 
	 $actualamt=$statement_report['totalamount'];
	 $total_gst=number_format((($actualamt*$gst_tax)/100),2,'.','');
	 $totalamount=$actualamt+$total_gst;
	 
	 $per_unit=($rate/$gst_tax)*100;
	 $tax_amt=$rate-$per_unit;
	 $gross=$per_unit*$statement_report['netqty'];
	 
	 $cgst=($tax_amt/2)*$statement_report['netqty'];
	 
	 $net=$cgst+$cgst+$gross;
}
$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,"exitdate"=>$statement_report['date'],"exittime"=>$statement_report['time'],"vehicleno"=>$statement_report['vehicleno'],"customername"=>$customer_name,"productname"=>$product_name,"salescode"=>$statement_report['salescode'],"netqty"=>$statement_report['netqty'], "price"=>$statement_report['price'],"GST"=>$gst_unit,"Rate/Unit"=>$rate_unit,"Gross Amount"=>$statement_report['totalamount'], "CGST"=>$total_gst/2,"SGST"=>$total_gst/2,"Total Amount"=>$totalamount,"delivery"=>$statement_report['delivery'],  "editby"=>$statement_report['editby'],"remark"=>$statement_report['remark']];
   
 $load_out[]=(object)["Sno"=>$sno,"exitdate"=>$statement_report['date'],"exittime"=>$statement_report['time'],"vehicleno"=>$statement_report['vehicleno'],"customername"=>$customer_name,"productname"=>$product_name,"salescode"=>$statement_report['salescode'],"netqty"=>$statement_report['netqty'], "price/Unit"=>$statement_report['price'],"GST Amt"=>$tax_amt,"Before Tax Price"=>$per_unit,"Gross Amt"=>$gross, "CGST"=>$cgst,"SGST"=>$cgst,"Total Amount"=>$net,"payment"=>$modeof,"delivery"=>$statement_report['delivery'],  "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);
?>
