<?php
include('includes/stockcalculation.php');
$CustId=$s->GetFieldValue('dbl_process_sale_order',"id='".$ReqId[0]."'",'customer');
$ReqestedId=$_REQUEST['id'];
$prefixtitle='Dispatch Sale Order';

$ReqId=explode(",",$_REQUEST['id']);

if(isset($_POST['Submit']))
{
			$prefixtype=$s->getField('dbl_prefix_tbl','prefix_name',$prefixtitle,'prefix_code');
			$SQLMax=DB_query("select pick_list_no as QutNo from dbl_dealer_dispatch order by id desc limit 1 ",$db);
			$MaxQut=DB_fetch_array($SQLMax);
			$num=DB_num_rows($SQLMax);
			if($num>0)
			{
			$MXNo=substr($MaxQut['QutNo'],-4);
	        $FinMx=$MXNo+1;
			$FinalCode=$prefixtype."/".str_pad($FinMx,5,0,STR_PAD_LEFT);
			}
			else
			{
			$FinalCode=$prefixtype."/".'00001';
			}				
			 
			$data['pick_list_type']=$_REQUEST['picklisttype'];
			$data['time']=$_REQUEST['time'];
			$data['date']=FormatDateForSQL($_REQUEST['date']);
			$data['pick_list_no']=$FinalCode;
			$data['dispatch_date']=FormatDateForSQL($_REQUEST['dispatch_date']);
			$data['delivery_date']=FormatDateForSQL($_REQUEST['delivery_date']);
			$data['dealer']=$_REQUEST['dealer_id'];
			$data['SOI_no']=$_REQUEST['sopno'];
			$data['order_type']=$_REQUEST['ordertype'];
			$data['SOI_date']=FormatDateForSQL($_REQUEST['soi_date']);
			$data['enquiry_no']=$_REQUEST['enquiry'];
			$data['quotation_no']=$_REQUEST['quotation_no'];  
			$data['zone']=$_REQUEST['zone'];
			$data['subzone']=$_REQUEST['subzone'];
			$data['engineer']=$_REQUEST['engname'];
			$data['customer']=$_REQUEST['customer'];
			//$data['dblstore']=$_REQUEST['dblstore'];
			$data['sales_remarks']=$_REQUEST['sales_remarks'];
			$data['dispatch_remarks']=$_REQUEST['dispatch_remarks'];
			$data['basic_amt']=$_REQUEST['basic_amt'];
	     	$data['discount']=$_REQUEST['discount'];
			$data['discount_Amt']=$_REQUEST['discount_Amt'];
			$data['EDBillRate']=$_REQUEST['EDBillRate'];
			$data['totalDiscountedPrice']=$_REQUEST['totalDiscountedPrice'];
			$data['VatCst']=$_REQUEST['VatCst'];
			$data['VatCstValue']=$_REQUEST['VatCstValue'];
			$data['surcharge']=$_REQUEST['surcharge'];
			$data['surcharge_Amt']=$_REQUEST['surcharge_Amt'];
			$data['ecess_per']=$_REQUEST['ECessPercentage'];
			$data['sah_per']=$_REQUEST['SAHCessPercentage'];
			$data['ecess_value']=$_REQUEST['ECessValue1'];
			$data['sah_value']=$_REQUEST['SAHCessValue1'];
			$data['totalexcise']=$_REQUEST['totalexcise'];
			$data['discountAmt']=$_REQUEST['discountAmt'];
			$data['additional_chrg']=$_REQUEST['additional_chrg'];
			$data['grand_tatal']=$_REQUEST['grand_tatal'];
			$data['term_and_condition']=$_REQUEST['term_and_condition'];
			$data['validity']=FormatDateForSQL($_REQUEST['validity']);
			$data['payment_term']=$_REQUEST['payment_term'];
			$data['consignee_code']=$_REQUEST['consignee_code'];
			$data['company']=$_SESSION['ACGCompanyId'];	
	
	$s->RecordInsert('dbl_dealer_dispatch', $data);//RECORD INSERTED INTO PARANT TABLE
	$masterId=DB_Last_Insert_ID($db,'dbl_dealer_dispatch','id');
	
	if($masterId!='')
	{
	for($j=0;$j<count($_REQUEST['mst_itemid']);$j++)
	{
	if($_REQUEST['dispatch'][$j]!='' && $_REQUEST['dispatch'][$j]!='0'){
	$dataArr1['parentId']=$masterId;	  
	$dataArr1['mst_itemid']=$_REQUEST['mst_itemid'][$j];
	$dataArr1['order_qty']=$_REQUEST['orderqty'][$j];
	$dataArr1['dispatch_qty']=$_REQUEST['dispatch'][$j];
	$dataArr1['rate']=$_REQUEST['rate'][$j];
	$dataArr1['dblstore']=$_REQUEST['dblstore'][$j];
	$dataArr1['sub_amt']=$_REQUEST['netrate'][$j];
	$dataArr1['discount_type']=$_REQUEST['PerAmo'][$j];
	$dataArr1['discount']=$_REQUEST['Discount'][$j];
	$dataArr1['total_amt']=$_REQUEST['total'][$j]; 
	$dataArr1['Exciseid']=$_REQUEST['EDBillRate'][$j]; 
	$dataArr1['Excisamt']=$_REQUEST['totalDiscountedPrice'][$j]; 
	$dataArr1['ECessPercentage']=$_REQUEST['ECessPercentage'][$j];
	$dataArr1['ECessValue']=$_REQUEST['ECessValue1'][$j];
	$dataArr1['SAHCessPercentage']=$_REQUEST['SAHCessPercentage'][$j];
	$dataArr1['SAHCessValue']=$_REQUEST['SAHCessValue1'][$j];
	$dataArr1['excise_amt']=$_REQUEST['exciseamt'][$j]; 
	$dataArr1['grnid']=$_REQUEST['grn'][$j];
	$dataArr1['final_amt']=$_REQUEST['totalamt'][$j]; 
	
	
	$s->RecordInsert('dbl_dealer_dispatch_details', $dataArr1);//RECORD INSERTED INTO CHIELD TABLE
	
	if($_REQUEST['stock'][$j]==$_REQUEST['dispatch'][$j] && $_REQUEST['grn'][$j]!='')
	{
	$update2="Update dbl_addmrnqty set dispatch_status=1 where issue_id='".$_REQUEST['grn'][$j]."' and 
	mst_itemid='".$_REQUEST['mst_itemid'][$j]."'";
	}
	$updateResult1=DB_query($update2,$db);
	
	
	}
	}
	
	for($CR=0;$CR<count($_REQUEST['itemid1']);$CR++)
	{
		if($_REQUEST['orderqty1'][$CR]!='')
		{
			if($_REQUEST['prqty1'][$CR]==$_REQUEST['orderqty1'][$CR])
			{
				$pendingqty=0;
				$update=DB_query("update dbl_process_sale_order_details set pendingQty='".$pendingqty."',status=1
				where id='".$_REQUEST['rowid'][$CR]."'",$db);
			}
			else if($_REQUEST['prqty1'][$CR]>$_REQUEST['orderqty1'][$CR])
			{
				$pendingqty=$_REQUEST['prqty1'][$CR]-$_REQUEST['orderqty1'][$CR];
				$update=DB_query("update dbl_process_sale_order_details set pendingQty='".$pendingqty."',status=0
				where id='".$_REQUEST['rowid'][$CR]."'",$db);
			}
		}
		
	}//EXIT OF FOR LOOP	
	
	
	
	
	

	
	$updateResult1=DB_query($update1,$db);
	}
	
	
	for($y=1;$y<$_REQUEST['hiditemcount'];$y++)
	{
	if($_REQUEST['chargeid'][$y]!=''){
	$data2['parentId']=$masterId;
	$data['chargeid']=$_REQUEST['chargeid'][$y]; 
	 	
	$s->RecordInsert('dbl_dealer_dispatch_charge', $data2);//RECORD INSERTED INTO CHIELD TABLE
	}
	}
	
	
	/*********** check dbl_enquiry_details item status  ******************/
		
	
	//echo "select * from dbl_enquiry_details where parent_id in(".$_REQUEST['id'].")";
	
	
	for($z=0;$z<count($ReqId);$z++)
	{
	$count1=0;
		$sql=DB_query("select * from dbl_process_sale_order_details where parentid='".$ReqId[$z]."'",$db);
		$num=DB_num_rows($sql);
		while($row1=DB_fetch_array($sql))
		{
			if($row1['status']==1)
			{
			$count1=$count1+1;	
			}
		}
		
		
		if($count1==$num)
		{
			$update=DB_query("update dbl_process_sale_order set status=1 where id='".$ReqId[$z]."'",$db);
		}	
		}
	
	
	
	$s->pageLocation('index.php?route=DealerDistrbutions/Transaction/dispatch_processsaleorder_index_data&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');

}



$sql=DB_query("select * from dbl_process_sale_order where id in('".$_REQUEST['id']."')",$db);
$row1=DB_fetch_array($sql);
extract($row1);
if(isset($_SESSION['dealerid']))
$function_filter[]=$_SESSION['dealerid'];
else
$function_filter[]=$dealer;
$function_filter[]='';
$function_filter[]=$SOI_date; 
$function_filter[]=$SOI_date; 





 


?>



<link rel="stylesheet" href="css/jquery.ui.all.css">
<link rel="stylesheet" href="css/new_mxerp.css"><h2 align="center"><strong><?php echo $s->getField('companies','CompanyId',$_SESSION['ACGCompanyId'],'CompanyName'); ?>
</strong></h2>
<script src="ui_new/jquery.ui.timepicker.js"></script>	

<h3 align="center" style="text-align:center; font-size:14PX;"><strong>Dispatch Sale Order</strong></h3>
<br />
<form name="form1" action="" method="post" >
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr>
    <th width="13%" align="left" valign="top"><strong>Pick List Type</strong></th>
    <td width="20%" align="left" valign="top">	
	<select name="picklisttype" id="picklisttype" style="width:100%">
	 <option value="0">--Select--</option>
<option value="Without Invoice"<?php if($picklisttype=='Without Invoice'){?>selected="selected" <?php } ?>>Without Invoice</option>
	<option value="With Invoice"<?php if($picklisttype=='With Invoice'){ ?> selected="selected" <?php } ?> >With Invoice </option>
	 </select></td>
    <th width="13%" align="left" valign="top">Date & Time</th>
	<td width="20%">
	<input type="text" name="date" value="<?php echo date('d/m/Y') ?>" id="date" class="datepicker" style="width:60%;" /> 
	<input type="text" name="time" id="time" class="timepicker" style="width:30%;" value="<?php echo $date = date('h:i', time()); ?>" />	</td>
<th width="13%" align="left" valign="top">Pick List No  </th>
<td width="20%"><input name="picklistno" type="text" style="width:100%"   id="picklistno"
	value="Auto Generated" readonly="readonly" /></td>
</tr>

<tr>
    <th width="13%" align="left" valign="top"><strong>Customer Name</strong></th>
    <td width="20%" align="left" valign="top"><select name="customer" id="customer" style="width:100%" >
      <?php 

	$sql=DB_query("select * from clientmaster where id='".$customer."'",$db);
	while($row1=DB_fetch_array($sql))
	{
	$c='';
	if($row1['id']==$customer)
	$c='selected';
	?>
      <option value="<?php echo $row1['id']; ?>"<?php echo $c; ?>><?php echo $row1['c_name']; ?></option>
      <?php } ?>
    </select></td>
    <th width="13%" align="left" valign="top">Customer Code</th>
	<td width="20%"><input name="customer_code" id="customer_code" type="text" style="width:100%" 
	value="<?php echo $s->getField('clientmaster','id',$customer,'client_code'); ?>"   readonly="readonly" /></td>
<th width="13%" align="left" valign="top">Category</th>
<td width="20%"><input name="category" type="text" style="width:100%"   id="category"
	value="<?php echo $category; ?>" readonly="readonly" /></td>
</tr>
<tr>
<th align="left" valign="top">Address</th>
<td align="left" valign="top" ><input name="address" id="address" type="text" style="width:100%"  value="<?php echo $s->getField('clientmaster','id',$customer,'pre_address1'); ?>" /></td>
<?php $precity=$s->getField('clientmaster','id',$customer,'pre_city');  ?> 
    <th align="left" valign="top">City</th>
    <td><input name="address2" id="address2" type="text" style="width:100%"  value="<?php echo $s->getField('city_master','id',$precity,'city'); ?>" /></td>
	<?php $pre_state=$s->getField('clientmaster','id',$customer,'pre_state');  ?> 
    <th align="left" valign="top">State</th>
    <td><input name="address3" id="address3" type="text" style="width:100%"  value="<?php echo $s->getField('state_master','id',$pre_state,'state'); ?>" /></td>
</tr>
<tr>
	<?php $pre_country=$s->getField('clientmaster','id',$customer,'pre_country');  ?> 

       <th align="left" valign="top">Country</th>
       <td align="left" valign="top" ><input name="country" id="country" type="text" style="width:100%"
  value="<?php echo $s->getField('country_master','id',$pre_country,'country'); ?>" /></td>


    
    <th align="left" valign="top">Quotation No </th>
    <td><input name="quotation_no" id="quotation_no" type="text" style="width:100%" value="<?php if(isset($quotation_no)) { echo $quotation_no; } ?>"  /></td>
	
    <th align="left" valign="top">Consignee Code</th>
    <td><input name="consignee_code" id="consignee_code" type="text" style="width:100%"
  value="<?php if(isset($consignee_code)) { echo $consignee_code; } ?>" /></td>
</tr>



<tr>
    <th width="13%" align="left" valign="top"><strong>Order Type</strong></th>
    <td width="20%" align="left" valign="top">	
	<select name="ordertype" id="ordertype" style="width:100%">
	 <option value="0">--Select--</option>
	 <option value="Exciable"<?php if($order_type=='Exciable'){ ?> selected="selected" <?php } ?> ?>Exciable </option>
	  <option value="Non Exciable"<?php if($order_type=='Non Exciable'){ ?> selected="selected" <?php } ?> ?>Non Exciable </option>
	 </select></td>
    <th width="13%" align="left" valign="top">Enquiry No</th>
	<td width="20%"><input type="text" name="enquire_no" id="enquiry_no" style="width:100%" 
	value="<?php echo $s->getField('dbl_enquiry_form','id',$enquiry_no,'form_no'); ?>" readonly="readonly" />
	<input type="hidden" name="enquiry" value="<?php echo $enquiry_no; ?>" />	</td>
<th width="13%" align="left" valign="top">Enquiry Date</th>
<td width="20%"><input name="enquiry_date" type="text" style="width:100%"   id="enquiry_date"
	value="<?php echo ConvertSQLDate($s->getField('dbl_enquiry_form','id',$enquiry_no,'date')); ?>" readonly="readonly" /></td>
</tr>

<tr>
<th align="left" valign="top"><strong> Dealer Name</strong></th>
<td align="left" valign="top"><input name="areaname" type="text" style="width:100%" id="areaname"
value="<?php echo $s->getField('dbl_dealer','id',$dealer,'dealer_name'); ?>" readonly="readonly"  />
<input name='dealer_id' type='hidden' style='width:100%' id='dealer_id' value="<?php echo $dealer; ?>"  /></td>
<th align="left" valign="top">SOI No</th>
<td><input name="soi_no" type="text" style="width:100%" id="soi_no" 
value="<?php if(isset($SOI_no)) { echo $SOI_no; } else { "Auto Generate"; }  ?>"  readonly="readonly"/>
<input type="hidden" name="sopno" value="<?php echo $id; ?>" /></td>
<th align="left" valign="top">SOI Date</th>
 <td><input name="soi_date" id="soi_date" type="text" style="width:100%"  value="<?php echo ConvertSQLDate($SOI_date); ?>"  class="datepicker" /> </td>
</tr>

<tr>
<th align="left" valign="top"><strong> Area Name</strong></th>
<td align="left" valign="top"><input name="areaname" type="text" style="width:100%" id="areaname"
value="<?php echo $s->getField('zone_master','id',$zone,'ZoneTitle'); ?>" readonly="readonly"  />
<input name='zone' type='hidden' style='width:100%' id='zone' value="<?php echo $zone; ?>"  /></td>
<th align="left" valign="top">Sub Area Name</th>
<td><input name="subareaname" type="text" style="width:100%" id="subareaname" 
value="<?php echo $s->getField('subzone_master','id',$subzone,'subzone'); ?>" readonly="readonly" />
<input name='subzone' type='hidden' style='width:100%' id='subzone' value="<?php echo $subzone; ?>" /></td>
<th align="left" valign="top">Engineer Name</th>
 <td><input name="engineer_name" id="engineer_name" type="text" style="width:100%"  value="<?php echo $s->getField('dbl_engineer_dealer_maste','id',$engineer,'engineer_name'); ?>"  readonly="readonly" />
 <input name='engname' type='hidden' style='width:100%' id='engname' value="<?php echo $engineer; ?>"  /></td>
</tr>
<tr>
 <th align="left" valign="top"> Delivery Date  </th>
 <td align="left" valign="top" ><input name="delivery_date" id="delivery_date" type="text" style="width:100%" value="<?php if(isset($delivery_date)) { echo ConvertSQLDate($delivery_date); } ?>"  class="datepicker" /></td>
 <th align="left" valign="top">Dispatch Date </th>
 <td><input name="dispatch_date" id="dispatch_data" type="text" style="width:100%"  class="datepicker" /></td>
 <th align="left" valign="top"></th>
 <td>
 </td>   
</tr>





<tr>
    <th align="left" valign="top"><strong>Sales Remarks</strong></th>
    <td colspan="5" align="left" valign="top"><textarea name="sales_remarks" id="sales_remarks"  style="width:100%"><?php echo trim($sales_remarks); ?></textarea></td>
    </tr>
	<tr>
    <th align="left" valign="top"><strong>Dispatch Remark</strong></th>
    <td colspan="5" align="left" valign="top"><textarea name="dispatch_remarks" id="dispatch_remarks"  style="width:100%"><?php echo trim($dispatch_remarks); ?></textarea></td>
    </tr>
</table> 
<br />
<div style="width:973px; height:auto; overflow:scroll;">
<table width="100%" border="1" cellpadding="3" cellspacing="0">

<?php
//$i=0;
$sql=DB_query("SELECT DISTINCT(mst_itemid) FROM dbl_process_sale_order_details WHERE parentid IN(".$_REQUEST['id'].") AND STATUS=0",$db);
$arr12='';
while($row=DB_fetch_array($sql))
{
$arr12[]=$row['mst_itemid'];
}
//print_r($arr12);

for($i=0;$i<count($arr12);$i++)
{//echo $arr12[$i];
$k=0;

$itemcode=$s->getField('item_master','id',$arr12[$i],'StockID');
$itemname=$s->getField('item_master','id',$arr12[$i],'item_name');
$unit=$s->getField('item_master','id',$arr12[$i],'mst_unitid');
$unitname=$s->getField('unitsofmeasure','unitid',$unit,'unitname'); 

$op=openingBalance($function_filter,$arr12[$i]);
$in_1=receivedMRN($function_filter,$arr12[$i]);
$out_1=issueSlip($function_filter,$arr12[$i]);
$out_2=issueSiteToSite($function_filter,$arr12[$i]);		
$in_2=receivedSiteToSite($function_filter,$arr12[$i]);
$out_3=issueMatarialReturn($function_filter,$arr12[$i]);
$in_3=receivedReturn($function_filter,$arr12[$i]);	
$out_4=Rgpchallan($function_filter,$arr12[$i]);
$out_5=Nrgp($function_filter,$arr12[$i]);
$in_4=Rgpreturn($function_filter,$arr12[$i]);	 
$in_5=FinishedMR($function_filter,$arr12[$i]);
$out_6=dispatchsaleorder($function_filter,$arr12[$i]);
$invalue=($op+$in_1+$in_2+$in_3+$in_4+$in_5);
$outvalue=($out_1+$out_2+$out_3+$out_4+$out_5+$out_6);
$tot=$invalue-$outvalue;
$stockbalance=$tot;	

?>
<tr class="row1">
	<td class="tabledata"  style="white-space:nowrap;"><strong>
	  <?php echo $itemcode?></strong></td>
	<td class="tabledata" style="white-space:nowrap;"><strong>
	  <?php echo $itemname; ?></strong></td>
	<td class="tabledata"><strong><?php echo $unitname; ?></strong></td>
	<td class="tabledata"><strong>Store</strong></td>
	<td class="tabledata"><strong>GRN</strong></td>
	<td class="tabledata"><strong>Stock Qty</strong></td>
	<td class="tabledata"><strong>Order Qty</strong></td>
	<td class="tabledata"><strong>Dispatch Qty</strong></td>
	<td class="tabledata"><strong>Rate</strong></td>
	<td class="tabledata"><strong>Sub Amt</strong></td>
	<td class="tabledata"><strong>Discount Type</strong></td>
	<td class="tabledata"><strong>Discount(%)</strong></td>
	<td class="tabledata"><strong>Amt</strong></td>
	<td class="tabledata"><strong>Excise</strong></td>
    <td class="tabledata"><strong>E-Cess</strong></td>
    <td class="tabledata"><strong>SAH-E Cess</strong></td>
	<td class="tabledata"><strong>Excise Amt</strong></td>
	<td class="tabledata"><strong>Total Amt</strong></td>
</tr>
<?php 
$DB1SQL=DB_query("SELECT * FROM dbl_process_sale_order_details where mst_itemid='".$arr12[$i]."' and parentid IN(".$_REQUEST['id'].") AND STATUS=0",$db); 
$stock1='';
$prqty='';
$tk=0;
while($GetRec1=DB_fetch_array($DB1SQL))
{
$rowtype=$s->getField('item_master','id',$GetRec1['mst_itemid'],'item_exciable');

$rate=$GetRec1['rate'];
if($rowtype=='exciable')
{
$exciseamt12=$exciseamt12+$GetRec1['amount'];
}
?>
<tr class="row1">
	<td><?php echo $s->GetFieldValue('dbl_process_sale_order',"id='".$GetRec1['parentid']."'",'SOI_no'); ?></td>
	<td><?php echo ConvertSQLDate($s->GetFieldValue('dbl_process_sale_order',"id='".$GetRec1['parentid']."'",'SOI_date')); ?></td>
	<td>&nbsp;</td>
	
	<input type="hidden" name="itemid1[]" id="itemid1" value="<?php echo $arr12[$i]; ?>" />
	<input type="hidden" name="rowid[]" id="rowid" value="<?php echo $GetRec1['id']; ?>" />
	<input type="hidden" name="EnqiId[]" id="EnqiId" value="<?php echo $GetRec1['parent_id']; ?>" />
	<input type="hidden" name="type[]" id="type1_<?php echo $i; ?>" value="<?php echo $rowtype; ?>" readonly="readonly" />
	</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><?php echo $stockbalance; $stock1[]=$stockbalance;	?></td>
	<td><?php $prqty[]=$GetRec1['pendingQty']; ?><input type="text" name="prqty1[]" value="<?php echo $GetRec1['pendingQty']; ?>" id="prqty1_<?php echo $i; ?><?php echo $k; ?>" size="6" style="width:100%;" readonly="readonly"/>
	</td>
	<td><input type="text" name="orderqty1[]" id="orderqty1_<?php echo $i; ?><?php echo $k; ?>" size="9"  value="<?php echo $row['order_qty']; ?>" onkeyup="caldispatch(<?php echo $i; ?>,<?php echo $k; ?>)" /></td>
	
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
</tr>
<?php $k++;$tk++;} ?>
<input type="hidden" name="count11" id="count11_<?php echo $i; ?>" value="<?php echo $tk; ?>" />
<tr class="row1">
	<td class="tabledata1" style="text-align:right;" colspan="3"><strong>Total</strong></td>
	<td class="tabledata1"><select name="dblstore[]" id="dblstore_<?php echo $i; ?>" style="width:70px;" onchange="showgrn(this.value,<?php echo $i; ?>)">
 <option value="0">Select</option>
<?php
$sql1=DB_query("select * from dbl_dealer_store where dealer='".$dealer."'",$db);
while($dbrow=DB_fetch_array($sql1))
{ 
?>
<option value="<?php echo $dbrow['id']; ?>"><?php echo $dbrow['dealer_store_name']; ?></option>
 <?php } ?>
 </select></td>
 <td class="tabledata1"><select name="grn[]" id="grn_<?php echo $i; ?>" style="width:70px;"
  onchange="showexciseamt1(this.value,<?php echo $i; ?>)">
 <option value="0">Select</option>

 </select></td>
	<td class="tabledata1"><input type="text" name="stock[]" id="stock_<?php echo $i; ?>" value="<?php echo $stockbalance; ?>" readonly="readonly" /><input type="hidden" name="mst_itemid[]" id="mst_itemid_<?php echo $i; ?>" value="<?php echo $arr12[$i]; ?>" />
	</td>
	<td class="tabledata1"><?php echo $prsum=array_sum($prqty); ?>
	<input type="hidden" name="orderqty[]" id="prqty" value="<?php echo $prsum;?>" />
	</td>
	<td class="tabledata1"><input type="text" name="dispatch[]" id="orderqty_<?php echo $i; ?>" size="9"  value="<?php echo $row['order_qty']; ?>" onkeyup="caldispatch(<?php echo $i; ?>,<?php echo $k; ?>)" readonly />
		
	</td>
	<td class="tabledata1"><input type="text" name="rate[]"  id="rate_<?php echo $i; ?>" size="9"  onkeyup="calamt(this.value,<?php echo $i; ?>)" value="<?php echo $rate; ?>" readonly  /></td>
	<td class="tabledata1"><input type="text" name="netrate[]"  id="netrate_<?php echo $i; ?>" size="9" value="<?php echo $row['sub_amt']; ?>" /></td>
	<td class="tabledata1"><select style="width:80px;" name="PerAmo[]" id="PerAmo_<?php echo $i; ?>">
<option value="Amount" <?php if($row['discount_type']=='Amount') { ?> selected="selected" <?php } ?> >Amount</option>
<option value="PercentAmt" <?php if($row['discount_type']=='PercentAmt') { ?> selected="selected" <?php } ?>>Percent Amt</option>
</select></td>
	<td class="tabledata1"><input type="text"  name="Discount[]" id="Discount_<?php echo $i; ?>" onblur="DiscountAmount(this.value,<?php echo $i; ?>)" style="width:80px;" value="<?php echo $row['discount_per']; ?>" /></td>
	<td class="tabledata1"><input type="text" name="total[]" id="total_<?php echo $i; ?>" style="width:50px;"  readonly="true"
value=""   onblur="showexciseamt(<?php echo $i; ?>)" /></td>
	<td class="tabledata1"><select name="EDBillRate" id="EDRate_<?php echo $i; ?>"   style="width:150px;" onchange="calculateBillExcise(this.value)"  >
				<option value="0">--Select--</option>
				</select>
				
				<input name="totalDiscountedPrice[]" id="totalDiscountedPrice_<?php echo $i; ?>" 
				type="text"  style="width:100%;" onfocus="calculateTotalExcise(<?php echo $i; ?>)"/></td>
 <td class="tabledata">
 <?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++;
			} 
			?>
			<input name="ECessPercentage[]" id="ECessPercentage_<?php echo $i; ?>" type="text" value="<?php echo $rate[1];?>" style="width:60px;" readonly />
						<input name="ECessValue1[]" id="ECessValue_<?php echo $i; ?>" type="text" value="<?php echo $ECessValue?>" style="width:60px;" readonly onfocus="calculateECess1(<?php echo $i; ?>)"/></td>	
 <td class="tabledata1"><input name="SAHCessPercentage[]" id="SAHCessPercentage_<?php echo $i; ?>" type="text" value="<?php echo $rate[2];?>" style="width:60px;" readonly/>
						<input name="SAHCessValue1[]" id="SAHCessValue_<?php echo $i; ?>" type="text" value="<?php echo $SAHCessValue?>" style="width:60px;" readonly onfocus="calculateSAHCess1(<?php echo $i; ?>)"/>
						</td>									
 <td class="tabledata1"><input type="text" name="exciseamt[]" value="<?php ?>" id="exciseamt_<?php echo $i; ?>" style="width:50px;" readonly="readonly"  /></td>
	<td class="tabledata1"><input type="text" name="total[]" id="totalamt_<?php echo $i; ?>" style="width:50px;"  readonly="true"
value="<?php echo $row['total']; ?>" onfocus="showtotalamt(<?php echo $i; ?>)" /></td>
 
</tr>


<?php  }?>
<input type="hidden" name="count" id="count" value="<?php echo $i; ?>" />

</table>
</div>

<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td width="50%" valign="top">
		<table width="100%" border="0" cellpadding="3" cellspacing="0">
			<tr class="row1">
				<td class="tabledata1">Term & Conditions</td>
				<td><input type="text" name="term_and_condition" id="term_and_condition" value="<?php echo $term_and_condition; ?>" style="width:150px;" /></td>
			</tr>
			<tr class="row1">
				<td class="tabledata1">Validity</td>
				<td><input type="text" name="validity" id="validity" value="<?php echo ConvertSQLDate($validity); ?>" class="datepicker" style="width:150px;" /></td>
			</tr>
			<tr class="row1">
				<td class="tabledata1">Payment Terms</td>
				<td><select name="payment_term" id="payment_term" style="width:150px;">
						<option value="">Select</option>
						<?php $SQLP=DB_query("SELECT * FROM paymentterms",$db);
						while($Payment=DB_fetch_array($SQLP)) {
						$selA='';
						if($payment_term==$Payment['termsindicator'])
						$selA='selected';
						?>
						<option value="<?php echo $Payment['termsindicator']; ?>" <?php echo $selA; ?>><?php echo $Payment['terms']; ?></option>
						<?php } ?>
					</select>
				</td>
			</tr>
		</table>
	</td>
	<td width="50%" valign="top">
		<table width="100%" border="1" cellpadding="3" cellspacing="0">
			<tr class="row1">
				<td class="tabledata1">Basic Amount</td>
				<td><input type="text"  name="basic_amt" id="basic_amt" style="width:70px;" 
				 onfocus="calamount()"  value="<?php //echo $basic_amt; ?>" readonly /></td>
			</tr>
			<tr class="row1">
				<td class="tabledata1">Discount</td>
				<td><input type="text" name="discount" id="discount" value="<?php echo $discount; ?>" style="width:60px;" readonly />
				<input type="text" name="discount_Amt" id="discountAmt" onfocus="caldiscountAmt()" value="<?php //echo $discount_Amt; ?>" style="width:90px;" readonly="readonly" />
				</td>
			</tr>
			<tr class="row1">
				<td class="tabledata1">Discounted Amount </td>
				<td><input type="text" name="discountAmt" id="discount_Amt"  value="<?php echo $discountAmt; ?>" style="width:150px;" readonly="readonly" />
				</td>
			</tr>
			<tr class="row1">
			<td class="tabledata1">P & F  </td>
				<td><input type="text" name="p_and_f" id="p_and_f" value="<?php echo $p_and_f; ?>" style="width:60px;" readonly />
				<input type="text" name="pnf_amt" id="pnf_amt" onfocus="calpnfAmt()" value="<?php //echo $pnf_amt; ?>"  style="width:90px;" readonly="readonly" />
				</td>
			</tr>
			<tr class="row1">
			<td class="tabledata1">Excise Item Total Amount </td>
				<td><input type="text" name="excise_value" id="excise_value" value="<?php echo $exciseamt12; ?>" readonly />
				</td>
			</tr>
			<tr class="row1"><?php 
			if($exciseamt12!=0)
			{
			$exciseamt_dis=($exciseamt12*$discount)/100;
			$exciseamt_dis1=$exciseamt12-$exciseamt_dis;
			} ?>
			<td class="tabledata1">Excise Item After Discount </td>
				<td><input type="text" name="excise_value" id="excise_value1" value="<?php echo $exciseamt_dis1; ?>" readonly />
				</td>
			</tr>
			
			<tr class="row1">
				<td class="tabledata1">Excise Duty</td>
				<td>
				<select name="EDBillRate" id="EDRate" style="width:150px" onchange="calculateBillExcise(this.value)"  >
						<option value="0">--Select--</option>
						<?php
						$ServiceSql="select * from  excise_tax WHERE caption LIKE '%excise%'" ;
						$ServiceRes=DB_query($ServiceSql,$db);
						while($ServiceRow=DB_fetch_array($ServiceRes))
						{
						$selA11='';
						if($EDBillRate==$ServiceRow['id'])
						$selA11='selected';
						?>
			<option value="<?php echo $ServiceRow['id'];?>" <?php echo $selA11; ?>><?php echo $ServiceRow['caption']?></option>
						<?php }	?>
						</select>
			<input name="totalDiscountedPrice" id="totalDiscountedPrice" type="text" 
			value="<?php echo $totalDiscountedPrice; ?>"  style="width:90px;" 
			onfocus="calculateTotalExcise()" readonly="readonly"/>
						</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>
					<td class="tabledata1">E-Cess</td>
					<td><input name="ECessPercentage" id="ECessPercentage" type="text" value="<?php echo $rate[1];?>" style="width:60px;" readonly />
						<input name="ECessValue1" id="ECessValue" type="text" value="<?php echo $ecess_value?>" style="width:120px;" readonly onfocus="calculateECess1()" /></td>
					</tr>
			<tr >
					<td class="tabledata1">SAH-E Cess</td>
					<td><input name="SAHCessPercentage" id="SAHCessPercentage" type="text" value="<?php echo $rate[2];?>" style="width:60px;" readonly/>
						<input name="SAHCessValue1" id="SAHCessValue" type="text" value="<?php echo $sah_value?>" style="width:120px;" readonly onfocus="calculateSAHCess1()" /></td>
					</tr>
					
				<tr>
				<td class="tabledata1">Total Excise Amout</td>
<td><input name="totalexcise" id="totalDiscountedamount" type="text" value="<?php echo $totalexcise; ?>" style="width:120px;" readonly onfocus="caltotalexciseamt()" /></td>
				
				</tr>	
			
			
			<tr class="row1">
				<td class="tabledata1">Sales Tax</td>
				<td><select name="VatCst" id="value3" onchange="calculateVATCST(this.value)" style="width:150px;">
						<option value="" >Select</option>
						<?php 
						$salesSql=DB_query("Select * from sales_tax_master",$db);
						while($salesRow=DB_fetch_array($salesSql))
						{?>
						<option value="<?php echo $salesRow['salestax_id'];?>" <?php echo $VatCst==$salesRow['salestax_id']?'selected':''?>><?php echo $salesRow['tax_code'];?></option>
						<?php 	
						}
						?>
						</select>
						<div id="VATId"><input name="VatCstValue" id="VatCstValue" type="text" value="<?php echo $VatCstValue?>" style="width:150px;" readonly  onfocus="calculateEditVATCST()"  /></div>
						<?php $rate2=$s->getField('sales_tax_master','salestax_id',$VatCst,'taxrate'); ?>
						<input type="hidden" name="" id="rate" value="<?php echo $rate2;?>" /> </td>
			</tr>
			<tr class="row1">
				<td class="tabledata1">Incidental Charges</td>
				<td><input type="text" name="Inc_chrg" id="Inc_chrg" value="<?php echo $Inc_chrg; ?>" style="width:60px;" onfocus="calincidentalchrg(this.value)" />
					<input type="text" name="incidental_Amt" id="incidental_Amt" value="<?php //echo $incidental_Amt; ?>"  style="width:90px;" readonly="readonly" />
				</td>
			</tr>
			<tr class="row1">
				<td class="tabledata1">SurCharges</td>
				<td><input type="text" name="surcharge" id="surcharge" value="<?php echo $surcharge; ?>" style="width:60px;" />
					<input type="text" name="surcharge_Amt" id="surcharge_Amt" value="<?php echo $surcharge_Amt; ?>" onfocus="calsurchrg()" style="width:90px;" readonly="readonly" />
				</td>
			</tr>
			<?php 
			$u=1;
			if($_REQUEST['action']=='Edit')
			{ 
			$sql_charge=DB_query("select * from dbl_sale_order_charge where parentid='".$_REQUEST['id']."'",$db);
			} 
			else
			{ 
			$sql_charge=DB_query("select * from dbl_enquiry_charge where parentid='".$_REQUEST['id']."'",$db);
			} 			
			while($row12=DB_fetch_array($sql_charge))
			{
			?>
			<tr class="row1">
			<input type='hidden' name='chargeid[]' value='<?php echo $row12['charge_id']; ?>'>
		<td class="tabledata1"><?php echo $s->getField('dbl_dealer_charge_master','id',$row12['charge_id'],'charge_name'); ?></td>
				<td><input type="text" name="freight[]" id="freight_<?php echo $u; ?>" value="<?php echo $row12['charge_per']; ?>"  style="width:60px;" readonly="readonly"  />
					<input type="text" name="freight_Amt[]" id="freight_Amt_<?php echo  $u; ?>" value="<?php echo $row12['charge_amt']; ?>" style="width:90px;" onfocus="calfreight(<?php echo $u; ?>)"  readonly/>
				</td>
			</tr>
			<?php $u++; }?>
			<input type='hidden' id='charge_count' name='charge_count' value='<?php echo $u; ?>'>
			
			<tr class="row1">
				<td class="tabledata1"><strong>Grand Total</strong></td>
				<td><input type="text" name="grand_tatal" id="grand_tatal" value="<?php echo $grand_tatal; ?>" onfocus="calgrandTotal()" style="width:150px;" readonly="readonly" /></td>
			</tr>
		</table>
	</td>
</tr>
</table>

<p align="center">
<input type="submit" name="Submit" onclick="return validate();" class="search" value="Save"  />
</p>
</form>
<script>
function calamt(str,i)
{
if(document.getElementById("orderqty_"+i).value=='')
var qty=0;
else
var qty=parseFloat(document.getElementById("orderqty_"+i).value);
if(str!='')
var amount=parseFloat(str)*qty;
else
var amount=0;
document.getElementById("netrate_"+i).value=amount.toFixed(2);
}

function DiscountAmount(DA,i)
{
	var discount_percent = document.getElementById("PerAmo_"+i).value;
	
	var rate = document.getElementById("netrate_"+i).value;
	var discount = document.getElementById("Discount_"+i).value;
	
	var totalPrice = parseFloat(rate);
	
	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("Discount_"+i).value);
		if(isNaN(percent)){percent=0;}
		var subTotal = totalPrice-parseFloat(percent);
	
	}
	
	document.getElementById("total_"+i).value=subTotal.toFixed(2);
	
	
}	

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

