<?php

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


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

set_time_limit(0);

$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 Login Table
$querylog=" select ifNull(FI_SorderCanceledQty,0) As FI_SorderCanceledQty, ifNull(Unmodify_sellingprice,0) As Unmodify_SellingPrice, ".
		  " ifNull(Login_AllowPrice,1) As Login_AllowPrice, ifNull(Login_ModifyDiscount,0) As Login_ModifyDiscount, ".
		  " ifNull(Login_ModifySellingPrice,0) As Login_ModifySellingPrice, ifNull(Login_negativeQty,0) As Login_negativeQty,  ".
		  " ifNull(Allow_MinimumPrice,0) As Allow_MinimumPrice  ".
		  " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}
//echo $querylog;
	
//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 Module Table
$querymodule=" Select ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ifNull(Module_Pack,0) As Module_Pack, ".
             " ifNull(Module_Delivery,0) As Module_Delivery, ifNull(module_negativeSorder,0) As Module_NegativeSorder, ".
             " ifNull(Module_SorderWithOneWh,0) As Module_SorderWithOneWh, ifNull(Module_DoubleUnit,0) As Module_DoubleUnit, ".
			 " ifNull(Module_SorderCheckQtyOnPost,0) As Module_SorderCheckQtyOnPost, ifNull(Module_Unit1Method,0) As Module_Unit1Method, ".
			 " ifNull(Module_ProjectDt,0) As Module_ProjectDt, ifNull(Module_CostCenterDt,0) As Module_CostCenterDt ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

//Read From SalesOrderSetup Table
$querySalesOrderSetup="Select SalesTTC, Warehouse As SetupDftWareHouse From SalesSetup";
$resultSalesOrderSetup=mysql_query($querySalesOrderSetup,$connection) or die(mysql_error());
while($rowSalesOrderSetup=mysql_fetch_array($resultSalesOrderSetup)){
	extract($rowSalesOrderSetup);
}



//Warehouse List
$querywarehouse=" select distinct warehouse.warehouse_code, warehouse_name ".
				" from warehouse where warehouse_code in (select warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ".
				" order by warehouse.warehouse_code";
$resultwh = mysql_query($querywarehouse,$connection) or die(mysql_error());
$whexist   = mysql_num_rows($resultwh);
if ($whexist == 0)
	$querywarehouse="select warehouse_code,warehouse_name from warehouse order by warehouse_code";
	
$resultwarehouse=mysql_query($querywarehouse,$connection) or die(mysql_error());


//Group List
$querygroup=" select distinct group_code, group_name ".
				" from grouping where group_code in (select group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ".
				" order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist   = mysql_num_rows($resultgroup);
if ($groupexist == 0)
	$querygroup="select group_code, group_name from grouping order by group_code";
	
$resultgroup=mysql_query($querygroup,$connection) or die(mysql_error());

$querydflwh="select warehouse_code as defaultwarehouse from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and ifNull(default_warehouse,0) = 1 ";
$resultdflwh=mysql_query($querydflwh,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflwh)){
  extract($rows);
}


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


function SubmitRecord(ModuleNegativeSorder,LoginNegativeQty,ItemNonStock,ItemZeroPrice,AllowZeroPrice,ModuleSorderCheckQtyOnPost,AllowMinPrice,ItemMinPrice) {
	x = document.salesorderdt;
	
	var result = CheckFields(ModuleNegativeSorder,LoginNegativeQty,ItemNonStock,ItemZeroPrice,AllowZeroPrice,ModuleSorderCheckQtyOnPost,AllowMinPrice,ItemMinPrice);
	var SorId  = x.PSorId.value;
	var SorDtId  = x.txtSorDtId.value;
	var vMainMode = x.mainaction.value;
	var vMode= x.actionmode.value;
	
	if (SorDtId == '')
		vMode = 'submitrecord';	
	else
		vMode = 'editdt';	
	   
	if (result == 1) {
		x.action="Form_SalesOrder_Actions.php?mode="+vMode+"&mainmode="+vMainMode+"&SSorId="+SorId+"&SSorDtId="+SorDtId;
		x.submit();
		
	}	
}

function ChangeLocation() {
	x = document.salesorderdt;
	var vMode = x.actionmode.value;
	
	if (vMode == 'submitrecord') {
		
		opener.document.salesorder.actionmode.value = x.actionmode.value;
		opener.document.salesorder.mainaction.value = x.mainaction.value;
		//opener.document.salesorder.message.value = x.message.value;
		opener.document.salesorder.txtSorId.value = x.PSorId.value;
		opener.document.salesorder.submit();
		
	} 
	else if (vMode == 'submitrecord' || vMode == 'editdt') {
		opener.document.salesorder.actionmode.value = x.actionmode.value;
		opener.document.salesorder.mainaction.value = x.mainaction.value;
		opener.document.salesorder.txtSorId.value = x.PSorId.value;
		opener.document.salesorder.submit();
		
		self.close();
	
	}
	
	//x.txtItemCode.focus();
}

