<div class="PurchaseprintBill">
	<div class="content">
	
	<div class="main-content">
		
		<!-- Puchase detail -->		
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-user"></i> Purchase detail</h3> <em> - Add New Purchase Record</em>
				<div class="btn-group widget-header-toolbar">
					<a class="btn-borderless btn-focus clickme" href="purchaseView" title="Focus" ><i class="fa fa-eye"></i></a>
				</div>
			</div>
			<div class="widget-content">
				<form id="frmPurchase" name="frmPurchase" data-parsley-validate novalidate method="POST">
				<input type="hidden" name="_token" value="{{ csrf_token() }}">
				<input type="hidden" name="billtype" id="billtype" value="Purchase">
					<div class="row">											
						<div class="alert  alert-info "  style="display:none">
							<ul><li class="fa fa-cog  fa-1x fa-fw"></li>Kindly enter Purchase Detail..</ul>
						</div>						
						<div class="alert alert-success print-success-msg" style="display:none">
							<ul></ul>
						</div>	
						<div class="alert alert-danger print-error-msg" style="display:none">
							<ul></ul>
						</div>
						
						<div class="col-md-6">
							<!-- ADVANCED VALIDATION -->
							<div class="widget-content">  
								
								<div class="form-group">
									<label for="text-input1">Bill No.*</label>
									<input type="text" name="billno" id="billno" class="form-control" value="{{ $bill['Add_Bill']}}" disabled>
									<small class="text-danger" id="erritemcode"></small>
								</div>
								
								<div class="form-group">
									<label for="multiselect1">Company*</label>
									<select name="itemcategory" id="itemcategory" class="form-control" >
										<option value="0">-- Select --</option>
										<?php foreach($bill['company'] as $comp){
											echo "<option value='".$comp->cmp_id."' >".$comp->cmp_name."";
											echo "</option>";
										}?>
									</select>
									<small class="text-danger" id="erritemcategory"></small>
								</div> 
							
								<div class="form-group">
									<label for="multiselect1">Shed*</label>
									<select name="shedName" id="shedName" class="form-control" >
										<option value="0">-- Select --</option>
										<?php foreach($bill['ShedName'] as $Shd)
											{
												echo "<option value='".$Shd->shd_id."' >".$Shd->shd_name."</option>";
											}
										?>
									</select>
									<small class="text-danger" id="erritemcategory"></small>
								</div> 
								
							</div>
							<!-- END ADVANCED VALIDATION -->
						</div>
						
						<div class="col-md-6">
							<!-- ADVANCED VALIDATION -->
							<div class="widget-content">
								
								<div class="form-group">
									<label for="text-input1">Bill date*</label>
									<input type="date" name="billdate" id="billdate" class="form-control"value="<?php echo date('Y-m-d');?>" data-date-format="yyyy-mm-dd" >
									<small class="text-danger" id="erritemtype"></small>
								</div>
								
								<div class="form-group">
									<label for="multiselect1">Seller*</label>
									<select name="itemcategory1" id="itemcategory1" class="form-control" required onchange="loadsubcat(this.value)">
										<option value="0">-- Select --</option>
									</select>
									<small class="text-danger" id="erritemcategory"></small>
								</div>
								
								<div class="form-group">
									<label for="multiselect1">Batch*</label>
									<select name="SubBatch" id="SubBatch" class="form-control" required onchange="CountItem()">
										<option value="0">-- Select --</option>
									</select>
									<small class="text-danger" id="erritemcategory"></small>
								</div>
								
							</div>
							<!-- END ADVANCED VALIDATION -->
						</div> 
					</div>		
			</div>
		</div>
		<!-- END Puchase detail -->		
		
		
		<!-- ITEM RECORD STARTS HERE-->		
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-user"></i> Item Record</h3> <em> - Add item details</em>
			</div>
			<div class="widget-content">
				<div class="row">
					<div class="col-sm-3 nopadding">
						<div class="form-group">
							<label for="multiselect1">Item Name*</label>
							<br/>
							<select name="itemname[]" id="itemname0" class="form-control" onchange="product(0)" required>
								<option value="0">-- Select --</option>
								<?php foreach($bill['itemCompany'] as $item)
									{
								      	echo "<option value='".$item->itm_id."' >".$item->itm_name."";
									    echo "</option>";
									}
								?>
							</select>
						</div>
					</div>
					<div class="col-sm-2 nopadding">
						<div class="form-group">
							<label for="multiselect1">Cost*</label>
							<br/>
							<input type="text" name="itemcost[]" id="itemcost0" class="form-control" onkeyup="calculate(0)" >
							<small class="text-danger" id="errstatus"></small>
						</div>
					</div>
					<div class="col-sm-2 nopadding">
						<div class="form-group">
							<label for="multiselect1">Quantity*</label>
							<br/>
							<input type="text" name="itemquantity[]" id="itemquantity0" class="form-control" onkeyup="calculate(0)" >
							<small class="text-danger" id="errstatus"></small>
						</div>
					</div>
					<div class="col-sm-2 nopadding">
						<div class="form-group">
							<label for="multiselect1">Total*</label>
							<br/>
							<input type="text" name="itemtotal[]" id="itemtotal0" onkeyup="findTotal()" class="form-control" readonly>
							<small class="text-danger" id="errstatus"></small>
						</div>
					</div>
					<div class="clear"></div>
				</div>
				
				<div id="education_fields"  class="row">
				</div>
				
				<div class="row">
					<div class="col-sm-3 nopadding">
						<div class="form-group">
							<div class="input-group">
								
								<div class="input-group-btn">
									
									<button type="button"  value="Add" class="form-control" onclick="education_fields();" >ADD</button>
									
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="widget-footer">
				<label>
					Sub Total - Rs. 
					<input readonly style="border:0px; font-size:30; color:red;" id="subtotal" name="subtotal" value="0" />
				</label>
			</div>			
		</div>
		<!-- ITEM RECORD ENDS HERE -->
		
				
		<!-- BILL DISCOUNT ENTRY STARTS HERE -->		
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-user"></i> Discount Entry</h3> <em> - Add Discount details</em>
			</div>
			<div class="widget-content">
				<div class="row">			
					<div class="col-sm-4 nopadding">
						<div class="form-group">
							<label for="multiselect1">Discount Type</label>
							<br/>                                
							<select name="discounttype" id="discounttype" class="form-control">
								<option value="0">-- Select --</option>
								<option value="Flat Discount">Flat Discount</option>
								<option value="Percentage Discount">Percentage Discount</option>
							</select>
						</div>
					</div>
					<div class="col-sm-4 nopadding">
						<div class="form-group">
							<label for="multiselect1">Discount Amount</label>
							<br/>
							<input type="text" name="discountAmount" id="discountAmount" class="form-control" onkeyup="calculatediscount()" value="0">
						</div>
					</div>
					<div class="col-sm-4 nopadding">
						<div class="form-group">
							<label for="multiselect1">Total Discount Amount</label>
							<br/>
							<input type="text" name="totdiscountAmount" id="totdiscountAmount" class="form-control" readonly value="0">
						</div>
					</div>			
				</div>						
			</div>			
			<div class="widget-footer">
				<label>Total after Discount - Rs. <input readonly style="border:0px; font-size:30; color:red;" id="totalafterdiscount" name="totalafterdiscount" value="0" /></label>
			</div>
		</div>
		<!-- BILL DISCOUNT ENTRY  ENDS HERE  -->		

		<!-- BILL TAX RECORD STARTS HERE -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-user"></i> TAX Entry</h3> <em> - Add Tax details</em>
			</div>
			<div class="widget-content">
				<div class="row">			
					<div class="col-sm-4 nopadding">
						<div class="form-group">
							<label for="multiselect1">Tax Type*</label>
							<br/>
							<input type="hidden" name="taxno[]" value="0">
							<select name="tax[]" id="tax0" class="form-control" required onchange="taxClick(0)">
								<option value="0">-- Select --</option>
								<?php
									foreach($bill['taxanount'] as $tax)
									{
										echo "<option value='".$tax->tax_id."' >".$tax->tax_type."(".$tax->tax_amt."% Tax)</option>";
									}
								?>
							</select>
						</div>
					</div>
					<div class="col-sm-2 nopadding">
						<div class="form-group">
							<label for="multiselect1">Tax Percentage</label>
							<br/>
					<input type="text" name="taxPercentage[]" id="taxPercentage0" class="form-control" readonly value="0">
						</div>
					</div>
					<div class="col-sm-3 nopadding">
						<div class="form-group">
							<label for="multiselect1">Tax Amount</label>
							<br/>
					<input type="text" name="taxAmount[]" id="taxAmount0" class="form-control" readonly value="0">
						</div>
					</div>
				</div>
				<div id="div_tax_fields" class="row">
					
				</div>
				<div class="row">
					<div class="col-sm-3 nopadding">
						<div class="form-group">
							<div class="input-group">
								
								<div class="input-group-btn">
									
									<button type="button"  value="Add" class="form-control" onclick="fun_tax_fields();" >ADD</button>
									
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>			
			<div class="widget-footer">
				<label>Total after Tax - Rs. <input readonly style="border:0px; font-size:30; color:red;" id="totalamtaftertax" name="totalamtaftertax"  value="0" /></label>
			</div>
		</div>
		<!-- BILL TAX RECORD ENDS HERE  -->
		
		<!-- OTHER CHARGES STARTS HERE -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-user"></i> Other Charges</h3> <em> - Add other charges details</em>
			</div>
			<div class="widget-content">
				<div class="row">
					<input type="hidden"  id="num" name="num" value="1" >
					<div class="col-sm-5 nopadding">
						<div class="form-group">
							<label for="multiselect1">Title*</label>
							<br/>
							<input type="text" name="otherchargetitle[]" id="otherchargetitle0" class="form-control" >
							<small class="text-danger" id="errstatus"></small>
						</div>
					</div>
					<div class="col-sm-3 nopadding">
						<div class="form-group">
							<label for="multiselect1">Cost*</label>
							<br/>
							<input type="text" name="otherchargecost[]" id="otherchargecost0" onkeyup="calculateothercharges()" class="form-control" value="0">
							<small class="text-danger" id="errstatus"></small>
						</div>
					</div>						
				</div>
				
				<div id="div_othercharge_fields" class="row">
					
				</div>
				
				<div class="row">
					<div class="col-sm-3 nopadding">
						<div class="form-group">
							<div class="input-group">								
								<div class="input-group-btn">									
									<button type="button"  value="Add" class="form-control" onclick="fun_othercharge_fields();" >ADD</button>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="widget-footer">
				<label>Total After Other Charges - Rs. <input readonly style="border:0px; font-size:30; color:red;" id="othchargetotal" name="othchargetotal" value="0" /></label>
			</div>
		</div>
		<!-- OTHER CHARGES ENDS HERE -->		
		
		<!-- GRAND TOTAL WITH BUTTON STARTS HERE -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-user"></i> Billing</h3> <em> -Billing Calculation</em>
			</div>
			<div class="widget-content">					
					<div class="col-md-12">
						<div class="form-group">
							<label for="text-input1">Sub Total.*</label>
							<input type="text" name="billingsubtotal" id="billingsubtotal" class="form-control" value="0" onchange="validatecode(this.value)" readonly>
							<small class="text-danger" id="erritemcode"></small>
						</div>
						<div class="form-group">
							<label for="text-input1">Discount.*</label>
							<input type="text" name="billingdiscount" id="billingdiscount" class="form-control" value="0" onchange="validatecode(this.value)" readonly>
							<small class="text-danger" id="erritemcode"></small>
						</div>							
						<div class="form-group">
							<label for="text-input1">Tax.*</label>
							<input type="text" name="billingtax" id="billingtax" class="form-control" value="0" onchange="validatecode(this.value)" readonly>
							<small class="text-danger" id="erritemcode"></small>
						</div>						
						<div class="form-group">
							<label for="text-input1">Other Charges.*</label>
							<input type="text" name="billingothercharge" id="billingothercharge" class="form-control" value="0" onchange="validatecode(this.value)" readonly>
							<small class="text-danger" id="erritemcode"></small>
						</div>
						<div class="form-group">
							<label for="text-input1">Grand Total.*</label>
							<input type="text" name="billinggrandtotal" id="billinggrandtotal" class="form-control" value="0" onchange="validatecode(this.value)" readonly>
							<small class="text-danger" id="erritemcode"></small>
						</div>
						<div class="form-group">
							<label>Note (Any note)</label>
							<textarea class="form-control" name="billingdescription" id="billingdescription" rows="4" cols="30" >{{ isset($data) ? $data[0]->itm_note : ''}}</textarea>
							<small class="text-danger" id="errdescription" ></small>
						</div>
						<div class="form-group">
							<label for="multiselect1">Status*</label>
							<br/>
							<select name="status" id="status" class="form-control" required>
								<option value="Active" 
									@if(isset($data))
										@if($data[0]->itm_status == "Active")
											selected='selected' 
										@endif
									@endif
								> Active</option>
								<option value="Inactive"
								@if(isset($data))
								@if($data[0]->itm_status == "Inactive")
									selected='selected' 
									@endif
									@endif
								>Inactive</option>
							</select>
						</div>
						<!-- END ADVANCED VALIDATION -->
					</div>
					<div class="col-md-12">
						<div class="form-group">
							<center><button  class="btn btn-primary billgenerate">Click Here to Add Purchase Report</button></center>
						</div>
					</div>                        
				</form>				
			</div>
		</div>
		<!-- GRAND TOTAL WITH BUTTON ENDS HERE -->
	</div>