function open_items1()
{
window.open("saleorder_temscondition.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=480,width=500,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
function calamount()
{

var amount=0;
var amount1=0;
var count1=document.getElementById("count").value;
for(var i=0;i<count1;i++)
{
if(document.getElementById("totalamt_"+i).value!='')
{
amount=parseFloat(amount)+parseFloat(document.getElementById("totalamt_"+i).value);

if(document.getElementById("type1_"+i).value=='exciable')
{
amount1=parseFloat(amount1)+parseFloat(document.getElementById("total_"+i).value);
}
}
}

document.getElementById("basic_amt").value=amount.toFixed(2);
document.getElementById("excise_value").value=amount1.toFixed(2);
}

function calsalamt()
{
var amount=0;
var totalamt=0;
var count2=document.getElementById("count1").value;
for(var k=1;k<count2;k++)
{
if(document.getElementById("chargeid_"+k).value!='')
{
amount=parseFloat(amount)+parseFloat(document.getElementById("chargeper_"+k).value);
}
}
totalamt=(parseFloat(document.getElementById("grnad_total").value)*amount)/100;
totalamt1=(parseFloat(document.getElementById("grnad_total").value)+parseFloat(totalamt));
document.getElementById("total_sal_amt").value=totalamt1.toFixed(2);
}
function caldispatch(str)
{
if(parseFloat(document.getElementById("orderqty_"+str).value)>parseFloat(document.getElementById("prqty_"+str).value))
{
alert("dispatch Qty Should Be less then Order Qty");
document.getElementById("orderqty_"+str).value='';
document.getElementById("orderqty_"+str).focus();
}
}
</script>
<script>
$('.timepicker').timepicker();

</script>
<script type="text/javascript" language="javascript">
function validate()
{
	if(document.getElementById("picklisttype").value=='0')
	{
		alert("Please Select The Pick List Type First");
		document.getElementById("picklisttype").focus();
		return false;
	}
	if(document.getElementById("date").value=='')
	{
		alert("Please Fill The Date First");
		document.getElementById("date").focus();
		return false;
	}
	if(document.getElementById("time").value=='')
	{
		alert("Please Fill The Time First");
		document.getElementById("time").focus();
		return false;
	}
	if(document.getElementById("ordertype").value=='0')
	{
		alert("Please Select The Order Type First");
		document.getElementById("ordertype").focus();
		return false;
	}
	if(document.getElementById("soi_date").value=='')
	{
		alert("Please Fill SOI Date First");
		document.getElementById("soi_date").focus();
		return false;
	}
	if(document.getElementById("po_no").value=='')
	{
		alert("Please Fill The PO No First");
		document.getElementById("po_no").focus();
		return false;
	}
	if(document.getElementById("po_date").value=='')
	{
		alert("Please Fill The PO Date First");
		document.getElementById("po_date").focus();
		return false;
	}
	if(document.getElementById("part_deliverydate").value=='')
	{
		alert("Please Fill The Part Delivery Date First");
		document.getElementById("part_deliverydate").focus();
		return false;
	}
	var count1=document.getElementById("count").value;
	var to=0;
	for(var i=0;i<count1;i++)
	{
		if(document.getElementById("orderqty_"+i).value!='' && document.getElementById("rate_"+i).value!='' && document.getElementById("rate_"+i).value!='' && document.getElementById("total_"+i).value!='')
		{
			to=1;
			break; 
		}
	}
	if(to==0)
	{
		alert("Please Fill The Items Field");
		return false;
	}
	
	if(document.getElementById("grnad_total").value=='' || document.getElementById("grnad_total").value=='0.00')
	{
		alert("Please Fill The Grand Total First");
		document.getElementById("grnad_total").focus();
		return false;
	}
	if(document.getElementById("total_sal_amt").value=='' || document.getElementById("total_sal_amt").value=='0.00')
	{
		alert("Please Fill Total Sales Order Amount First");
		document.getElementById("total_sal_amt").focus();
		return false;
	}
	
}


function caldispatch(str,str1)
{
var count=document.getElementById("count11_"+str).value;
var qry=0;



for(var j=0;j<count;j++)
	{
	
	
var value = "" + str + j;

if(parseFloat(document.getElementById("orderqty1_"+value).value)>parseFloat(document.getElementById("prqty1_"+value).value))
{
alert("Order Qty Should Be less then Order Qty!");
document.getElementById("orderqty1_"+value).value='';
document.getElementById("orderqty1_"+value).focus();
}
//alert(value);
if(document.getElementById("orderqty1_"+value).value!='')
var qry=qry+parseFloat(document.getElementById("orderqty1_"+value).value);
}

document.getElementById("orderqty_"+str).value=qry;

}


function showgrn(str,i)
{

var xmlhttp;
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)
    {
     var data=xmlhttp.responseText.split("__");
	
	document.getElementById("grn_"+i).innerHTML=data[0];
	document.getElementById("stock_"+i).value=data[1];

	
    }
  }
  var dealer=document.getElementById("dealer_id").value;
  var items=document.getElementById("mst_itemid_"+i).value;
  xmlhttp.open("GET","get_storegrnno.php?store="+str+"&items="+items+"&dealer="+dealer,true);
xmlhttp.send();	




}

