<?php ob_start();
$PageSecurity = 4;
$PageName = 'Purchase Bill Book';
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
if (isset($_GET['ModifyOrderNumber']) and $_SESSION[UserType]=='SuperAdmin')  {
	$title = _('Modify Purchase BillBook') . ' ' . $_GET['ModifyOrderNumber'];
} else {
}
if(isset($_GET['issueid'])  && isset($_GET['issueid']) !='')
{

	$title = _('Update Issue Slip');
}
else
{
	$title = _('Purchase BillBook');
}
if($_REQUEST['Save']) 
{	
	
	if($_REQUEST['PV_Type']=='Direct')
	{
	if($_REQUEST['site2']!=$_REQUEST['site']) 
	{
	$financial=$_SESSION['YearStart']."-".$_SESSION['YearEnd'];
	$prefixcode="CLD9/PV";
	$sql="select bill_book_no from purchase_billbook where site = '".$_REQUEST['site']."' order by id DESC";
	
	$result = DB_query($sql,$db);
	$numrows = DB_num_rows($result);
	$sqldata=DB_query("select locationname from locations where loccode='".$_REQUEST['site']."'",$db);
	$sqltext=DB_fetch_array($sqldata);
	$sitename=$sqltext['locationname'];
	$siteCode = $sitename;
	if($numrows>0)
	{
	$i=0;
	$strmax = "";
	$myrow=DB_Fetch_array($result);
	$ExplBill=explode("/",$myrow['bill_book_no']);
	if($siteCode!='')
	$val1= substr($myrow['purchase_bill'],0,18);
	$strChar1 = substr($myrow['purchase_bill'],0,9);
	$strChar = $strChar1."/".ucwords($_REQUEST['site'])."/".$financial."/";  
	
	if($numrows>0)
	{ 
	 $maxval = $ExplBill[4];
	}
	else
	{
	$maxval=0;
	}
	{
	$strChar = $prefixcode."/".ucwords($_REQUEST['site'])."/".$financial."/";
	}
	}
	else
	{
	$strChar = $prefixcode."/".ucwords($_REQUEST['site'])."/".$financial."/";
	}
	
	$strFinal = $maxval+1;
	$strBothFinal = $strChar.str_pad($strFinal,4,"0",STR_PAD_LEFT); 	
	}
	else
	{
	$strBothFinal =$_REQUEST['pvou']; 
	}
	
	$data1['site']=$_REQUEST['site'];    
	$data1['bill_book_no']=$strBothFinal;
	$data1['bill_book_date']=FormatDateForSQL($_REQUEST['bill_book_date']); 
	$data1['due_date']=FormatDateForSQL($_REQUEST['due_date']);  
	$data1['vendor']=$_REQUEST['vendor']; 
	$data1['bill_challan_no']=$_REQUEST['bill_challan_no'];  
	$data1['credit_days']=$_REQUEST['credit_days'];  
	$data1['cash_credit']=$_REQUEST['cash_credit'];  
	$data1['credit_days']=$_REQUEST['credit_days'];  
	$data1['ledger']=$_REQUEST['ledger']; 
	$data1['Excise_Effect ']=$_REQUEST['Excise_Effect']; 
	$data1['remarks']=$_REQUEST['remarks'];  
	$data1['total_basic_amount']=$_REQUEST['total_basic_amount'];  
	$data1['total_item_amount']=$_REQUEST['total_item_amount'];  
	$data1['surcharge_ledger']=$_REQUEST['surcharge_ledger'];  
	$data1['sur_oct_amt']=$_REQUEST['sur_oct_amt'];  
	$data1['freight_ledger']=$_REQUEST['freight_ledger'];  
	$data1['freight']=$_REQUEST['freight'];  
	$data1['pckg_ledger']=$_REQUEST['pckg_ledger'];  
	$data1['pkg_charges']=$_REQUEST['pkg_charges'];  
	$data1['addcharge_ledger']=$_REQUEST['addcharge_ledger'];  
	$data1['add_charges']=$_REQUEST['add_charges'];  
	$data1['round_off']=$_REQUEST['round_off'];  
	$data1['GrandTotal']=$_REQUEST['GrandTotal'];  
	$data1['bill_status']='Pending'; 
	$data1['pending_amount']=$_REQUEST['grand_total1']; 
	$data1['form_type']=$_REQUEST['form_type']; 
	$data1['modified_date']=FormatDateForSQL(date('d/m/Y'));  
	$data1['modified_by']=$_SESSION['UserID'];
	
	}
	
	else
	{
	$data1['bill_book_date']=FormatDateForSQL($_REQUEST['bill_book_date']); 
	$data1['due_date']=FormatDateForSQL($_REQUEST['due_date']); 
	$data1['cash_credit']=$_REQUEST['cash_credit'];  
	$data1['credit_days']=$_REQUEST['credit_days'];  
	$data1['ledger']=$_REQUEST['ledger']; 
	$data1['Excise_Effect ']=$_REQUEST['Excise_Effect']; 
	$data1['remarks']=$_REQUEST['remarks'];  
	$data1['total_basic_amount']=$_REQUEST['total_basic_amount'];  
	$data1['total_item_amount']=$_REQUEST['total_item_amount'];  
	$data1['surcharge_ledger']=$_REQUEST['surcharge_ledger'];  
	$data1['sur_oct_amt']=$_REQUEST['sur_oct_amt'];  
	$data1['freight_ledger']=$_REQUEST['freight_ledger'];  
	$data1['freight']=$_REQUEST['freight'];  
	$data1['pckg_ledger']=$_REQUEST['pckg_ledger'];  
	$data1['pkg_charges']=$_REQUEST['pkg_charges'];  
	$data1['addcharge_ledger']=$_REQUEST['addcharge_ledger'];  
	$data1['add_charges']=$_REQUEST['add_charges'];  
	$data1['round_off']=$_REQUEST['round_off'];  
	$data1['GrandTotal']=$_REQUEST['GrandTotal'];  
	$data1['bill_status']='Pending'; 
	$data1['pending_amount']=$_REQUEST['grand_total1']; 
	$data1['form_type']=$_REQUEST['form_type']; 
	$data1['modified_date']=FormatDateForSQL(date('d/m/Y'));  
	$data1['modified_by']=$_SESSION['UserID'];
	}
	$cond="id='".$_REQUEST['editId']."'";
	$s->RecordEdit('purchase_billbook', $data1,$cond);
  for($i=0;$i<$_REQUEST['count'];$i++)
  {
  $vat_value=DB_fetch_assoc(DB_query("select * from tbl_values_update",$db)); 
 if($vat_value[vat_cst]==0 && $vat_value[Excise_Duty_value]==0)
	{
		$total_amountc=$_REQUEST['basic_amt'][$i]/$_REQUEST['quantity'][$i];
	}
	elseif($vat_value[vat_cst]==1 && $vat_value[Excise_Duty_value]==0)
	{
	$total_amountc=($_REQUEST['basic_amt'][$i]+$_REQUEST['taxvalue'][$i])/$_REQUEST['quantity'][$i];
	}
	elseif($vat_value[vat_cst]==0 && $vat_value[Excise_Duty_value]==1) 
  	{
	$total_amountc=$_REQUEST['taxable_amt'][$i]/$_REQUEST['quantity'][$i];
	}
	elseif($vat_value[vat_cst]==1 && $vat_value[Excise_Duty_value]==1) 
	{
	$total_amountc=$_REQUEST['total_amount'][$i]/$_REQUEST['quantity'][$i];
	}
	
	$sql1= "select * from item_master where id= '".$_REQUEST['mst_productid'][$i]."'";
	$result1=DB_query($sql1,$db);
	$myrow1=DB_fetch_array($result1);
	$item_profit= $myrow1['item_profit'];
	$item_discount= $myrow1['item_discount'];
	$last_po_price=  $total_amountc;
	if($item_profit==0 || empty($item_profit)){
		$mrp_rate=$last_po_price;
	}else{
		$proval=$last_po_price*($item_profit/100);
		$mrp_rate=$last_po_price+$proval;
	}
	if($item_discount==0 || empty($item_discount)){
		$net_rate = $mrp_rate;
	}else{
		$disval=$mrp_rate*($item_discount/100);
		$net_rate =$mrp_rate-$disval;
	}
	
	 $update="update item_master set last_po_price='".$last_po_price."',net_rate='".$net_rate."',mrp_rate='".$mrp_rate."' where id='".$_REQUEST['mst_productid'][$i]."'";
	 //print_r($update);
	 //die();
	
	
	$update_res=DB_query($update,$db);
	
	$data11['created_date']=date("Y-m-d");
	$data11['effective_date']=date("Y-m-d");
	$data11['created_by']=$_SESSION['UserID'];
	$data11['mst_itemid']=$_REQUEST['mst_productid'][$i];	
	$data11['item_discount']  =$item_discount;
	$data11['last_po_price']  =$last_po_price;
	$data11['item_profit']  =$item_profit;
	$data11['mrp_rate']  =$mrp_rate;
	$data11['net_rate']  =$net_rate;
	$s->RecordInsert('rev_item_master', $data11);
  
  
  
		$data['rate']=$_REQUEST['rate'][$i];
		$data['total_price']=$_REQUEST['total_price'][$i];
		$data['discount_type']=$_REQUEST['discount_type'][$i];
		$data['discount']=$_REQUEST['discount'][$i];
		$data['discount_amount']=$_REQUEST['discount_amount'][$i];
		$data['sub_total']=$_REQUEST['sub_total'][$i];
		$data['specialdiscount']=$_REQUEST['specialdiscount'][$i];
		$data['specialdiscount_amount']=$_REQUEST['specialdiscount_amount'][$i];
		$data['make']=$_REQUEST['make'][$i];
		$data['color']=$_REQUEST['color'][$i];
		$data['grade']=$_REQUEST['grade'][$i];
		$data['basic_amt']=$_REQUEST['basic_amt'][$i];
		$data['tax_type']=$_REQUEST['tax_type'][$i];
		$data['Excise_Duty']=$_REQUEST['Excise_Duty'][$i];
		$data['ExcisePercentage']=$_REQUEST['ExcisePercentage'][$i];
		$data['Excise_Duty_value']=$_REQUEST['exciseValue'][$i];
		$data['cess_ID1']=$_REQUEST['cess_ID1'][$i];
		$data['Edu_Cess_perc']=$_REQUEST['Edu_Cess_perc'][$i];
		$data['Edu_Cess']=$_REQUEST['Edu_Cess'][$i];
		$data['cess_ID2']=$_REQUEST['cess_ID2'][$i];
		$data['SHE_Cess_perc']=$_REQUEST['SHE_Cess_perc'][$i];
		$data['SHE_Cess']=$_REQUEST['SHE_Cess'][$i];
		$data['taxable_amt']=$_REQUEST['taxable_amt'][$i];
		$data['vat_cst']=$_REQUEST['vat_cst'][$i];
		$data['vat_cst_amt']=$_REQUEST['taxvalue'][$i];
		$data['total_amount']=$_REQUEST['total_amount'][$i];
		$data['remarks']=$_REQUEST['item_remarks'][$i];
		$condition="id='".$_REQUEST['row_id'][$i]."'";
		$s->RecordEdit('purchase_bill_details',$data, $condition);
  }
//$update_mrn="Update addmrn set mrnstatus='closed' where id='".$_REQUEST['mrn_no']."'";
//$update_res=DB_query($update_mrn,$db);
echo "<script>location.href='index.php?route=PurchBillBook_list&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
prnMsg( _('Record Inserted Successfully!'),'success');

}