function CheckFields(ModuleNegativeSorder,LoginNegativeQty,ItemNonStock,ItemZeroPrice,AllowZeroPrice,ModuleSorderCheckQtyOnPost,AllowMinPrice,ItemMinPrice){
  
    x = document.salesorderdt;
	
	var SorType  	   = x.PSorType.value;
	var SorDtItem 	   = x.txtItemCode.value;
	var SorDtWarehouse = x.lstWarehouse.value;
	var SorDtQty       = x.txtSorDtOQty.value;
	var SorDtQty2	   = x.txtSorDtQty2.value;
	var SorDtPrice     = x.txtSorDtPrice.value;
	var SorDtDisc      = x.txtSorDtDiscount.value;
	var AvailableQty   = x.txtAvailableQty.value;
	
	
	var vmode = x.actionmode.value;
	
	if (SorDtPrice == '')	SorDtPrice = 0;
	if (SorDtQty2 == '')    SorDtQty2 = 0;
	SorDtQty = parseInt(SorDtQty);	
	AvailableQty = parseInt(Math.abs(AvailableQty));
	
	if (SorDtWarehouse=='')
	{
		alert("Warehouse is mandatory!");
		x.lstWarehouse.focus();
		return 0;
	}
	else if (SorDtItem=='')
	{
		alert("Item is mandatory!");
		x.txtItemCode.focus();
		return 0;
	}
	else if (SorDtQty == '' || SorDtQty*1 != SorDtQty/1 || SorDtQty < 0  ) {
		alert('Quantity ('+SorDtQty+') should be a NUMERIC and POSITIVE value. ');
		x.txtSorDtOQty.focus();
		return 0;
	}
	else if (SorDtQty > AvailableQty && ModuleSorderCheckQtyOnPost == 1 && (ModuleNegativeSorder == 0 || LoginNegativeQty == 0) && ItemNonStock == 0 && SorType == 'SO'){
		alert('Quantity ('+SorDtQty+') should be NOT MORE than [Available ('+AvailableQty+')] ');
		x.txtSorDtOQty.focus();
		return 0;
	}
	else if ((SorDtPrice*1 != SorDtPrice/1 || SorDtPrice < 0) ) {
		alert('Price should be a NUMERIC and POSITIVE value');
		x.txtSorDtPrice.focus();
		return 0;
	}
	else if ((SorDtPrice == 0 && AllowZeroPrice == 0 && ItemZeroPrice == 0) ) {
		alert('Not allow Zero price.');
		x.txtSorDtPrice.focus();
		return 0;
	}
	else if ((SorDtPrice < ItemMinPrice && AllowMinPrice == 0 && ItemMinPrice != 0) ) {
		alert('Price not allowed, minimum price '+ItemMinPrice);
		x.txtSorDtPrice.focus();
		return 0;
	}
	else if ((SorDtDisc != '' && SorDtDisc*1 != SorDtDisc/1) ) {
		alert('Discount should be a NUMERIC value');
		x.txtSorDtDiscount.focus();
		return 0;
	}
	else if ((SorDtQty2 != '' && SorDtQty2*1 != SorDtQty2/1) ) {
		alert('Item Qty2 should be a NUMERIC value.');
		x.txtSorDtQty2.focus();
		return 0;
	}
		
	
	else
	return 1 ;	
}






//All= All form; D= Detail Part
function ClearForm(Option){
    x = document.salesorderdt;
	var vmode = x.actionmode.value;
	
	if (Option == 'D')
	{
		x.txtSorDtId.value='';
		x.txtItemCode.value='';
		x.txtItemDesc.value='';
		x.lstWarehouse.value='';
		x.lstGroup.value='';
		x.txtSorDtRemark.value='';
		x.txtSorDtQty.value='';
		x.txtSorDtQty2.value='';
		x.txtSorDtOQty.value='';
		x.txtSorDtCQty.value='';
		x.txtPOQty.value='';
		x.txtAvailableQty.value='';
		x.txtExistingQty.value='';
		x.txtSorDtPrice.value='';
		x.txtSorDtDiscount.value='';
		x.txtSorDtTotal.value='';
		x.txtSorDtVat.value='';
		
		//x.message.value='';
		x.actionmode.value = '';
	
	}

}


function CalcSorDtTotal(dec) {
	x = document.salesorderdt;
	var vmode = x.actionmode.value;
	var Qty = x.txtSorDtOQty.value;
	var Price = x.txtSorDtPrice.value;
	var Discount = x.txtSorDtDiscount.value;
	if (Qty == '') Qty = 0;
	if (Price == '') Price = 0;
	if (Discount == '') Discount = 0;
	
	Qty = parseInt(Qty);
	Price = parseFloat(Price);
	Discount = parseFloat(Discount);
	
	if (Qty > 0 && Price >= 0)
	   x.txtSorDtTotal.value = Math.round(((Qty*Price) - (Qty*Price*Discount/100))*Math.pow(10,dec))/Math.pow(10,dec);
		
}

function CalcSorDtQty() {
	x = document.salesorderdt;
	var OQty = x.txtSorDtOQty.value;
	var CQty = x.txtSorDtCQty.value;
	
	if (OQty == '') OQty = 0;
	if (CQty == '') CQty = 0;
	
	OQty = parseInt(OQty);
	CQty = parseInt(CQty);
	
	if(CQty > OQty) {
		alert('Cancelled Qty ['+CQty+'] must be <= Ordered Qty ['+OQty+'].');
		x.txtSorDtCQty.focus();
	} else
		x.txtSorDtQty.value = (OQty - CQty);

}


function CalcQty2(ModuleUnit1Method,UnitCoef,Option) {
	x = document.salesorderdt;
	var Qty  = x.txtSorDtOQty.value;
	var Qty2 = x.txtSorDtQty2.value;
	
	if (Qty == '')  Qty = 0;
	if (Qty2 == '') Qty2 = 0;
	
	if(ModuleUnit1Method == 1) {
		if (Qty > 0 && Option == 1 && document.getElementById('txtSorDtQty2').type == 'text') {
			x.txtSorDtQty2.value = Qty*UnitCoef;
		}	
		else if (Qty == 0 && Option == 1 && document.getElementById('txtSorDtQty2').type == 'text') {
			x.txtSorDtQty2.value = '';
		}
	}
	//Case Modify Qty and Qty2 Should Be Empty
	else if(ModuleUnit1Method == 0 && Option == 1 && Qty2 == 0) {
		if (Qty > 0 && UnitCoef != 0 && document.getElementById('txtSorDtQty2').type == 'text') {
			x.txtSorDtQty2.value = Qty/UnitCoef;
		}	
		else if (Qty == 0 && document.getElementById('txtSorDtQty2').type == 'text') {
			x.txtSorDtQty2.value = '';
		}
	}
	//Case Modify Qty2 Get Qty
	else if(ModuleUnit1Method == 0 && Option == 2) {
		if (Qty2 > 0 && document.getElementById('txtSorDtQty2').type == 'text') {
			x.txtSorDtOQty.value = Qty2*UnitCoef;
		}	
		else if (Qty2 == 0 && document.getElementById('txtSorDtQty2').type == 'text') {
			x.txtSorDtOQty.value = '';
		}
	}
}


function SetItemVat() {
	x = document.salesorderdt;
	var vmode = x.actionmode.value;
	
	if (x.txtSorDtVat.value == 1)
	   x.chkSorDtVat.checked = true;

	   
}