function showexciseamt(i)
{
var xmlhttp;
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("exciseamt_"+i).value=xmlhttp.responseText;
	
	
    }
  }
  var dealer=document.getElementById("grn_"+i).value;
  var amt=document.getElementById("total_"+i).value;
 // var dealer1=document.getElementById("dealer_id").value;
  //var items=document.getElementById("itemid_"+i).value; 
  
  xmlhttp.open("GET","get_grnexciseamt.php?mrnid="+dealer+"&amt="+amt,true);
xmlhttp.send();


}




function showexciseamt1(j,i)
{
var xmlhttp;
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)
    {
	var data=xmlhttp.responseText.split("__");
	
	document.getElementById("EDRate_"+i).innerHTML=data[0];
	document.getElementById("totalDiscountedPrice_"+i).value=data[1];
	document.getElementById("ECessValue_"+i).value=data[2];
	document.getElementById("SAHCessValue_"+i).value=data[3];
	document.getElementById("stock_"+i).value=data[5];
	//document.getElementById("exciseamt_"+i).value=data[4];
	//document.getElementById("stock_"+i).value=data[5];
	
    }
  }
  var dealer=document.getElementById("grn_"+i).value;
  var amt=document.getElementById("total_"+i).value;
  var dealer1=document.getElementById("dealer_id").value;
  var items=document.getElementById("mst_itemid_"+i).value; 
  xmlhttp.open("GET","get_grnexcisedata.php?mrnid="+dealer+"&amt="+amt+"&dealer1="+dealer1+"&items="+items,true);
