<?php 
if($_REQUEST['Save']!='')
{
	$data['site']=$_REQUEST['site']	;
	$data['packingNo']=$_REQUEST['packingNo']	;
	$data['packingDate']=FormatDateForSQL($_REQUEST['packingDate'])	;
	$data['consignee']=$_REQUEST['consignee']	;
	$data['EONumber']=$_REQUEST['EONumber']	;
	$data['packingType']=$_REQUEST['packingType']	;
	$data['remarks']=$_REQUEST['remarks']	;
	$data['totalItemCum']=$_REQUEST['totalItemCum']	;
	$data['totalNetWeight']=$_REQUEST['totalNetWeight']	;
	$data['totalGrossWeight']=$_REQUEST['totalGrossWeight']	;
	$data['totalCartons']=$_REQUEST['totalCartons']	;
	$data['createdBy']=$_SESSION['UserID']	;
	$data['createdDate']=date('Y-m-d');
	$data['CompanyId']=	$_SESSION['ACGCompanyId'];
	
		
	$insert1=$fnLib->insertRecord('packing_list',$data);
	$result1=DB_query($insert1,$db);
	
	$lastId=DB_Last_Insert_Id($db,'packing_list','id');
	
	if($lastId!='')
	{
		for($i=0;$i<$_REQUEST['count'];$i++)
		{
			$details['parentId']=$lastId;	
			$details['itemCode']=$_REQUEST['itemCode'][$i];	
			$details['orderQty']=$_REQUEST['orderQty'][$i];
			$details['dispatchQty']=$_REQUEST['dispatchQty'][$i];
			$details['packingType']=$_REQUEST['packingType'][$i];
			$details['packageLength']=$_REQUEST['packageLength'][$i];
			$details['packageBreadth']=$_REQUEST['packageBreadth'][$i];
			$details['packageHeight']=$_REQUEST['packageHeight'][$i];
			$details['boxDetail']=$_REQUEST['boxDetailA'][$i]*$_REQUEST['boxDetailB'][$i];
			$details['qtyPerBox']=$_REQUEST['qtyPerBox'][$i];
			$details['totalFilledBox']=$_REQUEST['totalFilledBox'][$i];
			$details['totalLooseBox']=$_REQUEST['totalLooseBox'][$i];
			$details['netWeight']=$_REQUEST['netWeight'][$i];
			$details['grossWeight']=$_REQUEST['grossWeight'][$i];
			
			if($_REQUEST['dispatchQty'][$i]!=$_REQUEST['orderQty'][$i])
			{
				$pendingQty=$_REQUEST['orderQty'][$i]-$_REQUEST['dispatchQty'][$i];
				$update1="Update export_order_details set pendingQty='".$pendingQty."' where id='".$_REQUEST['rowId'][$i]."'";
				$res=DB_query($update1,$db);
			}
			else if($_REQUEST['dispatchQty'][$i]==$_REQUEST['orderQty'][$i])
			{
				$pendingQty=0;
				$update1="Update export_order_details set packingListStatus='packed', pendingQty='".$pendingQty."' where id='".$_REQUEST['rowId'][$i]."'";
				$res=DB_query($update1,$db);
			}
			
			$insert2=$fnLib->insertRecord('packing_list_details',$details);
			$result2=DB_query($insert2,$db);
		}	
		
			
echo "<script>location.href='index.php?route=packing_list&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
prnMsg( _('Record inserted successfully'),'success');			
			
		                            
		
	}
}
?>
<script language="JavaScript" type="text/javascript">
function partyDetails(str)
{
	if (str.length==0)
  {
  document.getElementById("partyId").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("partyId").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","showPartyDetails.php?code="+str,true);
xmlhttp.send();
}
function contactDetails(str)
{
	if (str.length==0)
  {
  document.getElementById("contactId").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("contactId").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","showPartyDetails.php?contact="+str,true);
xmlhttp.send();
}
function generateCode(str)
{
	
	if (str.length==0)
  {
  document.getElementById("codeId").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("codeId").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","codeGenerator.php?site="+str,true);
xmlhttp.send();
}

function calculateTotal(a)
{
	var qty=document.getElementById("quantity_"+a).value;
	var price=document.getElementById("price_"+a).value;
	var unit=document.getElementById("pricePer_"+a).value;
	
	
	var total=qty*(price/unit);
	document.getElementById("totalValue_"+a).value=total.toFixed(2);
}
function showExportOrders(str)
{
	if (str.length==0)
  {
  document.getElementById("EODiv").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("EODiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","showSalesOrders.php?q="+str,true);
xmlhttp.send();
}
function exportOrdersDetails(str)
{
	if (str.length==0)
  {
  document.getElementById("itemDiv").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("itemDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getExportItems.php?q="+str,true);
xmlhttp.send();
}
function calculateQtyPerBox(a)
{
	var dispatchQty=document.getElementById("dispatchQty_"+a).value;
	var boxDetailB=document.getElementById("boxDetailB_"+a).value;
	var y=dispatchQty/boxDetailB;
	document.getElementById("qtyPerBox_"+a).value=parseInt(y);
	document.getElementById("totalFilledBox_"+a).value=boxDetailB;
	var x=dispatchQty%boxDetailB;
	if(x!='')
	{var loose_box='1';}
	else
	{loose_box='0';}
	document.getElementById("totallooseBox_"+a).value=loose_box;
}
function calculateTotalNetWt()
{
	var count=document.getElementById("count").value;
	var totalNetWeight=0;
	for(var i=0;i<count;i++)
	{
		var netWeight=document.getElementById("netWeight_"+i).value;
		if(netWeight==0){netWeight=0;}
		totalNetWeight=parseFloat(totalNetWeight)+parseFloat(netWeight);
	}
	document.getElementById("netWeight").value=totalNetWeight.toFixed(2);
}
function calculateTotalGrossWt()
{
	var count=document.getElementById("count").value;
	var totalGrossWeight=0;
	for(var i=0;i<count;i++)
	{
		var grossWeight=document.getElementById("grossWeight_"+i).value;
		if(grossWeight==0){grossWeight=0;}
		totalGrossWeight=parseFloat(totalGrossWeight)+parseFloat(grossWeight);
	}
	document.getElementById("grossWeight").value=totalGrossWeight.toFixed(2);
}
function calculateTotalCartons()
{
	var count=document.getElementById("count").value;
	var totalCartns=0;
	var tot_boxDetailB=0;
	var tot_boxDetailB=0;
	for(var i=0;i<count;i++)
	{
		var boxDetailB=document.getElementById("totalFilledBox_"+i).value;
		var totallooseBox=document.getElementById("totallooseBox_"+i).value;
		if(boxDetailB==''){boxDetailB=0;}
		if(totallooseBox==''){totallooseBox=0;}
		var total_1=parseInt(boxDetailB)+parseInt(totallooseBox);
		totalCartns=parseInt(totalCartns)+parseInt(total_1);
	}
	document.getElementById("totalCartons").value=totalCartns;
}
</script>
<p>
<h2 align="center">Packing List</h2></p>
<form action="" method="post">
	
	
<table width="98%" align="center" cellpadding="0" cellspacing="0" border="0">	
	<tr>
    	<td>
        	<table width="40%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF" style="border:1px solid #CCC;">
          <tr>
            <th width="41%">Branch Name</th>
            <td width="59%"><select name="site" style="width: 100%" onchange="generateCode(this.value)" >
              <option value="0">Select</option>
              <?php $site_sql=DB_query("Select loccode, locationname from locations",$db);
                while($site_row=DB_fetch_array($site_sql))
                {?>
              <option value="<?php echo $site_row['loccode'];?>"><?php echo $site_row['locationname'];?></option>
              <?php 	
                }
                ?>
            </select></td>
          </tr>
        </table>
        </td>
    </tr>
    <tr>
    <td>
    <fieldset style="background:#CCC;">
    <legend><strong>Packing Details</strong></legend>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td bgcolor="#FFFFFF" valign="top">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="33%">
    <fieldset>
        <table width="100%" border="0" cellspacing="1" cellpadding="3">
          <tr  bgcolor="#B7B7B7">
            <td width="16%"><strong>Packing No.</strong></td>
            <td width="16%"><strong>Packing Date</strong></td>
            <td width="35%"><strong>Consignee/Buyer</strong></td>
            <td width="17%"><strong>Export Order Ref.</strong></td>
            <td width="16%"><strong>Packing List Type</strong></td>
            </tr>
          <tr>
            <td><input name="packingNo" type="text" value=""/></td>
            <td><input name="packingDate" type="text" value="" class="datepicker" /></td>
            <td><select name="consignee" id="consignee" style="width: 100%" onchange="showExportOrders(this.value)">
              <option value="0">Select</option>
              <?php $v_sql=DB_query("Select vendor_code,vendor_name from suppliers",$db);
                while($v_row=DB_fetch_array($v_sql))
                {?>
              <option value="<?php echo $v_row['vendor_code'];?>"><?php echo $v_row['vendor_name'];?></option>
              <?php 	
                }
                ?>
            </select></td>
            <td id="EODiv"><select name="EONumber" style="width: 100%" onchange="exportOrdersDetails(this.value)" >
              <option value="0">Select</option>
              <?php $e_sql=DB_query("Select id,EONumber from export_sales_order",$db);
                while($e_row=DB_fetch_array($e_sql))
                {?>
              <option value="<?php echo $e_row['id'];?>"><?php echo $e_row['EONumber'];?></option>
              <?php 	
                }
                ?>
            </select></td>
            <td><select name="packingType" id="packingType" style="width: 100%">
              <option value="Mixed">Mixed</option>
              <option value="Standard">Standard</option>
            </select></td>
            </tr>
        </table>
    </fieldset>
    </td>
  </tr>
  <tr>
  	<td height="8"></td>
  </tr>
  
</table>
</td>
</tr>
</table>
</fieldset>
</td></tr></table>
<br />
<div style="overflow: auto; width:800px;"><div id="itemDiv">
  <table width="100%" border="0" cellpadding="2" cellspacing="2" style="border: solid 1px #CCC">
    <tr>
      <th>Item Code</th>
      <th  width="18%">Item Name</th>
      <th>Unit</th>
      <th>Qty</th>
      <th>Packing Type</th>
      <th>Pkg. Dimension</th>
      <th>Box Detail</th>
      <th  width="8%">Qty Per Box</th>
      <th  width="8%">Order Qty</th>
      <th width="8%">Total Box</th>
      <th width="8%">L.Box</th>
      <th width="8%">Dispatch Qty</th>
      <th width="8%">Act. Net Wt</th>
      <th width="8%">Act. Gr. Wt</th>
      </tr>
    
    <input type="hidden" name="count" value="<?php echo $i;?>" />
  </table></div>
</div>
<div style="width:100%; margin:20px 0px 0px 0px;">
<table width="100%" border="0" cellspacing="0" cellpadding="5" style="border:1px solid #CCC;" bgcolor="#FFFFFF";>
  <tr>
    <td valign="top"><strong>Remarks</strong><br />
      <textarea name="remarks" id="textarea" cols="45" rows="5" style="width:100%;"></textarea></td>
    <td width="40%"><table width="95%" border="0" align="right" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF"; style="border:1px solid #CCC;">
  <tr>
    <th width="49%">Total Item (cu. mt)</th>
    <td width="51%" align="right"><input name="totalItemCum" id="totalItemCum" type="text" value="" size="25"  style="width:100%;" /></td>
  </tr>
  <tr>
    <th>Net Weight</th>
    <td align="right"><input name="totalNetWeight" id="netWeight" type="text" value="" size="25"  style="width:100%;" onfocus="calculateTotalNetWt()" /></td>
  </tr>
  <tr>
    <th>Gross Weight</th>
    <td align="right"><input name="totalGrossWeight" id="grossWeight" type="text" value="" size="25"  style="width:100%;" onfocus="calculateTotalGrossWt()"/></td>
  </tr>
  <tr>
    <th>Total Cartons</th>
    <td align="right"><input name="totalCartons" id="totalCartons" type="text" value="" size="25"  style="width:100%;" onfocus="calculateTotalCartons()" /></td>
  </tr>
</table></td>
  </tr>
</table>


<div style="clear:both;"></div>
</div>
<div>
<p align="center"><input type="submit" name="Save" value="Save" /><input type="submit" name="Cancel" value="Cancel"></p>
</div>

</form>