//***************************FETCH DATA FOR EDIT**********************START HERE**********************************************/
 $sql="Select * from purchase_billbook where id='".$_REQUEST['editId']."'";
$result=DB_query($sql,$db);
$myrow=DB_fetch_array($result);
extract($myrow);
//***************************FETCH DATA FOR EDIT**********************END HERE**********************************************/
if($mrn_no!=0) {
$sql12=DB_query("Select ReceivedAgainst,PO_WO_MTO,BillNo,Type from addmrn where id='".$mrn_no."'",$db);
$MRNData=DB_fetch_array($sql12);
extract($MRNData);
}
?>
<!-------------------------AJAX FILES----------------------------START HERE--------------------------------------------->
<script>
var xmlHttp
function changestorename(str)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	alert ("Your browser does not support AJAX!");
	return;
	}
	var url="Store_Name.php";
	url=url+"?store_code="+str;
	xmlHttp.onreadystatechange=NumberChanged1;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function NumberChanged1()
{
	if (xmlHttp.readyState==4)
	{
	document.getElementById("txtid").innerHTML=xmlHttp.responseText;
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
	try
	{
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	}
	return xmlHttp;
}
  
  
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 =no");
}
 $(function() {
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
		changeYear: true,
		yearRange: '-80:+20'});
});


function Completetotal(id)
{
 	var  Item_Amount = document.getElementById('total_Item_Amount').value;
 	var  Charge_Amount = document.getElementById('Charge_Amount').value;
	var inclusive=document.getElementById('tax_suroct_value').value;
	var total_tax=document.getElementById('tax_chargeamount').value;
 	var Grand_Total = parseFloat(Item_Amount) + parseFloat(Charge_Amount)+parseFloat(total_tax)+parseFloat(inclusive);
  	document.getElementById('grandtotal').value =Grand_Total;
}

function calculate_short(id)
{
	var x = document.getElementById('reciveqty_'+id).value;
	var poqty = document.getElementById('poqty_'+id).value;
	var reject = document.getElementById('rejectqty_'+id).value;
	var rate = document.getElementById('itemrate_'+id).value;
	//var tot = document.getElementById('totalrate_'+id).value;
	//alert("total rate is : "+tot);
	
	if(x != '')
	{
	if(poqty >= x)
	{
	//alert(rate);
	if(reject >0){
	document.getElementById('rejectqty_'+id).disabled =false;
	}
	else{
	document.getElementById('short_accessid_'+id).value = parseInt(poqty)- parseInt(x);
	document.getElementById('rejectqty_'+id).disabled =true;
	document.getElementById('totalrate_'+id).value = parseInt(rate) * parseInt(x);
	//alert(document.getElementById('totalrate_'+id).value);
	}
	}
	else
	{
	alert("Received Quantity can not be greater than PO Quantity");
	return false;
	}
	return true;
	}
	else
	{
	document.getElementById('short_accessid_'+id).value = '0';
	}
}


//--------------  ADD Sub charges----------------------------------
function cal_surcharges(id)
{
	 
	   document.getElementById("hidden_percent_inclusivecharge").value=id;
}
 