</div>
</div>
<!-- /main -->
<script>
	$('.clickme').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href'));
	});
	
	$('.view').click(function(e)
	{
		$('#sideMenu').load('purchaseView');
	});
	
	//###Coding to create new row for items starts here
	var room = 0;
	function education_fields() 
	{
		room++;
		var objTo = document.getElementById('education_fields');
		var divtest = document.createElement("div");
		divtest.setAttribute("class", "form-group removeclassa"+room);
		var rdiv = 'removeclass'+room;
		divtest.innerHTML = '<div class="col-sm-3 nopadding"><div class="form-group"><select name="itemname[]" id="itemname'+ room +'" onchange="product('+ room +')" class="form-control" required><option value="0">-- Select --</option><?php foreach($bill['itemCompany'] as $item){echo"<option value=".$item->itm_id.">".$item->itm_name."</option>";}?></option></select></div></div><div class="col-sm-2 nopadding"><div class="form-group"><input type="text" onkeyup="calculate('+ room +')" name="itemcost[]" id="itemcost'+ room +'" class="form-control"></div></div><div class="col-sm-2 nopadding"><div class="form-group"><input type="text" name="itemquantity[]" id="itemquantity'+ room +'" class="form-control" onkeyup="calculate('+ room +')" ></div></div><div class="col-sm-2 nopadding"><div class="form-group"><input type="text" name="itemtotal[]"  id="itemtotal'+ room +'" class="form-control" readonly ></div></div><div class="col-sm-3 nopadding"><div class="form-group"><div class="input-group"> <div class="input-group-btn"><button type="button" class="form-control" onclick="remove_education_fields('+ room +');" >REMOVE</button></div></div></div></div><div class="clear"></div>';		
		objTo.appendChild(divtest)
		$('#num').val(room);
		ijk=$('#num').val(room);
	}
	//###Coding to create new row for items ends here
	
	//###Coding to remove row of items starts here
	function remove_education_fields(rid) {
		$('.removeclassa'+rid).remove();
		findTotal();
	}
	//###Coding to remove row of items ends here
	
	//###Coding to create new row for tax starts here
	var vartax = 0;
	function fun_tax_fields() {
		vartax++;
		var objTo = document.getElementById('div_tax_fields');
		var divtest = document.createElement("div");
		divtest.setAttribute("class", "form-group removeclassb"+vartax);
		var rdiv = 'removeclass'+vartax;
		divtest.innerHTML = '<div class="col-sm-4 nopadding"><div class="form-group"><input type="hidden" name="taxno[]" value="'+ vartax +'"><select name="tax[]" id="tax'+ vartax +'" class="form-control" required onchange="taxClick('+ vartax +')"><option value="0">-- Select --</option><?php foreach($bill['taxanount'] as $tax){ echo "<option value=".$tax->tax_id." >".$tax->tax_type."(".$tax->tax_amt."% Tax)</option>";} ?></select></div></div><div class="col-sm-2 nopadding"><div class="form-group"><input type="text" name="taxPercentage[]" id="taxPercentage'+ vartax +'" class="form-control" readonly></div></div><div class="col-sm-3 nopadding"><div class="form-group"><input type="text" name="taxAmount[]" id="taxAmount'+ vartax +'" class="form-control" readonly></div></div><div class="col-sm-3 nopadding"><div class="form-group"><div class="input-group"> <div class="input-group-btn"><button type="button" class="form-control" onclick="remove_tax_fields('+ vartax +');" >REMOVE</button></div></div></div></div><div class="clear"></div>';
		objTo.appendChild(divtest)
		$('#num').val(vartax);
		ijk=$('#num').val(vartax);
		
	}
	//###Coding to create new row for tax ends here
	
	//###Coding to remove row of tax starts here
	function remove_tax_fields(rid) {
		$('.removeclassb'+rid).remove();
	}
	//###Coding to remove row of items ends here
	
	//###Coding to create new row for Other charge starts here	
	var varothercharge = 1;
	function fun_othercharge_fields() {
		varothercharge++;
		var objTo = document.getElementById('div_othercharge_fields');
		var divtest = document.createElement("div");
		divtest.setAttribute("class", "form-group removeclassc"+varothercharge);
		var rdiv = 'removeclass'+varothercharge;
		
		divtest.innerHTML = '<div class="col-sm-5 nopadding"><div class="form-group"><input type="text" name="otherchargetitle[]" id="otherchargetitle'+ varothercharge +'" class="form-control" ><small class="text-danger" id="errstatus"></small></div></div><div class="col-sm-3 nopadding"><div class="form-group"><input type="text" name="otherchargecost[]" onkeyup="calculateothercharges()" id="otherchargecost'+ varothercharge +'" class="form-control" value="0"><small class="text-danger" id="errstatus"></small></div></div><div class="col-sm-4 nopadding"><div class="form-group"><div class="input-group"> <div class="input-group-btn"><button type="button" class="form-control" onclick="remove_othercharge_fields('+ varothercharge +');" >REMOVE</button></div></div></div></div><div class="clear"></div>';
		
		objTo.appendChild(divtest)
		$('#num').val(varothercharge);
		ijk=$('#num').val(varothercharge);		
	}
	//###Coding to create new row for Other charge ends here
	
	//###Coding to remove row of Other charge starts here
	function remove_othercharge_fields(rid) {
		$('.removeclassc'+rid).remove();
	}
	//###Coding to remove row of Other charge ends here
	
	//###Load seller records on select of company starts here
	$('#itemcategory').change(function(e)
	{
		id=$('#itemcategory').val();
		e.preventDefault();
		$.ajax({
			url: 'SubCompanyDetail',
			type: 'get',
			data:'&compId='+id,
			success: function(data) {
				
				var sel = document.getElementById('itemcategory1');
				for (i = sel.length - 1; i >= 0; i--) {
					sel.remove(i);
				}
				
				var jsonData = JSON.parse(data);
				for (var i = 0; i < jsonData.length; i++) {
					var prop = jsonData[i];
					$("#itemcategory1").append("<option value=\"" + prop.cst_id +"\">"+ prop.cst_name +"</option>");
				}
				
			}
		});
	});
	//###Load seller records on select of company ends here
	
	//### On change of shed load batch starts here
	$('#shedName').change(function(e)
	{
		id=$('#shedName').val();
		e.preventDefault();
		
		$.ajax({
			url: 'BatchshedNameDetail',
			type: 'get',
			data:'&Batch='+id,
			success: function(data) {
				
				var sel = document.getElementById('SubBatch');
				for (i = sel.length - 1; i >= 0; i--) {
					sel.remove(i);
				}
				
				var jsonData = JSON.parse(data);
				for (var i = 0; i < jsonData.length; i++) {
					var prop = jsonData[i];
					$("#SubBatch").append("<option value=\"" + prop.bch_id +"\">"+ prop.bch_batchcode +"</option>");
				}				
			}
		});
	})
	//### On change of shed load batch ends here
	
	//### Item price load on item select starts here
	i=1;
	function product(ij)
	{
		number=$('#num').val();
		id=$('#itemname'+ij).val();
		// e.preventDefault();
		$.ajax({
			url: 'itemAmount',
			type: 'get',
			data:'&ItemId='+id,
			success: function(data)
			{
				$('#itemcost'+ij).val(data['itemAmount1']);
				$('#itemquantity'+ij).val(1);
				$('#itemtotal'+ij).val(data['itemAmount1']);
				//calculate(ij);
				findTotal();
			}
		});
		//});
	}
	//### Item price load on item select ends here
	
	//### Calculate individual Item cost starts here
	function  calculate(number) {
		var summs;
		var nValue;
		var amount;
		
		var price=$('#itemcost'+number).val();
		nValue = document.getElementById("itemquantity"+number).value;
		amount=(nValue*price);
		
		document.getElementById("itemtotal"+number).value=amount ;	
		findTotal();
	}
	//### Calculate individual Item cost ends here
	
	//### Total amount of the items starts here
	function findTotal()//quantity
	{
		var sum=0;
		var inps = document.getElementsByName('itemtotal[]');
		for (var i = 0; i <inps.length; i++) 
		{
			sum = sum + parseFloat(inps[i].value);
			//alert("pname["+i+"].value="+inp.value);
		}
		document.getElementById('subtotal').value=sum;
		document.getElementById('billingsubtotal').value=sum;
		calculatediscount();
	}
	//### Total amount of the items ends here
	
	//###Discount calculation starts here
	function calculatediscount()
	{
		if(document.getElementById('discounttype').value == "Flat Discount" )
		{
			document.getElementById('totdiscountAmount').value=document.getElementById('discountAmount').value;
		}
		if(document.getElementById('discounttype').value == "Percentage Discount")
		{
			document.getElementById('totdiscountAmount').value=(parseFloat(document.getElementById('discountAmount').value)*parseFloat(document.getElementById('subtotal').value)) / 100;
		}
		//alert(parseFloat(document.getElementById('subtotal').value));
		document.getElementById('totalafterdiscount').value = parseFloat(document.getElementById('subtotal').value) - parseFloat(document.getElementById('totdiscountAmount').value);
		document.getElementById('billingdiscount').value = parseFloat(document.getElementById('totdiscountAmount').value);
		taxcalculation();
	}
	//###Discount calculation ends here

	//### Tax calculation code starts here
	function taxClick(txid)
	{
		id=document.getElementById("tax"+txid).value;
		$.ajax({
			url: 'TaxAmount',
			type: 'get',
			data:'&TaxId='+id,
			success: function(data)
			{
				$tax=data['tax_amt'];
				$tax1=$tax/100; 
				discountAmount=document.getElementById("totalafterdiscount").value;
				$totalAmount=parseInt(discountAmount)*$tax1;
				$('#taxAmount').val($totalAmount);
				tota1=parseInt(discountAmount)+parseInt($totalAmount);
				document.getElementById("taxPercentage"+txid).value=$tax;
				taxcalculation()
			}		
		});
	}
	//### Tax calculation code ends here
	
	//### Tax Total calculation starts here
	function taxcalculation()
	{
		var sum=0;
		var inps = document.getElementsByName('taxno[]');
		for (var i = 0; i <inps.length; i++)
		{		
			document.getElementById('taxAmount'+inps[i].value).value=(parseFloat(document.getElementById("totalafterdiscount").value)*parseFloat(document.getElementById("taxPercentage"+inps[i].value).value)) / 100;
			sum = sum + parseFloat(document.getElementById('taxAmount'+inps[i].value).value);
		}
		document.getElementById("totalamtaftertax").value= parseFloat(document.getElementById("totalafterdiscount").value) + sum;
		document.getElementById("billingtax").value= sum;		
		calculateothercharges();
	}
	//### Tax Total calculation ends here
	
	//###Other Charges calculation starts here
	function calculateothercharges()
	{
		var sum=0;
		var inps = document.getElementsByName('otherchargecost[]');
		for (var i = 0; i <inps.length; i++)
		{
			sum = sum + parseFloat(inps[i].value);
		}
		document.getElementById("othchargetotal").value= parseFloat(document.getElementById("totalamtaftertax").value) + parseFloat(sum);
		document.getElementById("billingothercharge").value= sum;		
		totalcalculate();
	}
	//###Other Charges calculation ends here
	
	function totalcalculate()
	{
		document.getElementById("billinggrandtotal").value = parseFloat(document.getElementById("billingsubtotal").value) - parseFloat(document.getElementById("billingdiscount").value) + parseFloat(document.getElementById("billingtax").value) + parseFloat(document.getElementById("billingothercharge").value);
	}
	
	function  subctruct(price)
	{
		var amount;
		
		price=price;
		totalAmount1=$('#grandTotal').val();
		amount=(totalAmount1-price);
		
		document.getElementById("grandTotal1").value=amount;
		document.getElementById("grandTotal").value=amount;
		document.getElementById("sometotal").value=amount;
	}
	
	//### Billing submission entry code starts here
	flag=0;
	$('.billgenerate').click(function(e)
	{
		//billno 	itemcategory shedName billdate itemcategory1 SubBatch 
		e.preventDefault();
		var _token = $("input[name='_token']").val();
		var billtype = $("input[name='billtype']").val();
		var billno = $("input[name='billno']").val();
		var company = $("#itemcategory").val();
		var shedName = $("#shedName").val();
		var billdate = $("input[name='billdate']").val();
		var seller = $("#itemcategory1").val();
		var SubBatch = $("#SubBatch").val();
		var bil_note = $("#billingdescription").val();
		var status = $("#status").val();
		var billinggrandtotal = $("input[name='billinggrandtotal']").val();							   
		
		//itemname[] itemcost[] itemquantity[] itemtotal[]		
		arritemnames = [];
		arritemcosts = [];
		arritemquantities = [];
		var itemname= document.getElementsByName("itemname[]");
		var itemcost= document.getElementsByName("itemcost[]");
		var itemquantity= document.getElementsByName("itemquantity[]");
		for (index = 0; index < itemname.length; ++index) {
			arritemnames[index] = itemname[index].value;
			arritemcosts[index] = itemcost[index].value;
			arritemquantities[index] = itemquantity[index].value;
		}

		//Discount		
		var discounttype = $("#discounttype").val();
		var discountAmount = $("input[name='discountAmount']").val();
		 
		//Tax
		arrtaxids = [];
		arrtaxPercentages = [];
		var taxid= document.getElementsByName("tax[]");
		var taxPercentage= document.getElementsByName("taxPercentage[]");
		for (index = 0; index < taxid.length; ++index) {
			arrtaxids[index] = taxid[index].value;
			arrtaxPercentages[index] = taxPercentage[index].value;
		}		
		
		//Other charges
		otherchargetitles = [];
		otherchargecosts = [];
		var otherchargetitle= document.getElementsByName("otherchargetitle[]");
		var otherchargecost= document.getElementsByName("otherchargecost[]");
		for (index = 0; index < otherchargetitle.length; ++index) {
			otherchargetitles[index] = otherchargetitle[index].value;
			otherchargecosts[index] = otherchargecost[index].value;
		}		
		
		
		$.ajax({
			url: "billgenerate",
			type:'POST',
			dataType: 'json',
    //cache : false,
	//processData: false,
			data: {_token:_token,billtype:billtype, billno:billno, company:company, shedName:shedName, billdate:billdate, seller:seller, SubBatch:SubBatch,bil_note:bil_note,status:status,billinggrandtotal:billinggrandtotal,itemname:arritemnames,itemcost:arritemcosts,itemquantity:arritemquantities,discounttype:discounttype,discountAmount:discountAmount,taxid:arrtaxids,taxPercentage:arrtaxPercentages,otherchargetitle:otherchargetitles,otherchargecost:otherchargecosts},
			success: function(data)
			{
				if(data.status == "Success")
				{
					BillPrint(data.bil_id);
					//$('#sideMenu').load('PurchaseprintBill');
					//BillPrint(Billno)
				}
				else
				{
					alert("Failed to Generate Purchase Bill..");
					//printErrorMsg(data.message)
				}
			}
		});
	});
	//### Billing submission entry code ends here
	
	function BillPrint(Billno)
	{
		$.ajax({
					url: 'PurchaseprintBill',
					type: 'get',
					data:'&Billno='+Billno,
					success: function(data)
					{
						$('.PurchaseprintBill').html(data)
					}
			});	
	}
</script>