
<link href="css/datepicker.css" rel='stylesheet'>
<div class="content">
	
	<div class="main-content">
		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-user"></i> Sale detail</h3> <em> -Add New Sale Record</em>
				<div class="btn-group widget-header-toolbar">
					<!--<a class="btn-borderless btn-focus clickme" href="purchaseView" title="Focus" ><i class="fa fa-plus"></i></a>-->
					<!--<a href="#" title="Expand/Collapse" class="btn-borderless btn-toggle-expand"><i class="fa fa-chevron-up"></i></a>
					<a href="#" title="Remove" class="btn-borderless btn-remove"><i class="fa fa-times"></i></a>-->
				</div>
			</div>
			<div class="widget-content">
				<form id="frmmain" name="frmmain" data-parsley-validate novalidate>
					<input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">						
					
					<div class="alert  alert-info " >
						<ul><li class="fa fa-cog  fa-1x fa-fw"></li>Kindly enter Sale 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="<?php echo e($sale['Add_Bill']); ?>" onchange="validatecode(this.value)" 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" required onchange="loadsubcat(this.value)">
									<option value="0">-- Select --</option>
									<?php foreach($sale['company'] as $comp){
								      	echo "<option value='".$comp->cmp_id."' >".$comp->cmp_name."";
									    echo "</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 datepicker"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>
						<!-- END ADVANCED VALIDATION -->
					</div>                    
				</form>
				
			</div>
			
		</div>
		<!-- END DATA EXPORT -->
		
		
		<!-- DATA EXPORT -->
		<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">
				<form id="billing" name="billing" data-parsley-validate novalidate>
					
					<!--<div class="col-md-3">
						<div class="form-group">
						<label for="multiselect1">Item Name*</label>
						<br/>
						<select name="status" id="status" class="form-control" required>
						</select>
						<small class="text-danger" id="errstatus"></small>
						</div>
						</div>
						<div class="col-md-3">
						<div class="form-group">
						<label for="multiselect1">Cost*</label>
						<br/>
						<input type="text" name="emailid" id="emailid" class="form-control" >
						<small class="text-danger" id="errstatus"></small>
						</div>
						</div>					
						<div class="col-md-3">
						<div class="form-group">
						<label for="multiselect1">Quantity*</label>
						<br/>
						<input type="text" name="emailid" id="emailid" class="form-control" >
						<small class="text-danger" id="errstatus"></small>
						</div>
						</div>			
						<div class="col-md-3">
						<div class="form-group">
						<label for="multiselect1">Total*</label>
						<br/>
						<input type="text" name="emailid" id="emailid" class="form-control" >
						<small class="text-danger" id="errstatus"></small>
						</div>
					</div>-->
					<div class="panel-body">
						<input type="hidden"  id="num" name="num" value="1" ><br>
						
						<div class="col-sm-3 nopadding">
							<div class="form-group">
								<label for="multiselect1">Item Name*</label>
								<br/>
								<select name="Schoolname1" id="Schoolname1" class="form-control" onchange="product(1)" required>
									<option value="0">-- Select --</option>
									<?php foreach($sale['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="cost1" id="cost1" class="form-control" >
								<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="quantity1" id="quantity1" class="form-control" onkeyup="calculate()" >
								<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="totals1" onkeyup="findTotal()" id="totals1" 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">     ****</label>
								<br/>
								<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 class="clear"></div>
						<div id="education_fields">
							
						</div>
					</div>
					<div class="col-md-12">
						<!-- ADVANCED VALIDATION -->
						<div class="widget-content">  
							<!--<table id="visit-stat-table" class="table table-sorting table-striped table-hover datatable">
								<thead>
								<tr>
								<th>Item Name</th>
								<th>Cost</th>
								<th>Quantity</th>
								<th>Total cost</th>
								<th>Action</th>
								</tr>
								</thead>
							</table>-->
							<div class="pull-right">
								<b>Total: <input readonly style="border:0px; font-size:30; color:red;" id="grandTotal1" name="grandTotal1" /><input type="hidden" id="sometotal" name="sometotal" /><input type="hidden" id="discounttotal" name="discounttotal" /></b><br>
							</div>
							
						</div>
						<!-- END ADVANCED VALIDATION -->
					</div>
					
					
				</form>
				
			</div>
			
		</div>
		<!-- END DATA EXPORT -->
		
		
		<!-- DATA EXPORT -->
		<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">
				<form id="frmmain" name="frmmain" data-parsley-validate novalidate>			
					
					
					
					<div class="col-md-6">
						<!-- ADVANCED VALIDATION -->
						<div class="widget-content">  
							
							
							<div class="form-group">
								<label for="text-input1">Discount.*</label>
								<input type="text" name="discount" id="discount" class="form-control" onblur="discountF()">
								<small class="text-danger" id="erritemcode"></small>
							</div>
							
							<div class="form-group">
								<label for="multiselect1">Tax percentage*</label>
                                <select name="tax" id="tax" class="form-control" required onchange="taxClick()">
									<option value="0">-- Select --</option>
									<?php foreach($sale['taxanount'] as $tax){
								      	echo "<option value='".$tax->tax_id."' >".$tax->tax_type."";
									    echo "</option>";
									}?>
								</select>
								<small class="text-danger" id="erritemcategory"></small>
								
							</div>
							
							<div class="form-group">
								<label for="multiselect1">Tax Amount</label>
                                <input type="text" name="taxAmount" id="taxAmount" class="form-control">
								<small class="text-danger" id="erritemcategory"></small>
								
							</div> 
							
							
							
							<div class="form-group">
								<label for="text-input1">Grand Total.*</label>
								<input type="text" name="grandTotal" id="grandTotal" class="form-control" value="<?php echo e(isset($data) ? $data[0]->itm_code : ''); ?>" onchange="validatecode(this.value)">
								<small class="text-danger" id="erritemcode"></small>
							</div>
							
							
						</div>
						<!-- END ADVANCED VALIDATION -->
					</div>
					
					<div class="col-md-6">
						<!-- ADVANCED VALIDATION -->
						<div class="widget-content">
							
							<div class="form-group">
								<label>Note (Any note)</label>
								<textarea class="form-control" name="description" id="description" rows="4" cols="30" ><?php echo e(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" 
									<?php if(isset($data)): ?>
									<?php if($data[0]->itm_status == "Active"): ?>
										selected='selected' 
										<?php endif; ?>
										<?php endif; ?>
									> Active</option>
                                    <option value="Inactive"
									<?php if(isset($data)): ?>
									<?php if($data[0]->itm_status == "Inactive"): ?>
										selected='selected' 
										<?php endif; ?>
										<?php endif; ?>
									>Inactive</option>
								</select>
								<small class="text-danger" id="errstatus"></small>
							</div>
						</div>
						<!-- END ADVANCED VALIDATION -->
					</div>
					<div class="col-md-12">
						<div class="form-group">
                            <center><button  class="btn btn-primary billgenerate">Click Here to Generate Bill</button></center>
						</div>
					</div>                        
				</form>
				
			</div>
			
		</div>
		<!-- END DATA EXPORT -->
		
	</div>
</div>
<script src="js/bootstrap-datepicker.js"/>
<!-- /main -->
<script>
	$('.clickme').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href'));
	});
	
	
	$('.datepicker').datepicker().on('changeDate',function(e){
		$(this).datepicker('hide');
	});
	
	$('#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>");
				}
				
			}
		});
		
		
		
		
	});var room = 1;
	function education_fields() {
		
		room++;
		var objTo = document.getElementById('education_fields')
		var divtest = document.createElement("div");
		divtest.setAttribute("class", "form-group removeclass"+room);
		var rdiv = 'removeclass'+room;
		divtest.innerHTML = '<div class="col-sm-3 nopadding"><div class="form-group"> <select name="Schoolname'+ room +'" id="Schoolname'+ room +'" onchange="product('+ room +')" class="form-control" required><option value="0">-- Select --</option><?php foreach($sale['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" name="cost'+ room +'" id="cost'+ room +'" class="form-control" disabled ></div></div><div class="col-sm-2 nopadding"><div class="form-group"> <input type="text" name="quantity'+ room +'" id="quantity'+ room +'" class="form-control" onkeyup="calculate('+ room +')" ></div></div><div class="col-sm-2 nopadding"><div class="form-group"> <input type="text" name="totals'+ room +'"  id="totals'+ room +'" class="form-control" ></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);
	}
	function remove_education_fields(rid) {
		price=$('#totals'+rid).val();
		subctruct(price);
		room--;
		$('.removeclass'+rid).remove();
		$('#num').val(room);
		ijk=$('#num').val(room);
	}
	
	// $('#tax').click(function(e)
	function taxClick()
	{
		id=$('#tax').val();
		$.ajax({
			url: 'TaxAmount',
			type: 'get',
			data:'&TaxId='+id,
			success: function(data)
			{
				$tax=data['tax_amt'];
				$tax1=$tax/100; 
				discountAmount=document.getElementById("discounttotal").value;
				$totalAmount=parseInt(discountAmount)*$tax1;
				$('#taxAmount').val($totalAmount);
				
			    tota1=parseInt(discountAmount)+parseInt($totalAmount);
				document.getElementById("grandTotal").value=tota1;
			}
			
		});
		// });
	}
	tota1=0;
	function totalcalculate()
	{
		//alert("hai");
		taxAmount=document.getElementById("taxAmount").value; 
		discountAmount=document.getElementById("grandTotal").value; 
		tota1=parseInt(discountAmount)+parseInt(taxAmount);
		document.getElementById("grandTotal").value=tota1; 
	}
	i=1;
	//$('#Schoolname'+i).change(function(e)
	function product(ij)
	{
		
		//alert("welcome");
		number=$('#num').val();
		id=$('#Schoolname'+ij).val();
		// e.preventDefault();
		
		$.ajax({
			url: 'itemAmount',
			type: 'get',
			data:'&ItemId='+id,
			success: function(data)
			{
				$('#cost'+number).val(data['itemAmount1']);
				// alert(data);
			}
		});
		//});
	}
	
	function  calculate() { 
		number=$('#num').val();
		var summs;
		var nValue;
		var amount;
		
		var price=$('#cost'+number).val();
		nValue = document.getElementById("quantity"+number).value;
		amount=(nValue*price);
		findTotal(number);
		
		document.getElementById("totals"+number).value=amount ;
		
	}
	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 ;
		
	}
	flag=0;
	$('.billgenerate').click(function(e)
	{
		e.preventDefault();
		tax=$('#tax').val();
		discount=$('#discount').val();
		grandTotal=$('#grandTotal').val();
		itemcategory=$('#itemcategory').val();
		itemcategory1=$('#itemcategory1').val();
		billdate=$('#billdate').val();
		billdano=$('#billno').val();
		description=$('#description').val();
		taxAmount=$('#taxAmount').val();
		status=$('#status').val();
		if(flag==0)
		{
			$.ajax({
				url: 'saleBillGenerate',
				type: 'get',
				data:$('#billing').serialize()+'&billdano='+billdano+'&billdate='+billdate+'&itemcategory1='+itemcategory1+'&itemcategory='+itemcategory+'&grandTotal='+grandTotal+'&discount='+discount+'&tax='+tax+'&description='+description+'&status='+status+'&taxAmount='+taxAmount,
				success: function(data)
				{
					BillPrint(data);
					$('#sideMenu').load('Newsale');
				}
			});
		}
		flag++;
	});
	
	function BillPrint(Billno)
	{
		$.ajax({
			url: 'saleprintBill',
			type: 'get',
			data: '&Billno='+Billno,
			success: function(data) 
			{
				//$('.print').html(data)
				var divContents = data;
				
				var printWindow = window.open(divContents, '', 'height=400,width=800');
				printWindow.document.write('<html><head><title>SALE BILL</title>');
				printWindow.document.write('</head><body >');
				printWindow.document.write(divContents);
				//printWindow.print();
				printWindow.document.write('</body></html>');
				printWindow.document.close();
				printWindow.onload=function()
				{
					printWindow.print();
				}
				
				return false;
				
			}
		});
		
	}
	
	n=1;
	function findTotal(num)//quantity
	// $('#quantity'+n).change(function(e)
	{
		
		var tot=0;
		for(i=1;i<=num;i++)
		{
			price=$('#cost'+i).val();
			nValue = $('#quantity'+i).val();
			amount=(nValue*price);
			tot+=amount;
			//alert(amount)
			//tot+= parseInt(totals);
		}
		
		
		document.getElementById('grandTotal').value=tot;
		document.getElementById('grandTotal1').value=tot;
		document.getElementById('sometotal').value=tot;
		n++;
	}
	discount=0;
	discountAmount=0;
	
	
	function discountF()
	{
		totalAmount=0;
		discount = document.getElementById("discount").value;
		discountAmount=document.getElementById("sometotal").value;
		
		$totalAmount=parseInt(discountAmount)-parseInt(discount);
		document.getElementById("grandTotal").value=$totalAmount;
		document.getElementById("discounttotal").value=$totalAmount;
		
	}
</script>