xmlhttp.send();


}





function showtotalamt(i)
{
if(document.getElementById("exciseamt_"+i).value=='0')
var qty=0;
else
var qty=parseFloat(document.getElementById("exciseamt_"+i).value);
var amt1=parseFloat(document.getElementById("total_"+i).value);
var amount=amt1+qty;
document.getElementById("totalamt_"+i).value=amount.toFixed(2);


}

function calculateTotalExcise(i)
{
if(document.getElementById("total_"+i).value!='')
var amt1=parseFloat(document.getElementById("total_"+i).value);
else
var amt1=0;
if(document.getElementById("EDRate_"+i).value!='')
{
var exciseid=parseFloat(document.getElementById("EDRate_"+i).value);
var amount=(amt1*exciseid)/100;

}
else
{
var exciseid=0;
var amount=0;

}
document.getElementById("totalDiscountedPrice_"+i).value=amount.toFixed(2);


}

function calculateSAHCess1(i)
{
if(document.getElementById("totalDiscountedPrice_"+i).value!='')
var amt=parseFloat(document.getElementById("totalDiscountedPrice_"+i).value);
else
var amt=0;
if(document.getElementById("SAHCessPercentage_"+i).value!='')
{
var exciseid=parseFloat(document.getElementById("SAHCessPercentage_"+i).value);
var amount=(amt*exciseid)/100;
}
else
{
var amount=0;
}
document.getElementById("SAHCessValue_"+i).value=amount.toFixed(2);

}

