<?php 
include "../config.php";
$fromd = $_GET['fromd'];
$tod = $_GET['tod'];
$shift = $_GET['shift'];
    $datefr = date_create($fromd);
    $date_from = date_format($datefr,"Y-m-d");
    $datet = date_create($tod);
    $date_to = date_format($datet,"Y-m-d");
    if($shift == 1){
        $date_from = $date_from.'07:00:00';
        $date_to  = $date_to.'19:00:00';
    }else if($shift == 2){
        $date_from = $date_from.'19:00:00';
        $date_to  = $date_to.'07:00:00';
    }else if($shift == 0){
        $date_from = $date_from.'00:00:00';
        $date_to  = $date_to.'23:59:59';
    }
$sno = 0;
$load_out = [];
$collection_report = [];
$cash_report = [];
$load_out_single = [];
$single_load = [];
$cash_collection_report_gst=[];
$sql_statement="select salescode,ticket,vehicleno, customer, delivery, product, price, priceori, date, time,  unitvalue, netweight, netqty, netqtyori, cft, totalamount,modeof, remark, tablename, driver_amount,('-') as driver_name,editby as user from second_transaction where status!='deleted' && status !='cancel' && concat(date,time) between '$date_from' and '$date_to'
union
select salescode,ticket,vehicleno, customer, delivery, product, price, priceori, date, time,  unitvalue, netweight, netqty, netqtyori, cft, totalamount,modeof, remark, tablename,('0') as driver_amount,('-') as driver_name,editby as user from second_transaction_est where status!='deleted' && status !='cancel' && concat(date,time) between '$date_from' and '$date_to'";

//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,unittokg from productdetail where status !='deleted' && productcode='$product_code'");
while($proshow = mysqli_fetch_array($pro))
{
   
    $product_name = $proshow['productname'];
	$tax = $proshow['tax'];
	$unittokg=$proshow['unittokg'];
}
$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'];
}
$driver_name = $statement_report['driver_name'];
if($statement_report['driver_name'] != '-'){
    $driver_code = $statement_report['driver_name'];
    $driver_details = mysqli_query($connect,"SELECT `drivercode`,`drivername` FROM `drivermaster` WHERE `status` != 'deleted' && drivercode = '$driver_code'");
    $driver_name = mysqli_fetch_assoc($driver_details)['drivername'];
}
$sno+=1;
$adjustment = number_format(($statement_report['netqtyori']-$statement_report['netqty']),2,'.','');
$adjustment_kg =number_format(( $adjustment * $statement_report['unitvalue']),2,'.','');

                             // $tonn=($statement_report['netqty']*$unittokg)/1000;
							 $tonn=($statement_report['netweight'])/1000;
    $load_out[]=(object)["date"=>$entry_date,"entrytime"=>$entry_time,"exitdate"=>$statement_report['date'],"exittime"=>$statement_report['time'],"ticket"=>$statement_report['ticket'],"salescode"=>$statement_report['salescode'],"tripno"=>$trip_no,"customer"=>$statement_report['customer'],"customername"=>$customer_name,"productname"=>$product_name,"vehicleno"=>$statement_report['vehicleno'],"delivery"=>$statement_report['delivery'],"netqty"=>$statement_report['netqty'],"adjedqty"=>$statement_report['netqtyori'], "price"=>$statement_report['price'], "priceori"=>$statement_report['priceori'],"gst"=>$tax,
    "totalamount"=>$statement_report['totalamount'],"adjustment"=>$adjustment,"adjkg"=>$adjustment_kg,"remark"=>$statement_report['remark'],"driveramount"=>$statement_report['driver_amount'],'drivername'=>$driver_name,"mode"=>$statement_report['modeof'],'createdby'=>$statement_report['user'],"tonn"=>$tonn];
    $driver_name = '';
}

$sql_statement_single="SELECT `id`, `ticket`, `vehicleno`,('-') as `driver_code`, `customer`, `trip`, `delivery`, `chakka`, `cft`, `entrydate`, `entrytime`, `matentrytype`, `product`, `salestype`, `emptyweight`, `loadweight`, `netweight`, `quantity`, `quantityori`, `adjustment`, `remark`, `editby`, `edittime`, `editdate`, `status`, `financialyear` FROM `single_transaction` WHERE concat(entrydate,entrytime) between '$date_from' and '$date_to'";
//echo $sql_statement_single;
$statement_single = mysqli_query($connect,$sql_statement_single);
while($statement_report_single=mysqli_fetch_array($statement_single)){
   
$product_code_single=$statement_report_single['product'];
$pro_single=mysqli_query($connect,"select productname,tax,unittokg from productdetail where status !='deleted' && productcode='$product_code_single'");
while($proshow_single = mysqli_fetch_array($pro_single))
{
   
    $product_name_single = $proshow_single['productname'];
	$tax_single = $proshow_single['tax'];
	$unittokg_single=$proshow_single['unittokg'];
}
$tonn=($statement_report_single['netweight'])/1000;
$single_load[]=(object)['product'=>$product_name_single,'quantity'=>$statement_report_single['quantity'],'tonn'=>$tonn];
}

