<?php ob_start();
session_name('CAKEPHP'); session_start();
$_SESSION['test']=$_REQUEST['route'];
include('includes/Fun-ChkOpeningStock.php');
?>
<script>
function open_items()
{
	window.open("edit_misc_purchase_items.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&editId=<?php echo $_REQUEST['editId'];?>","item_popup_window","height=500,width=500,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}

function getstoredata(str)
{
	
if (str=="")
  {
  document.getElementById("storedata1").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("storedata1").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getselectdata.php?str="+str,true);
xmlhttp.send();
}
function getvender(str)
{
if (str=="")
  {
  document.getElementById("venderdata").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("venderdata").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getselectdata.php?vender="+str,true);
xmlhttp.send();
}
function calsubtoal(i)
{
var rate=document.getElementById("rate_"+i).value;
var qty=document.getElementById("qty_"+i).value;

if(rate==''){ rate=0;}
if(qty==''){ qty=0;}
var amt=parseFloat(rate)*parseFloat(qty);

if(amt==''){ amt=0;}document.getElementById("subtotal_"+i).value=amt.toFixed(2);	
}
function caldiscount(i)
{
var qty=parseFloat(document.getElementById("subtotal_"+i).value);
if(document.getElementById("discount_"+i).value!='')
{	
var rate=parseFloat(document.getElementById("discount_"+i).value);
var amt=(rate*qty)/100;
document.getElementById("discount_price_"+i).value=amt.toFixed(2);	
}
else
{
document.getElementById("discount_price_"+i).value=0;		
	
}	
	
}
function calamt(i)
{
var discount=parseFloat(document.getElementById("discount_price_"+i).value);
var subtotal=parseFloat(document.getElementById("subtotal_"+i).value);
var amt=(subtotal-discount);
document.getElementById("amount_"+i).value=amt.toFixed(2);	
}
function calc_basicamt()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value;
	if(cnt=='')
	{
	var cnt=document.getElementById("counter").value;	
	}
	
	for(var j=0;j<cnt;j++)
	{
		var netrate=document.getElementById("amount_"+j).value;
		if(netrate==''){netrate=0;}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("basicAmount").value=total_basic.toFixed(2);
}
function calculateDiscounted()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value;
	if(cnt=='')
	{
	var cnt=document.getElementById("counter").value;	
	}
	for(var j=0;j<cnt;j++)
	{
		var netrate=document.getElementById("discount_price_"+j).value;
		if(netrate==''){netrate=0;}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("totalDiscountedPrice").value=total_basic.toFixed(2);
}


function claculateExcise(str)
{
	
	//var discounted=document.getElementById("basic").value;
	if (str.length==0)
  		{
  			document.getElementById("exciseId").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("exciseId").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","calculateExcise1.php?quotTaxId="+str,true);
		xmlhttp.send();
}
function calculateExciseDuty()
{
	var basic=document.getElementById("basicAmount").value;
	var ExcisePercentage=document.getElementById("ExcisePercentage").value;
	
	var exciseVal=basic*(ExcisePercentage/100);
	document.getElementById("exciseValue").value=exciseVal.toFixed(2);
}
function calculateECess()
{
	var exciseValue=document.getElementById("exciseValue").value;
	var ECessPercentage=document.getElementById("ECessPercentage").value;
	var total=exciseValue*(ECessPercentage/100);
	document.getElementById("ECessValue").value=total.toFixed(2);
}
function calculateSAHCess()
{
	var exciseValue=document.getElementById("exciseValue").value;
	var SAHCessPercentage=document.getElementById("SAHCessPercentage").value;
	var total=exciseValue*(SAHCessPercentage/100);
	document.getElementById("SAHCessValue").value=total.toFixed(2);
}
function calculateTaxable()
{
	//var totalDiscountedPrice=document.getElementById("basicAmount").value;
	var exciseValue=document.getElementById("exciseValue").value;
	var ECessValue=document.getElementById("ECessValue").value;
	var SAHCessValue=document.getElementById("SAHCessValue").value;
	
	if(totalDiscountedPrice==''){totalDiscountedPrice=0;}
	if(exciseValue==''){exciseValue=0;}
	if(ECessValue==''){ECessValue=0;}
	if(SAHCessValue==''){SAHCessValue=0;}
	
	var TOTAL=parseFloat(exciseValue)+parseFloat(ECessValue)+parseFloat(SAHCessValue);
	document.getElementById("taxable_amt").value=TOTAL.toFixed(2);
}
function cal_grandtotal()
{
	
	var basic=document.getElementById("basicAmount").value;
	var Totaltaxable=document.getElementById("taxable_amt").value;
	var vatcstAmt=document.getElementById("freightValue").value;
	var suramt=document.getElementById("packagingCharges").value;
	var TotalCharge=document.getElementById("additionalCharges").value;
	
	
	if(basic==''){basic=0;}
	if(Totaltaxable==''){Totaltaxable=0;}
	if(vatcstAmt==''){vatcstAmt=0;}
	if(suramt==''){suramt=0;}
	if(TotalCharge==''){TotalCharge=0;}
	var grand_tot=parseFloat(basic)+parseFloat(vatcstAmt)+parseFloat(suramt)+parseFloat(TotalCharge)+parseFloat(Totaltaxable);
	
	 
	document.getElementById("grandTotal").value=grand_tot.toFixed(2) ;
}
function calroundoff()
{
	
var k=parseFloat(document.getElementById("grandTotal").value);
var j=parseFloat(document.getElementById("roundOff").value);	
var amt=k+j;
//alert(amt);
document.getElementById("grandTotal").value=amt.toFixed(2);	
}
 function chk_Poqty(str,i)
{
	
	var bill_qty=document.getElementById("bill_qty_"+i).value;
	if(str > bill_qty)
	{
		alert("Receive Qty is not Greater than Bill Qty.");
		document.getElementById("qty_"+i).value='';
	}

}
</script>


<?php 
$PageSecurity = 4;
$PageName = 'Purchase Bill Book';
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');

$sql="Select * from miss_purchase_bill where id='".$_REQUEST['editId']."'";
$result=DB_query($sql,$db);
$myrow=DB_fetch_array($result);
?>
<?php 

if(isset($_POST['DeleteRow']))		 
{
 if(!empty($_REQUEST['delitem']))
 {	
 $ListItem=explode('#',$_SESSION['ACGRequisition-ItemList_1']);
  for($i=0;$i<count($_REQUEST['delitem']);$i++)
   {	  
		 $key=array_search($_REQUEST['delitem'][$i],$ListItem);
  		 if($key!==false)
		 unset($ListItem[$key]);
		 $itemlist1=implode("#",$ListItem);
		 $_SESSION['ACGRequisition-ItemList_1']=$itemlist1;
		  }
 }
 if(!empty($_REQUEST['deldate']))
 { 		
 for($i=0;$i<count($_REQUEST['deldate']);$i++)
 { 	
 $update=DB_query("update miss_purchase_bill_details set edit_status=1 where id='".$_REQUEST['deldate'][$i]."'",$db);	
 }		
 }
 }

if($_REQUEST['Update']) 
{
$sqldata=DB_query("delete  from miss_purchase_bill_details where edit_status=1 and miss_purc_id='".$_REQUEST['editId']."'",$db);
//$project=$_REQUEST['site'];
//$autoNo=$autoID->GenerateId('miss_purchase_bill','miss_purchase_bill_no','branch='."'$project'",'/Cld9/');	
$insert_sql="update  miss_purchase_bill set
			  branch='".$_REQUEST['site']."',    
			  godown='".$_REQUEST['godown']."',
			  vender_id='".$_REQUEST['vender']."',   	
			  vender_code='".$_REQUEST['vender_code']."',    
			  vender_bill_no='".$_REQUEST['vender_bill']."',  
			  vender_date='".FormatDateForSQL($_REQUEST['vender_date'])."',  
			  miss_purchase_bill_no='".$_REQUEST['m_purch_bill']."',  
			  date='".FormatDateForSQL($_REQUEST['date'])."',  
			  transporter='".$_REQUEST['transporter']."',  
			  against_form='".$_REQUEST['against_form']."',  
			  basic_amt='".$_REQUEST['basicAmount']."', 
			  discout_amt ='".$_REQUEST['totalDiscountedPrice']."',
			  excise_duty='".$_REQUEST['exciseDuty']."',
			  excise_duty_per='".$_REQUEST['ExcisePercentage']."',
			  excise_duty_value ='".$_REQUEST['exciseValue']."', 
			  ECessPercentage='".$_REQUEST['ECessPercentage']."',
			  ECessValue='".$_REQUEST['ECessValue']."',
			  SAHCessPercentage='".$_REQUEST['SAHCessPercentage']."',  
			  SAHCessValue='".$_REQUEST['SAHCessValue']."',  
			  taxable_amt='".$_REQUEST['taxable_amt']."', 
			  freight ='".$_REQUEST['freightValue']."', 
			  pkg_charge='".$_REQUEST['packagingCharges']."',  
			  additional_charges='".$_REQUEST['additionalCharges']."',  
			  round_off='".$_REQUEST['roundOff']."',  
			  net_amt='".$_REQUEST['grandTotal']."' where id='".$_REQUEST['editId']."'";
$insert_res=DB_query($insert_sql,$db);
if($s->DeleteRecord('miss_purchase_bill_details',"miss_purc_id=".$_REQUEST['editId']))
{
	$lastid=$_REQUEST['editId'];
for($i=0;$i<count($_REQUEST['mst_itemid']);$i++)
{
 $insert=DB_query("insert into miss_purchase_bill_details set 
          miss_purc_id='".$lastid."',item_code='".$_REQUEST['item_code'][$i]."'
          ,item_name='".$_REQUEST['item_name'][$i]."',unit='".$_REQUEST['Unit'][$i]."'
          ,stock_qty='".$_REQUEST['bill_qty'][$i]."',qty='".$_REQUEST['qty'][$i]."'
          ,rate='".$_REQUEST['rate'][$i]."',sub_total='".$_REQUEST['subtotal'][$i]."'
          ,disc_per='".$_REQUEST['discount'][$i]."',discount_amt='".$_REQUEST['discount_price'][$i]."'
          ,amount='".$_REQUEST['amount'][$i]."'",$db);
          
}
}   
$inserttems=DB_query("update  miss_purchasebillbook_tems_details set
              accounting_ledger='".$_REQUEST['account_ledger']."'
              ,excise_effect='".$_REQUEST['excise_effect']."',pkg_forwarding_terms='".$_REQUEST['pack_forward']."',
              pkg_forwarding_terms_value='".$_REQUEST['PFRemarks']."',freight_terms='".$_REQUEST['freight']."'
              ,freight_terms_value='".$_REQUEST['freightRemarks']."',payment_terms='".$_REQUEST['payTerms']."',
              sales_tax_terms='".$_REQUEST['salesTax']."',sales_tax_terms_value='".$_REQUEST['salesRemarks']."'
              ,excise_tems='".$_REQUEST['Excise']."',excise_terms_value='".$_REQUEST['exciseRemarks']."',
              insurance_tems='".$_REQUEST['insurance']."',insurance_tems_value='".$_REQUEST['insRemarks']."'
              ,remarks='".$_REQUEST['textarea']."' where miss_purch_id='".$_REQUEST['editId']."'",$db);
  unset($_SESSION['ACGRequisition-ItemList_1']); unset($_SESSION['requsition_itemcode']); 
  
      
 $_SESSION['success_msg']="Update Successfully";   
 echo "<script>location.href='index.php?route=POModule/Miscellaneous_Purchase_ Bill_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
if($_POST['Cancel'])
{
unset($_SESSION['ACGRequisition-ItemList_1']); unset($_SESSION['requsition_itemcode']);

}

?>
<h2 align="center"><strong>Miscellaneous Purchase Bill </strong></h2>
<form action="" method="post">
<div align="center">
<table width="80%" align="center" cellpadding="2"  cellspacing="1" bgcolor="#FFFFFF" style="border:1px solid #ccc;">
<tr>
<th width="19%"><strong>Branch</strong></th>
<td width="28%"> 
<?php 
$sql = "select loccode, locationname from locations where company='".$_SESSION['ACGCompanyId']."'";
$result=DB_query($sql, $db);
?>
<select name="site" id="site"  class="select_purch" style="width:98%; font-size:12px;"  onchange="getstoredata(this.value);">

<?php
while($myrow1=DB_fetch_array($result))
{ 
$p='';
if($myrow1['loccode']==$myrow['branch'])
$p="selected";

?>
<option value="<?php echo $myrow1['loccode']; ?>" <?php echo $p; ?>> <?php echo $myrow1['locationname']; ?> </option>
<?php } ?>
</select></td>
<th width="20%" >
 <strong>Godown </strong></th>
<td width="33%" id="storedata1">
<select name="godown" class="select_purch" style="width:98%; font-size:12px;" id="godown">
<?php 
$sql="select id,StoreCode,StoreName from storetype where Store_Site='".$myrow['branch']."' ";
$result=DB_query($sql,$db);
while($row3=DB_fetch_array($result)){
	$p='';
if($row3['id']==$myrow['godown'])
$p="selected";
?> 	
<option value="<?php echo $row3['id']; ?>"<?php echo $p; ?>><?php echo $row3['StoreCode']."-". $row3['StoreName']; ?></option>		
<?php } ?>	
</select>	
</td>
</tr>
<tr>
<th><strong>Vender Name</strong></th>
<td id="data">
<?php

$sql = "select id,vendor_name from suppliers  ";
$result=DB_query($sql, $db);
?>
<select name="vender" id="vender" class="select_purch" style="width:98%; font-size:12px;"  onchange="getvender(this.value);">
<?php
while($myrow2=DB_fetch_array($result))
{
$p='';
if($myrow2['id']==$myrow['vender_id'])
$p="selected";
?>
<option value="<?php echo $myrow2['id']; ?>" <?php echo $p; ?>><?php echo $myrow2['vendor_name']; ?> </option>
<?php } ?>
</select></td>

<th><strong>Vender Code</strong></th>
<td id="venderdata"><input name="vender_code" type="text" value="<?php echo $myrow['vender_code'];?>" size="15" id="vender_code" readonly="true"  style="width:98%;"/></td>
<!-- <input type="text" name="textfield210" />-->
</tr>
<tr>
<th><strong>Vender Bill No </strong></th>
<td><input type="text" name="vender_bill" id="vender_bill" size="15" value="<?php echo $myrow['vender_bill_no']; ?>"  style="width:98%;"/>
	
</td>
<th><strong>Vender Date</strong></th>
<td><input name="vender_date" type="text" value="<?php echo ConvertSQLDate($myrow['vender_date']); ?>" size="15" id="vender_date" class="datepicker" style="width:98%;"/></td>
</tr>
<tr>
<th><strong>M. Purchase Bill No</strong></th>
<td>
  <input name="m_purch_bill" type="text" value="<?php echo $myrow['miss_purchase_bill_no']; ?>" size="15" id="m_purch_bill"  readonly="true" style="width:98%;"/>
</td> 
<th><strong>Date</strong></th>
<td><input name="date" type="text" value="<?php echo ConvertSQLDate($myrow['date']); ?>" size="15" id="date" class="datepicker"  style="width:98%;"/></td>
</tr>
<tr><th>Transporter</th>
<td><input name="transporter" type="text" value="<?php echo $myrow['transporter']; ?>" size="15" id="transporter" style="width:98%;"/></td>
<th>Against Form</th>
  <td><input type="text" name="against_form"  value="<?php echo $myrow['against_form']; ?>" id="against_form" size="15"  style="width:98%;"/>
  	  	</td></tr></table>
<br />
  <table width="100%" cellpadding="0" cellspacing="0" border="0" align="center">
  	<tr>
    	<td><input type="submit" name="item_popup" id="item_popup" value="Add Items"  onclick="open_items();"/>
    		<input type="submit"   name="DeleteRow" value="Delete" onclick="return confirm('Are you sure you want to delete?')"/></td>
    </tr>
    <tr>
  	  <td><table width="100%" border="0" cellspacing="2" cellpadding="2">
  	    <tr>
  	      <th width="7%">Item Code</th>
  	      <th width="17%">Item Name</th>
  	      <th width="8%">Unit</th>
  	     <!-- <th width="6%">Bill Qty</th>-->
  	      <th width="8%">Bill Qty</th>
  	     <!-- <th width="7%">Short Qty</th>-->
  	      <th width="10%">Receive Qty</th>
  	      <th width="8%">Rate</th>
  	      <th width="8%">Sub Total</th>
  	      <th width="8%">Disc%</th>
  	       <th width="8%">Discount Amt</th>
  	      <th width="9%">Amount</th> 
  	      <th width="10%">Action </th> 	     
  	    </tr>
<?php 

if($_REQUEST['editId']!='')
{
	$i=0;
	$_SESSION['requsition_itemcode']=array();
$edit_row=DB_query("select * from miss_purchase_bill_details where miss_purc_id='".$_REQUEST['editId']."' and edit_status=0",$db);	
while($editdata=DB_fetch_array($edit_row))
{
	$product_id=$editdata['item_code'];
	$_SESSION['requsition_itemcode'][]=$product_id;
	$unitid=$s->getField('item_master','StockID',$editdata['item_code'],'unit');
	?>  	    
  	<tr bgcolor="#FFFFFF">	
	<td style="height:10px"><?php echo $editdata['item_code'];?>
   <input type="hidden" name="mst_itemid[<?php echo $i;?>]" id="mst_itemid_<?php echo $i;?>" value="<?php echo $item_row['id'];?>">	
	<input type="hidden" name="item_code[<?php echo $i;?>]" id="item_code_<?php echo $i;?>" value="<?php echo $editdata['item_code'];?>">	</td>
	<td style="height:10px"><?php echo $editdata['item_name'];?><input type="hidden" name="item_name[<?php echo $i;?>]" id="item_name[<?php echo $i;?>]" value="<?php echo $editdata['item_name'];?>"/>	</td>
		<td style="height:10px"><?php
			
		$grade_query=DB_query("select * from unitsofmeasure where unitid='".$unitid."'", $db);
		while($grade_row=DB_fetch_array($grade_query))
		 {
		 echo $grade_row['unitname']; ?>
		  <?php } ?><input type="hidden" name="Unit[<?php echo $i;?>]" id="Unit[<?php echo $i;?>]" value="<?php $item_row['unit'];?>"/></td>
	<td style="height:10px">
	
	<input type="text" name="bill_qty[<?php echo $i;?>]" id="bill_qty_<?php echo $i;?>"  value="<?php echo $editdata['stock_qty']; ?>" style="width:100%"/></td>
	<!--<td align="center" style="height:10px">  <input name="Act_Rec_qty[<?php echo $i;?>]" type="text" id="Act_Rec_qty_<?php echo $i;?>"  style="width:100%" size="14" onblur="CalrequiredQty(this.value,<?php echo $i;?>)" /></td>
	<!--<td> <input type="text" name="short_qty[<?php echo $i;?>]" id="short_qty_<?php echo $i;?>" value=""></td>-->
    <td align="center" style="height:10px"><input type="text" name="subtotal[]2" id="subtotal_<?php echo $i; ?>" readonly onfocus="calsubtoal(<?php echo $i; ?>)" value="<?php echo $editdata['sub_total']; ?>" onchange="chk_Poqty(this.value,<?php echo $i; ?>)"/></td>
    <td align="center" style="height:10px">
	 
	<input name="rate[<?php echo $i;?>]" type="text" id="rate_<?php echo $i;?>" style="width:100%" value="<?php echo $editdata['rate']; ?>"/>	 </td>
	  <td align="center" style="height:10px"><input name="qty[<?php echo $i;?>]" type="text" id="qty_<?php echo $i;?>" style="width:100%" value="<?php echo $editdata['qty']; ?>"/>
	  
	  <td align="center" style="height:10px">
	 
	<input name="discount[<?php echo $i;?>]" type="text" id="discount_<?php echo $i;?>" style="width:100%" value="<?php echo $editdata['disc_per']; ?>"/>	 </td>
	 <td align="center" style="height:10px">
	 <input type="text" name="discount_price[]" id="discount_price_<?php echo $i; ?>" onfocus="caldiscount(<?php echo $i; ?>)"  value="<?php echo $editdata['discount_amt']; ?>"/>	 </td>
	 <td align="center" style="height:10px">	 
	<input name="amount[<?php echo $i;?>]" type="text" id="amount_<?php echo $i;?>" style="width:100%" onfocus="calamt(<?php echo $i; ?>)" value="<?php echo $editdata['amount']; ?>"/>	 </td>
	 <td><input name="deldate[]" type="checkbox" value="<?php echo $editdata['id'];?>" /><img src="images/Icon_Delete.png" height="15" width="15"></td>
	 	</tr>
	 	<?php $i++; } ?> 
	 	<input type="hidden" name="counter" id="counter" value="<?php echo $i; ?>" /> 

<?php   	   
if(isset($_SESSION['ACGRequisition-ItemList_1']))
{
$itemlist=array();

$itemlist=explode('#',$_SESSION['ACGRequisition-ItemList_1']);
?>
	<?php 
			
	for($j=0,$k=$i;$j<count($itemlist);$j++,$k++)
	{
		$yy=explode(",",$itemlist[$j]);
		$_SESSION['requsition_itemcode'][]=$yy[0];
	 $itemquery=DB_query("select * from item_master where StockID='".$yy[0]."'" , $db);
	while($item_row=DB_fetch_array($itemquery))
	{		
	?>
	<tr bgcolor="#FFFFFF">	
	<td style="height:10px"><?php echo $item_row['StockID'];?>
   <input type="hidden" name="mst_itemid[<?php echo $k;?>]" id="mst_itemid_<?php echo $k;?>" value="<?php echo $item_row['id'];?>">	
	<input type="hidden" name="item_code[<?php echo $k;?>]" id="item_code_<?php echo $k;?>" value="<?php echo $itemlist[$j];?>">	</td>
	<td style="height:10px"><?php echo $item_row['item_name'];?><input type="hidden" name="item_name[<?php echo $i;?>]" id="item_name[<?php echo $k;?>]" value="<?php echo $item_row['item_name'];?>"/>	</td>
		<td style="height:10px"><?php
		$grade_query=DB_query("select * from unitsofmeasure where unitid='".$item_row['unit']."'", $db);
		while($grade_row=DB_fetch_array($grade_query))
		 {
		 echo $grade_row['unitname'];?>
		  <?php } ?><input type="hidden" name="Unit[<?php echo $k;?>]" id="Unit[<?php echo $k;?>]" value="<?php $item_row['unit'];?>"/></td>
	<td style="height:10px">
	<?php $openingstock=openingStock($_SESSION['site'],$itemlist[$j]);;
	$received=receivedQty($_SESSION['site'],$itemlist[$j]);;
	$issue=issueQty($_SESSION['site'],$itemlist[$j]);;
		 ?>
	<input type="text" name="bill_qty[<?php echo $k;?>]" id="bill_qty_<?php echo $k;?>"   value="" style="width:100%"/></td>
	<!--<td align="center" style="height:10px">  <input name="Act_Rec_qty[<?php echo $i;?>]" type="text" id="Act_Rec_qty_<?php echo $i;?>"  style="width:100%" size="14" onblur="CalrequiredQty(this.value,<?php echo $i;?>)" /></td>
	<!--<td> <input type="text" name="short_qty[<?php echo $i;?>]" id="short_qty_<?php echo $i;?>" value=""></td>-->
    <td align="center" style="height:10px"><input name="qty[<?php echo $k;?>]" type="text" id="qty_<?php echo $k;?>" style="width:100%"/></td>
     <td align="center" style="height:10px">
	 
	<input name="rate[<?php echo $k;?>]" type="text" id="rate_<?php echo $k;?>" style="width:100%" />	 </td>
	  <td align="center" style="height:10px"><input type="text" name="subtotal[]" id="subtotal_<?php echo $k; ?>" readonly onfocus="calsubtoal(<?php echo $k; ?>)"/>
	 <td align="center" style="height:10px">
	 
	<input name="discount[<?php echo $k;?>]" type="text" id="discount_<?php echo $k;?>" style="width:100%"/>	 </td>
	 <td align="center" style="height:10px">
	 <input type="text" name="discount_price[]" id="discount_price_<?php echo $k; ?>" onfocus="caldiscount(<?php echo $k; ?>)" />	 </td>
	 <td align="center" style="height:10px">
	 
	<input name="amount[<?php echo $k;?>]" type="text" id="amount_<?php echo $k;?>" style="width:100%" onfocus="calamt(<?php echo $k; ?>)"/>	 </td>
	<td><input name="delitem[]" type="checkbox" value="<?php echo $item_row['StockID'];?>" /><img src="images/Icon_Delete.png" height="15" width="15"></td>
	 	</tr><?php }} }?> 
	 	
	 	<input type="hidden" name="count" value="<?php echo $k; ?>" id="count" />
	 
  <?php } ?>

    </table></td></tr>
  </table>
  <table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
  	<td colspan="2">&nbsp;</td>
  </tr>
  <tr>
  <td valign="top">
  <?php
  $terms=DB_query("select * from miss_purchasebillbook_tems_details where miss_purch_id='".$_REQUEST['editId']."'",$db);
  $termsdata=DB_fetch_array($terms); 
  ?>	
  	
   <table width="100%" border="0" cellpadding="0" cellspacing="0">
     <tr>
            <td colspan="2">
                <table width="100%" border="0" cellspacing="1" cellpadding="2" align="center" bgcolor="#FFFFFF" style="border:1px solid #CCC;">
        <tr>
            <th style="text-align:center;" colspan="2">Account Posting Ledger/Excise Effect</th>
        </tr>
        <tr>
          <th width="40%" scope="row"><strong>Accounting Ledger</strong></th>
          <td width="60%">
          <select name="account_ledger" id="menu1" style="width:98%; font-size:12px;" onchange="checkApplicable(this.value)">
            <option value="AS" <?php if($termsdata['accounting_ledger']=='AS'){ ?>selected="selected" <?php } ?>>As per calculation</option>
            <option value="NA"<?php if($termsdata['accounting_ledger']=='NA'){ ?>selected="selected" <?php } ?>>N/A</option>
          </select></td>
          </tr>
          <tr>
        <th width="40%" scope="row"><strong>Excise Effect</strong></th>
        <td width="60%"><select name="excise_effect" id="menu2" style="width:98%; font-size:12px;" onchange="checkApplicable(this.value)">
            <option value="AS"<?php if($termsdata['excise_effect']=='AS'){ ?>selected="selected" <?php } ?>>As per calculation</option>
            <option value="NA"<?php if($termsdata['excise_effect']=='NA'){ ?>selected="selected" <?php } ?>>N/A</option>
          </select></td>
        </tr>
    </table>
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
        </tr>
	<tr>
    <td valign="top" width="50%">
    <table width="100%" border="0" cellspacing="1" cellpadding="2" align="center" bgcolor="#FFFFFF" style="border:1px solid #CCC;">
        
        <tr>
            <th style="text-align:center;" colspan="2">Terms And Conditions</th>
        </tr>
        <tr>
          <th width="40%" scope="row"><strong>Pkg &amp; Forwarding</strong></th>
          <td width="60%">
          <select name="pack_forward" id="menu1" style="width: 48%; font-size:12px;" onchange="checkApplicable(this.value)">
            <option value="AS"<?php if($termsdata['pkg_forwarding_terms']=='AS'){ ?>selected="selected" <?php } ?>>As per calculation</option>
            <option value="NA"<?php if($termsdata['pkg_forwarding_terms']=='NA'){ ?>selected="selected" <?php } ?>>N/A</option>
          </select><input type="text" name="PFRemarks" id="remarks1" value="<?php echo $termsdata['pkg_forwarding_terms_value']; ?>" size="23%" />
          </td>
          </tr>
          <tr>
        <th width="40%" scope="row"><strong>Freight</strong></th>
        <td width="60%"><select name="freight" id="menu2" style="width: 48%; font-size:12px;" onchange="checkApplicable(this.value)">
            <option value="AS"<?php if($termsdata['freight_terms']=='AS'){ ?>selected="selected" <?php } ?>>As per calculation</option>
            <option value="NA"<?php if($termsdata['freight_terms']=='NA'){ ?>selected="selected" <?php } ?>>N/A</option>
          </select><input type="text" name="freightRemarks" id="remarks2" value="<?php echo $termsdata['pkg_forwarding_terms_value']; ?>" size="23%" /></td>
        </tr>
      <tr>
        <th scope="row"><strong>Payment Terms</strong></th>
        <td><select name="payTerms" style="width: 98%; font-size:12px;">
            <?php 
            $payment_sql=DB_query("Select * from paymentterms",$db);
            while($payment_row=DB_fetch_array($payment_sql))
            {
            $t='';
			if($payment_row['termsindicator']==$termsdata['payment_terms'])
			{
			$t="selected";	
			}	
            	?>
                <option value="<?php echo $payment_row['termsindicator'];?>"<?php echo $t; ?>><?php echo $payment_row['terms'];?></option>
            <?php 	
            }
            ?>
          </select></td>
        </tr>
        <tr>
        <th scope="row"><strong>Sales Tax</strong></th>
        <td><select name="salesTax" id="menu3" style="width: 48%; font-size:12px;" >
            <option value="AS"<?php if($termsdata['sales_tax_terms']=='AS'){ ?>selected="selected" <?php } ?>>As per calculation</option>
            <option value="NA"<?php if($termsdata['sales_tax_terms']=='NA'){ ?>selected="selected" <?php } ?>>N/A</option>
          </select><input type="text" name="salesRemarks" id="remarks3" value="<?php echo $termsdata['sales_tax_terms_value']; ?>" size="23%" /></td>
      </tr>
      <tr>
        <th>Excise</th>
        <td><select name="Excise" id="" style="width: 48%; font-size:12px;" >
            <option value="AS"<?php if($termsdata['excise_tems']=='AS'){ ?>selected="selected" <?php } ?>>As per calculation</option>
            <option value="NA"<?php if($termsdata['excise_tems']=='NA'){ ?>selected="selected" <?php } ?>>N/A</option>
          </select><input type="text" name="exciseRemarks" id="" value="<?php echo $termsdata['excise_terms_value']; ?>" size="23%" /></td>
        </tr>
        <tr>
          <th nowrap="nowrap">Insurance</th>
          <td><select name="insurance" id="menu4" style="width: 48%; font-size:12px;" onchange="checkApplicable(this.value)">
            <option value="AS"<?php if($termsdata['insurance_tems']=='AS'){ ?>selected="selected" <?php } ?>>As per calculation</option>
            <option value="NA"<?php if($termsdata['insurance_tems']=='NA'){ ?>selected="selected" <?php } ?>>N/A</option>
            </select><input type="text" name="insRemarks" id="remarks4" value="<?php echo $termsdata['insurance_tems_value']; ?>" size="23%" /></td>
        </tr>
        <tr>
          <th>Remarks</th>
          <td><textarea name="textarea" cols="" rows="" style="width:100%; height:40px;"><?php echo $termsdata['remarks']; ?></textarea></td>
        </tr>
    </table>	
    </td>
    </tr>
  </table>
  </td>
  
<td valign="top" rowspan="2" width="50%">
<table width="90%" border="0" cellspacing="1" cellpadding="0" align="right" bgcolor="#FFFFFF" style="border:1px solid #CCC;">
    		<tr>
    	<th style="text-align:center;" colspan="2">Amount And Taxes</th>
    </tr>
    <tr>
    	  <tr>
    	    <th width="14%">Basic Amount</th>
    	    <td width="31%"><input name="basicAmount" type="text" value="<?php echo $myrow['basic_amt'];?>" size="25" style="width:98%;" onfocus="calc_basicamt()" id="basicAmount" readonly /></td>
    	    
  	    </tr>
    	  <tr>
    	    <th nowrap="nowrap">Discounted Price</th>
    	    <td><input name="totalDiscountedPrice" id="totalDiscountedPrice" type="text" value="<?php echo $myrow['discout_amt'];?>" size="25" style="width:98%;" readonly onfocus="calculateDiscounted()" /></td>
    	    
  	    </tr>
    	  <tr>
    	    <th>Excise</th>
    	    <td nowrap="nowrap">
    	    <select name="exciseDuty" id="exciseDuty" style="width:98%;" onchange="claculateExcise(this.value)">
    	    <option value="">Select</option>
    	    <?php 
    	    $excise_sql=DB_query("Select * from excise_tax where caption like '%excise%' ",$db);
			while($excise_row=DB_fetch_array($excise_sql))
			{
			$c='';
			if($excise_row['id']==$myrow['excise_duty'])
			{
			$c="selected";	
			}
				
				?>
				<option value="<?php echo $excise_row['id'];?>" <?php echo $c; ?>><?php echo $excise_row['caption'];?></option>
			<?php 	
			}
    	    ?> 
  	      </select><div id="exciseId"><input name="ExcisePercentage" id="ExcisePercentage" type="text" value="<?php echo $myrow['excise_duty_per']; ?>"  style="width:20%;" readonly /> <input name="exciseValue" id="exciseValue" type="text" value="<?php echo $myrow['excise_duty_value']; ?>" size="25" style="width:75%;" onfocus="calculateExciseDuty()" /></div>
    	   </td></tr>
    	   <?php 
    	     $cessSql=DB_query("Select caption from excise_tax where caption like '%cess%'",$db);
			 $k=1;
			 while($cessRow=DB_fetch_array($cessSql))
			 {
			 	$caption[$k]=$cessRow['caption']; 
				
				$rate_sql=DB_query("Select def_rate from excise_tax where caption='".$caption[$k]."'",$db);
				while($rate_row=DB_fetch_array($rate_sql))
				{
				$rate[$k]=$rate_row['def_rate'];
				}
				$k++;
			 } 
    	     ?>
    	     <tr>
    	    <th>E-Cess</th>
    	    <td><input name="ECessPercentage" id="ECessPercentage" type="text" value="<?php echo $rate[1];?>" size="25" style="width:20%;" readonly />
    	    	<input name="ECessValue" id="ECessValue" type="text" value="<?php echo $myrow['ECessValue']; ?>" size="25" style="width:75%;" readonly onfocus="calculateECess()"/></td>
    	    </tr>
    	    <tr>
            <th nowrap="nowrap">SAH-E Cess </th>
    	    <td><input name="SAHCessPercentage" id="SAHCessPercentage" type="text" value="<?php echo $rate[2];?>" size="25" style="width:20%;" readonly/>
    	    	<input name="SAHCessValue" id="SAHCessValue" type="text" value="<?php echo $myrow['SAHCessValue']; ?>" size="25" style="width:75%;" readonly onfocus="calculateSAHCess()"/></td>
  	    	</tr> 
  	    	<tr>
			<th><strong>Taxable Amount </strong></th>
			<td><input type="text" name="taxable_amt" id="taxable_amt" value="<?php echo $myrow['taxable_amt']; ?>" size="15" onfocus="calculateTaxable()" readonly/></td>
			</tr>	
    	  <tr>
    	    <th>Freight</th>
    	    <td><input name="freightValue" id="freightValue" type="text" value="<?php echo $myrow['freight']; ?>" size="25" style="width:98%;" /></td>
    	    
  	    </tr>
    	  <tr>
    	    <th>Pkg Charges</th>
    	    <td><input name="packagingCharges" id="packagingCharges" type="text" value="<?php echo $myrow['pkg_charge']; ?>" size="25" style="width:98%;" /></td>
    	    
  	    </tr>
  	    <tr>
    	    <th>Additional Charges</th>
    	    <td><input name="additionalCharges" id="additionalCharges" type="text" value="<?php echo $myrow['additional_charges']; ?>" size="25" style="width:98%;" /></td>
    	    
  	    </tr>
    	  <tr>
    	    <th>Round Off</th>
    	    <td><input name="roundOff" id="roundOff" type="text" value="<?php echo $myrow['round_off']; ?>" size="25" style="width:98%;" onchange="calroundoff();"/></td>
    	    
  	    </tr>
    	  <tr>
    	    <th>Net Total :<input type="button" name="Cal Net Amt" id="calnetamt" onclick="cal_grandtotal();" value="Cal Amount"></th>
    	    <td><input name="grandTotal" type="text" value="<?php echo $myrow['net_amt']; ?>" size="25" style="width:98%;"  id="grandTotal"/></td>
    	    
  	    </tr>
  	    </table>
</td>
</tr></table>
<p align="center"><input type="submit" name="Update" value="Update" />
	<input type="reset" name="Cancel" value="Reset" />
	<a href="index.php?route=POModule/Miscellaneous_Purchase_ Bill_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&NewReturn=yes"><input type="button" value="Cancel" name="Back" /></a>
</p>
</form>
<?php 
 include('includes/footer.inc');
?>