function calculateECess1(i)
{
if(document.getElementById("totalDiscountedPrice_"+i).value!='')
var amt=parseFloat(document.getElementById("totalDiscountedPrice_"+i).value);
else
var amt=0;
if(document.getElementById("ECessPercentage_"+i).value)
{
var exciseid=parseFloat(document.getElementById("ECessPercentage_"+i).value);
var amount=(amt*exciseid)/100;
}
else
{
var amount=0;
}
document.getElementById("ECessValue_"+i).value=amount.toFixed(2);

}

function calgrandTotal()
{    
    var  Freight=0;
	//var basicamt=document.getElementById("basic_amt").value;
	var discAmt=document.getElementById("discount_Amt").value;
	var ExciseAmt=document.getElementById("totalDiscountedPrice").value;
	var vatAmt=document.getElementById("VatCstValue").value;
	var SurAmt=document.getElementById("surcharge_Amt").value;
	if(document.getElementById("charge_count"))
	{
	var count1=document.getElementById("charge_count").value;
	if(count1!='')
	{
	for(var i=1;i<count1;i++)
	{
	 Freight=parseFloat(Freight)+parseFloat(document.getElementById("freight_Amt_"+i).value);
	}
	}
	}
	else
	{
	Freight=0;
	}
	
	var PF=document.getElementById("pnf_amt").value;
	var IncChrg=document.getElementById("incidental_Amt").value;
	//var AddChrg=document.getElementById("additional_chrg").value;
	var ecess=document.getElementById("ECessValue").value;
	var sahcess=document.getElementById("SAHCessValue").value;
	
	//if(basicamt==''){basicamt=0;}
	if(discAmt==''){discAmt=0;}
	if(ExciseAmt==''){ExciseAmt=0;}
	if(vatAmt==''){vatAmt=0;}
	if(SurAmt==''){SurAmt=0;}
	if(Freight==''){Freight=0;}
	if(PF==''){PF=0;}
	if(IncChrg==''){IncChrg=0;}
	//if(AddChrg==''){AddChrg=0;}
	if(ecess==''){ecess=0;}
    if(sahcess==''){sahcess=0;}

/*	var Total=parseFloat(discAmt)+parseFloat(ExciseAmt)+parseFloat(vatAmt)+parseFloat(SurAmt)+parseFloat(Freight)+parseFloat(PF)+parseFloat(IncChrg)+parseFloat(AddChrg)+parseFloat(ecess)+parseFloat(sahcess);
	*/
	
	var Total=parseFloat(discAmt)+parseFloat(ExciseAmt)+parseFloat(vatAmt)+parseFloat(SurAmt)+parseFloat(Freight)+parseFloat(ecess)+parseFloat(sahcess)+parseFloat(PF)+parseFloat(IncChrg);
	
	document.getElementById("grand_tatal").value=Total.toFixed(2);
}



