<?php

session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("Functions_Sales.php");
include("function.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

//Read From Module Table
$querymodule=" Select ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ifNull(Module_Delivery,0) As Module_Delivery, ".
			 " ifNull(Module_DoubleUnit,0) As Module_DoubleUnit ".
             " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}
	
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}
	
//Read From SalesSetup Table
$querySalesSetup="Select SalesTTC From SalesSetup";
$resultSalesSetup=mysql_query($querySalesSetup,$connection) or die(mysql_error());
while($rowSalesSetup=mysql_fetch_array($resultSalesSetup)){
	extract($rowSalesSetup);
}	

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>
<body>
<form name='additemserials' method='post' action=''>
<input type='hidden' name='ItemSerialNo' id='ItemSerialNo' value='$ItemSerialNo' size='20' maxlength='40'/>
<input type='hidden' name='SerialId' id='SerialId' value='$SerialId' size='10' maxlength='40'/>
<input type='hidden' name='ItemCode' id='ItemCode' value='$ItemCode' size='20' maxlength='40'/>
<input type='hidden' name='RSaDtId' id='RSaDtId' value='$RSaDtId' size='10' maxlength='40'/>
<input type='hidden' name='RSaId' id='RSaId' value='$RSaId' size='10' maxlength='40'/>
<input type='hidden' name='ItemQty' id='ItemQty' value='$ItemQty' size='10' maxlength='40'/>
<input type='hidden' name='ItemWhCode' id='ItemWhCode' value='$ItemWhCode' size='10' maxlength='40'/>
<input type='hidden' name='PerPage' id='PerPage' value='$PerPage' size='10' maxlength='40'/>
<input type='hidden' name='HideSerialNo' id='HideSerialNo' value='$HideSerialNo' size='10' maxlength='40'/>
<input type='hidden' name='Mode' id='Mode' value='$Mode' size='10' maxlength='40'/>		    
HERE;

