<?php 
include "config.php";
include "financial_year_calculation.php";
include "current_date_time.php";
session_start();
$user=$_SESSION['log'];
$ticket = $_POST['ticketadd'];
$vehicleno = $_POST['vehicleno'];
$customer = $_POST['customeradd'];
$delivery = $_POST['delivery'];
$product = $_POST['productadd'];
$productname= $_POST['productnameadd'];
$price = $_POST['priceadd'];
$priceori = $_POST['priceaddori'];
$date = (empty($_POST['datesecd'])?$cur_date:$_POST['datesecd']);
$time =(empty($_POST['timesecd'])?$cur_time:$_POST['timesecd']);
$load = $_POST['loadtype'];
$emptyweight = ($_POST['emptyweightadd']> $_POST['loadweightadd'])?$_POST['loadweightadd']:$_POST['emptyweightadd'];
$loadweight = ($_POST['emptyweightadd']< $_POST['loadweightadd'])?$_POST['loadweightadd']:$_POST['emptyweightadd'];
$unitvalue = $_POST['unitvalue'];
$netweight = $_POST['netweightadd'];
$netqty = $_POST['netqtyadd'];
$netqtyori = $_POST['netqtyori'];
//$customertype = $_POST['customertypeadd'];
$totalamt = $_POST['totalamtadd'];
$totalamount = round($totalamt);
$remark = $_POST['remarkadd'];
$block_date = $_POST['blockdate'];
//$credit = $_POST['credit']; 
$customerori = $_POST['customerori'];
$productori = $_POST['productaddori'];
$deliveryori=$_POST['deliveryori'];
$chakka = $_POST['chakkaname'];
$cft = $_POST['cft'];
$mode_of=$_POST['modetrans'];
$dispatch_district = $_POST['dispatchdistrict'];
$dispatch_state_code = $_POST['dispatchStateCode'];
$dispatch_state = $_POST['dispatchStateName'];
$dispatch_pincode = $_POST['dispatchPincode'];
$eway_trans_id = $_POST['transportId'];
$eway_trans_name = $_POST['transportName'];
$eway_distance  = $_POST['distance'];
$accountyear    = $financialYearStart."-".$financialYearEnd;
$year_code_end = substr($financialYearEnd,2);
$year_code_start = substr($financialYearStart,2);
echo $customer;
echo $customerori;
echo $product;
echo $productori;
echo $delivery;
echo $deliveryori;
//echo $billtype;
echo $vehicleno;
// if($emptyweight > $loadweight)
// {
//       $loadout = 'In';
// }
// if($emptyweight < $loadweight)
// {
//       $loadout = 'Out';
// }
echo $load;
//foreach ($productcode as $id => $value ){
    if( $load == 'Return')
    {
        $updatetkt=mysqli_query($connect,"UPDATE ticket SET loadtype='$load' WHERE ticket='$ticket'");
         $procount = mysqli_query($connect,"select count(salescode)as code from return_transaction where return_transaction.status!='deleted' && financialyear='$accountyear'");
         while($dis = mysqli_fetch_array($procount))
         {
             $salescode = ($dis['code']+1);
             //$sales_code = 'YSS/'.$year_code_start.'-'.$year_code.'/CN'.$salescode;
             $sales_code = 'CN/'.$salescode.'/'.$year_code_start.'-'.$year_code_end;
            }
         echo $salescode;
         $code_validate = mysqli_query($connect,"select count(ticket) as code from return_transaction where ticket='$ticket' && status!='deleted'&& status!='cancel'&& financialyear='$accountyear'");
         $code_count=mysqli_fetch_assoc($code_validate);
         $code_val = $code_count['code'];
         if($code_val==0)
         {
  $sql = "insert into return_transaction (salescode,ticket,vehicleno,customer,delivery,product,productname,price,priceori,date,time,processtype,emptyweight,loadweight,unitvalue,netweight,netqty,netqtyori,cft,customertype,totalamount,modeof,remark,blockdate,chakka,editby,edittime,editdate,status,dispatch_district, dispatch_state_code, dispatch_state, dispatch_pincode, eway_trans_id, eway_trans_name, eway_distance,financialyear,tablename) values('$sales_code','$ticket','$vehicleno','$customer','$delivery','$product','$productname','$price','$priceori','$date','$time','$load','$emptyweight','$loadweight','$unitvalue','$netweight','$netqty','$netqtyori','$cft','','$totalamount','$mode_of','$remark','$block_date','$chakka','$user','$time','$date','','$dispatch_district','$dispatch_state_code','$dispatch_state','$dispatch_pincode','$eway_trans_id','$eway_trans_name','$eway_distance','$accountyear','return_transaction')";

$update = "update ticket set process ='completed' where ticket.ticket = '$ticket'";
         }
    }
    if($customer != $customerori)
    {
        $upcustkt = "update  ticket set customer ='$customer' where ticket.ticket = '$ticket'";
        //$upcusfst = "update   first_transaction set customer ='$customer' where ticket = '$ticket'";
        mysqli_query($connect,$upcustkt);
        //$goupcusfst = mysqli_query($connect,$upcusfst);
echo $upcustkt;

    }
    if($product != $productori)
    {
        $upprotkt = "update   ticket set product ='$product' where ticket.ticket = '$ticket'";
       // $upprofst = "update   first_transaction set product ='$product' where ticket = '$ticket'";
        mysqli_query($connect,$upprotkt);
       // $goupcusfst = mysqli_query($connect,$upprofst);


    }
    if($delivery != $deliveryori)
    {
        $updistkt = "update   ticket set delivery ='$delivery' where ticket.ticket = '$ticket'";
        //$upcusfst = "update   first_transaction set delivery ='$customer' where ticket.ticket = '$ticket'";
         mysqli_query($connect,$updistkt);
      //  $goupcusfst = mysqli_query($connect,$upcusfst);
echo $updistkt;

    }

    echo $sql;
 mysqli_query($connect,$sql);
 mysqli_query($connect,$update);

 //header('Location:returntransaction.php');
//  }
?>
<script>
 let loadType = "<?php echo $load?>";
 console.log(loadType);
 let tableName = '';
  if(loadType == 'Return') {
    tableName = 'return_transaction';
  }
  let folderName = '<?php echo $folder_name ?>';
     window.open("salesbillall.php?print=<?php echo $sales_code ?>&table="+tableName,"OriginalWindowa40",'width=722,height=950,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,left=25,top=25');
	 let srcVariable=`${window.location.origin}/${folderName}/secondtransaction.php`;
	 console.log(srcVariable);
	 window.location.href = srcVariable;
</script>