function calfreight(i)
{
    if(document.getElementById("basic_amt").value!='')
	var basic=parseFloat(document.getElementById("basic_amt").value);
	else
	var basic=0;
	if(document.getElementById("freight_"+i).value!='')
	var freihgt=parseFloat(document.getElementById("freight_"+i).value);
	else
	var freihgt=0;
	var totalfr=(basic*freihgt)/100;
	document.getElementById("freight_Amt_"+i).value=totalfr;
	
}





function calsurchrg()
{
	var salamt=parseFloat(document.getElementById("VatCstValue").value);
	var surchrg;
	if(document.getElementById("surcharge").value=='')
	{
		surchrg=0;
	}
	else
	{
		surchrg=parseFloat(document.getElementById("surcharge").value);
	}
	if(surchrg==0)
	{
		document.getElementById("surcharge_Amt").value=0;
	}
	else
	{
		var suramt=(salamt*surchrg)/100;
		document.getElementById("surcharge_Amt").value=suramt.toFixed(2);
	}
}






function calculateEditVATCST()
{
	var rate=document.getElementById("rate").value;
	var taxableAmount=document.getElementById("totalDiscountedamount").value;
	var taxableAmount1=document.getElementById("discount_Amt").value;
	var taxableAmount2=document.getElementById("pnf_amt").value;
	var taxamt11=parseFloat(taxableAmount)+parseFloat(taxableAmount1)+parseFloat(taxableAmount2);
	var vat_cst=taxamt11*(rate/100);
	document.getElementById("VatCstValue").value=vat_cst.toFixed(2);
}