if ($Mode == 'insert' && $ItemSerialNo != '') {
	
	
	$query= " Select Serial_ID, ifNull(SerialNo_Used,0), Purchasesserialno.warehouse_code, ifNull(SerialNo_Quantity,1), ifNull(SerialNo_QuantitySold,0)  ".
			" From Purchasesserialno ".
			" Inner Join DeliveryDt on DeliveryDt.DeliveryDt_ID = Purchasesserialno.PurchaseDt_ID ".
			" Inner Join Temp_DeliveryDt on Temp_DeliveryDt.RelatedDt_ID = DeliveryDt.DeliveryDt_ID ".
			" where Purchasesserialno.Item_Code = '".$ItemCode."' ".
			" And Purchasesserialno.SerialNo_Code =  '".$ItemSerialNo."' ".
			" And Purchasesserialno.Journal_Type =  'DI' ".
			" And Temp_DeliveryDt.DeliveryDt_ID = '".$RSaDtId."' ".
			" And Temp_DeliveryDt.Journal_Type = 'DR' ".
			" And DeliveryDt.Journal_Type = 'DI' ";
				  
	$result = selectData($connection, $query);
	$SerialNoExists = $result[0];
	//echo $query; exit;
	$IsAvailable = 1;
	$ErrorExists = 0;
	$SelectedSerialID = 0;
	$SelectedSerialQty     = 0;
	$SelectedSerialQtySold = 0;
		
	if ($SerialNoExists != '') {
		if ($ItemWhCode != '' && isset($ItemWhCode)) {
			
			$sub_query= " Select *  ".
						" From temp_purchasesserialno ".
						" where Item_Code = '".$ItemCode."' ".
						" And SerialNo_Code =  '".$ItemSerialNo."' ".
						" And Journal_Type in ('DR')";
			 //echo $sub_query; exit;	 
			$sub_result=mysql_query($sub_query,$connection)  or die(mysql_error());
			$RecCount=mysql_num_rows($sub_result);
			
			if ($RecCount == 0 || $RecCount == '') {
				$sub_query= " Select Serial_ID, ifNull(SerialNo_Used,0), Purchasesserialno.warehouse_code, ifNull(SerialNo_Quantity,1), ifNull(SerialNo_QuantitySold,0) As SerialNo_QuantitySold  ".
							" From Purchasesserialno ".
							" Inner Join DeliveryDt on DeliveryDt.DeliveryDt_ID = Purchasesserialno.PurchaseDt_ID ".
							" Inner Join Temp_DeliveryDt on Temp_DeliveryDt.RelatedDt_ID = DeliveryDt.DeliveryDt_ID ".
							" where Purchasesserialno.Item_Code = '".$ItemCode."' ".
							" And Purchasesserialno.SerialNo_Code =  '".$ItemSerialNo."' ".
							" And Purchasesserialno.Warehouse_Code =  '".$ItemWhCode."' ".
							" And Purchasesserialno.Journal_Type =  'DI' ".
							" And Temp_DeliveryDt.DeliveryDt_ID = '".$RSaDtId."' ".
							" And Temp_DeliveryDt.Journal_Type = 'DR' ".
							" And DeliveryDt.Journal_Type = 'DI' ";
								
								
				//echo $sub_query; exit;			
				$sub_result=mysql_query($sub_query,$connection)  or die(mysql_error());
				$sub_row=mysql_fetch_array($sub_result);
				$RecCount=mysql_num_rows($sub_result);
					
				if ($RecCount == 0) {
					$IsAvailable = 0;
					$ErrorExists = 1;
					$message  = "Insert not allowed, Serial No (".$ItemSerialNo.") is not available in the warehouse (".$ItemWhCode.") . "; 
				}	
				else {
						
					if ($sub_row[1] == 1)
					{   //In Edit Case And Delete A Serial For The Return Invoice Then
					    // Allow To Add That Serial Again By Getting The Serials Related For That Return Invoice(First Part Of Query Below)
						$sub_query= " Select RelatedSerial_Id As Serial_ID, ifNull(SerialNo_Used,0), Purchasesserialno.warehouse_code, ifNull(SerialNo_Quantity,1), ifNull(SerialNo_QuantitySold,0) As SerialNo_QuantitySold, 1 As OrderColumn ".
									" From purchasesserialno ".
									" Inner Join DeliveryDt on DeliveryDt.DeliveryDt_ID = Purchasesserialno.PurchaseDt_ID ".
									" Where DeliveryDt.Delivery_ID = '".$RSaId."' ".
									" And Purchasesserialno.Item_Code = '".$ItemCode."' ".
									" And Purchasesserialno.SerialNo_Code =  '".$ItemSerialNo."' ".
									" And Purchasesserialno.Warehouse_Code =  '".$ItemWhCode."' ".
									" And Purchasesserialno.Journal_Type = 'DR' ".
									" And DeliveryDt.Journal_Type = 'DR' ".
									" Union ".
									" Select Serial_ID, ifNull(SerialNo_Used,0), Purchasesserialno.warehouse_code, ifNull(SerialNo_Quantity,1), ifNull(SerialNo_QuantitySold,0) As SerialNo_QuantitySold, 2 As OrderColumn  ".
									" From Purchasesserialno ".
									" Inner Join DeliveryDt on DeliveryDt.DeliveryDt_ID = Purchasesserialno.PurchaseDt_ID ".
									" Inner Join Temp_DeliveryDt on Temp_DeliveryDt.RelatedDt_ID = DeliveryDt.DeliveryDt_ID ".
									" where Purchasesserialno.Item_Code = '".$ItemCode."' ".
									" And Purchasesserialno.SerialNo_Code =  '".$ItemSerialNo."' ".
									" And Purchasesserialno.Warehouse_Code =  '".$ItemWhCode."' ".
									" And ifNull(Purchasesserialno.SerialNo_Used,0) = 0 ".
									" And Purchasesserialno.Journal_Type =  'DI' ".
									" And Temp_DeliveryDt.DeliveryDt_ID = '".$RSaDtId."' ".
									" And Temp_DeliveryDt.Journal_Type = 'DR' ".
									" And DeliveryDt.Journal_Type = 'DI' ".
									" Order by OrderColumn ";
								
						//echo $sub_query;exit;			
						$sub_result=mysql_query($sub_query,$connection)  or die(mysql_error());
						$sub_row=mysql_fetch_array($sub_result);
						$RecCount=mysql_num_rows($sub_result);
							
						if ($RecCount == 0) {
							$IsAvailable = 0;
							$ErrorExists = 1;
							$message  = "Insert not allowed, Serial No (".$ItemSerialNo.") is used in warehouse (".$ItemWhCode."). "; 
						}  else {
							$SelectedSerialID      = $sub_row[0];
							$SelectedSerialQty     = $sub_row[3];
							$SelectedSerialQtySold = $sub_row[4];
							$IsAvailable = 1;
						}	
						
					} else {
						$SelectedSerialID      = $sub_row[0];
						$SelectedSerialQty     = $sub_row[3];
						$SelectedSerialQtySold = $sub_row[4];
						$IsAvailable = 1;
					}	

				}	
			}
			else {
				$IsAvailable = 0;
				$ErrorExists = 1;
				$message  = "Insert not allowed, duplicate serial no. "; 
			}	
		}
		else {
			$IsAvailable = 0;
			$ErrorExists = 1;
			$message  = "Insert not allowed, warehouse is empty. "; 
		}	
	}
	else {
		$IsAvailable = 0;
		$ErrorExists = 1;
		$message  = "Insert not allowed, Serial No (".$ItemSerialNo.") is not available. "; 
	}
	
	//Added Tarek on 20120626
	if ($IsAvailable == 0 && $DirectReturn == 1) {
		//If Direct Return True, Check First If The Serial Is Related To An Invoice
		$sub_query= " Select Serial_ID, ifNull(SerialNo_Used,0), Purchasesserialno.warehouse_code, ifNull(SerialNo_Quantity,1), ifNull(SerialNo_QuantitySold,0) As SerialNo_QuantitySold  ".
					" From Purchasesserialno ".
					" Inner Join DeliveryDt on DeliveryDt.DeliveryDt_ID = Purchasesserialno.PurchaseDt_ID ".
					" where Purchasesserialno.Item_Code = '".$ItemCode."' ".
					" And Purchasesserialno.SerialNo_Code =  '".$ItemSerialNo."' ".
					" And Purchasesserialno.Warehouse_Code =  '".$ItemWhCode."' ".
					" And ifNull(Purchasesserialno.SerialNo_Used,0) = 0 ".
					" And Purchasesserialno.Journal_Type =  'DI' ".
					" And DeliveryDt.Journal_Type = 'DI' ".
					" And DeliveryDt.Item_Code = '".$ItemCode."' ".
					" And DeliveryDt.Warehouse_Code =  '".$ItemWhCode."' ".
					" Limit 1 ";
								
		//echo $sub_query; exit;			
		$sub_result=mysql_query($sub_query,$connection)  or die(mysql_error());
		$sub_row=mysql_fetch_array($sub_result);
		$RecCount=mysql_num_rows($sub_result);
					
		if ($RecCount > 0) {
			$SelectedSerialID      = $sub_row[0];
			$SelectedSerialQty     = $sub_row[3];
			$SelectedSerialQtySold = $sub_row[4];
			$IsAvailable = 1;		
			$ErrorExists = 0;
		} else {	
			$IsAvailable = 1;
			$ErrorExists = 0;
			$SelectedSerialID	   = 0;
			$SelectedSerialQty     = 1;
			$SelectedSerialQtySold = 0;
		}
		
		$message  = ""; 
	}
	
	
	if ($IsAvailable == 1)
	{
		fbegin();
		$SerId = get_nextsequence('Serial_Id','',$connection);	
		fcommit();
					
		fbegin();
		$query = " INSERT INTO temp_purchasesserialno (Serial_ID, Journal_Type, DeliveryDt_ID, PurchaseDt_ID, Item_Code, SerialNo_Code, SerialNo_Used, RelatedSerial_Id, SerialNo_Quantity, SerialNo_QuantitySold, Warehouse_code, Logged_Code ) ".
				 " VALUES ( ".$SerId.", 'DR', $RSaDtId, $RSaDtId, '".$ItemCode."', '".$ItemSerialNo."', 0, '".$SelectedSerialID."', '".$SelectedSerialQty."', $SelectedSerialQtySold - 1, '".$ItemWhCode."', '".$HTTP_SESSION_VARS['useraccount']."' ) ";
									 
		$result=mysql_query($query,$connection);
						
		if ($result){
			
			$query = " Update Temp_DeliveryDt Set ".
					 " DeliveryDt_Quantity =  (ifNull(DeliveryDt_Quantity,0) + 1) ";
					 
			if ($Module_DoubleUnit == 1) {
				$query = $query.", Item_Unit2Qty = ifNull(Item_UnitCoef,0)*(ifNull(DeliveryDt_Quantity,0)) ";
			}		 
			$query = $query." WHERE DeliveryDt_ID = '".$RSaDtId."' ";
			//echo $query."<br>";exit;				
			if (!updateData($connection, $query, true))		 
				$message  = mysql_error(); 	
			
			$message = "Serial no (".$ItemSerialNo.") inserted successfully.";
			fcommit();
		} else {
			$message  = mysql_error(); 
			frollback();
		}
	
	}
}