$sno_collec = 0;
$collection_data = mysqli_query($connect,"SELECT `id`, `date`, `customer`,`sales_code`, `totalamount`, `pendingamount`, `amount`, `otheramount`, `paymentmethod`, `remark`, `cheque`, `code`, `user`, `time`, `financialyear`, `editdate`, `edittime`, `status` FROM `creditdetails` where concat(date,time) between '$date_from' and '$date_to' && status != 'deleted'");
while($collection_record = mysqli_fetch_array($collection_data)){
    $sno_collec+=1;
    $customer_code = $collection_record['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'];
}
$collection_report[]=(object)['collectionid'=>$collection_record['code'],'customername'=>$customer_name,'salescode'=>$collection_record['sales_code'],'amount'=>($collection_record['amount']),'otheramount'=>($collection_record['otheramount']),'mode'=>$collection_record['paymentmethod'],'remark'=>$collection_record['remark'],'collectedby'=>$collection_record['user']];



//$collection_report[]=(object)['collectionid'=>$collection_record['code'],'customername'=>$customer_name,'salescode'=>$collection_record['sales_code'],'amount'=>($collection_record['amount'] + $collection_record['otheramount'] ),'mode'=>$collection_record['paymentmethod'],'remark'=>$collection_record['remark'],'collectedby'=>$collection_record['user']];
}


$sno_cashhandlind = 0;
$cash_data = mysqli_query($connect,"SELECT `id`, `date`, `givento`, `givenby`, `given_amount`, `remark`,  `code`, `user`, `time`, `financialyear`, `editdate`, `edittime`, `status` FROM `cash_handling` where concat(date,time) between '$date_from' and '$date_to' && status != 'deleted'");
while($cash_record = mysqli_fetch_array($cash_data)){
    $sno_cashhandlind+=1;
   
   
$cash_report[]=(object)['collectionid'=>$cash_record['code'],'givendate'=>$cash_record['date'],'givento'=>$cash_record['givento'],'givenby'=>$cash_record['givenby'],'given_amount'=>($cash_record['given_amount'] ),'remark'=>$cash_record['remark'],'status'=>$cash_record['status']];
}

$sno_cashcollection_gst = 0;
$cash_collection_data_gst = mysqli_query($connect,"SELECT totalamount as gsttotalsales  FROM `second_transaction` where concat(date,time) between '$date_from' and '$date_to' && status != 'deleted' and modeof='cash' &&  status !='cancel'");



while($cash_collection_record_gst = mysqli_fetch_array($cash_collection_data_gst)){
    $sno_cashcollection_gst+=1;
   
   
$cash_collection_report_gst[]=(object)['gsttotalsales'=>number_format($cash_collection_record_gst['gsttotalsales'],2,'.','')];
}


/*$sno_cashcollection_est = 0;
$cash_collection_data_est = mysqli_query($connect,"SELECT totalamount as esttotalsales  FROM `second_transaction_est` where concat(date,time) between '$date_from' and '$date_to' && status != 'deleted' and modeof='cash'");



while($cash_collection_record_est = mysqli_fetch_array($cash_collection_data_est)){
    $sno_cashcollection_est+=1;
	
	$estamt=$cash_collection_record_est['esttotalsales'];
	
   echo $estamt."jega";
   
$cash_collection_report_est[]=(object)['esttotalsales'=>number_format($estamt,2,'.','')];
}*/



$final_report = (object)['load'=>$load_out,'collection'=>$collection_report,'cashhandling'=>$cash_report,'cashcollectiongst'=>$cash_collection_report_gst,'load_single'=>$single_load];

//$final_report = (object)['load'=>$load_out,'collection'=>$collection_report,'cashhandling'=>$cash_report,'cashcollectiongst'=>$cash_collection_report_gst,'cashcollectionest'=>$cash_collection_report_est];
echo json_encode($final_report);
?>