function cal_surcharges1()
{
	var surcharge=document.getElementById("percent_inclusivecharge").value;
	var per=document.getElementById('percent_inclusive').value;
	var amount=document.getElementById('tax_chargeamount').value;
	var total=document.getElementById('total_Item_Amount').value;
	var   total_tax=document.getElementById('tax_chargeamount').value;
	if(surcharge=="SUR"){  
	totals=(parseInt(per)*parseFloat(amount))/100;
	var	 final=totals;
	}
	else if(surcharge=="OCT"){
	totals=parseFloat(total)+parseFloat(total_tax);
	var 	final=(parseFloat(totals)*  parseFloat(per))/100;
	}	
	document.getElementById('tax_suroct_value').value=final;
}

 


function update_amount(id)
{
document.getElementById("item_total_rate_"+id).value= (document.getElementById("reciveqty_"+id).value) * (document.getElementById("itemprice_"+id).value);
 }
 
function update_basic()
{
	var  itemamount = 0;
	var table = document.getElementById("totalrow").value;
	for(i=1; i<=table; i++)
	{
	var totalrate = document.getElementById('itemprice_'+i).value;
	itemamount  =parseInt(itemamount)+  parseInt(totalrate);
	document.getElementById('total_Item_Amount').value = itemamount;
	}
}
 
function showitem_details(str)
{
	if (str=="")
	{
	document.getElementById("txt_item_details").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("txt_item_details").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","getitem_details.php?addmrn="+str,true);
	xmlhttp.send();
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
	try
	{
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	}
	return xmlHttp;
}

function get_mrn_details(str)
{
	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("orderid").innerHTML=data[0];
	document.getElementById("itemid").innerHTML=data[1];
	document.getElementById("amount_tax").innerHTML=data[2];
	}
	}
	xmlhttp.open("GET","mrn_order_details.php?mrnno="+str,true);
	xmlhttp.send();
}


function showmrn(str)
{
	var site= document.getElementById('site').value;
	//var store= document.getElementById('store').value;
	//alert(site);
	//alert(store);
	//alert(site);
	if (str=="")
	{
	document.getElementById("getmrn").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("getmrn").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","get_mrn.php?vendor="+str+"&site="+site,true);
	xmlhttp.send();
}


function validate()
{
	var site=document.getElementById("site").value;
	var store=document.getElementById("store").value;
	var vendor=document.getElementById("vendor").value;
	var mrn_no=document.getElementById("mrn_no").value;
	var bill=document.getElementById("bill").value;
	var bill_date=document.getElementById("bill_date").value;
	var purchase_orderno=document.getElementById("purchase_orderno").value;
	var cash_credit=document.getElementById("cash_credit").value;
	var credit_days=document.getElementById("credit_days").value;
	if(site=='000')
	{
	alert("Please Select Site");
	document.getElementById("site").focus();
	return false;
	}
	if(store=='000')
	{
	alert("Please Select Store");
	document.getElementById("store").focus();
	return false;
	}
	if(vendor=='000')
	{
	alert("Please Select Vendor");
	document.getElementById("vendor").focus();
	return false;
	}
	if(mrn_no=='000')
	{
	alert("Please Select MRN Number");
	document.getElementById("mrn_no").focus();
	return false;
	}
	if(bill=='')
	{
	alert("Please Enter Bill Number");
	document.getElementById("bill").focus();
	return false;
	}
	if(bill_date=='')
	{
	alert("Please Enter Bill Date");
	document.getElementById("bill_date").focus();
	return false;
	}
	if(purchase_orderno=='')
	{
	alert("Please Enter Order Number");
	document.getElementById("purchase_orderno").focus();
	return false;
	}
	if(cash_credit=='000')
	{
	alert("Please Select Cash/Credit");
	document.getElementById("cash_credit").focus();
	return false;
	}
	if(credit_days=='')
	{
	alert("Please Enter Credit Days");
	document.getElementById("credit_days").focus();
	return false;
	}
	
}

function check()
{
	if(document.getElementById("ledger").value == '000')
	{
		alert("Please Select Ledger");
		document.getElementById("ledger").focus();
		return false;
	}
}

function calc_basicamt()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value;
	
	for(var j=1;j<cnt;j++)
	{
		var netrate=document.getElementById("basic_"+j).value;
		if(netrate==''){netrate=0;}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("basic_amount").value=total_basic.toFixed(2);
}

function calc_total_excise_amount()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value;
	
	for(var j=1;j<cnt;j++)
	{
		var netrate=document.getElementById("exciseValue_"+j).value;
		if(netrate==''){netrate=0;}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("exciseValue").value=total_basic.toFixed(2);
}

function calc_total_ecess_amount()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value;
	
	for(var j=1;j<cnt;j++)
	{
		var netrate=document.getElementById("ECessValue_"+j).value;
		if(netrate==''){netrate=0;}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("ECessValue").value=total_basic.toFixed(2);
}

function calc_total_sah_amount()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value;
	
	for(var j=1;j<cnt;j++)
	{
		var netrate=document.getElementById("SAHCessValue_"+j).value;
		if(netrate==''){netrate=0;}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("SAHCessValue").value=total_basic.toFixed(2);
}

function calc_total_taxable_amount()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value;
	
	for(var j=1;j<cnt;j++)
	{
		var netrate=document.getElementById("taxable_amt_"+j).value;
		if(netrate==''){netrate=0;}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("taxable_amt").value=total_basic.toFixed(2);
}

function calc_total_tax_amount()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value;
	
	for(var j=1;j<cnt;j++)
	{
		var netrate=document.getElementById("taxvalue_"+j).value;
		if(netrate==''){netrate=0;}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("taxvalue").value=total_basic.toFixed(2);
}

function calc_total_total_amount()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value;
	
	for(var j=1;j<cnt;j++)
	{
		var netrate=document.getElementById("TotalSpecial_"+j).value;
		if(netrate==''){netrate=0;}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("total_item_amount").value=total_basic.toFixed(2);
}

function claculateExcise(i,str)
{
	if(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("__");
	
	if(data[0]==1)
	{    			
	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("ECessPercentage_"+i).value='';
	document.getElementById("ECessValue_"+i).value='';
	//document.getElementById("SAHCessPercentage_"+i).value='';
	document.getElementById("SAHCessValue_"+i).value='';
	document.getElementById("exciseId_"+i).innerHTML=data[1];
	}	
	else 
	{
	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("exciseId_"+i).innerHTML=data[1];
	}
	}
	}
	xmlhttp.open("GET","checkdata.php?quotTaxId="+str+"&data="+i,true);
	xmlhttp.send();	
}


function claculateExcise1(str)
{
	var discounted=document.getElementById("basic_amount").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","calculateExcise.php?qid="+str+"&discounted="+discounted,true);
	xmlhttp.send();
}


