<?php ob_start();
$PageSecurity = 4;
$PageName = 'MaterialReceiptNote';
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
if (isset($_GET['ModifyOrderNumber']) and $_SESSION[UserType]=='SuperAdmin')
{
	$title = _('Modify Purchase Order') . ' ' . $_GET['ModifyOrderNumber'];
}
else
{
}
if(isset($_GET['issueid'])  && isset($_GET['issueid']) !='')
{
	$title = _('Update Issue Slip');
}
else
{
	$title = _('Modify Material Receipt Note');
}
	$url_cancel= "index.php?route=Inventory/transation/material_receipt_note_index".$rurl;
	$url_add= "index.php?route=Inventory/transation/material_receipt_note_edit".$rurl;
if($_REQUEST['action']=='del')
{
	$del_sql="Update dbl_addmrnqty SET delete_status='0' WHERE issue_id='".$_REQUEST['mrnid']."'
	AND itemcode='".$_REQUEST['itemcode']."'"; 
	header("Location: view_MRN.php?Type=".$_REQUEST['Type']."&ModfingOnly=1&mrnid=".$_REQUEST['mrnid']);
	$del_res=DB_query($del_sql,$db);
}
	
//**************************************** Update Mrn Data     *****************************************//
if($_REQUEST['Update'])
	{
		$updatemrn_sql="UPDATE dbl_addmrn SET 
		DocumentType='".$_POST['DocumentType']."',
		BillNo='".$_POST['billno']."',
		ReceivedFrom ='".$_REQUEST['site1']."',
		MRNVoucherDate='".FormatDateForSQL($_POST['MRNVoucherDate'])."',
		GetPassNo='".$_POST['gatepassno']."',
		Inwardsdate='".FormatDateForSQL($_POST['inwardate_save'])."',
		Transp_name='".$_POST['transport']."',
		VehicleNo='".$_POST['vehicalno']."',
		TimeIn='".$_POST['timein']."',
		Timeout='".$_POST['timeout']."',
		LRno='".$_POST['lrno']."',
		LRdate='".FormatDateForSQL($_POST['lrdate'])."',
		TotalWeight='".$_POST['total_weight']."',
		form_type='".$_REQUEST['form_type']."',
		form_no='".$_REQUEST['form_no']."',
		gatepassdate='".FormatDateForSQL($_REQUEST['gatepassdate'])."',
		RemarksIfAny='".$_POST['RemarksIfAny']."',				   
		total_Item_Amount='".$_POST['total_item_amount']."',
		pkg_perc='".$_POST['pkg_perc']."',  
		pkg_charges ='".$_POST['pkg_charges']."', 
		vat_cst='".$_POST['vat_cst']."',  
		vat_cst_val ='".$_POST['taxvalue']."', 
		sur_oct_per='".$_POST['sur_oct_per']."',  
		sur_oct_amt='".$_POST['sur_oct_amt']."',  
		tot_freight='".$_POST['tot_freight']."',  
		add_charges='".$_POST['add_charges']."',  
		grandtotal ='".$_POST['grandtotal']."', 
		round_off='".$_POST['round_off']."'
		WHERE id='".$_REQUEST['mrnid']."' ";	
		$updatemrn_res=DB_query($updatemrn_sql,$db);
		
		$del=DB_query("delete from dbl_addmrnqty where issue_id='".$_REQUEST['mrnid']."'",$db);
		$countval= count($_REQUEST['mst_itemid']);
		for($i=0;$i<$countval;$i++)
		{
			if($_REQUEST['qty_as_per_bill'][$i]!='')
			{				
			$data_arr['issue_id']= $_REQUEST['mrnid'];
			$data_arr['issue_qty']= $_POST['issuebalanc'][$i];				
			$data_arr['mst_itemid']= $_POST['mst_itemid'][$i];
			$data_arr['itemprice']= $_POST['itemrate'][$i];
			$data_arr['POQty']= $_REQUEST['poqty'][$i];
			$data_arr['reciveqty']= $_REQUEST['reciveqty'][$i];
			$data_arr['short_accessid']= $_REQUEST['short_accessid'][$i];
			$data_arr['qty_as_per_bill']= $_REQUEST['qty_as_per_bill'][$i];				
			$data_arr['item_total_rate']= $_REQUEST['item_total_rate'][$i];						
			$insert_mrm=$s->insertRecord('dbl_addmrnqty', $data_arr);
			$ErrMsg = _('An error occurred Record Note Save') . '. ' . _('The error says');
			$insertResult = DB_query($insert_mrm,$db,$ErrMsg);
			/******************** insert update_mrn_qty Po QTY   **************************************/	
			}
		}
		$_SESSION['success_msg']="Update Successfully";
		echo "<script>location='index.php?route=DealerDistrbutions/Inventory/transation/material_receipt_note_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
	 

//**************************Modify Section*******************************************************
if(isset($_REQUEST['mrnid']))
{
	    $condition="id='".$_REQUEST['mrnid']."'";
		$GetData=$s->GetRecord('dbl_addmrn', $condition);
		$GetResultData= DB_query($GetData,$db);
		$GetRowData=DB_fetch_array($GetResultData);
		//echo "<pre>";
		//print_r($GetRowData);
		$MRNNo=$GetRowData['MRNNo'];
		$MRNDate= ($GetRowData['MRNDate']);
		$sitecode=$GetRowData['OrderFromSite'];
		$OrderFromSite=$GetRowData['site'];
		$MRNVoucherDate=($GetRowData['MRNVoucherDate']);
	 	$ReceivedFrom=$GetRowData['ReceivedFrom'];
		$MaterialReceiptOnBasis=$GetRowData['MaterialReceiptOnBasis'];
		 $receiviedagaist=$GetRowData['ReceivedAgainst'];
		$rmrs_no=$GetRowData['rmrs_no'];
		$Type=$GetRowData['Type'];
		$site_name=$GetRowData['site_name'];
	    $PO=$GetRowData['PO_WO_MTO'];
		$BillNo=$GetRowData['BillNo'];
		$Inwardsdate=$GetRowData['Inwardsdate'];
		$DocumentType=$GetRowData['DocumentType'];
		$GetPassNo=$GetRowData['GetPassNo'];
		$Transp_name=$GetRowData['Transp_name'];
		$VehicleNo=$GetRowData['VehicleNo'];
		$TimeIn=$GetRowData['TimeIn'];
		$Timeout=$GetRowData['Timeout'];
		$LRno=$GetRowData['LRno'];
		$LRdate=$GetRowData['LRdate'];
		$TotalWeight=$GetRowData['TotalWeight'];
		$Form_Type=$GetRowData['form_type'];
		$Form_No=$GetRowData['form_no'];
		$gatepassdate=$GetRowData['gatepassdate'];
		$RemarksIfAny=$GetRowData['RemarksIfAny'];
		$OrderDate=$GetRowData['OrderDate'];
	    $StoreCode=$GetRowData['StoreCode'];
		$StoreName=$GetRowData['StoreName'];
		$totalqty=$GetRowData['totalqty'];
		$_REQUEST['chargeamount']=$GetRowData['Charge_Amount'];
		$_REQUEST['tax_chargeamount']=$GetRowData['tax_chargeamount'];
		$_REQUEST['tax_suroct_value']=$GetRowData['tax_suroct_value'];
		$_REQUEST['grandtotal']=$GetRowData['grandtotal'];
		$_REQUEST['percent_inclusive']=$GetRowData['percent_inclusive'];
		$_REQUEST['percent_inclusivecharge']=$GetRowData['percent_inclusivecharge'];
		$_REQUEST['itemamount']=$GetRowData['total_Item_Amount'];
		$_REQUEST['taxable_amt']=$GetRowData['taxable_amt'];
		$_REQUEST['taxname']=$GetRowData['taxname'];
		$_REQUEST['itemamount']=$GetRowData['total_Item_Amount'];
		 if($percent_inclusivecharge=='SUR')
		 {
		  $tax_suroct_value	=($tax_chargeamount*$percent_inclusive)/100;
		 }
		 else{
		  $tax_suroct_value	=(($tax_chargeamount+$total_Item_Amount)*$percent_inclusive)/100;
 		 }
		$Remarks=$GetRowData['Remarks'];}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Material Receipt Note</h1>
<div style="width:auto; overflow:auto; margin-left:auto; margin-right:auto;">
<form action="" method="post">
<br />
<table width="100%"border="0" cellpadding="2" cellspacing="1">
<?php if($_REQUEST['r'])
	{
	   ?>
	     <tr>
		 <td colspan="2" style="color:#FF0000; font-weight:bold">Record Updated Successfully ! </td>
		 </tr>
	   <?php 
	} ?>

</table>
<h3><strong>GENERAL DETAILS</strong></h3>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<?php if(isset($msg_error)){?>
<tr class="row1">
	<td colspan="">
		<table width="50%" border="0"  style="color:#FF0000;font-weight:bold" align="left">
	  	<tr class="row1">
 		<?php for($i=0;$i<count($msg_error);$i++){	
		  if($i%2==0)	
		  echo "</tr><tr>";
 	      echo "<td>".$msg_error[$i]."</td>" ;}?>
		</table>
	</td>
</tr>
<?php }?>
<tr class="row1">
    <td class="tabledata1"><strong>Mrn No.</strong></td>
    <td><input type="text" value="<?php echo $MRNNo;?>" style="width:150px;"/></td>
    <td class="tabledata1"><strong>MRN Date</strong></td>
    <td colspan="2"><input type="text" name="MRNdate" value="<?php echo ConvertSQLDate($MRNDate);?>" style="width:150px"/></td>
</tr>
  <tr class="row1">
   <td class="tabledata1"><strong>Site Code - Name </strong></td>
    <td><?php 
	echo $site;
	
	$ss="Select * from dbl_dealer  where company='".$_SESSION['ACGCompanyId']."' and id='".$OrderFromSite."'";
		  $site_sql=DB_query($ss,$db); ?>
     <select name="sitecode"  id="sitecode" style="width:150px;"    >
     <?php
	 while($site_row=DB_fetch_array($site_sql))
	 {
     ?>
      <option value="<?php echo $site_row['id'];?>"<?php echo ($OrderFromSite==$site_row['id']?'selected':'')?>><?php echo $site_row['dealer_name'];?></option>
      <?php } ?>
    </select></td>
	
    <td class="tabledata1"><strong>Store Code - Name </strong></td>
     <td>	
		<?php
		 $sql_store = "select * from dbl_dealer_store where dealer='".$OrderFromSite."'"; 
		$resultStkLocs = DB_query($sql_store,$db);
		$store_name=DB_fetch_array($resultStkLocs);
		?><input type="text" value="<?php echo $store_name['dealer_store_name'];?>" style="width:150px;" name="storecode" 
		id="storecode"  readonly="readonly"  />	</td>
    </tr>
  
  <tr class="row1">
   <td class="tabledata1"><strong>Received Against</strong></td>
   <?php echo $ReceivedAgainst; ?>
     <td > <select name="receiviedagaist" id="receiviedagaist"	style="width:150px;" onchange="checkdata(this.value)">
    <option value="0">Select</option>
	<option value="DirectPOOrder"<?php if($receiviedagaist=="DirectPOOrder") {?> selected="selected"<?php } ?>>DirectPOOrder</option>
        </select> 	</td>
    <td class="tabledata1" scope="row"><strong>Recevied From </strong></td>
    <td>
	<?php 
	$locationname=$s->getField('locations','loccode',$ReceivedFrom,'locationname');
	?>
	<input type="text" name="vendor" id="vendor" value="<?php echo $locationname;  ?>" readonly="readonly" style="width:150px;" /> 
	<input type="hidden" name="site1" id="site1" value="<?php echo $ReceivedFrom; ?>" />
	 </td>
   </tr>
  <tr class="row1">
    <td class="tabledata1" scope="row"><strong></strong></td>
    <td > </td>
    <td class="tabledata1"><strong>Order Date </strong></td>
    <td><input type="text" value="<?php echo ConvertSQLDate($OrderDate); ?>" style="width:150px;"/>	   </td>
  </tr>
  
  <tr class="row1">
    <td class="tabledata1" scope="row"><strong>Document Type</strong></td>
    <td> <select name="DocumentType" id="DocumentType" style="width:150px;">
	<option value="Challan">Challan</option>
	<option value="Bill">Bill</option>
	</select></td>
    <td class="tabledata1"><strong>Document No. </strong></td>
    <td colspan="2"><input name="billno" type="text" id="billno" style="width:150px;" value="<?php  echo $BillNo; ?>" /></td>
  </tr>
  
  <tr class="row1">
    <td class="tabledata1" scope="row"><strong>Challan / Bill Date</strong></td>
    <td><input type="text" name="MRNVoucherDate" style="width:150px;" class="datepicker"  onblur="valdate();"  value="<?php echo ConvertSQLDate($MRNVoucherDate); ?>"/></td>
    <td class="tabledata1"><strong>Gate Entry no</strong></td>
    <td><input type="text" name="gatepassno"  style="width:150px;" id="gatepassno"  value="<?php echo $GetPassNo;?>" /></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1" scope="row"><strong>Transporter Name</strong></td>
    <td><?php   echo $Transp_name;      ?>	  </td>
    <td class="tabledata1"><strong>Vehicle No.</strong></td>
    <td><input name="vehicalno" type="text" id="vehicalno"  style="width:150px;" value="<?php echo $VehicleNo; ?>"/></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1" scope="row"><strong>In Time</strong></td>
    <td> <select name="timein" id="timein" style="width:150px;">
	<option value="Day" <?php echo $timein=='Day'?'selected':''?>>Day</option>
	<option value="Night" <?php echo $timein=='Night'?'selected':''?>>Night</option>
	</select><br />
	<input type="text" name="timeout" value="<?php if(isset($_SESSION['timeout'])) echo $timeout; else echo date("h:i:s",time());?>" style="width:150px;" ></td>
    <td class="tabledata1"><strong>L R No.</strong></td>
    <td><input name="lrno" type="text" id="lrno" style="width:150px;" value="<?php echo $LRno; ?>"/></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1" scope="row"><strong>L R Date</strong></td>
    <td><input name="lrdate" type="text" id="lrdate" style="width:150px;" value="<?php echo ConvertSQLDate($LRdate); ?>"  onblur="LRdate();" class="datepicker"/></td>
    <td class="tabledata1"><strong>Form Type</strong></td>
    <td> <input type="text" name="form_type"  style="width:150px;" value="<?php echo $Form_Type; ?>" id="form_type" /></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1" scope="row"><strong>Form No</strong></td>
    <td><input type="text" name="form_no"  style="width:150px;" value="<?php echo $Form_No; ?>" id="form_no" /></td>
    <td class="tabledata1"><strong>Get Pass Date</strong></td>
    <td><input type="text" name="gatepassdate" id="gatepassdate" value="<?php echo ConvertSQLDate($gatepassdate); ?>" style="width:150px;" class="datepicker" /></td>
  </tr> 
  <tr class="row1">
    <td class="tabledata1" scope="row"><strong>Remarks If Any</strong></td>
    <td colspan="3"><input name="RemarksIfAny" type="text" id="RemarksIfAny" value="<?php echo $RemarksIfAny;?>" style="width:630px;height:50px;" /></td>
  </tr>  
<tr class="row1">
	<td colspan="4"><h3>Item Details</h3></td>
</tr>
<tr class="row1">
	<td style="text-align:left;" colspan="4">
		<input type="button" onClick="open_items()" value="Add Rows" accesskey="A" class="search" />&nbsp;
		<input type="button" class="search"  onclick="deleteRowOrder('dataTable')" value="Delete" />
	</td>			 
</tr>

<tr class="row1">
	<td class="tabledata1" colspan="4">
	
			<table width="100%" border="1" cellspacing="1" cellpadding="2">
			<tr class="row1">
			<td class="tabledata"><strong>S. NO. </strong></td>
			<td class="tabledata"><strong></strong></td>
			<td class="tabledata"><strong>ITEM CODE </strong></td>
			<td class="tabledata"><strong>DESCRIPTION</strong></td>  
			<td class="tabledata"><strong>UOM</strong></td>
			<td class="tabledata"><strong>QTY AS PER BILL </strong></td>
			<td class="tabledata"><strong>QTY RECEIVED </strong></td>
			<td class="tabledata"><strong>SHORT QUANTITY </strong></td>
			<td class="tabledata"><strong>RATE</strong></td>
			<td class="tabledata"><strong>TOTAL ITEM PRICE</strong></td>
			</tr>
				<tbody id='dataTable'>
			<?php  
			$i=1;
			$sqlQty = "SELECT * From dbl_addmrnqty Where issue_id='".$_GET['mrnid']."'";
			$Qty_result = DB_query($sqlQty, $db);
			while($row2=DB_fetch_array($Qty_result))
			{
			$itemcode=$s->getField('item_master','id',$row2['mst_itemid'],'StockID'); 
			$itemname=$s->getField('item_master','id',$row2['mst_itemid'],'item_name'); 
			?>
			<tr class="row1">
			<td class="tabledata"><input name='delitem[]' type='checkbox' value='' /></td>
			<td class="tabledata"><input type="hidden" id="mstitemid_<?php echo $i; ?>" name="mst_itemid[]" 
			value="<?php echo $row2['mst_itemid'] ?>" /></td>
			<td class="tabledata"><input type='text' name='' id='itemcode_<?php echo $i; ?>' value='<?php echo $itemcode;?>'  readonly /></td>
			<td class="tabledata"><input type='text' name='item_name[]' id='itemname_<?php echo $i; ?>' value='<?php echo $itemname;?>' readonly /></td>
			<td class="tabledata"><input type='text' name='uom[]' id='uom_<?php echo $i; ?>' value='<?php echo $itemname;?>' readonly /></td>
			<td class="tabledata"><input name="qty_as_per_bill[]" type="text" id="qtyasperbill_<?php echo $i?>" 
			value="<?php echo $row2['qty_as_per_bill'];?>" 
			nblur="check_bill_qty(this.value,this.id)"/></td>
			<td class="tabledata"><input name="reciveqty[]" type="text" id="reciveqty_<?php echo $i?>"  
			value="<?php echo $row2['reciveqty'];?>"  /></td>
			<td class="tabledata"><input name="short_accessid[]" type="text" id="shortaccessid_<?php echo $i?>" value="<?php echo $row2['short_accessid'];?>" style="width:100px;" readonly="true" onfocus="calculate_poshort(this.id);" /></td>
			<td class="tabledata"><input name="itemrate[]" type="text" id="itemrate_<?php echo $i?>" value="<?php echo $row2['itemprice'];?>" 
			style="width:90px;" /></td>
			<td class="tabledata"><input name="item_total_rate[]" type="text" id="itemtotalrate_<?php echo $i?>" value="<?php echo $row2['item_total_rate'];?>"   onfocus="calc_total_price(this.id)" style="width:90px;"/></td>
			</tr><?php  $i++; } ?>
			<input type="hidden" name="" value="<?php echo $i; ?>" id="edit_cnt" />
		
			</tbody>
			</table>
	</td>
</tr>

<input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
</table>
<br />
<table width="100%" cellpadding="3" cellspacing="0" border="1">
	<tr class="row1">
		<td width="50%">&nbsp;</td>
		<td width="50%">
				<table width="100%" cellpadding="3" cellspacing="0" border="0">
				<tr class="row1">
                  <td><strong>TOTAL AMOUNT</strong> </td>
                  <td ><input type="text" name="total_item_amount" id="total_item_amount" style="width:150px;" onfocus="calculateBasicAmt()" value="<?php echo $GetRowData['total_Item_Amount'];?>"/></td>
               </tr>
              <tr class="row1">
                  <td><strong>Pkg Charges</strong> </td>
                  <td><input type="text" name="pkg_perc" id="pkg_perc" value="<?php echo $GetRowData['pkg_perc'];?>" style="width:60px" onblur="calcPackaging(this.value)" /><input name="pkg_charges" id="pkg_charges1" type="text" value="<?php echo $GetRowData['pkg_charges'];?>" style="width:90px;" /></td>
                </tr>
                <tr class="row1">
                  <td><strong>Vat/Cst(discription) </strong> </td>
                  <?php
				$Taxsql='SELECT salestax_id,tax_code, taxrate FROM sales_tax_master';
				$Taxresult=DB_query($Taxsql,$db);
     	?>
              <td><select id="taxname" name="vat_cst" onchange="tax_rate1(this.value)" style="width:150px;">
	          <option value="0">---Select---</option>
                  <?php 
			while($Taxmyrow=DB_fetch_array($Taxresult)){
			?>
                  <option  value="<?php echo $Taxmyrow['salestax_id']; ?>"<?php echo ($GetRowData['vat_cst']==$Taxmyrow['salestax_id']?'selected':'')?>><?php echo $Taxmyrow['tax_code']." --- ".$Taxmyrow['taxrate']."%"; ?></option>
                  <?php } ?>
              </select></td>
 
              </tr>
              <tr class="row1">
              	<td><strong>Tax Value</strong></td>
              <td><div id="taxval">
                  <input type="text" name="taxvalue" id="taxvalue" style="width:150px;" value="<?php echo $GetRowData['vat_cst_val'];?>" readonly="true"/>
              </div>

	</td>
                   </tr>
                <tr class="row1">
                  <td><strong>Surcharge Amount</strong> </td>
                  <td><input type="text" name="sur_oct_per" id="sur_oct_per"  style="width:60px;" onchange="calsuroctamt(this.value)" value="<?php echo $GetRowData['sur_oct_per'];?>"/><input name="sur_oct_amt" type="text" id="sur_oct_amt" value="<?php echo $GetRowData['sur_oct_amt'];?>" style="width:90px;" /></td>
                </tr>
                <tr class="row1">
                  <td><strong>Freight</strong> </td>
                  <td><input type="text" name="tot_freight" id="TotalCharge" style="width:150px;" value="<?php echo $GetRowData['tot_freight'];?>"/></td>
                </tr>
				 <td><strong>Loading/Unloading</strong> </td>
                  <td><input type="text" name="loading" id="loading" style="width:150px;" value="<?php echo $GetRowData['loading'];?>"/></td>
                </tr>
                
                <tr class="row1">
                  <td><strong>Additional Charges</strong> </td>
                  <td><input name="add_charges" id="additionalCharges" type="text" value="<?php echo $GetRowData['add_charges'];?>" size="25" style="width:150px;" /></td>
                </tr>
				<!--<tr class="row1">
					<td><strong>Round Off</strong></td>
					<td><input type="text" name="round_off" id="round_off" style="width:100%;"/></td>
				</tr>-->
				
                <tr class="row1">
				<td><strong>Grand Total</strong></td>
                  <td><input type="text" name="grandtotal" id="GrandTotal" onfocus="cal_grandamount()"  size="12" value="<?php echo $GetRowData['grandtotal'];?>"/><input type="hidden"  id="orig_grand_tot" value="<?php echo $GetRowData['grandtotal'];?>"/>&nbsp;<strong>Round Off</strong>&nbsp;<select name="round_off" id="round_off" style="width:50px; height:20px" onchange="calcRounded(this.value)">
				  <option value="">None</option>
				  <option value="+" <?php if($GetRowData['round_off']=='+') echo "selected";?>>+</option>
				  <option value="-" <?php if($GetRowData['round_off']=='-') echo "selected";?>>-</option>
				  </select></td>
                </tr>
				</table>
		</td>
	</tr>
</table>
<p align="center">
    <input type="submit" class="search" name="Update" value="Update" onclick="return confirmPost1()" />
	  <input type="reset" class="search" name="reset" value="Reset" /> <a href="index.php?route=DealerDistrbutions/Inventory/transation/material_receipt_note_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&NewReturn=yes"><input type="button" class="search" value="Cancel" name="Back" /></a></p>
  <?php 
if($Type!='PO'){
	$sqlmax="SELECT max_price FROM direct_mrn_config";
	$resultmax=DB_query($sqlmax,$db); 
	$rowmax=DB_fetch_array($resultmax);
?>

	<input type="hidden" name="max_price" id="max_price" value="<?php echo $rowmax['max_price'];;?>" />
<?php }?>
</form>

</div>



<script language="javascript" type="text/javascript">
function DiscountAmount(DA,i)
{
	var discount_percent = document.getElementById("PerAmo_"+i).value;
	var discount = document.getElementById("Discount_"+i).value;
	var totalPrice = document.getElementById("item_total_rate_"+i).value;
	
	if(discount_percent=='PercentAmt')
	{
		if(discount==''){discount=0;}
		var percent = parseFloat(totalPrice) * parseFloat(discount)/100;
		var subTotal = parseFloat(totalPrice)-parseFloat(percent);
	}
	else if(discount_percent=='Amount')
	{ 
		
	//alert(totalnetrate);
	
		var percent = parseFloat(document.getElementById("Discount_"+i).value);
		if(isNaN(percent)){percent=0;}
		var subTotal = totalPrice-parseFloat(percent);
	
	}
	document.getElementById("perc_"+i).value=percent.toFixed(2);
	document.getElementById("NetRate_"+i).value=subTotal.toFixed(2);
	
	/*Total=parseFloat(Total) + parseFloat(subTotal);
	document.getElementById("Total").value=Total;*/
	//return true;
}
function DiscountAmountspe(DA,i)
{
	var total1=document.getElementById("NetRate_"+i).value;
	var discount1 = document.getElementById("SpecialDiscount_"+i).value;
	var Specialperc=document.getElementById("Specialperc_"+i).value;
	
	if(discount1=='' && Specialperc=='')
	{
		var total1=document.getElementById("NetRate_"+i).value;
		
		document.getElementById("basic_"+i).value=total1;
	
	}
	else
	{
		if(discount1=='' && Specialperc!='')
		{
			var elx=document.getElementById("Specialperc_"+i).value;
			var total1=document.getElementById("NetRate_"+i).value;
			
			var TotalAmount2 = total1-parseFloat(elx);
			
			document.getElementById("basic_"+i).value=TotalAmount2.toFixed(2);
		}
		else
		{
			var percent1 = total1 * parseFloat(discount1)/100;
			var TotalAmount2 = total1-parseFloat(percent1);
	
			document.getElementById("Specialperc_"+i).value=percent1.toFixed(2);
			document.getElementById("basic_"+i).value=TotalAmount2.toFixed(2);
		}
	
	}
}
function checkExcisable(str,i)
{	
	if(str=='NonExcisable')
	{
		document.getElementById("exciseDuty_"+i).disabled=true;
		document.getElementById("ECessPercentage_"+i).disabled=true;
		document.getElementById("ECessValue_"+i).disabled=true;
		document.getElementById("SAHCessPercentage_"+i).disabled=true;
		document.getElementById("SAHCessValue_"+i).disabled=true;
		document.getElementById("taxableamount_"+i).disabled=true;
	}
	else if(str=='Excisable' || str==0)
	{
		document.getElementById("exciseDuty_"+i).disabled=false;
		//document.getElementById("exciseValue_"+i).disabled=false;
		document.getElementById("ECessPercentage_"+i).disabled=false;
		document.getElementById("ECessValue_"+i).disabled=false;		
		document.getElementById("SAHCessPercentage_"+i).disabled=false;	
		document.getElementById("SAHCessValue_"+i).disabled=false;
		document.getElementById("taxableamount_"+i).disabled=false;
	}
	else{
		document.getElementById("exciseDuty_"+i).disabled=true;
		//document.getElementById("exciseValue_"+i).disabled=true;
		document.getElementById("ECessPercentage_"+i).disabled=true;
		document.getElementById("ECessValue_"+i).disabled=true;
		document.getElementById("SAHCessPercentage_"+i).disabled=true;
		document.getElementById("SAHCessValue_"+i).disabled=true;
		document.getElementById("taxable_amt_"+i).disabled=true;
		document.getElementById("taxname_"+i).disabled=true;
		document.getElementById("taxvalue_"+i).disabled=true;
		document.getElementById("sur_oct_perc_"+i).disabled=true;
		document.getElementById("sur_oct_amt_"+i).disabled=true;
		document.getElementById("TotalCharge_"+i).disabled=true;
		document.getElementById("packagingCharges_"+i).disabled=true;
		document.getElementById("additionalCharges_"+i).disabled=true;
		document.getElementById("roundOff_"+i).disabled=true;
		document.getElementById("exciseDuty_"+i).value='';
		//document.getElementById("exciseValue_"+i).value='';
		document.getElementById("ECessPercentage_"+i).value='';
		document.getElementById("ECessValue_"+i).value='';
		document.getElementById("SAHCessPercentage_"+i).value='';
		document.getElementById("SAHCessValue_"+i).value='';
		document.getElementById("taxable_amt_"+i).value='';
		document.getElementById("taxname_"+i).value='';
		document.getElementById("taxvalue_"+i).value='';
		document.getElementById("sur_oct_perc_"+i).value='';
		document.getElementById("sur_oct_amt_"+i).value='';
		document.getElementById("TotalCharge_"+i).value='';
		document.getElementById("packagingCharges_"+i).value='';
		document.getElementById("additionalCharges_"+i).value='';
		document.getElementById("roundOff_"+i).value='';
	}
	document.getElementById("hid_billing_type_"+i).value=str;
}
function claculateExcise(i,str)
{
	
	var discounted=document.getElementById("basic_"+i).value;
	if (str.length==0)
  		{
  			document.getElementById("exciseId_"+i).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_"+i).innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","calculateExcise.php?mrnTaxId="+str+"&data="+i,true);
		xmlhttp.send();
}
function calcExcise(str,i)
{
	var basic=document.getElementById("basic_"+i).value;
	var excise_val=basic*(str/100);
	document.getElementById("exciseValue_"+i).value=excise_val.toFixed(2);
}
function calculateECess(i)
{
	var exciseValue=document.getElementById("exciseValue_"+i).value;
	var ECessPercentage=document.getElementById("ECessPercentage_"+i).value;
	var total=exciseValue*(ECessPercentage/100);
	document.getElementById("ECessValue_"+i).value=total.toFixed(2);
}

function calculateSAHCess(i)
{
	var exciseValue=document.getElementById("exciseValue_"+i).value;
	var SAHCessPercentage=document.getElementById("SAHCessPercentage_"+i).value;
	var total=exciseValue*(SAHCessPercentage/100);
	document.getElementById("SAHCessValue_"+i).value=total.toFixed(2);
}

function caltaxamout(i)
{
	var excise=parseFloat(document.getElementById("exciseDuty_"+i).value);
	if(excise!=0)
	{
	var basic_amount=document.getElementById("exciseValue_"+i).value;
	var ecessamt=document.getElementById("ECessValue_"+i).value;
	var sahamt=document.getElementById("SAHCessValue_"+i).value;
	var totaltax=parseFloat(basic_amount)+parseFloat(ecessamt)+parseFloat(sahamt);	
	document.getElementById('taxableamount_'+i).value=totaltax.toFixed(2);	
	}
	else
	{
	document.getElementById('taxableamount_'+i).value=0;		
	}	
}

function value_after_discount(i)
{
	var basic = parseFloat(document.getElementById('basic_'+i).value);
	if(document.getElementById('taxableamount_'+i).value=='')
	{
	var taxableamt = 0;	
	}
	else
	{
	var taxableamt = parseFloat(document.getElementById('taxableamount_'+i).value);
	}
 	var val_after_disc = parseFloat(basic+taxableamt);
	document.getElementById('total_'+i).value=val_after_disc.toFixed(2);	
}


function calculateExciseDuty(i)
{
	var basic=document.getElementById("basic_"+i).value;
	var ExcisePercentage=document.getElementById("ExcisePercentage_"+i).value;
	var exciseVal=basic*(ExcisePercentage/100);
	document.getElementById("exciseValue_"+i).value=exciseVal.toFixed(2);
}





function calculateBasicAmt()
{
	if(document.getElementById("hiditemcount").value=='')
	var cnt=0;
	else
	var cnt=document.getElementById("hiditemcount").value;
	var cnt1=document.getElementById("edit_cnt").value;
	var cnt2=parseInt(cnt)+(parseInt(cnt1)-1);
	var totalprice=0;
	for(var i=1;i<=cnt2;i++)
	{
	if(document.getElementById("itemtotalrate_"+i))
	{ 
	if(document.getElementById("itemtotalrate_"+i).value!='')
	totalprice=parseFloat(totalprice)+parseFloat(document.getElementById("itemtotalrate_"+i).value);
	else
	totalprice=0;
	}
	else
	{
	continue;
	}
	}
	var amt=totalprice;
	document.getElementById("total_item_amount").value=amt.toFixed(2);
}
	

/*function cal_totalamount()
{	
var total=0;
var t=document.getElementById("count").value;

for(var i=0;i<t;i++)
	{
		var amt=parseFloat(document.getElementById("item_total_rate_"+i).value);
		if(isNaN(amt)){amt=0;}
		total=parseFloat(total)+amt;	
	}	
document.getElementById("total_item_amount").value=total.toFixed(2);	
}*/




function calcPackaging(str)
{
	if(str!='')
	{
		var tota_amt=document.getElementById("total_item_amount").value;
		var pckgng_val=(str/100)*tota_amt;
		document.getElementById("pkg_charges1").value=pckgng_val.toFixed(2);
	}
}

function cal_grandamount()
{
	var total=document.getElementById("total_item_amount").value;
	var sur_oct_amt=document.getElementById("sur_oct_amt").value;
	var TotalCharge=document.getElementById("TotalCharge").value;
	var taxamt=document.getElementById("taxvalue").value;
	var packagingCharges=document.getElementById("pkg_charges1").value;
	var additionalCharges=document.getElementById("additionalCharges").value;	
	if(total==''){total=0;}
	if(sur_oct_amt==''){sur_oct_amt=0;}
	if(TotalCharge==''){TotalCharge=0;}
	if(packagingCharges==''){packagingCharges=0;}
	if(additionalCharges==''){additionalCharges=0;}
	if(taxamt==''){taxamt=0;}
	grandtotal=parseFloat(total)+parseFloat(sur_oct_amt)+parseFloat(TotalCharge)+parseFloat(packagingCharges)+parseFloat(additionalCharges)+parseFloat(taxamt);
	document.getElementById("GrandTotal").value=grandtotal.toFixed(2);	
	document.getElementById("orig_grand_tot").value=grandtotal.toFixed(2);	
}


function calcRounded(str)
{
	var gtotal=document.getElementById('orig_grand_tot').value;
	var gtotal_brk=gtotal.split('.');
	if(str=='+')
	{var final=parseInt(gtotal_brk[0])+1;}
	else if(str=='-')
	{var final=parseInt(gtotal_brk[0]);}
	else if(str=='')
	{var final=gtotal;}
	document.getElementById('GrandTotal').value=final;
}
function tax_rate1(str)
{
	var taxableAmount=document.getElementById("total_item_amount").value;
	var taxableAmount1=document.getElementById("pkg_charges1").value;
	if(taxableAmount1==''){taxableAmount1=0;}
	var taxableAmount2=parseFloat(taxableAmount)+parseFloat(taxableAmount1);
	if (str.length==0)
	{
	document.getElementById("taxval").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("taxval").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","get_tax_valdata.php?taxid="+str+"&amt="+taxableAmount2,true);
	xmlhttp.send();
}


function DiscountAmountdpc(DA,i)
{
	var discount_percent = document.getElementById("Perspc_"+i).value;
	var totalPrice = document.getElementById("NetRate_"+i).value;
	var discount = document.getElementById("special_discount_"+i).value;
	if(discount_percent=='PercentAmt' )
	{
	if(discount==''){discount=0;}
	var percent = parseFloat(totalPrice) * parseFloat(discount)/100;
	var subTotal = parseFloat(totalPrice)-parseFloat(percent);
	}
	else if(discount_percent=='Amount')
	{
	var percent = parseFloat(document.getElementById("special_discount_"+i).value);
	if(isNaN(percent)){percent=0;}
	var subTotal = parseFloat(totalPrice)-parseFloat(percent);
	}
	if(parseFloat(discount)>=100 && discount_percent=='PercentAmt')
	{
	alert("Discount Percentage Should Be Less Then 100");
	document.getElementById("Specialperc_"+i).value='';
	document.getElementById("special_discount_"+i).value='';
	document.getElementById("basic_"+i).value='';
	document.getElementById("special_discount_"+i).focus();
	return false;
	}
	else
	{
	document.getElementById("Specialperc_"+i).value=percent.toFixed(2);
	document.getElementById("basic_"+i).value=subTotal.toFixed(2);
	/*Total=parseFloat(Total) + parseFloat(subTotal);
	document.getElementById("Total").value=Total;*/
	return true;
	}
}
	
function calamt(str)
{
	if(parseFloat(document.getElementById("reciveqty_"+str).value)>parseFloat(document.getElementById("qty_as_per_bill_"+str).value))
	{
		alert("Receive qty should not greater then po qty");
		return false;
	}
	return true;
}


function check_bill_qty(str,id)
{
	var id1=id.split("_");
	var i=id1[1];
	var poqty=parseInt(document.getElementById("poqty_"+i).value);
	str1=parseInt(str);
	if(poqty!='')
	{
	var qty=(poqty*20)/100;
	var poqty1=parseInt(poqty)+parseInt(qty);
	//var poqty2=poqty-qty;	
	if(str1>poqty1)
	{
		alert("Quantity As Per Bill Cannot Be Greater Than 20 % of PO Quantity");
		document.getElementById("qty_as_per_bill_"+id).value='';
		document.getElementById("qty_as_per_bill_"+id).focus();
		return false;
	}
	}
}
  

</script>
<link rel="stylesheet" href="css/jquery.ui.all.css">
<script src="js/jquery-1.5.1.js"></script>
<script src="ui/jquery.ui.core.js"></script>
<script src="ui/jquery.ui.widget.js"></script>
<script src="ui/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="js/dynamicRow.js"></script>
<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>-->
<script src="js/edit_tax_calculation.js"></script>
<script src="js/modify_add_mrn.js"></script>
<!--  To display calender ---->
<script type="text/javascript">

function short_qty(i)
{
	var qty_as_per_bill=parseFloat(document.getElementById('qty_as_per_bill_'+i).value);
	var reciveqty=parseFloat(document.getElementById('reciveqty_'+i).value);
	var short_accessid=parseFloat(qty_as_per_bill-reciveqty);
	document.getElementById('short_accessid_'+i).value=parseFloat(qty_as_per_bill-reciveqty);
	return true;

}
function calc_total_price(str)
{
	var str1=str.split("_");
	var i=str1[1];
	var item_total_rate=0;
	var reciveqty = document.getElementById("reciveqty_"+i).value;
	var itemrate=document.getElementById("itemrate_"+i).value;	
	if(reciveqty!='' && itemrate!='')
	{
		document.getElementById("itemtotalrate_"+i).value=(parseFloat(reciveqty)*parseFloat(itemrate)).toFixed(2);
	}
}

function calculate_short(id)
{
	
	var poqty = parseFloat(document.getElementById('qtyasperbill_'+id).value);
	var x = parseFloat(document.getElementById('reciveqty_'+id).value);
	if(x != '')
	{
		if(poqty >= x)
		{
 				document.getElementById('shortaccessid_'+id).value = parseInt(poqty)- parseInt(x);
				return true;
		}
	  }
	else
	{
		document.getElementById('shortaccessid_'+id).value = 0;
			return true;
	}
}

function openPopupProfile(i)
{
  	window.open("Search_item_MRN.php?rowcount="+i,"profiles_popup_window","height=425,width=640,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
//function value_after_discount(id)
//{
   // var item_total_rate = parseInt(document.getElementById('item_total_rate_'+id).value);
	//var discount_amount = parseInt(document.getElementById('discount_amount_'+id).value);
 	//var val_after_disc = parseInt(item_total_rate-discount_amount);
	//document.getElementById('val_after_disc_'+id).value=val_after_disc;
//}

function cal_total(x)
{
	var dis_amt=document.getElementById("val_after_disc_"+x).value;
	var dis_faight=document.getElementById("freight_"+x).value;
	//alert(dis_amt);
	var Total=parseFloat(dis_amt)+parseFloat(dis_faight);
	document.getElementById("total_amt_"+x).value=Total;
}

function confirmPost1()
{
		var max_price=document.getElementById("max_price").value;
		var grand_price=document.getElementById("GrandTotal").value;
		if(parseFloat(grand_price) > parseFloat(max_price)){
			$alertmasg="Grandtotal not more than Maximum Direct Purchase Allowed Amount - "+max_price;
			alert($alertmasg);
			return false;
		}
return true;
}


function calsuroctamt(str)
{
	var amt=parseFloat(document.getElementById("taxvalue").value);
	var	suroctper=parseFloat(document.getElementById("sur_oct_per").value);
	var amount=(amt*suroctper)/100;
	document.getElementById("sur_oct_amt").value=amount.toFixed(2);	
}

function check_po_qty2(k)
  {
  var id=k.split("_");
  var i=id[1];
reciveqty=parseFloat(document.getElementById("reciveqty_"+i).value);
if(document.getElementById("convfac_"+i).value!='')
   var k=parseFloat(document.getElementById("convfac_"+i).value); 
 else
 var k=0;
 var amt=reciveqty/k;
 document.getElementById("scecondarqty_"+i).value=amt.toFixed(2);
 }


function addRow(tableID) 
{
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var rowCount = table.rows.length;
	r = rowCount+1;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {		
	var newcell = row.insertCell(i);
	//newcell.style.background='#e7e7e7';
	newcell.className='tabledata';
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	//alert(newcell.childNodes[0].type);
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	break;		
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id.split("_");
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id = newcell.childNodes[0].id.split("_");
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id.split("_");
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
	//document.getElementById("stock_perunit_"+rowCount).value='1';
}
	
function deleteRowOrder(tableID) 
{
		try 
		{
			var table = document.getElementById(tableID);
			//alert(table);
			var rowCount = table.rows.length;
			//alert(rowCount);
			for(var i=0; i<rowCount; i++) 
				{
					var row = table.rows[i];
					var chkbox = row.cells[0].childNodes[0];
					if(null != chkbox && true == chkbox.checked) 
					{
						if(rowCount <= 1) 
						{
							alert("Cannot delete all the rows.");
							break;
						}
					table.deleteRow(i);
					rowCount--;
					i--;
					}
					
				}
	var t=document.getElementById("edit_cnt").value;
	if(document.getElementById("hiditemcount").value!='')
	var t1=document.getElementById("hiditemcount").value;
	else
	var t1=0;
	r=parseInt(t1)+parseInt(t)-1;
		document.getElementById("hiditemcount").value=r;		
		}
		catch(e) 
		{
			alert(e);
		}

	}	

function calculate_poshort(id)
{	
var id1=id.split("_");
var i=id1[1];
var qty_per_bill=parseInt(document.getElementById("qtyasperbill_"+i).value);
var reciveqty=parseInt(document.getElementById("reciveqty_"+i).value);	
if(document.getElementById("reciveqty_"+i).value=='')
{
var reciveqty=0;	
}
else
{
var reciveqty=parseInt(document.getElementById("reciveqty_"+i).value);	
}
var short_accessid=qty_per_bill-reciveqty;
if(short_accessid<0){short_accessid=0;}
document.getElementById("shortaccessid_"+i).value=short_accessid;
}


function open_items()
{
window.open("directmrnitems.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=<?php echo $_REQUEST['action']?>","itempopup_window","height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}

</script>