<?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'];

$driver_amount=$_POST['driver_amount'];
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($bill_type_ori == $bill_type){
    if( $load == 'Empty' && $tablename=='second_transaction')
    {
        $update = mysqli_query($connect,"update second_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 second_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 second_transaction (salescode,ticket,vehicleno,customer,delivery,product,productname,price,priceori,date,time,processtype,emptyweight,loadweight,unitvalue,netweight,netqty,netqtyori,cft,customertype,totalamount,remark,driver_amount,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','$driver_amount','$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','second_transaction')";

$update = "update ticket set process ='completed' where ticket.ticket = '$ticket'";
         }
    }
    if( $load == 'Empty' && $tablename=='purchasedetails')
    {
        $update=mysqli_query($connect,"UPDATE purchasedetails SET product='$product',productname='CancelProduct',price='0',priceori='0',netqty='0',netqtyori='0',totalamount='0',editby='$user',edittime=CURRENT_TIME(),editdate=CURRENT_DATE(),status='cancel' WHERE ticket='$ticket'");
        $updatetkt=mysqli_query($connect,"UPDATE ticket SET loadtype='$load' WHERE ticket='$ticket'");
        echo $accountyear;
        if($financial_year==$accountyear){
        $procount = mysqli_query($connect,"select count(salescode)as code from second_transaction where second_transaction.status!='deleted' && financialyear='$accountyear'");
        while($dis = mysqli_fetch_array($procount))
        {
            $year_code_end = substr($financialYearEnd,2);
            $year_code_start = substr($financialYearStart,2);
            $salescode = ($dis['code']+11162);
             $sales_code = 'SC'.$salescode.'/'.$year_code_start.$year_code_end;
        }
    }if($financial_year !=$accountyear){
        $procount = mysqli_query($connect,"select count(salescode)as code from second_transaction where second_transaction.status!='deleted' && financialyear='$financial_year'");
        echo "select count(salescode)as code from second_transaction where second_transaction.status!='deleted' && financialyear='$financial_year'";
        while($dis = mysqli_fetch_array($procount))
        {
            //echo $financial_year;
            $year_code_end = substr($financialYearEnd,2);
            $year_code_start = substr($financialYearStart,2);
            $salescode = ($dis['code']+11162);
             $sales_code = 'SC'.$salescode.'/'.$year_code_start.$year_code_end;
            $accountyear = $financial_year;
        }
    }
         echo "purchase-update to sales";
         //echo $salescode;
         $code_validate = mysqli_query($connect,"select count(ticket) as code from second_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 second_transaction (salescode,ticket,vehicleno,customer,delivery,product,productname,price,priceori,date,time,processtype,emptyweight,loadweight,unitvalue,netweight,netqty,netqtyori,cft,customertype,totalamount,remark,driver_amount,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('$sales_code','$ticket','$vehicleno','$customer','$delivery','$product','$productname','$price','$priceori','$date','$time','$load','$emptyweight','$loadweight','$unitvalue','$netweight','$netqty','$netqtyori','$cft','','$totalamount','$remark','$driver_amount','$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','second_transaction')";
  

$update = "update ticket set process ='completed' where ticket.ticket = '$ticket'";
         }
    }
    if( $load == 'Load' && $tablename=='purchasedetails')
    {
        $update = mysqli_query($connect,"update purchasedetails set status='deleted',editdate=CURRENT_DATE(),edittime=CURRENT_TIME() where ticket ='$ticket'");
         echo "purchase-update";
         //echo $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,remark,modeof,blockdate,chakka,editby,edittime,editdate,status,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','','$financial_year','purchasedetails')";

$update = "update ticket set process ='completed' where ticket.ticket = '$ticket'";
         }
    }
    if( $load == 'Load' && $tablename=='second_transaction')
    {
        
        $update=mysqli_query($connect,"UPDATE second_transaction SET product='$product',productname='CancelProduct',price='0',priceori='0',netqty='0',netqtyori='0',totalamount='0',editby='$user',edittime=CURRENT_TIME(),editdate=CURRENT_DATE(),status='cancel' WHERE ticket='$ticket' && status!='deleted'");
        $updatetkt=mysqli_query($connect,"UPDATE ticket SET loadtype='$load' WHERE ticket='$ticket'");
        if($financial_year==$accountyear){
            $procount = mysqli_query($connect,"select count(salescode)as code from purchasedetails where purchasedetails.status!='deleted' && financialyear='$accountyear'");
            while($dis = mysqli_fetch_array($procount))
            {
                $year_code = substr($financialYearEnd,2);
                $salescode = ($dis['code']+1);
                $sales_code = 'PUR'.$year_code.'/'.$salescode;
            }
        }if($financial_year !=$accountyear){
            $procount = mysqli_query($connect,"select count(salescode)as code from purchasedetails where purchasedetails.status!='deleted' && financialyear='$financial_year'");
            while($dis = mysqli_fetch_array($procount))
            {
                //echo $financial_year;
                $year_code = substr($financial_year,7);
                $salescode = ($dis['code']+1);
                $sales_code = 'PUR'.$year_code.'/'.$salescode;
                $accountyear = $financial_year;
            }
        }
         echo "sales-update to purchase";
         //echo $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,remark,modeof,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','$remark','$mode_of','$block_date','$chakka','$user',CURTIME(),'$date','','$financial_year','purchasedetails')";

$update = "update ticket set process ='completed' where ticket.ticket = '$ticket'";
         }
    }
} 
if($bill_type_ori != $bill_type && $bill_type=='gst' && $bill_type_ori=='est'){
    $year_code_end = substr($financialYearEnd,2);
    $year_code_start = substr($financialYearStart,2);
        if( $load == 'Empty')
        {
            $updatetkt=mysqli_query($connect,"UPDATE ticket SET loadtype='$load' WHERE ticket='$ticket'");
            $update_est = mysqli_query($connect,"update second_transaction_est set status='cancel',editdate=CURRENT_DATE(),edittime=CURRENT_TIME(),netqty=0,netqtyori=0,totalamount=0 where ticket ='$ticket'");
             $procount = mysqli_query($connect,"select count(salescode)as code from second_transaction where second_transaction.status!='deleted' && financialyear='$accountyear'");
             while($dis = mysqli_fetch_array($procount))
             {
                  $salescode = ($dis['code']+11162);
             $sales_code = 'SC'.$salescode.'/'.$year_code_start.$year_code_end;
             }
             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 (salescode,ticket,vehicleno,customer,delivery,product,productname,price,priceori,date,time,processtype,emptyweight,loadweight,unitvalue,netweight,netqty,netqtyori,cft,customertype,totalamount,remark,driver_amount,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('$sales_code','$ticket','$vehicleno','$customer','$delivery','$product','$productname','$price','$priceori','$date','$time','$load','$emptyweight','$loadweight','$unitvalue','$netweight','$netqty','$netqtyori','$cft','','$totalamount','$remark','$driver_amount','$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','second_transaction')";
    
    $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'");
            $update_est = mysqli_query($connect,"update second_transaction_est set status='cancel',editdate=CURRENT_DATE(),edittime=CURRENT_TIME(),netqty=0,netqtyori=0,totalamount=0 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',CURTIME(),'$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');
//  }
?>