<?php
session_start();
require_once '../../Server/connection.php';
if($_POST){	
  $file_id = $_POST['file_id'];
  $quot_id = $_POST['quot_id'];
  $assign_id = $_POST['assign_id'];
  $client_id = $_POST['client_id'];
  $entryId = $_POST['entryId'];
  $entryNameId = $_POST['entryNameId'];
  $b_lNo = $_POST['b_lNo'];
  $tra_RefNo = $_POST['tra_RefNo'];
  $consignee = $_POST['consignee'];
  $goods = $_POST['goods'];
  $vesselFlight = $_POST['vesselFlight'];
  $sline = $_POST['sline'];
  $eTa = $_POST['eta'];
  $destination = $_POST['dest_id'];
  //$assigned_to  = $_POST['returnedto_id'];
  //$remarks = addslashes($_POST['remarks']);
  $space = " ";
  $eta = date('Y-m-d', strtotime($eTa));
  $currencyId = $_POST['currency_id'];
  $userId = $_SESSION['user_id'];
  //$perm_id = 95;
  
  $quotationDate = date("Y-m-d");
  
   $subTotalValue  = $_POST['subTotalValue'];
   
   $sql814 = "SELECT * FROM quotation WHERE file_id = {$file_id}";
				$result814 = $conn->query($sql814);
				$num814 = mysqli_num_rows($result814);
				
				
				//while($data814 = mysqli_fetch_array($result814))
							//{
							//$quot_id = $data814['quot_id'];
		if($num814==0){		            //}
  
  $sql8123 = "SELECT * FROM quotation";
  $result8123 = $conn->query($sql8123);
  $num22 = mysqli_num_rows($result8123);
  //$invo_no = $count + 1;
  
  if($num22>=0 && $num22<9){
  $invo_no = $num22 + 1;
  $y = date("Y");
  $InvNo = "QUOTKLCL"."/".$y."/".'000'.$invo_no;  
    }else if($num22>=9 && $num22<99){
   $invo_no = $num22 + 1;
   $y = date("Y");
  $InvNo = "QUOTKLCL"."/".$y."/".'00'.$invo_no;  	 
	 }else if($num22>=99 && $num22<999){
   $invo_no = $num22 + 1;
   $y = date("Y");
  $InvNo = "QUOTKLCL"."/".$y."/".'0'.$invo_no;  	 
	 }else{
	$invo_no = $num22 + 1;
	$y = date("Y");
  $InvNo = "QUOTKLCL"."/".$y."/".$invo_no;  	 
	 }
				
				
  
  
  $sql812 = "SELECT vat_settings.rate FROM vat_settings";

				$result812 = $conn->query($sql812);
				//$data81 = $result81->fetch_row();
				
				while($data812 = mysqli_fetch_array($result812))
							{
							$vat_rate = $data812['rate'];
							}
  
  
  $vat = $vat_rate/100 * $subTotalValue;
  $grand_total_Value = $subTotalValue + $vat;
	
  
    $sql = "UPDATE client_files SET client_id = '$client_id',entryId = '$entryId',entryName_id='$entryNameId',b_lNo='$b_lNo',tra_RefNo='$tra_RefNo',consignee='$consignee',vessel_flight='$vesselFlight',sline='$sline',eta='$eta',dest_id='$destination',goods='$goods' WHERE file_id = {$file_id}";		

	$result = $conn->query($sql);
	
	if($result){
		
	$sql2 = "INSERT INTO quotation (quot_no,client_id,file_id,currency_id,quot_date,total_amount,vat,grand_total_amount,vat_rate) VALUES ('$InvNo','$client_id','$file_id','$currencyId','$quotationDate','$subTotalValue','$vat','$grand_total_Value','$vat_rate')";			
	$result2 = $conn->query($sql2);
	
	if($result2){
		
	$quot_id = $conn->insert_id;
		
		
		for($x = 0; $x < count($_POST['item_desc']); $x++) {		
	$invItemSql = "INSERT INTO quotation_items (quot_id,services_desc,unit_price,amount) 
				VALUES ('$quot_id','".$_POST['item_desc'][$x]."','".$_POST['rate'][$x]."','".$_POST['totalValue'][$x]."')";

	$result11 = $conn->query($invItemSql);
	
	
	
	
	}
	
	
	
				   
				$_SESSION['success_message'] = "Customer File Has Been Updated Successfully";
					   header("Location: ../editCustoFile.php?i=$file_id");
                             exit(); 
								 
                 
	}
	
	}
	
}else{
	

  
  $sql812 = "SELECT vat_settings.rate FROM vat_settings";

				$result812 = $conn->query($sql812);
				//$data81 = $result81->fetch_row();
				
				while($data812 = mysqli_fetch_array($result812))
							{
							$vat_rate = $data812['rate'];
							}
  
  
  $vat = $vat_rate/100 * $subTotalValue;
  $grand_total_Value = $subTotalValue + $vat;
	
  
    $sql = "UPDATE client_files SET client_id = '$client_id',entryId = '$entryId',entryName_id='$entryNameId',b_lNo='$b_lNo',tra_RefNo='$tra_RefNo',consignee='$consignee',vessel_flight='$vesselFlight',sline='$sline',eta='$eta',dest_id='$destination',goods='$goods' WHERE file_id = {$file_id}";		

	$result = $conn->query($sql);
	
	if($result){
		
	//$sql2 = "INSERT INTO quotation (quot_no,client_id,file_id,currency_id,quot_date,total_amount,vat,grand_total_amount,vat_rate) VALUES ('$InvNo','$client_id','$file_id','$currencyId','$quotationDate','$subTotalValue','$vat','$grand_total_Value','$vat_rate')";			
	//$result2 = $conn->query($sql2);
	
	$sql2 = "UPDATE quotation SET currency_id='$currencyId', total_amount='$subTotalValue',vat='$vat',grand_total_amount='$grand_total_Value',vat_rate='$vat_rate' WHERE quot_id = {$quot_id}";		

	$result2 = $conn->query($sql2);
	
	if($result2){
		
		
		for($x = 0; $x < count($_POST['item_desc']); $x++) {			
		$removeInvSql = "DELETE FROM quotation_items WHERE quot_id = {$quot_id}";
		$result1 = $conn->query($removeInvSql);	
	}
		
	//$quot_id = $conn->insert_id;
		
		if($result1){
		for($x = 0; $x < count($_POST['item_desc']); $x++) {		
	$invItemSql = "INSERT INTO quotation_items (quot_id,services_desc,unit_price,amount) 
				VALUES ('$quot_id','".$_POST['item_desc'][$x]."','".$_POST['rate'][$x]."','".$_POST['totalValue'][$x]."')";

	$result11 = $conn->query($invItemSql);
	
	
	
	
	}
	}
	
	
				   
				$_SESSION['success_message'] = "Customer File Has Been Updated Successfully";
					   header("Location: ../editCustoFile.php?i=$file_id");
                             exit(); 
								 
                 
	}
	
	}	
	
	
}


}