function calculateVATCST(str)
{
	
	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)
	{
	var data=xmlhttp.responseText.split("__");
	document.getElementById("rate").value=data[0];
	document.getElementById("VatCstValue").value=data[1];
	}
	}
	var amt1=document.getElementById("totalDiscountedamount").value;
	var amt2=document.getElementById("discount_Amt").value;
	var amt3=document.getElementById("pnf_amt").value;
	
	var amount=parseFloat(amt1)+parseFloat(amt2)+parseFloat(amt3);

	xmlhttp.open("GET","calculateExcise.php?salesIdamount="+str+"&amt="+amount,true);
	xmlhttp.send();
}

function calincidentalchrg(str)
{
if(str!='')
var str1=parseFloat(str);
else
var str1=0;
if(document.getElementById("discount_Amt").value!='')
var discountamt=parseFloat(document.getElementById("discount_Amt").value);
else
var discountamt=0;
if(document.getElementById("pnf_amt").value!='')
var pnf=parseFloat(document.getElementById("pnf_amt").value);
else
var pnf=0;
var totalamt=parseFloat(discountamt)+parseFloat(pnf);
var totalamt1=(parseFloat(totalamt)*parseFloat(str1))/100;
document.getElementById("incidental_Amt").value=totalamt1.toFixed(2);

}