else if ($Mode == 'delete' && $SerialId != '') {
	
	$ErrorExists = 0;
	
	$query = " SELECT ifNull(SerialNo_Used, 0) As SerialNo_Used from temp_purchasesserialno ".
	         " WHERE Serial_ID = ".$SerialId;
			 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
	$result = selectData($connection, $query);
	$SerialNoUsed = $result[0];
	
	if ($SerialNoUsed == 0) {
		fbegin();
		
		$query = " DELETE from temp_purchasesserialno ".
				 " WHERE Serial_ID = ".$SerialId.
				 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ".
				 " AND ifNull(SerialNo_Used, 0) = 0  ";
		
		$result=mysql_query($query,$connection);
			
		if ($result){
			$query = " Update Temp_DeliveryDt Set ".
					 " DeliveryDt_Quantity =  (ifNull(DeliveryDt_Quantity,0) - 1) ";
					 
			if ($Module_DoubleUnit == 1) {
				$query = $query.", Item_Unit2Qty = ifNull(Item_UnitCoef,0)*(ifNull(DeliveryDt_Quantity,0)) ";
			}		 
			$query = $query." WHERE DeliveryDt_ID = '".$RSaDtId."' ";
			//echo $query."<br>";exit;				
			if (!updateData($connection, $query, true))		 
				$message  = mysql_error(); 	
				
			
			$message = "Delete serial no (".$ItemSerialNo.") done successfully.";
			fcommit();
		}else{
			$ErrorExists = 1;
			$message  = mysql_error(); 
			frollback();
		}			
	} 
	else {	
		$ErrorExists = 1;
		$message  = "Delete not allowed, Serial No ".$ItemSerialNo." is used. "; 
	}	
}

