<?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'];
$accountyear    = $financialYearStart."-".$financialYearEnd;
$year_code = 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 == 'Empty')
    {
        $updatetkt=mysqli_query($connect,"UPDATE ticket SET loadtype='$load' WHERE ticket='$ticket'");
         $procount = mysqli_query($connect,"select count(salescode)as code from second_transaction_est where second_transaction_est.status!='deleted' && financialyear='$accountyear'");
         while($dis = mysqli_fetch_array($procount))
         {
             $salescode = ($dis['code']+1);
             $sales_code = $year_code_start.'-'.$year_code.'/'.$salescode;
         }
         echo $salescode;
         $code_validate = mysqli_query($connect,"select count(ticket) as code from second_transaction_est 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 second_transaction_est (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,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','','$accountyear','second_transaction_est')";

$update = "update ticket set process ='completed' where ticket.ticket = '$ticket'";
         }
    }
    
    
    if ($load == 'Load' )
    {
        $updatetkt=mysqli_query($connect,"UPDATE ticket SET loadtype='$load' WHERE ticket='$ticket'");
        $procount = mysqli_query($connect,"select count(salescode)as code from purchasedetails  where purchasedetails.status!='deleted' && financialyear='$accountyear'");
        while($dis = mysqli_fetch_array($procount))
        {
            $salescode = $dis['code']+1;
            $sales_code = 'PUR'.$year_code.'/'.$salescode;
        }
        $code_validate = mysqli_query($connect,"select count(ticket) as code from purchasedetails 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 purchasedetails (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,financialyear,tablename) values('$sales_code','$ticket','$vehicleno','$customer','$delivery','$product','$productname','$price','$priceori','$date','$time','$load','$emptyweight','$loadweight','$unitvalue','$netweight','$netqty','$netqtyori','$cft','$customertype','$totalamount','$mode_of','$remark','$block_date','$chakka','$user','$time','$date','','$accountyear','purchasedetails')";

$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:secondtransaction.php');
//  }
?>