function calculateSAHCess1()
{
	var exciseValue=document.getElementById("totalDiscountedPrice").value;
	var SAHCessPercentage=document.getElementById("SAHCessPercentage").value;
	var total=exciseValue*(SAHCessPercentage/100);
	document.getElementById("SAHCessValue").value=total.toFixed(2);
}






function calculateECess1()
{
	var exciseValue=document.getElementById("totalDiscountedPrice").value;
	var ECessPercentage=document.getElementById("ECessPercentage").value;
	var total=exciseValue*(ECessPercentage/100);
	document.getElementById("ECessValue").value=total.toFixed(2);
}




function calculateTotalExcise()
{
	var EDRate=document.getElementById("EDRate").value;
	var first_row=document.getElementById("excise_value1").value;
	if(first_row==''){first_row=0;}	
	if(window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest();
	}
	else
	{
	xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	document.getElementById("totalDiscountedPrice").value=xmlhttp.responseText;
	}
	}
	if(document.getElementById("p_and_f").value!='')
	var pnf=parseFloat(document.getElementById("p_and_f").value);
	else
	var pnf=0;
	var pnf_excise=(parseFloat(first_row)*pnf)/100;
	
	var amount=parseFloat(first_row)+parseFloat(pnf_excise);
	xmlhttp.open("GET","calculateExcise.php?type=bill&id="+EDRate+"&value="+amount,true);
	xmlhttp.send();	
	
}



function calculateBillExcise(str)
	{
		var value=document.getElementById("excise_value1").value;
		if(str.length==0)
		{
		document.getElementById("totalDiscountedPrice").innerHTML="";
		return;
		}
		if(window.XMLHttpRequest)
		{
		xmlhttp=new XMLHttpRequest();
		}
		else
		{
		xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("totalDiscountedPrice").value=xmlhttp.responseText;
		}
		}
		if(document.getElementById("p_and_f"),value!='')
		var pnf=parseFloat(document.getElementById("p_and_f").value);
		else
		var pnf=0;
		var pnf_excise=(parseFloat(value)*pnf)/100;
		
		var amount=parseFloat(value)+parseFloat(pnf_excise);
		
		xmlhttp.open("GET","calculateExcise.php?type=bill&id="+str+"&value="+amount,true);
		xmlhttp.send();	
	}






caldiscountAmt()
{   
   if(document.getElementById("basic_amt").value!='')
	var basic=parseFloat(document.getElementById("basic_amt").value);
	else
	var basic=0;
	if(document.getElementById("excise_value").value!='')
	var excise_amt=parseFloat(document.getElementById("excise_value").value);
	else
	var excise_amt=0;
	var discount;
	if(document.getElementById("discount").value=='')
	{
	discount=0;
	}
	else
	{
	 discount=parseFloat(document.getElementById("discount").value);
	}
	if(discount==0)
	{
	document.getElementById("discountAmt").value=0;
	document.getElementById("discount_Amt").value=basic.toFixed(2);
	document.getElementByid("excise_value1").value=excise_amt.toFixed(2);
	if(document.getElementById("p_and_f").value!='')
	var pnf=parseFloat(document.getElementById("p_and_f").value);
	else
	var pnf=0;
	var discount_pnf=(parseFloat(basic)*pnf)/100;
	document.getElementByid("pnf_amt").value=discount_pnf.toFixed(2);
	}
	else
	{
	var disAmt=(basic*discount)/100;
	var disexciseamt=(excise_amt*discount)/100;
	document.getElementById("discountAmt").value=disAmt.toFixed(2);
	var amt=basic-parseFloat(disAmt);
	var amt1=excise_amt-parseFloat(disexciseamt);
	if(document.getElementById("p_and_f").value!='')
	var pnf=parseFloat(document.getElementById("p_and_f").value);
	else
	var pnf=0;
	var discount_pnf=(parseFloat(amt)*pnf)/100;
	document.getElementById("pnf_amt").value=discount_pnf.toFixed(2);
	document.getElementById("discount_Amt").value=amt.toFixed(2);
	document.getElementById("excise_value1").value=amt1.toFixed(2);
	
	}
}



function caltotalexciseamt()
{
if(document.getElementById("totalDiscountedPrice").value!='')
var exciseValue=document.getElementById("totalDiscountedPrice").value;
else
var exciseValue=0;
if(document.getElementById("ECessValue").value!='')
var ecessvalue=document.getElementById("ECessValue").value;
else
var ecessvalue=0;
if(document.getElementById("SAHCessValue").value!='')
var SAHCessValue=document.getElementById("SAHCessValue").value;
else
var SAHCessValue=0;

var amt=parseFloat(exciseValue)+parseFloat(ecessvalue)+parseFloat(SAHCessValue);

document.getElementById("totalDiscountedamount").value=amt.toFixed(2);
}

</script>