function calamt(i)
{
	var amt=parseFloat(document.getElementById('rate_'+i).value);
	var qty=parseFloat(document.getElementById('reciveqty_'+i).value);
	var amount=amt*qty;
	document.getElementById('valuedata_'+i).value=amount.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 calculateExciseDuty1()
{
	var basic=document.getElementById("basic_amount").value;
	var ExcisePercentage=document.getElementById("ExcisePercentage").value;
	var exciseVal=basic*(ExcisePercentage/100);
	document.getElementById("exciseValue").value=exciseVal.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 calculateECess1()
{
	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(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 calculateSAHCess1()
{
	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(i)
{
	var totalDiscountedPrice=document.getElementById("basic_"+i).value;
	var exciseValue=document.getElementById("exciseValue_"+i).value;
	var ECessValue=document.getElementById("ECessValue_"+i).value;
	var SAHCessValue=document.getElementById("SAHCessValue_"+i).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);
	var TOTAL=parseFloat(totalDiscountedPrice)+parseFloat(exciseValue)+parseFloat(ECessValue)+parseFloat(SAHCessValue);
	document.getElementById("taxable_amt_"+i).value=TOTAL.toFixed(2);
}

function calculateTaxable1()
{
	var totalDiscountedPrice=document.getElementById("basic_amount").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(totalDiscountedPrice)+parseFloat(exciseValue)+parseFloat(ECessValue)+parseFloat(SAHCessValue);
	document.getElementById("taxable_amt").value=TOTAL.toFixed(2);
}




function calc_tax_amount()
{
	var taxable=document.getElementById("taxable_amt").value;
	var taxval=document.getElementById("taxvalue").value;
	var final=taxable*(taxval/100);
	document.getElementById("tot_tax_amount").value=final.toFixed(2);
}

function calculateVatCstValue()
{
	var taxable=document.getElementById("taxable_amt").value;
	var vatcstRate=document.getElementById("VatCstPerc").value;
	var VatCstAmount=taxable*(vatcstRate/100);
	document.getElementById("taxvalue").value=VatCstAmount.toFixed(2);
}

function tax_rate1(str)
{
	var taxableAmount=document.getElementById("taxable_amt").value;
	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_purchtax_val.php?taxid="+str+"&amt="+taxableAmount,true);
	xmlhttp.send();
}

function tax_rate(str,i)
{
	var billing_type=document.getElementById("hid_billing_type_"+i).value;
	
	if(billing_type=='NonExcisable')
	{
	var taxableAmount=document.getElementById("basic_"+i).value;}
	else if(billing_type=='Excisable')
	{
	var taxableAmount=document.getElementById("taxable_amt_"+i).value;}
	//alert(taxableAmount);
	if (str.length==0)
	{
	document.getElementById("taxval_"+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("taxval_"+i).innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","get_tax_val.php?taxid="+str+"&amt="+taxableAmount+"&data="+i,true);
	xmlhttp.send();
}


function cal_suroct(str)
{
	var total_tax=document.getElementById("taxvalue").value;
	var final_sur=total_tax*(str/100);
	document.getElementById("sur_oct_amt").value=final_sur.toFixed(2);
}

function cal_grandtotal(i)
{
	var type_of_bill=document.getElementById("hid_billing_type_"+i).value;
	var basic=document.getElementById("basic_"+i).value;
	//alert(basic);
	var Totaltaxable=document.getElementById("taxable_amt_"+i).value;
	var vatcstAmt=document.getElementById("taxvalue_"+i).value;
	
	
	//var roundOffVal=document.getElementById("roundOff_").value;
	
	//if(roundOffVal==''){roundOffVal='-.0';}
	
	//var test=roundOffVal.split(".");
	
	//alert(test[0]);
	
	if(basic==''){basic=0;}
	if(Totaltaxable==''){Totaltaxable=0;}
	if(vatcstAmt==''){vatcstAmt=0;}
	
	
	
	
	if(document.getElementById("hid_billing_type_"+i).value=='NonExcisable')
	{
	
	var grand_tot1=parseFloat(basic)+parseFloat(vatcstAmt);
	var grand_tot=grand_tot1.toFixed(2);
	
	}	
	
	
	else if(document.getElementById("hid_billing_type_"+i).value=='IncludeTax'){
	
	var grand_tot1= parseFloat(basic) ;
	var grand_tot=grand_tot1.toFixed(2);
	}
	else{
	
	var grand_tot1=parseFloat(Totaltaxable)+parseFloat(vatcstAmt);
	var grand_tot=grand_tot1.toFixed(2);
	
	}
	//alert(	document.getElementById("TotalSpecial_"+i).value);
	//alert(grand_tot);
	document.getElementById("TotalSpecial_"+i).value=grand_tot ;	
	//alert(	document.getElementById("TotalSpecial_"+i).value);
	
}


function cal_grandtotal1()
{
	var basic=document.getElementById("basic_amount").value;
	var Totaltaxable=document.getElementById("taxable_amt").value;
	var vatcstAmt=document.getElementById("taxvalue").value;
	var suramt=document.getElementById("sur_oct_amt").value;
	var TotalCharge=document.getElementById("TotalCharge").value;
	var packagingCharges=document.getElementById("packagingCharges").value;
	var additionalCharges=document.getElementById("additionalCharges").value;
	//var entry_tax=document.getElementById("entry_tax").value;
	if(basic==''){basic=0;}
	if(Totaltaxable==''){Totaltaxable=0;}
	if(vatcstAmt==''){vatcstAmt=0;}
	if(suramt==''){suramt=0;}
	if(TotalCharge==''){TotalCharge=0;}
	if(packagingCharges==''){packagingCharges=0;}
	if(additionalCharges==''){additionalCharges=0;}
	//if(entry_tax==''){entry_tax=0;}
	var grand_tot=parseFloat(Totaltaxable)+parseFloat(vatcstAmt)+parseFloat(suramt)+parseFloat(TotalCharge)+parseFloat(packagingCharges)+parseFloat(additionalCharges); 
	document.getElementById("grand_total").value=grand_tot.toFixed(2);
}

function calculateTotalPrice(i)
{
	if(document.getElementById("Rate_"+i).value=='')
	{
	alert("Must Enter Rate");
	document.getElementById("Rate_"+i).focus();
	}	
	else
	{
	Quantity = document.getElementById("Quantity_"+i).value;
	Rate= document.getElementById("Rate_"+i).value;
	var Total=Quantity*Rate;
	document.getElementById("totalValue_"+i).value=Total.toFixed(2);
	}
}
</script>
<!-------------------------AJAX FILES----------------------------END HERE--------------------------------------------->
<h2 align="center"><strong>Edit Purchase Bill Book</strong></h2>
<form action="" method="post">
<h3 align="center"><font color="#FF0000">Please Enter All General Details First</font></h3>
<h3 align="center">General Details</h3>
<div align="center">
<table width="60%"  cellspacing="2" cellpadding="2" style="background:#CCCCCC; border: solid 1px #999999">
<tr>
	<th width="19%"><strong>Site</strong></th>
	<td width="27%"><?php if($PV_Type=='Direct') { ?>
	<?php 
	$sql = "select loccode, locationname from locations";
	$result=DB_query($sql, $db);
	?>
	<select name="site" id="site"  class="select_purch" style="width:150px;">  <!--onchange="showdata(this.value)"-->
	<option value ="000">---Select Site---</option>
<?php
	while($myrow=DB_fetch_array($result))
	{ 
	$p='';
	if($myrow['loccode']==$site)
	$p="selected";
	
	?>
	<option value="<?php echo $myrow['loccode']; ?>" <?php echo $p; ?>> <?php echo $myrow['locationname']; ?> </option>
<?php } ?>
	</select>
	
	
	
	<?php } else { 
	echo  $s->GetFieldValue('locations',"loccode='".$site."'",'locationname'); } ?>
	 <input type="hidden" name="PV_Type" value="<?php echo $PV_Type; ?>" />
	 <input type="hidden" name="site2" value="<?php echo $site; ?>" />
	 
	 </td>
	<th width="23%" nowrap="nowrap">
	 <strong>Purchase Voucher No  </strong></th>
	<td width="31%"><input type="hidden" name="pvou" value="<?php echo $bill_book_no; ?>" />
	<?php echo $bill_book_no; ?></td>
</tr>
<tr>
	<th><strong>Vendor Name </strong></th>
	<td><?php  if($PV_Type=='Direct') { 
	
	$sql = "select id,vendor_code, vendor_name from suppliers";
$result=DB_query($sql, $db);
?>
  <select name="vendor" id="vendor" class="select_purch"  style="width:150px;">
    <option value="000">---Select Vendor---</option>
    <?php
while($myrow = DB_fetch_array($result))
{

?>
    <option value="<?php echo $myrow['id']; ?>" <?php echo($vendor==$myrow['id']?'selected':''); ?>><?php echo $myrow['vendor_name']; ?></option>
    <?php } ?>
  </select>
  <?php } else { 	echo $s->GetFieldValue('suppliers',"id='".$vendor."'",'vendor_name'); }?></td>
	<th><strong>Cash/Credit</strong></th>
	<td>
		<select name="cash_credit" class="select_purch" style="width:100%" id="cash_credit">
		  <option value="000">---Select Cash/Credit---</option>
		  <option value="Cash"<?php echo($cash_credit=='Cash'?'selected':''); ?>>Cash</option>
		  <option value="Credit"<?php echo($cash_credit=='Credit'?'selected':''); ?>>Credit</option>
		</select>
	</td>
</tr>
<tr>
<th><strong>MRN No</strong></th>
<td><?php if($PV_Type=='Direct') echo "N/A"; else  echo $s->GetFieldValue('addmrn',"id='".$mrn_no."'",'MRNNo');?></td> 
<th><strong>Credit Days</strong></th>
<td><input name="credit_days" type="text" value="<?php echo $credit_days; ?>"  style="width:100%" id="credit_days"/></td>
</tr><tr>
<th><strong>Due Date</strong></th>
<td><input type="text" name="due_date" id="due_date" class="datepicker" value="<?php echo ConvertSQLDate($due_date); ?>"  style="width:100%" /></td>
<th><strong>Date</strong></th>
<td><input type="text" name="bill_book_date" id="bill_date" class="datepicker" value="<?php echo ConvertSQLDate($bill_book_date); ?>"  style="width:100%" /></td>
<!-- <input type="text" name="textfield210" />-->

</tr>
<tr>
<th>Bill/Challan No</th>
<td><div id="orderid">
<?php if($PV_Type=='Direct') { ?> 
<input type="text" name="bill_challan_no" value="<?php echo $bill_challan_no; ?>" id="documenttype" style="width:150px;" />
<?php } else { echo $bill_challan_no; } ?></div></td>	
	
</tr>

</table>






<fieldset><legend><strong>ITEM DETAILS <?php echo $type_row['Type'];?></strong></legend>

<div style="overflow:scroll; width:950px ; ">
	<div id="itemid">
<table width="100%" border="0" cellspacing="3" cellpadding="5">
  <tr>
    <th scope="col"><strong>ITEM CODE </strong></th>
    <th scope="col"><strong>DESCRIPTION</strong></th>
    <th scope="col"><strong>MAKE</strong></th>
    <th scope="col"><strong>GRADE</strong></th>
    <th scope="col"><strong>COLOR</strong></th>
    <th scope="col"><strong>UOM</strong></th>
    <th scope="col"><strong>PO QTY </strong></th>
    <th scope="col"><strong>QTY AS PER BILL </strong></th>
    <th scope="col"><strong>QTY ACCEPTED </strong></th>
    <th scope="col"><strong>SHORT QUANTITY </strong></th>
    <th scope="col"><strong>RATE</strong></th>
   <!-- <th scope="col"><strong>DISCOUNT TYPE </strong></th>
    <th scope="col"><strong>DISCOUNT</strong></th>
	<th scope="col"><strong>DISCOUNT AMOUNT</strong></th>-->
    <!--<th scope="col"><strong>VALUE AFTER DISCOUNT </strong></th>-->
    <!--<th scope="col"><strong>TAX TYPE </strong></th>
    <th scope="col"><strong>TAX RATE </strong></th>
    <th scope="col"><strong>TAX VALUE </strong></th>
    <th scope="col"><strong>VALUE WITH TAX </strong></th>-->
    <th ><strong>Total Price</strong></th>
	<th ><strong>Discount Percent</strong></th>
	<th ><strong>Discount</strong></th>
	<th ><strong>Discount Amount</strong></th>
	<th ><strong>Sub Total</strong></th>
	<TH><strong>SPECIAL DISCOUNT</strong></TH>
	<TH><strong>SPECIAL DISCOUNT AMOUNT</strong></TH>
	<th ><strong>BASIC AMOUNT</strong></th>
	<th ><strong>Tax Type</strong></th>
	<th width="33%"><strong>Excise</strong></th>
	<th><strong>E-Cess</strong></th>
	<th><strong>SAH-E Cess</strong></th>
	<th><strong>Taxable Amount </strong></th>
	<th><strong>VAT/CST</strong></th>
	<th><strong>VAT/CST Amount</strong></th>
	<TH><strong>TOTAL AMOUNT</strong></TH>    
    <th scope="col"><strong>REMARKS</strong></th>
  </tr>
<?php 
$DetailsSQL=DB_query("SELECT * FROM purchase_bill_details where purch_billbookid='".$id."'",$db);
$i=0;
while($Details=DB_fetch_array($DetailsSQL)){
$ItemCode=$s->GetFieldValue('item_master',"id='".$Details['mst_productid']."'",'StockID');
$ItemName=$s->GetFieldValue('item_master',"id='".$Details['mst_productid']."'",'item_name');
$UnitId=$s->GetFieldValue('item_master',"id='".$Details['mst_productid']."'",'unit');
$UnitName=$s->GetFieldValue('unitsofmeasure',"unitid='".$UnitId."'",'unitname');
$PO_QTY=$s->GetFieldValue('addmrnqty',"issue_id='".$mrn_no."'",'POQty');
$QTY_As_Per_Bill=$s->GetFieldValue('addmrnqty',"issue_id='".$mrn_no."'",'qty_as_per_bill');
$Recieve_Qty=$s->GetFieldValue('addmrnqty',"issue_id='".$mrn_no."'",'reciveqty');
$ShortAccId=$s->GetFieldValue('addmrnqty',"issue_id='".$mrn_no."'",'short_accessid');
if($ReceivedAgainst!='Without PO')
{
	$Ratereadonly='readonly';
}
 ?>
  <tr>
  	<td><?php echo $ItemCode; ?><input type="hidden" name="row_id[<?php echo $i; ?>]" id="" value="<?php echo $Details['id']; ?>"></td>
	<td><?php echo $ItemName; ?><input type="hidden" name="" id="" value=""></td>
	<td><input type="hidden" name="mst_productid[<?php echo $i; ?>]" id="" value="<?php echo $Details['mst_productid'];?>"></td>
	<td><input type="hidden" name="" id="" value=""></td>
	<td><input type="hidden" name="" id="" value=""></td>
	<td><?php echo $UnitName; ?><input type="hidden" name="" id="" value=""></td>
	<td><?php echo $PO_QTY; ?><input type="hidden" name="po_qty" id="po_qty_<?php echo $i; ?>" value="<?php echo $Details['quantity']; ?>"></td>
	<td><?php echo $QTY_As_Per_Bill; ?><input type="hidden" name="" id="" value=""></td>
	<td><?php echo $Recieve_Qty; ?><input type="hidden" name="quantity[<?php echo $i; ?>]" id="Quantity_<?php echo $i; ?>" value="<?php echo $Recieve_Qty; ?>"></td>
	<td><?php echo $ShortAccId; ?></td>
	<td><input type="text" name="rate[<?php echo $i; ?>]" id="Rate_<?php echo $i; ?>" value="<?php echo $Details['rate']; ?>" onchange="calamt(<?php echo $i; ?>)" <?php echo $Ratereadonly; ?>></td>
	<td><input type="text" name="total_price[<?php echo $i; ?>]" id="totalValue_<?php echo $i; ?>" value="<?php echo $Details['total_price']; ?>"  onfocus='calculateTotalPrice(<?php echo $i; ?>)'></td>
	<td>
		<select name="discount_type[<?php echo $i; ?>]" id="PerAmo_<?php echo $i; ?>">
			<option value="0">Select</option>
			<option value="Amount"<?php echo($Details['discount_type']=='Amount'?'selected':'');?>>Amount</option>
			<option value="PercentAmt"<?php echo($Details['discount_type']=='PercentAmt'?'selected':'');?>>Percent Amt</option>
		</select>
	</td>
	<td><input type="text" name="discount[<?php echo $i; ?>]" id="Discount_<?php echo $i; ?>" onblur="DiscountAmount(this.value,<?php echo $i; ?>)" style="width:100%;" value="<?php echo $Details['discount'] ?>"  /></td>
	<td><input type="text" name="discount_amount[<?php echo $i; ?>]" id="perc_<?php echo $i; ?>"  readonly="true" style="width:100%;" onblur="checkamount(this.value,<?php echo $i; ?>)"
            value="<?php echo $Details['discount_amount'] ?>" /></td>
	<td><input type="text" id="NetRate_<?php echo $i; ?>" name="sub_total[<?php echo $i; ?>]" style="width:100%;" readonly value="<?php echo $Details['sub_total'] ?>" /></td>
	<td><input type="text" name="specialdiscount[<?php echo $i; ?>]" id="SpecialDiscount_<?php echo $i; ?>" size="10" style="width:100%;" onblur="DiscountAmountspe(this.value,<?php echo $i; ?>)" value="<?php echo $Details['specialdiscount'] ?>" /></td>
	<td><input type="text" name="specialdiscount_amount[<?php echo $i; ?>]" id="Specialperc_<?php echo $i; ?>"  style="width:100%;" size="10" onblur="checkspecialamt(this.value,<?php echo $i; ?>)" value="<?php echo $Details['specialdiscount_amount'] ?>" /></td>
	<td><input type="text" id="basic_<?php echo $i; ?>" name="basic_amt[<?php echo $i; ?>]" size="8" style="width:100%;" readonly value="<?php echo $Details['basic_amt'] ?>" /></td>
	<td><select name="tax_type[<?php echo $i; ?>]" style="width:70px" id="hid_billing_type_<?php echo $i; ?>"  onchange="checkExcisable(this.value,<?php echo $i; ?>)">
			<option value="">Select</option>
			<option value="NonExcisable"<?php echo($Details['tax_type']=='NonExcisable'?'selected':''); ?>>NonExcisable</option>
			<option value="Excisable"<?php echo($Details['tax_type']=='Excisable'?'selected':''); ?>>Excisable</option>
			<option value="IncludeTax"<?php echo($Details['tax_type']=='IncludeTax'?'selected':''); ?>>IncludeTax</option>
		</select>
	</td>
	<?php if($Details['tax_type']=='NonExcisable'){
			$disable='disabled';
			$disable1='';
			}
			else if($Details['tax_type']=='IncludeTax')
			{
			$disable='disabled';
			$disable1='disabled';
			}
			else
			{
			$disable='';
			$disable1='';
			}
	?>
	
	
	<td><select name="Excise_Duty[<?php echo $i; ?>]" id="exciseDuty_<?php echo $i; ?>" style="width:100%;" onchange="claculateExcise(<?php echo $i; ?>,this.value)" onfocus="checkexiseflag(<?php echo $i; ?>)" <?php echo $disable; ?>>
			<option value="">Select</option>
		<?php	$excise_sql=DB_query("Select * from excise_tax where caption like '%excise%' and form_type='Purchase'",$db);
			while($excise_row=DB_fetch_array($excise_sql))
			{
			$g='';
			if($Details['Excise_Duty']==$excise_row['id'])
			{$g='selected';}?>	
			<option value="<?php echo $excise_row['id']; ?>"<?php echo $g; ?>><?php echo $excise_row['caption']; ?></option>
		<?php	} ?>
	</select>
	<div id="exciseId_<?php echo $i; ?>" style="width: 100px;">
		<input <?php echo $disable; ?> name="ExcisePercentage[<?php echo $i; ?>]" id="ExcisePercentage_<?php echo $i; ?>" type="text" value="<?php echo $Details['ExcisePercentage']; ?>"  style="width:80%;" readonly />
		<input name="exciseValue[<?php echo $i; ?>]" <?php echo $disable; ?> id="exciseValue_<?php echo $i; ?>" type="text" value="<?php echo $Details['Excise_Duty_value']; ?>" style="width:70%;" onfocus="calculateExciseDuty(<?php echo $i; ?>)" />
	</div>
	</td>
	<?php 
	$cessLedgerSql1=DB_query("Select * from excise_tax where ledgertype='cess' LIMIT 0,1",$db);	 
		$cessLedgerSql2=DB_query("Select * from excise_tax where ledgertype='cess' LIMIT 1,1",$db);	 	?>
	<td>
	<select name="cess_ID1[<?php echo $i; ?>]" id="" style="width:70px;">
	<?php 
	while($cessLedger1=DB_fetch_array($cessLedgerSql1))
	{?>
		<option value="<?php echo $cessLedger1['id'];?>"><?php echo $cessLedger1['caption'];?></option>
	<?php 
	}
	?>
	</select>
	<input <?php echo $disable; ?> name="Edu_Cess_perc[<?php echo $i; ?>]" id="ECessPercentage_<?php echo $i; ?>" type="text" value="<?php echo $Details['Edu_Cess_perc']; ?>" style="width:100%;" readonly />
	<input <?php echo $disable; ?> name="Edu_Cess[<?php echo $i; ?>]" id="ECessValue_<?php echo $i; ?>" type="text" value="<?php echo $Details['Edu_Cess']; ?>" size="25" style="width:100%;"  onfocus="calculateECess(<?php echo $i; ?>)"/>
	</td>
	<td>
	<select name="cess_ID2[<?php echo $i; ?>]" id="" style="width:70px;">
	<?php 
	while($cessLedger2=DB_fetch_array($cessLedgerSql2))
	{?>
		<option value="<?php echo $cessLedger2['id'];?>"><?php echo $cessLedger2['caption'];?></option>
	<?php 
	}
	?>
	</select>
	<input <?php echo $disable; ?> name="SHE_Cess_perc[<?php echo $i; ?>]" id="SAHCessPercentage_<?php echo $i; ?>" type="text" value="<?php echo $Details['SHE_Cess_perc'] ?>" style="width:100%;" readonly/><input <?php echo $disable; ?> name="SHE_Cess[<?php echo $i; ?>]" id="SAHCessValue_<?php echo $i; ?>" type="text" value="<?php echo $Details['SHE_Cess'] ?>" size="25" style="width:100%;" readonly onfocus="calculateSAHCess(<?php echo $i; ?>)"/></td>
	<td><input type="text" <?php echo $disable; ?> name="taxable_amt[<?php echo $i; ?>]" id="taxable_amt_<?php echo $i; ?>" value="<?php echo $Details['taxable_amt'] ?>" style="width:100%;" onfocus="calculateTaxable(<?php echo $i; ?>)" readonly/></td>
	<td>
	<?php 
	$Taxsql='SELECT salestax_id,tax_code, taxrate FROM sales_tax_master';
	$Taxresult=DB_query($Taxsql,$db);?>
	<select <?php echo $disable1; ?> id="taxname_<?php echo $i; ?>" name="vat_cst[<?php echo $i; ?>]" onchange="tax_rate(this.value,<?php echo $i; ?>)" style="width:141px;">
		<option value="0">---Select---</option>
		<?php while($Taxmyrow=DB_fetch_array($Taxresult)){ ?>
		<option  value="<?php echo $Taxmyrow['salestax_id']; ?>"<?php echo($Details['vat_cst']==$Taxmyrow['salestax_id']?'selected':'') ?>><?php 
		echo $Taxmyrow['tax_code']."---".$Taxmyrow['taxrate'].'%';?></option>
		<?php } ?>
	</select></td>
	<td>
	<div id="taxval_<?php echo $i; ?>">
		<input <?php echo $disable1; ?> type="text" name="taxvalue[<?php echo $i; ?>]" id="taxvalue_<?php echo $i; ?>" style="width:100%;" value="<?php echo $Details['vat_cst_amt'] ?>" readonly="true"/>
	</div>
	</td>
	<td><input type="text" id="TotalSpecial_<?php echo $i; ?>" name="total_amount[<?php echo $i; ?>]" size="8" style="width:100%;" readonly  onfocus="cal_grandtotal(<?php echo $i; ?>);" value="<?php echo $Details['total_amount'] ?>" /></td>
	<td><input type="text" name="item_remarks[<?php echo $i; ?>]" value="<?php echo $Details['remarks'] ?>" /></td>
  </tr>
<?php $i++;}?>
<input type="hidden" name="count" id="count" value="<?php echo $i; ?>">
  </table>
  </div>
</div>

</fieldset>
<br />


<table border="1" cellspacing="1" cellpadding="2" width="100%">
<tr>
<td valign="top">
<table width="100%" align="left" style="border:solid 1px #999999; background:#CCCCCC">
<tr>
<th><strong>Ledger</strong></th>
<td>
<select name="ledger" id="ledger" style="width:220px">
<option value="000">---Select Ledger---</option>
<?php
$SQLP=DB_query("SELECT GroupId FROM accountgroupmaster where GroupName='Purchase Accounts'",$db);
					$GPId=DB_fetch_array($SQLP);
					$ledger_sql=DB_query("select AccountsID, AccountName from accountledger where UnderGroup='".$GPId['GroupId']."' ORDER BY AccountName ASC",$db);
while($ledger_row=DB_fetch_array($ledger_sql))
{
?>
<option value="<?php echo $ledger_row['AccountsID'];?>"<?php echo ($ledger_row['AccountsID']==$ledger?'selected':'')?>><?php echo $ledger_row['AccountName'];?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<th><strong>Remarks If Any</strong></th>
<td>
<textarea name="remarks" id="remarks" rows="3" cols="25"><?php echo $remarks;?></textarea></td>
</tr>
  <tr>
    <th valign="top"><strong>Excise Effect</strong></th>
    <td valign="top"><select name='Excise_Effect' style="width:auto">
	<option value="0">---</option>
	<option value="RG-23A"  <?php echo ( $Excise_Effect=='RG-23A')?'selected':''?>  >RG 23A &Iota; &Iota;nd</option>
		<option value="RG-23C"  <?php echo ( $Excise_Effect=='RG-23C')?'selected':''?>  >RG 23C &Iota; &Iota;nd</option>
		<option value="modvat"  <?php echo ( $Excise_Effect=='modvat')?'selected':''?>  >Mod Vat</option>
 	</select>&nbsp;</td>
  </tr>
  <tr>
  <th>Form Type</th>
  <td><select  name="form_type" id="form_type" style="width:100%; height:20px;">
  <option value="">Select</option>
  <?php
 $formSql=DB_query("Select * from form_type_master",$db);
while($formRow=DB_fetch_array($formSql))
{
?>
  <option value="<?php  echo $formRow['id']?>"<?php echo ($form_type==$formRow['id']?'selected':'')?>><?php  echo $formRow['form_name']?></option>
  <?php }?>
</select></td>
  </tr>
  </table></td>
<td align="right" width="50%">
<table width="100%" border="0" cellspacing="2" cellpadding="2"  style="border:solid 1px #999999; background:#CCCCCC">
                
				<tr>
                  <td><strong>TOTAL BASIC AMOUNT</strong> </td>
                  <td ><input type="text" name="total_basic_amount" id="total_basic_amount" style="width:100%;" onfocus="cal_totalbasicamount()" readonly="readonly" value="<?php echo $total_basic_amount; ?>" /></td>
                </tr>
				
				<tr>
                  <td><strong>TOTAL AMOUNT</strong> </td>
                  <td ><input type="text" name="total_item_amount" id="total_item_amount" style="width:100%;" onfocus="cal_totalamount()" readonly="readonly" value="<?php echo $total_item_amount; ?>" /></td>
                </tr>
               
                <tr>
                  <td><strong>Surcharge Amount</strong> </td>
                  <td><select name="surcharge_ledger" id="">
				  <?php 
				  $surLedger=DB_query("Select * from accountledger where ledgertype='surcharge'",$db);
				  while($surLedgerRow=DB_fetch_array($surLedger))
				  {?>
				  <option value="<?php echo $surLedgerRow['AccountsID'];?>" <?php if($surcharge_ledger==$surLedgerRow['AccountsID'])echo "selected";?>><?php echo $surLedgerRow['AccountName'];?></option>
				  <?php 	
				  }
				  ?>
				  </select>
				  <input name="sur_oct_amt" type="text" id="sur_oct_amt" value="<?php echo $sur_oct_amt; ?>" style="width:50%;" /></td>
                </tr>
                <tr>
                  <td><strong>Freight</strong> </td>
                  <td><select name="freight_ledger">
				  	<?php 
				  $frLedger=DB_query("Select * from accountledger where ledgertype='freight'",$db);
				  while($frLedgerRow=DB_fetch_array($frLedger))
				  {?>
				  <option value="<?php echo $frLedgerRow['AccountsID'];?>" <?php if($freight_ledger==$frLedgerRow['AccountsID'])echo "selected";?>><?php echo $frLedgerRow['AccountName'];?></option>
				  <?php 	
				  }
				  ?>
				  </select>
				  <input type="text" name="freight" id="TotalCharge" value="<?php echo $freight; ?>" style="width:50%;"/></td>
                </tr>
                <tr>
                  <td><strong>Pkg Charges</strong> </td>
                  <td><select name="pckg_ledger">
				  	<?php 
				  $pckLedger=DB_query("Select * from accountledger where ledgertype='pckcharge'",$db);
				  while($pckLedgerRow=DB_fetch_array($pckLedger))
				  {?>
				  <option value="<?php echo $pckLedgerRow['AccountsID'];?>" <?php if($pckg_ledger==$pckLedgerRow['AccountsID'])echo "selected";?>><?php echo $pckLedgerRow['AccountName'];?></option>
				  <?php 	
				  }
				  ?>
				  </select>
				  <input name="pkg_charges" id="packagingCharges" type="text" value="<?php echo $pkg_charges; ?>" style="width:50%;" /></td>
                </tr>
                <tr>
                  <td><strong>Additional Charges</strong> </td>
                  <td><select name="addcharge_ledger">
				  	<?php 
				  $addLedger=DB_query("Select * from accountledger where ledgertype='addcharge'",$db);
				  while($addLedgerRow=DB_fetch_array($addLedger))
				  {?>
				  <option value="<?php echo $addLedgerRow['AccountsID'];?>" <?php if($addcharge_ledger==$addLedgerRow['AccountsID'])echo "selected";?>><?php echo $addLedgerRow['AccountName'];?></option>
				  <?php 	
				  }
				  ?>
				  </select>
				  <input name="add_charges" id="additionalCharges" type="text" value="<?php echo $add_charges; ?>" style="width:50%;" /></td>
                </tr>
				<tr>
					<td><strong>Round Off</strong></td>
					<td><input type="text" name="round_off" id="round_off" value="<?php echo $round_off; ?>" style="width:100%;" /></td>
				</tr>
				
                <tr>
                  <td><input type="button" name="grandbutton" value="Grand Total" onclick="cal_roundamt()"/></td>
                  <td><input type="text" name="GrandTotal" id="GrandTotal" style="width:100%;" readonly="readonly" onfocus="cal_grandamount()" value="<?php echo $GrandTotal; ?>" /></td>
                </tr>
              </table>
</td>
</tr>
</table>
<br />
<p align="center"><input type="submit" name="Save" value="Save" onclick="return check()"/></p>
</form>
<script language="javascript" type="text/javascript">
	function cal_totalbasicamount()
{
	var totalbas=0;
	var tt=document.getElementById("count").value;
	for(var i=0;i<tt;i++)
	{
		var basamt=parseFloat(document.getElementById("basic_"+i).value);
		totalbas=parseFloat(totalbas)+basamt;	
	}	
	document.getElementById("total_basic_amount").value=totalbas.toFixed(2);	

}	
function showdata(str)
{
	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("__");	
	//alert(data);
	document.getElementById("vender").innerHTML=data[0];
	document.getElementById("hid-Godown-div").innerHTML=data[1];
	}
	}
	xmlhttp.open("GET","purchasebillnogenerate.php?site="+str,true);
	xmlhttp.send();
}
	
function DiscountAmount(DA,i)
{
	var discount_percent = document.getElementById("PerAmo_"+i).value;
	var rate = document.getElementById("Rate_"+i).value;
	var discount = document.getElementById("Discount_"+i).value;
	var quantity = document.getElementById("Quantity_"+i).value;
	var totalPrice = quantity*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 = parseFloat(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 checkamount(str,i)
{
	var k=parseFloat(document.getElementById("totalValue_"+i).value);
	var t=parseFloat(str);
	if(t>k)
	{
	alert("Discount Amount Not Greater Then Rate");
	document.getElementById("Discount_"+i).value='';
	document.getElementById("NetRate_"+i).value='';
	document.getElementById("perc_"+i).value='';
	document.getElementById("Discount_"+i).focus();	
	}	
}

function checkspecialamt(str,i)
{
	var k=parseFloat(document.getElementById("NetRate_"+i).value);
	var t=parseFloat(str);
	if(t>k)
	{
	alert("Special Discount Amount Not Greater Then Sub Total");
	document.getElementById("SpecialDiscount_"+i).value='';
	document.getElementById("Specialperc_"+i).value='';
	document.getElementById("basic_"+i).value='';
	document.getElementById("SpecialDiscount_"+i).focus();	
	}		
}	

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("ExcisePercentage_"+i).disabled=true;
	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("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("ExcisePercentage_"+i).value='';
	//	document.getElementById("taxvalue").disabled=true;
	//document.getElementById("sur_oct_perc").disabled=true;
	//document.getElementById("sur_oct_amt").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("taxable_amt_"+i).disabled=false;
	document.getElementById("taxname_"+i).disabled=false;
	document.getElementById("taxvalue_"+i).disabled=false;
	document.getElementById("sur_oct_perc_"+i).disabled=false;
	document.getElementById("sur_oct_amt_"+i).disabled=false;
	document.getElementById("TotalCharge_"+i).disabled=false;
	document.getElementById("packagingCharges_"+i).disabled=false;
	document.getElementById("additionalCharges_"+i).disabled=false;
	document.getElementById("roundOff_"+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 checkexiseflag(i)
{
	if(document.getElementById("hid_billing_type_"+i).value=='')
	{
	alert("Must Select Tax Type");
	}	
}

function cal_totalamount()
{
	var total=0;
	var t=document.getElementById("count").value;
	for(var i=0;i<t;i++)
	{
	var amt=parseFloat(document.getElementById("TotalSpecial_"+i).value);
	total=parseFloat(total)+amt;	
	}	
	document.getElementById("total_item_amount").value=total.toFixed(2);	
}

function cal_roundamt()
{
	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 packagingCharges=document.getElementById("packagingCharges").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;}
	var roundOffVal=document.getElementById("round_off").value;
	var grand=document.getElementById("GrandTotal").value;
	grandtotal=parseFloat(total)+parseFloat(sur_oct_amt)+parseFloat(TotalCharge)+parseFloat(packagingCharges)+parseFloat(additionalCharges);
	if(roundOffVal==''){roundOffVal='-.0';}
	var test=roundOffVal.split(".");
	if(test[0]=='-')
	{
	var dot=".";
	var roundval=dot + test[1];
	document.getElementById("GrandTotal").value=parseFloat(grandtotal) - parseFloat(roundval);
	}
	else
	{
	var dot=".";
	var roundval=dot + test[1];
	document.getElementById("GrandTotal").value=(parseFloat(grandtotal) + parseFloat(roundval)).toFixed(2);	
	}
}

function cal_grandamount()
{
	var total=document.getElementById("basic_amount").value;
	var sur_oct_amt=document.getElementById("sur_oct_amt").value;
	var TotalCharge=document.getElementById("TotalCharge").value;
	var packagingCharges=document.getElementById("packagingCharges").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;}
	var roundOffVal=document.getElementById("round_off").value;
	var grand=document.getElementById("GrandTotal").value;
	grandtotal=parseFloat(total)+parseFloat(sur_oct_amt)+parseFloat(TotalCharge)+parseFloat(packagingCharges)+parseFloat(additionalCharges);
	if(roundOffVal==''){roundOffVal='-.0';}
	var test=roundOffVal.split(".");
	if(test[0]=='-')
	{
	var dot=".";
	var roundval=dot + test[1];
	document.getElementById("GrandTotal").value=parseFloat(grandtotal) - parseFloat(roundval);
	}
	else
	{
	var dot=".";
	var roundval=dot + test[1];
	document.getElementById("GrandTotal").value=parseFloat(grandtotal) + parseFloat(roundval);	
	}
	//document.getElementById("GrandTotal").value=grandtotal.toFixed(2);	
}
</script>


