<?php 


include "config.php";
include "financial_year_calculation.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 = $_POST['datesecd'];
$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'];
$salescode = $_POST['salescode'];
//$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'];
$tablename = $_POST['tablename'];
$financial_year = $_POST['financialyear'];
$mode_of = $_POST['modetrans'];
$cft = $_POST['cft'];
$bill_type_ori = $_POST['billtypeori'];
$bill_type = $_POST['billtype'];
$accountyear    = $financialYearStart."-".$financialYearEnd;
$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'];
echo $customer;
echo $customerori;
echo $product;
echo $productori;
echo $delivery;
echo $deliveryori;
echo $vehicleno;
echo $tablename;

//echo $financial_Year;
// if($emptyweight > $loadweight)
// {
//       $loadout = 'In';
// }
// if($emptyweight < $loadweight)
// {
//       $loadout = 'Out';
// }
echo $load;
//foreach ($productcode as $id => $value ){
    if( $load == 'Return' && $tablename=='return_transaction')
    {
        echo 'inside if';
        $update = mysqli_query($connect,"update return_transaction set status='deleted',editdate=CURRENT_DATE(),edittime=CURRENT_TIME() where ticket ='$ticket'");
         echo "Sales-update";
         $code_validate = mysqli_query($connect,"select count(ticket) as code from return_transaction where ticket='$ticket' && status!='deleted'&& status!='cancel'");
         $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,remark,modeof,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('$salescode','$ticket','$vehicleno','$customer','$delivery','$product','$productname','$price','$priceori','$date','$time','$load','$emptyweight','$loadweight','$unitvalue','$netweight','$netqty','$netqtyori','$cft','','$totalamount','$remark','$mode_of','$block_date','$chakka','$user',CURTIME(),'$date','','$dispatch_district','$dispatch_state_code','$dispatch_state','$dispatch_pincode','$eway_trans_id','$eway_trans_name','$eway_distance','$financial_year','return_transaction')";
echo 'sql'.$sql;
$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');
//  }
?>