function showlistItems(str){
  var vdate, vid, vItemType, vItemSort, vItemCode, vItemWh;
  x = document.salesorderdt;
  
  vdate = x.PSorDate.value;
  vid   = x.PSorId.value;
  vItemType  = x.lstItemType.value;
  vItemSort  = x.lstItemSort.value;
  vItemGroup = x.lstGroup.value;
  vItemWh    = x.lstWarehouse.value;
  vItemCode  = x.txtItemCode.value;
  
  vItemWh = vItemWh.replace('&','%26');
  
  window.open('List_SorItems.php?frm=1&PItem_Code='+vItemCode+'&ItemType='+vItemType+'&ItemSort='+vItemSort+'&ItemGroup='+vItemGroup+'&ItemWh='+vItemWh+'&TransDate='+vdate,'mywindow','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistProject(str){

  window.open('List_SorProject.php?frm=20&item='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistCostcenter(str){

  window.open('List_SorCostcenter.php?frm=20&item='+str,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function RefreshPage()
{
  //document.salesorderdt.action='Form_SalesOrderDetail.php';
  document.salesorderdt.submit();

}

function kd(e)
{
	x=document.salesorderdt;
    var intKey = (window.Event) ? e.which : e.keyCode;
    if (intKey == 13) { //enter key
        //x.btnSubmitRecord.click();
		return false;
    }
    return true;
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Preparation Order Detail - Add Items</title>


<script type="text/javascript" src="dropdown.js"></script>
<link rel="stylesheet" href="default.css" type="text/css" />
<link rel="stylesheet" href="short.css" type="text/css" />
<link rel="stylesheet" href="dropdown.css" type="text/css" />
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>
<body onLoad='ChangeLocation();' >
<form name='salesorderdt' method='post'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='PSorDate' value='$PSorDate'>
<input type='hidden' name='PSorId' value='$PSorId'>
<input type='hidden' name='PSorDtId' value='$PSorDtId'>

<input type='hidden' name='PSorMaturityDate' value='$PSorMaturityDate'>
<input type='hidden' name='PSorRequestDate' value='$PSorRequestDate'>
<input type='hidden' name='PSorClientCode' value='$PSorClientCode'>
<input type='hidden' name='PSorClientDtCode' value='$PSorClientDtCode'>
<input type='hidden' name='PSorSalesmanCode' value='$PSorSalesmanCode'>
<input type='hidden' name='PSorCurr' value='$PSorCurr'>
<input type='hidden' name='PSorType' value='$PSorType'>
<input type='hidden' name='PSorProjectCode' value='$PSorProjectCode'>
<input type='hidden' name='PSorCostCentCode' value='$PSorCostCentCode'>
<input type='hidden' name='PSorDiscount' value='$PSorDiscount'>
<input type='hidden' name='PSorDiscountAmt' value='$PSorDiscountAmt'>
<input type='hidden' name='PSorVatChkbx' value='$PSorVatChkbx'>


HERE;

echo "<input type='hidden' name='PDriverName' value='".htmlspecialchars($PDriverName, ENT_QUOTES)."' > 
	  <input type='hidden' name='PVehicalNo' value='".htmlspecialchars($PVehicalNo, ENT_QUOTES)."' > 
	  <input type='hidden' name='PVehicleWeight' value='".htmlspecialchars($PVehicleWeight, ENT_QUOTES)."' > 
	  <input type='hidden' name='PSorRemark' value='".htmlspecialchars($PSorRemark, ENT_QUOTES)."' > 
      <input type='hidden' name='PSorIAddress' value='".htmlspecialchars($PSorIAddress, ENT_QUOTES)."'>
	  <input type='hidden' name='PSorDAddress' value='".htmlspecialchars($PSorDAddress, ENT_QUOTES)."' > ";

	

//Pack Item Module
$ModePackItem = '';
if ($Module_Pack == 0)	$ModePackItem = ' disabled ';
	

// User Has No Access To Modify The Price / Discount / Qty
$ModeModifySellPrice = '';
$ModeModifyQty = '';
$ModeCancelQty = '';
$DiscountMode = '';
if ($Login_ModifySellingPrice == 0) $ModeModifySellPrice = ' readonly ';
if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';
if ($FI_SorderCanceledQty == 0)	$ModeCancelQty = ' readonly ';

$PriceLabel = 'Price';	
if ($SalesTTC == 1)  $PriceLabel = 'Price TTC';	

$lstWarehouse = $_POST["lstWarehouse"]; if ($lstWarehouse == ""){$lstWarehouse = $_GET["lstWarehouse"];}   

$lstItemType = $_POST['lstItemType']; if ($lstItemType == ""){$lstItemType = $_GET["lstItemType"];} if ($lstItemType == ""){$lstItemType = "All";}
$lstItemSort = $_POST['lstItemSort']; if ($lstItemSort == ""){$lstItemSort = $_GET["lstItemSort"];} if ($lstItemSort == ""){$lstItemSort = "ItemCode";}	

$i_nonstock  = 0;
$i_zeroprice = 0;
$i_minprice  = 0;
$i_unitcoef  = 1;

if (empty($lstWarehouse)) {
	if(!empty($defaultwarehouse))
		$lstWarehouse = $defaultwarehouse;
	else	
		$lstWarehouse = $SetupDftWareHouse;
}

if ($Module_ProjectDt == 1 && empty($txtProjectCodeDt))     $txtProjectCodeDt  = $PSorProjectCode; 
if ($Module_CostCenterDt == 1 && empty($txtCostCentCodeDt)) $txtCostCentCodeDt = $PSorCostCentCode; 
	
	
//Edit SalesOrder Detail Mode
if($PSorDtId != ''){
	$querydt = " Select SorderDt_ID, SorderDt.Item_code, Item_Description, SorderDt.Warehouse_Code, ".
	           " Sorderdt_Remark, SorderDt_Quantity, Round(SorderDt_Price, ".$DecimalBase."), SorderDt_PriceBase1, SroderDt_PriceBase2, ".
			   " Round(SorderDt_Discount, ".$DecimalBase."), if(Items.Item_Vat !='', 1, 0) As ItemVat, ".
			   " Round(((SorderDt_Quantity*SorderDt_Price)-((SorderDt_Quantity*SorderDt_Price*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As Total, ".
			   " Round(SorderDt_PriceTTC, ".$DecimalBase."), ".
			   " Round(((SorderDt_Quantity*SorderDt_PriceTTC)-((SorderDt_Quantity*SorderDt_PriceTTC*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As TotalTTC, ".
			   " if(Sorderdt_promotion !='', 1, 0) As SorderdtPromotion, ifNull(SorderDt_oqty,0), ifNull(SorderDt_cqty,0), SorderDt.Item_CodePacked, ".
			   " SorderDt.Item_Unit2Qty, SorderDt.Project_code, SorderDt.CostCent_Code ".
			   " From Temp_SorderDt As SorderDt Left Join Items on SorderDt.Item_code = Items.Item_code ".
	           " Where SorderDt.SorderDt_ID = ".$PSorDtId;
			   //" And Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";			   
	//echo $querydt;
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
    $rowsdt=mysql_fetch_array($resultdt);
	
	$txtSorDtId        = $rowsdt[0];
	$txtItemCode       = $rowsdt[1];
	$txtItemDesc       = $rowsdt[2];
	$lstWarehouse      = $rowsdt[3];
	$txtSorDtRemark    = $rowsdt[4];
	$txtSorDtQty       = $rowsdt[5];
	$txtSorDtOQty      = $rowsdt[15];
	$txtSorDtCQty      = $rowsdt[16];
	$txtSorDtQty2      = $rowsdt[18];
	
	if ($SalesTTC == 1) 	
		$txtSorDtPrice 	   = $rowsdt[12];
	else
		$txtSorDtPrice 	   = $rowsdt[6];
		
	$txtSorDtDiscount  = $rowsdt[9];
	
	if ($SalesTTC == 1) 	
		$txtSorDtTotal     = $rowsdt[13];
	else
		$txtSorDtTotal     = $rowsdt[11];
		
	$chkSorDtVat  = $rowsdt[10];
	$txtSorDtVat  = $rowsdt[10];
	
	$txtProjectCodeDt	= $rowsdt[19]; 
	$txtCostCentCodeDt	= $rowsdt[20];
	
	// If in Edit case and Item is related to Pack/Promo Pack Item then
	// If user has no access to modify Price then he should has no access to modify Qty
	
	if ($rowsdt[17] != '' && $Unmodify_SellingPrice == 0)
		$ModeModifyQty = ' readonly ';
	
}

$WhSelected = 0;
if($PSorId != '' && $Module_SorderWithOneWh == 1){
	$query = " Select Warehouse_Code ".
	         " From Temp_SorderDt As SorderDt ".
	         " Where Sorder_ID = ".$PSorId;
	$result  = selectData($connection, $query);		 
	
	if (!(empty($result[0]))) {
		$lstWarehouse  = $result[0];
		$WhSelected = 1;
	}	

}



if($txtItemCode != ''){

	$min_sorder_qty = 0;
	$min_available_qty = 9999999999;
	$min_exists_qty = 9999999999;
	$available_qty = 0;
	$exist_qty = 0;
	
	$vItemCode    = '';
	$vItemPackQty = 0;
	
	if (empty($PSorId)) $PSorId = 0;
	if (empty($PSorDtId)) $PSorDtId = 0;
	
	if($lstItemType == 'Packed' || $lstItemType == 'PromoPacked'){
		$query = "SELECT Item_CodePackDt, Sum(PackDt_Quantity) FROM packdt WHERE Item_Code = '".$txtItemCode."' Group By Item_CodePackDt ";
		$result = mysql_query($query, $connection);			
		//echo $query;
		while($cur = mysql_fetch_row($result)){
							
			/*$Qry_QtyExists    = " Select cast(Sum(ifNull(QuantityProduceIn,0) - ifNull(QuantityProduceOut,0) + ifNull(QuantityEntered,0) - ifNull(QuantityDelivered,0)) as decimal(20,2)) as QuantityExists From StkTransaction ".
		    				    " Where StkTransaction.Item_Code = '".$cur[0]."' ".
			    			    " And (('".$lstWarehouse."' = '' and 1 = 1) or ('".$lstWarehouse."' != '' and ifNull(StkTransaction.Warehouse_Code, -1) = ifNull('".$lstWarehouse."', ifNull(StkTransaction.Warehouse_Code, -1))) ) ".
				    		    " And Transaction_Date <= '".insertMySQLDate($PSorDate)."' ";*/
								
		    $Qry_QtyExists = " SELECT ifnull(QtyExists('".$cur[0]."', '".$lstWarehouse."', '".insertMySQLDate($PSorDate)."', 0, 'SO', '', ''),0) As QtyExists ";
			
			//echo "<br><br>".$Qry_QtyExists;
			$sub_result3 = selectData($connection, $Qry_QtyExists);
		
			//if (!empty($sub_result3[0])) { 
				$exist_qty = ($sub_result3[0] / $cur[1]); 
				if ($exist_qty < $min_exists_qty ){
					$min_exists_qty = $exist_qty;
					
					$vItemCode    = $cur[0];
					$vItemPackQty = $cur[1];
				}
			//}	
		}
		
		$Qry_QtyBookTemp  = " SELECT ifnull(QtyBooking_Temp('".$vItemCode."', '".$lstWarehouse."', '".insertMySQLDate($PSorDate)."', $PSorId),0)";
		$Qry_QtyAvailable = " SELECT ifnull(QtyExistsWSorder_Temp('".$vItemCode."', '".$lstWarehouse."', '".insertMySQLDate($PSorDate)."', $PSorId, 'SO', '', '', 0),0) QtyExists ";
		$Qry_QtyInTemp    = " Select ifNull(Sum(ifNull(SorderDt_Quantity,0)),0) FROM Temp_Sorderdt ".
						    " WHERE Item_code = '".$vItemCode."'  And  Warehouse_code = '".$lstWarehouse."' And Sorder_ID = $PSorId And SorderDt_ID != ifNull('".$PSorDtId."',0) ";
		
		$Qry_QtyTransferInTemp = " Select ifNull(Sum(ifNull(TransferDt_Quantity,0)),0) FROM Temp_TransferDt ".
								 " WHERE Item_code = '".$vItemCode."'  And  TransferDt_WarehouseFrom = '".$lstWarehouse."' ";					
		
		//echo "<br><br>".$Qry_QtyBookTemp;
		//echo "<br><br>".$Qry_QtyInTemp;
		//echo "<br><br>".$Qry_QtyAvailable;
					
		$sub_result  = selectData($connection, $Qry_QtyBookTemp);
		$sub_result1 = selectData($connection, $Qry_QtyAvailable);
		$sub_result2 = selectData($connection, $Qry_QtyInTemp);
		$sub_result3 = selectData($connection, $Qry_QtyTransferInTemp);
			
		if ($sub_result[0] < 0) {$sub_result[0] = 0;}
		if ($sub_result1[0] < 0){$sub_result1[0] = 0;}
		if ($sub_result2[0] < 0){$sub_result2[0] = 0;}
		if ($sub_result3[0] < 0){$sub_result3[0] = 0;}
		
		$booking_qty = (($sub_result[0] + $sub_result2[0] + $sub_result3[0]) / $vItemPackQty);
		$min_sorder_qty = $booking_qty;
			
		$available_qty = (($sub_result1[0] - $sub_result2[0] - $sub_result3[0]) / $vItemPackQty);
		$available_qty = abs($available_qty);
		$min_available_qty = $available_qty;

			
		$min_sorder_qty = ceil($min_sorder_qty);
		//$min_available_qty = floor($min_available_qty);
		$min_available_qty = floor($min_exists_qty) - ceil($min_sorder_qty);
		$min_exists_qty = floor($min_exists_qty);

	}else{
		$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$txtItemCode."', '".$lstWarehouse."', '".insertMySQLDate($PSorDate)."', $PSorId),0) ";
		$result = selectData($connection, $Qry_QtyBookTemp);
		if ($result[0] < 0){$result[0] = 0;}
		$min_sorder_qty = $result[0];
		
		/*$Qry_QtyExists = " Select cast(ifNull(Sum(ifNull(QuantityProduceIn,0) - ifNull(QuantityProduceOut,0) + ifNull(QuantityEntered,0) - ifNull(QuantityDelivered,0)),0) as decimal(20,2)) as QuantityExists From StkTransaction ".
						 " Where StkTransaction.Item_Code = '".$txtItemCode."' ".
						 " And (('".$lstWarehouse."' = '' and 1 = 1) or ('".$lstWarehouse."' != '' and ifNull(StkTransaction.Warehouse_Code, -1) = ifNull('".$lstWarehouse."', ifNull(StkTransaction.Warehouse_Code, -1))) ) ".
						 " And Transaction_Date <= '".insertMySQLDate($PSorDate)."' ";*/
						 
		$Qry_QtyExists = " SELECT ifnull(QtyExists('".$txtItemCode."', '".$lstWarehouse."', '".insertMySQLDate($PSorDate)."', 0, 'SO', '', ''),0) As QtyExists ";				 
		$result = selectData($connection, $Qry_QtyExists);
		$min_exists_qty = $result[0];
		//echo $Qry_QtyExists;
		
		$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$txtItemCode."', '".$lstWarehouse."', '".insertMySQLDate($PSorDate)."', $PSorId, 'SO', '', '', 0) QtyExists From Dual ";
		$result = selectData($connection, $Qry_QtyAvailable);
		$min_available_qty = $result[0];

		$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(SorderDt_Quantity,0)),0) FROM Temp_Sorderdt ".
		                 " WHERE Item_code = '".$txtItemCode."'  And  Warehouse_code = '".$lstWarehouse."' And Sorder_ID = $PSorId And SorderDt_ID != ifNull('".$PSorDtId."',0) ";
		$result = selectData($connection, $Qry_QtyInTemp);
		
		$Qry_QtyTransferInTemp = " Select ifNull(Sum(ifNull(TransferDt_Quantity,0)),0) FROM Temp_TransferDt ".
								 " WHERE Item_code = '".$txtItemCode."'  And  TransferDt_WarehouseFrom = '".$lstWarehouse."' ";
		$result1 = selectData($connection, $Qry_QtyTransferInTemp);						 
			
		$min_available_qty = $min_available_qty - $result[0] - $result1[0];
		$min_exists_qty = $min_exists_qty;
		$min_sorder_qty = $min_sorder_qty + $result[0] + $result1[0];

	}
	//echo $query;
	$txtPOQty = $min_sorder_qty;
	$txtAvailableQty = $min_available_qty;
	$txtExistingQty = $min_exists_qty;
	
	
	$query = "SELECT Client_SellingPrice, ifNull(Client_DiscountPer,0) FROM Clients WHERE Ledger_Number = '".$PSorClientCode."'";
	//echo "<br><br>".$query;
	$result = selectData($connection, $query);
	$c_sp = $result[0];
	$c_discper = $result[1];

	
	
	$query = " SELECT Item_SellingPrice1, Item_SellingPrice2, Item_SellingPrice3, Item_SellingPrice4, Item_SellingPrice5, Item_SellingPrice6, ".
	         " ifNull(Item_Promotion, 0), ifNull(Item_NonStock, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ".
			 " Currency_Code, ifNull(Item_MinimumPrice, 0) As Item_MinimumPrice, ifNull(Item_Unit,1) ".
			 " FROM items WHERE Item_Code = '".$txtItemCode."'";
		
	//echo "<br><br>".$query;
	$result = selectData($connection, $query);
	$i_sp = $result[0];
	$i_promo     = $result[6];
	$i_nonstock  = $result[7];
	$i_zeroprice = $result[8];
	$i_unit1     = $result[9];
	$i_unit2 	 = $result[10]; 
	$i_curr  	 = $result[11];
	$i_minprice	 = $result[12];
	$i_unitcoef	 = $result[13];
		
	switch ($c_sp){
		case "II":
			$i_sp = $result[1]; break;
		case "III":
			$i_sp = $result[2]; break;
		case "IV":
			$i_sp = $result[3]; break;
		case "V":
			$i_sp = $result[4]; break;
		case "VI":
			$i_sp = $result[5]; break;
	}
		
	
	if(($i_sp == 0 || $i_sp == '') && ($lstItemType == 'Packed' || $lstItemType == 'PromoPacked')){
		$query = " SELECT Sum(if(ifNull(PackDt_CostPercentage,0) > 0, (ifNull(Item_SellingPrice1,0)*PackDt_Quantity), 0)) As Item_SellingPrice1, ".
				 " Sum(if(ifNull(PackDt_CostPercentage,0) > 0, (ifNull(Item_SellingPrice2,0)*PackDt_Quantity), 0)) As Item_SellingPrice2, ".
				 " Sum(if(ifNull(PackDt_CostPercentage,0) > 0, (ifNull(Item_SellingPrice3,0)*PackDt_Quantity), 0)) As Item_SellingPrice3, ".
				 " Sum(if(ifNull(PackDt_CostPercentage,0) > 0, (ifNull(Item_SellingPrice4,0)*PackDt_Quantity), 0)) As Item_SellingPrice4, ".
				 " Sum(if(ifNull(PackDt_CostPercentage,0) > 0, (ifNull(Item_SellingPrice5,0)*PackDt_Quantity), 0)) As Item_SellingPrice5, ".
				 " Sum(if(ifNull(PackDt_CostPercentage,0) > 0, (ifNull(Item_SellingPrice6,0)*PackDt_Quantity), 0)) As Item_SellingPrice6, ".
				 " 0 As Item_Promotion ".
				 " FROM items ".
				 " INNER JOIN packdt on packdt.item_codepackdt = items.item_code ".
				 " WHERE packdt.Item_Code = '".$txtItemCode."'";
		
		//echo "<br><br>".$query;
		$result = selectData($connection, $query);
		$i_sp = $result[0];
		$i_promo = $result[6];	
		
		switch ($c_sp){
			case "II":
				$i_sp = $result[1]; break;
			case "III":
				$i_sp = $result[2]; break;
			case "IV":
				$i_sp = $result[3]; break;
			case "V":
				$i_sp = $result[4]; break;
			case "VI":
				$i_sp = $result[5]; break;
		}
		
	} 
	
	//Get The Last Selling Price For The Item
	if ($lstItemType == 'Packed' || $lstItemType == 'PromoPacked'){
		if ($Module_Delivery == 0) {
			$query = " SELECT if($SalesTTC = 1, DeliveryDt_PriceTTC, DeliveryDt_Price) FROM DeliveryDt WHERE Client_Code = '".$PSorClientCode."' And Item_CodePacked = '".$txtItemCode."' And ifNull(DeliveryDt_Price,0) > 0 ".
					 //" And (('".$lstWarehouse."' = '' And 1 = 1) Or ('".$lstWarehouse."' != '' And Warehouse_Code = '".$lstWarehouse."' )) ".
					 " Order By Delivery_Date Desc Limit 1 ";
		} else {
			$query = " SELECT if($SalesTTC = 1, SaleDt_PriceTTC, SaleDt_Price) FROM SalesDt WHERE Client_Code = '".$PSorClientCode."' And Item_CodePacked = '".$txtItemCode."' And ifNull(SaleDt_Price,0) > 0 ".
					 //" And (('".$lstWarehouse."' = '' And 1 = 1) Or ('".$lstWarehouse."' != '' And Warehouse_Code = '".$lstWarehouse."' )) ".
					 " Order By Sale_Date Desc Limit 1 ";
		
		}
	} else {
		if ($Module_Delivery == 0) {
			$query = " SELECT if($SalesTTC = 1, DeliveryDt_PriceTTC, DeliveryDt_Price) FROM DeliveryDt WHERE Client_Code = '".$PSorClientCode."' And Item_code = '".$txtItemCode."' And ifNull(DeliveryDt_Price,0) > 0 ".
					 //" And (('".$lstWarehouse."' = '' And 1 = 1) Or ('".$lstWarehouse."' != '' And Warehouse_Code = '".$lstWarehouse."' )) ".
					 " Order By Delivery_Date Desc Limit 1 ";
		} else {
			$query = " SELECT if($SalesTTC = 1, SaleDt_PriceTTC, SaleDt_Price) FROM SalesDt WHERE Client_Code = '".$PSorClientCode."' And Item_code = '".$txtItemCode."' And ifNull(SaleDt_Price,0) > 0 ".
					 //" And (('".$lstWarehouse."' = '' And 1 = 1) Or ('".$lstWarehouse."' != '' And Warehouse_Code = '".$lstWarehouse."' )) ".
					 " Order By Sale_Date Desc Limit 1 ";
		
		}
	}	
	
	$result = selectData($connection, $query);
	$i_lsp = $result[0];
	if(!isset($i_lsp)) $i_lsp = 0;
	
	//if($i_promo == 1) $i_sp = 0; 
	if($i_promo == 1 && $lstItemType != 'Packed' && $lstItemType != 'PromoPacked') $i_sp = 0; 
	
	if($PSorDtId == '') $txtSorDtPrice = $i_sp;
	if($PSorDtId == '') $txtSorDtDiscount = $c_discper;
	
	$txtItemLastSellPrice = number_format($i_lsp, $DecimalBase);
	
	$txtItemUnit1 = $i_unit1;
	$txtItemUnit2 = $i_unit2;
	
	//Convert MinPrice To Invoice Currency
	$query = "SELECT ifNull(convertcurfunc('".$i_curr."', '".$PSorCurr."', '".insertMySQLDate($PSorDate)."', 0, 0, '', '', ifNull(".$i_minprice.",0)),0) From Dual ";
	$result = selectData($connection, $query);
	$i_minprice	= $result[0];
	if (empty($i_minprice)) $i_minprice = 0;
	
}


$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

echo"   <tr><td valign='top'>
        <table border='0' style='table-layout:fixed' align='center' valign='top'>
          <col width=75>
		  <col width=170>
		  <col width=80>
		  <col width=300>
		  <col width=100>
		  <col width=70>
		   <tr bgcolor='#F7F7F7' height='25'>
		     <th colspan='6'><span class='style3'>Add Items </span></th></tr>
		   <tr bgcolor='#F7F7F7' >	
			 <td colspan='6' align='center'><font size='1' color='#FF0000' face='Verdana'><b>".$message."<br>".$WarningMsg."</b></font></td></tr>			 
			 
		   <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Warehouse&nbsp;<span class='style26'>*</span></span>
			    <input type='hidden' name='txtSorDtId' value='$txtSorDtId' ></td>";
			 
		if($WhSelected == 1){	 
		    echo"<td colspan='3'><b>$lstWarehouse</b><input type='hidden' name='lstWarehouse' id='lstWarehouse' value='$lstWarehouse'></td>";
		} else 	{
			echo"<td colspan='3'><select name='lstWarehouse' id='lstWarehouse' style='font-size:11px; width:200px; height:20px;' onchange='RefreshPage();' >";
			echo"<option></option>";
			while($wrhf=mysql_fetch_row($resultwarehouse)){
			echo"<option ";
				if (($lstWarehouse==$wrhf[0]) || ($DftWareHouse==$wrhf[0]))
					echo " selected ";
			echo" value='$wrhf[0]'>$wrhf[0] - $wrhf[1]</option>";
			}
			
			echo"</select></td>";
		
		}	
			
		echo"<td align='left'><span class='style12'>Existing Qty</span></td>
			 <td align='right'><b>".number_format($txtExistingQty,2)."</b> <input type='hidden' name='txtExistingQty' value='$txtExistingQty' ></td>
		  </tr>	 
		  <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Group</span></td>
		     <td colspan='3'><select name='lstGroup' id='lstGroup' style='font-size:11px; width:350px; height:20px;' >";
			echo"<option></option>";
			while($groupf=mysql_fetch_row($resultgroup)){
			echo"<option ";
				if ($lstGroup==$groupf[0] )
					echo " selected ";
			echo" value='$groupf[0]'>$groupf[0] - $groupf[1]</option>";
			}
			
			echo"</select></td>

			<td align='left'><span class='style12'>Booking Qty</span></td>
			<td align='right'><b>".number_format($txtPOQty,2)."</b><input type='hidden' name='txtPOQty' value='$txtPOQty' ></td>
		  </tr>	
		  <tr bgcolor='#F7F7F7' height='25'>
			<td align='left'><span class='style12'>Item Type</span></td>
			<td colspan='3'><select name='lstItemType' style='font-size:11px; width:200px; height:20px;'>";
			
			if ($lstItemType == "All"){
				echo "<option selected value='All'>All Items</option>";
			}else {
				echo "<option value='All'>All Items</option>";
			}
			if ($lstItemType == "Packed" && $Module_Pack == 1){
				echo "<option selected value='Packed'>Packed</option>";
			}else if ($Module_Pack == 1){
				echo "<option value='Packed'>Packed</option>";
			}
			if ($lstItemType == "PromoPacked" && $Module_Pack == 1){
				echo "<option selected value='PromoPacked'>Promo Packed</option>";
			}else{
				echo "<option value='PromoPacked'>Promo Packed</option>";
			}
			if ($lstItemType == "Trading"){
				echo "<option selected value='Trading'>Trading</option>";
			}else{
				echo "<option value='Trading'>Trading</option>";
			}
			if ($lstItemType == "PromoTrading"){
				echo "<option selected value='PromoTrading'>Promo Trading</option>";
			}else{
				echo "<option value='PromoTrading'>Promo Trading</option>";
			}
			/*if ($lstItemType == "Production"){
				echo "<option selected value='Production'>Production</option>";
			}else{
				echo "<option value='Production'>Production</option>";
			}
			if ($lstItemType == "NonStock"){
				echo "<option selected value='NonStock'>Non-Stock</option>";
			}else{
				echo "<option value='NonStock'>Non-Stock</option>";
			}*/
			echo "</select></td>
			 				
			<td align='left'><span class='style12'>Real Qty</span></td>
			<td align='right'><b>".number_format($txtAvailableQty,2)."</b> <input type='hidden' name='txtAvailableQty' value='$txtAvailableQty' ></td>
		  </tr>
		
		  <tr bgcolor='#F7F7F7' height='25'>
			<td align='left'><span class='style12'>Sort By</span></td>
			<td colspan='5'><select name='lstItemSort' style='font-size:11px; width:200px; height:20px;'>";
			if ($lstItemSort == "ItemCode"){
				echo "<option selected value='ItemCode'>Item Code</option>";
			}else{
				echo "<option value='ItemCode'>Item Code</option>";
			}
			if ($lstItemSort == "ItemDesc"){
				echo "<option selected value='ItemDesc'>Item Desc</option>";
			}else{
				echo "<option value='ItemDesc'>Item Desc</option>";
			}
			echo "</select></td>
		  </tr>
	
		  <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Item&nbsp;<span class='style26'>*</span></span></td>";
			 
		if ($PSorDtId == '') {
		    echo " <td colspan='5'><input type='text' name='txtItemCode' value='$txtItemCode' onBlur='SetItemVat();'   maxlength='20' style='font-size:12px; width:110px; height:20px;'>
						<input type='text' name='txtItemDesc' id='txtItemDesc' value='$txtItemDesc' style='font-size:12px; width:270px; height:20px;' />
						<label><span class='style12'><img src='img/btnopen.jpg' title=' Select Item '  width='15' height='17' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistItems(1); ' /></span></label></td>";
		} else {
			echo " <td colspan='5'><input type='hidden' name='txtItemCode' value='$txtItemCode' ><input type='hidden' name='txtItemDesc' id='txtItemDesc' value='$txtItemDesc'  />
					<b>$txtItemCode &nbsp;&nbsp; $txtItemDesc</b>";
		}
	
		
		echo "	
		  </tr>	 
		  <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Remark</span></td>
			 <td  colspan='5'><input type='text' name='txtSorDtRemark' value='".htmlspecialchars($txtSorDtRemark, ENT_QUOTES)."'  maxlength='51' style='font-size:12px; width:400px; height:20px;'></td>
		  </tr>	";
		  
if ($Module_ProjectDt == 1 || $Module_CostCenterDt == 1) {
	echo"   <tr bgcolor='#F7F7F7'>";

	if ($Module_ProjectDt == 1) {
		echo" <td align='left'><span class='style12'>Project&nbsp;<span class='style12'>*</span></span></td>
			  <td align='left'> <span class='style12'>
				<input type='text' name='txtProjectCodeDt' id='txtProjectCodeDt' value='$txtProjectCodeDt' style='width:120px;' maxlength='15' readonly /></span>
				<label><span class='style12'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(1); '  /></span></label></td>";
	} else {
		echo" <td align='left'>&nbsp;</td>
			  <td align='left'><input type='hidden' name='txtProjectCodeDt' id='txtProjectCodeDt' value='' /></td>";
	}

	if ($Module_CostCenterDt == 1) {		  
		echo" <td align='left'><span class='style12'>Costcenter&nbsp;<span class='style12'>*</span></span></td>
			  <td align='left'><span class='style12'>
				<input type='text' name='txtCostCentCodeDt' id='txtCostCentCodeDt' value='$txtCostCentCodeDt' style='width:130px;' maxlength='15' readonly /></span>
				<label><span class='style12'><img src='img/btnopen.jpg' title=' Select Costcenter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistCostcenter(1); ' /></span></label></td>";
	} else {
		echo" <td align='left'>&nbsp;</td>
			  <td align='left'><input type='hidden' name='txtCostCentCodeDt' id='txtCostCentCodeDt' value='' /></td>";

	}
	
	echo" 	  <td colspan='2' align='left'>&nbsp;</td>";
	echo"   </tr>";
}
		  
echo"	  <tr bgcolor='#F7F7F7'><td valign='top' colspan='6'>
		  <table border='0' style='table-layout:fixed' align='left' valign='top' >";
		  
	if ($Module_DoubleUnit == 1) {	  
		echo"<col width=75>
			 <col width=75>
			 <col width=40>
			 <col width=70>
			 <col width=40>
			 <col width=75>
			 <col width=90>
			 <col width=90>
			 <col width=60>
			 <col width=100>
			 <col width=50>";
	} else {
		echo"<col width=75>
			 <col width=75>
			 <col width=60>
			 <col width=75>
			 <col width=90>
			 <col width=100>
			 <col width=60>
			 <col width=100>
			 <col width=70>";
	}
	echo"<tr>";
	echo"	<td valign='center' align='center' ><span class='style12'>Ordered Qty&nbsp;<span class='style26'>*</span></span></td> ";
	echo" 	<td valign='center' align='center' ><span class='style12'>Cancelled Qty</span></td>";
			 
	//if ($Module_DoubleUnit == 1)
		echo"<td valign='center' align='center' ><span class='style12'>Unit1</span></td>";
		
	echo"	<td valign='center' align='center' ><span class='style12'>Qty</span></td>";
	
	if ($Module_DoubleUnit == 1) {
		echo"<td valign='center' align='center' ><span class='style12'>Unit2</span></td>";
		echo"<td valign='center' align='center' ><span class='style12'>Unit2 Qty</span></td>";
	}
			
	echo"	<td valign='center' align='center' ><span class='style12'>$PriceLabel&nbsp;<span class='style26'>*</span></span></td> ";
	echo"	<td valign='center' align='center' ><span class='style12'>Last Sell. Price</span></td> ";
	echo"	<td valign='center' align='center' ><span class='style12'>Disc(%)</span></td> ";
	echo"	<td valign='center' align='center' ><span class='style12'>Total</span></td> ";
	echo"	<td valign='center' align='center' ><span class='style12'>VAT</span></td> ";
	echo" </tr>
		  <tr bgcolor='#F7F7F7' height='25'>
		     <td align='center'><input type='text' name='txtSorDtOQty' value='$txtSorDtOQty' maxlength='20' onBlur='CalcSorDtQty(); CalcSorDtTotal($DecimalBase);' onchange='CalcQty2($Module_Unit1Method,$i_unitcoef,1);' style='font-size:12px; width:60px; height:20px;' $ModeModifyQty></td>
			 <td align='center'><input type='text' name='txtSorDtCQty' value='$txtSorDtCQty' maxlength='20' onBlur='CalcSorDtQty(); CalcSorDtTotal($DecimalBase);'  style='font-size:12px; width:60px; height:20px;' $ModeCancelQty ></td>";
	//if ($Module_DoubleUnit == 1) 
	echo"<td align='center'><b>$txtItemUnit1</b></td>";		 
	echo"	<td align='center'><input type='text' name='txtSorDtQty' value='$txtSorDtQty' maxlength='20'  style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; background-color:#F7F7F7; width:60px; height:20px;'  readonly></td>";
	
	if ($Module_DoubleUnit == 1) {
		echo"<td align='center'><b>$txtItemUnit2</b></td>";
		echo "<td align='center'><input type='text' name='txtSorDtQty2' id='txtSorDtQty2' value='$txtSorDtQty2' maxlength='20' onchange='CalcQty2($Module_Unit1Method,$i_unitcoef,2);' style='font-size:12px; width:60px; height:20px;' ></td>";
	} else {
		echo "<input type='hidden' name='txtSorDtQty2' id='txtSorDtQty2' value='$txtSorDtQty2' >";
	}
	
	echo"	<td align='center'><input type='text' name='txtSorDtPrice' value='$txtSorDtPrice' maxlength='20' onBlur='CalcSorDtTotal($DecimalBase);' style='text-align:right; font-size:12px; width:80px; height:20px;' $ModeModifySellPrice ></td>
			<td align='center'><b>$txtItemLastSellPrice</b><input type='hidden' name='txtItemLastSellPrice' value='$txtItemLastSellPrice' maxlength='20' style='text-align:right; font-size:12px; width:90px; height:20px;' ></td>
			 
			<td align='center'><input type='text' name='txtSorDtDiscount' value='$txtSorDtDiscount' maxlength='20' onBlur='CalcSorDtTotal($DecimalBase);'  style='text-align:right; font-size:12px; width:50px; height:20px;' $DiscountMode ></td>
			<td align='center'><input type='text' name='txtSorDtTotal' value='$txtSorDtTotal' maxlength='20' style='text-align:right; font-size:12px; width:90px; height:20px;' ></td>
			<td align='center'><input type='hidden' name='txtSorDtVat' value='$txtSorDtVat'><input name='chkSorDtVat' type='checkbox' id='chkSorDtVat' value='1' disabled "; 
	        if($chkSorDtVat==1 || $txtSorDtVat == 1) echo "checked /></td>"; else echo " /></td>";   
echo"        
		  </tr>
		  </table></td></tr>
		  <tr bgcolor='#F7F7F7' height='30'>
		     <td colspan='6' align='center' bgcolor=#D8D8D8>
			    <input type='button' id='btnSubmitRecord' name='btnSubmitRecord'  value='Submit Record'  onclick='SubmitRecord($Module_NegativeSorder,$Login_negativeQty,$i_nonstock,$i_zeroprice,$Login_AllowPrice,$Module_SorderCheckQtyOnPost,$Allow_MinimumPrice,$i_minprice)' $style title='Submit Record'></td>
			 
		  </tr>
        </table>";

	

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

HERE;

?>