//Calculating Master Amounts
if ( $ErrorExists == 0 && $Module_Delivery == 0){ 
	
	$vSaDtTotal = 0;
	$vItemVat   = 0;
	$vSaVatChk  = 0;
	
	$vSaTaxable        = 0;
	$vSaTaxableTTC     = 0;
	$vSaNoneTaxable    = 0;
	$vSaGrossTotal     = 0;
	$vSaDiscountAmt    = 0;
	$vSaDiscountPer    = 0;
	$vSaDiscountAmtTTC = 0;
	$vSaTransportAmt   = 0;
	$vSaTransportAmtB1 = 0;
	$vSaTransportAmtB2 = 0;
	$vSaVat      = 0;
	$vSaVatBase1 = 0;
	$vSaVatBase2 = 0;
		
	$vSaTax1 = 0;
	$vSaTax2 = 0;
	$vSaTax3 = 0;
	$vSaTax4 = 0;
	$vSaTax5 = 0;
	$vSaTax1B1 = 0;
	$vSaTax2B1 = 0;
	$vSaTax3B1 = 0;
	$vSaTax4B1 = 0;
	$vSaTax5B1 = 0;
	$vSaTax1B2 = 0;
	$vSaTax2B2 = 0;
	$vSaTax3B2 = 0;
	$vSaTax4B2 = 0;
	$vSaTax5B2 = 0;

	
	
	$sub_query = " Select Temp_Deliveries.Currency_code, ifNull(Delivery_DiscountPer,0), ".
	             " ifNull(Delivery_VatChkbx,0) As Delivery_VatChkbx  ".
				 " From Temp_Deliveries ".
				 " Left Join Clients on Temp_Deliveries.Client_code = Clients.Ledger_Number ".
				 " Where Delivery_ID = '".$RSaId."'";
	$sub_result = selectData($connection, $sub_query);
	
	$vSaCurr        = $sub_result[0];
	$vSaDiscountPer = $sub_result[1];
	$vSaVatChk      = $sub_result[2];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$vSaCurr."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$vSaCurr."' )";
	$result = selectData($connection, $query);
	$vRate = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base1."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base1."' )";
	$result = selectData($connection, $query);
	$vRate1 = $result[0];
			
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base2."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base2."' )";
	$result = selectData($connection, $query);
	$vRate2 = $result[0];
	
	
	//Calculating Master Amounts
	$sub_query = " Select Item_Code, ((ifNull(DeliveryDt_Quantity,0)*ifNull(DeliveryDt_Price,0))-((ifNull(DeliveryDt_Quantity,0)*ifNull(DeliveryDt_Price,0)*ifNull(DeliveryDt_DiscountPer,0))/100)) As Total, ifNull(Item_Vat,0),  ".
				 " DeliveryDt_Quantity, ifNull(DeliveryDt_DiscountAmount,0), ifNull(Item_Tax1,0), ifNull(Item_Tax2,0), ifNull(Item_Tax3,0), ifNull(Item_Tax4,0), ifNull(Item_Tax5,0) ".
				 " From Temp_DeliveryDt ".
				 " Where Delivery_ID = '".$RSaId."'";
										 
	//echo $sub_query;
	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
	while($sub_rows=mysql_fetch_array($sub_result)){

		$vSaDtTotal = $sub_rows[1];
		$vItemVat   = $sub_rows[2];
						
		$tax_flag = 0;
		$notax_flag = 1;
		if ($vItemVat > 0){
			$tax_flag = 1;
			$notax_flag = 0;
		}	
						
							
		$vSaTaxable     = $vSaTaxable     + ($vSaDtTotal*$tax_flag);
		$vSaNoneTaxable = $vSaNoneTaxable + ($vSaDtTotal*$notax_flag);
						
		
		// Calculation of Vat Amount
		if ($vSaVatChk == 1 && $vSaDtTotal > 0)
			$vSaVat = $vSaVat + (($vSaDtTotal - $sub_rows[4])*$vItemVat/100);
		
		if ($Module_AdditionalTaxes == 1 && $vSaDtTotal > 0) {
			if ($vItemVat > 0 && $sub_rows[5] != 0)
				$vSaTax1 = $vSaTax1 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[5]/100);
			if ($vItemVat > 0 && $sub_rows[6] != 0)
				$vSaTax2 = $vSaTax2 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[6]/100);
			if ($vItemVat > 0 && $sub_rows[7] != 0)
				$vSaTax3 = $vSaTax3 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[7]/100);
			if ($vItemVat > 0 && $sub_rows[8] != 0)
				$vSaTax4 = $vSaTax4 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[8]/100);
			if ($vItemVat > 0 && $sub_rows[9] != 0)
				$vSaTax5 = $vSaTax5 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[9]/100);	
		}
					
	}			
					
					
	$vSaGrossTotal = $vSaTaxable + $vSaNoneTaxable;
					
	// Calculation of Discount Amount	
	if ($vSaDiscountPer > 0)
		$vSaDiscountAmt = (($vSaGrossTotal * $vSaDiscountPer) / 100);
			
	if ($SalesTTC == 1)	$vSaDiscountAmtTTC = $vSaDiscountAmt;	

		
			
	$vSaTaxableTTC = $vSaTaxable + $vSaVat;
					
	$vSaNetTotal         = $vSaGrossTotal + $vSaVat - $vSaDiscountAmt;
	$vSaTaxableBase1     = ($vSaTaxable*$vRate)/$vRate1;
	$vSaTaxableBase2     = ($vSaTaxable*$vRate)/$vRate2;
	$vSaNoneTaxableBase1 = ($vSaNoneTaxable*$vRate)/$vRate1;
	$vSaNoneTaxableBase2 = ($vSaNoneTaxable*$vRate)/$vRate2;
	$vSaDiscountAmtBase1 = ($vSaDiscountAmt*$vRate)/$vRate1;
	$vSaDiscountAmtBase2 = ($vSaDiscountAmt*$vRate)/$vRate2;
	$vSaGrossTotalBase1  = ($vSaGrossTotal*$vRate)/$vRate1;
	$vSaGrossTotalBase2  = ($vSaGrossTotal*$vRate)/$vRate2;
	$vSaVatBase1 		 = ($vSaVat*$vRate)/$vRate1;
	$vSaVatBase2 		 = ($vSaVat*$vRate)/$vRate2;
					

	$vSaTax1B1  = ($vSaTax1*$vRate)/$vRate1;
	$vSaTax1B2  = ($vSaTax1*$vRate)/$vRate2;		
	$vSaTax2B1  = ($vSaTax2*$vRate)/$vRate1;
	$vSaTax2B2  = ($vSaTax2*$vRate)/$vRate2;		
	$vSaTax3B1  = ($vSaTax3*$vRate)/$vRate1;
	$vSaTax3B2  = ($vSaTax3*$vRate)/$vRate2;		
	$vSaTax4B1  = ($vSaTax4*$vRate)/$vRate1;
	$vSaTax4B2  = ($vSaTax4*$vRate)/$vRate2;		
	$vSaTax5B1  = ($vSaTax5*$vRate)/$vRate1;
	$vSaTax5B2  = ($vSaTax5*$vRate)/$vRate2;	
	
	fbegin();
	
	$sub_query1 = " UPDATE temp_deliveries SET ".
				  " Delivery_Taxable = $vSaTaxable, Delivery_TaxableBase1 = $vSaTaxableBase1, Delivery_TaxableBase2 = $vSaTaxableBase2, ".
				  " Delivery_NoneTaxable = $vSaNoneTaxable, Delivery_NoneTaxableBase1 = $vSaNoneTaxableBase1, Delivery_NoneTaxableBase2 = $vSaNoneTaxableBase2, ".
				  " Delivery_DiscountAmount = $vSaDiscountAmt, Delivery_DiscountBase1 = $vSaDiscountAmtBase1, Delivery_DiscountBase2 = $vSaDiscountAmtBase2, ".
				  " Delivery_VAT = $vSaVat, Delivery_VATBase1 = $vSaVatBase1, Delivery_VATBase2 = $vSaVatBase2, ".
				  " Delivery_Total = $vSaGrossTotal, Delivery_TotalBase1 = $vSaGrossTotalBase1, Delivery_TotalBase2 = $vSaGrossTotalBase2, ".
				  " Delivery_DiscountTTC = $vSaDiscountAmtTTC, Delivery_TaxableTTC = $vSaTaxableTTC,  ".
				  " Delivery_Tax1 = $vSaTax1, Delivery_Tax1Base1 = $vSaTax1B1, Delivery_Tax1Base2 = $vSaTax1B2,  ".
				  " Delivery_Tax2 = $vSaTax2, Delivery_Tax2Base1 = $vSaTax2B1, Delivery_Tax2Base2 = $vSaTax2B2,  ".
				  " Delivery_Tax3 = $vSaTax3, Delivery_Tax3Base1 = $vSaTax3B1, Delivery_Tax3Base2 = $vSaTax3B2,  ".
				  " Delivery_Tax4 = $vSaTax4, Delivery_Tax4Base1 = $vSaTax4B1, Delivery_Tax4Base2 = $vSaTax4B2,  ".
				  " Delivery_Tax5 = $vSaTax5, Delivery_Tax5Base1 = $vSaTax5B1, Delivery_Tax5Base2 = $vSaTax5B2  ".
				  " WHERE Delivery_ID = '".$RSaId."'";
		
	$sub_result1=mysql_query($sub_query1,$connection);
	//echo $sub_query1."<br>";exit;
	if ($sub_result1){
		fcommit();
						
	}else{
		$message  = mysql_error(); 
		$error = 'Error In Function Calculate_MasterAmounts: '.$message;
		frollback();
	}
	
}	

$message = ereg_replace(' ','%20',$message);

header ("Location: Form_ReturnDeliverySerialNo.php?SRRSaDtId=".$RSaDtId."&SRRSaId=".$RSaId."&perpage=".$PerPage."&SRHideSerialNo=".$HideSerialNo."&message=".$message);



function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}

print <<<HERE
</form>
</body>

HERE;

?>