<?php include 'dheader.php'; ?>
	<div class="modal fade hidden-print" id="myModal" tabindex="-1" role="dialog" aria-hidden="true"></div>
	<div class="wrapper ">
		<?php include 'leftbar.php'; ?>
		<!-- left-bar -->
		<?php include 'topbar.php'; ?>
			<!-- top-bar -->
			<div class="main-content">

			<?php include 'newcategoryform.php'; ?>
			<?php include 'newcustomerintosalearea.php'; ?>
			<?php include 'newsupplierintosalearea.php'; ?>
			<?php include 'newitemintosalearea.php'; ?>

 
<div id="sales_page_holder">
 <div id="sale-grid-big-wrapper" class="clearfix register">
	<div class="row">
		<div class="clearfix" id="category_item_selection_wrapper">
  <div class="">
		<div class="spinner" id="grid-loader" style="display: none;">
		  <div class="rect1"></div>
		  <div class="rect2"></div>
		  <div class="rect3"></div>
		</div>

 
	</div>
</div>
	</div>
</div>

<div id="register_container" class="sales clearfix">
  				
		
<div class="row register">

	<!-- col-lg-4 @start of right Column -->
	<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 no-padding-right">
		<div class="register-box register-right">

			<!-- Sale Top Buttons  -->
			<div class="sale-buttons" style="text-align: left;">
				<!-- Extra links -->
				<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">	
					<input type="radio" name="type" id="sale" value="Quotation" style="display: inline;" checked> Quotation
					<!-- <input type="radio" name="type" id="return" value="Return" style="display: inline;"> Return -->
					<!-- <input type="radio" name="type" id="misc" value="Misc" style="display: inline;"> Misc  -->
				</div>

				<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12" style="display:none;">
					<input type="radio" name="payment" id="cash" value="Cash" style="display: inline;" checked> Cash
					<!-- <input type="radio" name="payment" id="credit" value="Credit" style="display: inline;"> Credit -->
					<!-- <input type="radio" name="payment" id="misc2" value="Misc2" style="display: inline;"> Misc -->
				</div>

				<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
					<div class="input-group contacts">
					<span class="input-group-addon">
						<a href="#"  class="none" title="Search Invoice" id="new-search" tabindex="-1">
						<i class="ion-android-search" style="color: #fff; font-size: 20px;"></i></a>					</span>
					<span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input type="text" id="search" name="search" class="add-customer-input ui-autocomplete-input">
				</div>
				</div>	
				<div class="clearfix"></div> 
		</div>

		<!-- If customer is added to the sale -->
		
		<div class="customer-form">
			<?php
	            $max_query = "SELECT * FROM quotations WHERE invoiceNo != '' ORDER BY invoiceNo DESC LIMIT 1";
			    //$max_query = "SELECT MAX(invoiceNo) AS inv FROM quotations";
			    $invoiceID_run = $db->query($max_query);
	            $invoiceID_row = $invoiceID_run->fetchArray();
        	?>
			<div class="col-lg-2 col-md-2 col-sm-4 col-xs-4">
				<div class="input-group contacts">
					<span class="input-group-addon">
						<a href="#"  class="none" title="Invoice" id="new-invoice" tabindex="-1"><i class="ion-android-create"></i></a>					</span>
					<span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input type="text" id="invoice" name="invoice" class="add-customer-input ui-autocomplete-input" value="<?php echo $invoiceID_row['invoiceNo']+1; ?>">
				</div>
			</div>

			<div class="col-lg-3 col-md-3 col-sm-8 col-xs-8">
				<div class="input-group contacts">
					<span class="input-group-addon">
						<a href="#" class="none" title="Date" id="invoice-date" tabindex="-1"><i class="ion-ios-calendar-outline"></i></a>					</span>
					<span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input type="text" id="datesale" name="date" class="add-customer-input ui-autocomplete-input datepicker" value="<?php echo date("m/d/Y"); ?>">
				</div>
			</div>

			<!-- if the customer is not set , show customer adding form -->
			<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
				<div class="input-group contacts">
					<span class="input-group-addon">
						<a href="customers.php" target="_blank" class="none" title="New Customer" id="new-customer" tabindex="-1"><i class="ion-person-add"></i></a>					</span>
						
					<span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input type="text" id="customer" name="customer" class="add-customer-input " value="Counter Sale" list="nameLists">
					<datalist id="nameLists">
						<option value="Counter Sale">
						<?php 
							$allCustomers = $db->query("SELECT * FROM customer_setup");
							while ($fcus = $allCustomers->fetchArray()) {
						?>
							<option value="<?php echo $fcus['customer_name']; ?>">:customer
						<?php
							}
						?>	
						<?php 
							$allSuppliers = $db->query("SELECT * FROM supplier_setup");
							while ($fsup = $allSuppliers->fetchArray()) {
						?>
							<option value="<?php echo $fsup['supplier_name']; ?>">:supplier
						<?php
							}
						?>	
						</datalist>
				</div>
			</div>

			<div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
				<div class="input-group contacts">
					<span class="input-group-addon">
						<a href="#"  class="none" title="Bilty #" id="new-customer" tabindex="-1"><i class="ion-android-bus"></i></a>					</span>
					<span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input type="text" id="bilty" name="bilty" class="add-customer-input ui-autocomplete-input">
				</div>
			</div>
			<div class="clearfix"></div>
		</div> 
			</div>


</div>

	<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 no-padding-right no-padding-left">
		
		<!-- Register Items. @contains : Items table -->
		<div class="register-box register-items paper-cut">
			<div class="register-items-holder">
									
					<table id="register" class="table table-hover">

					<thead>
						<tr class="register-items-header">
							<th class="item_name_heading">PC/Bar Code</th>
							<th class="item_name_heading">Item Name</th>
							<th class="item_name_heading" style="display: none">Unit</th>
							<th class="sales_price">Price<span id="lst" style="color: red;"></span><input type="checkbox" id="chkp"/><!-- Show --></th>
							<th class="sales_quantity">Qty<span id="stShow" style="color: red;"></span></th>

							<th class="sales_discount">Disc %</th>
							<th class="sales_discount">Disc RS</th>
							<th>Total</th>
							<th>Action</th>
						</tr>
					</thead>
					
					<tbody class="register-item-content">
						<td>
							<input type="text" autofocus="on" id="item_code" class="form-control form-inps" size="5" value="" />
						</td>	
						<td>
							<input type="text" id="item_name" class="form-control form-inps" value="" list="itemList" />
							<datalist id="itemList">
							<?php
								$itemList = $db->query("SELECT * FROM itemsetup");
								while ($row = $itemList->fetchArray()) {
							?>
								<option value="<?php echo $row['item_name']; ?>">:Item
							<?php
							}
							?>	
							<?php
								$listItems = $db->query("SELECT DISTINCT kit_name FROM kitsetup");
								while($listrow = $listItems->fetchArray()){
							?>
								<option value="<?php echo $listrow['kit_name']; ?>">:Kit
							<?php } ?>	
							</datalist>
						</td>
						<td style="display: none">
							<select name="unit" id="unit" class="form-control form-inps" >
								<option value="pcs">pcs</option>
								<option value="dzn">dzn</option>
								<option value="ctn">ctn</option>
							</select>
						</td>
						<td>
							<input type="hidden" id="actual_price" value="" />
							<input type="text" id="item_price" class="form-control form-inps" value="" size="5" />
						</td>
						<td>
							<input type="text" id="item_qty" class="form-control form-inps" value="" size="5" />
						</td>
						<td>
							<input type="text" id="item_dispr" class="form-control form-inps" value="" size="5" />
						</td>
						<td>
							<input type="text" id="item_disrs" class="form-control form-inps" value="" size="5" />
						</td>
						<td>
							<input type="text" id="item_total" class="form-control form-inps" value="XXX-XXX" size="5" style="text-align: center;" />
						</td>
						<td style="vertical-align: top; text-align: center;"><input type="button" id="addItem"  value="Add" class="submit_button btn btn-success" style="line-height: 1.80"></td>							
					</tbody>
					<tbody id="addItems"></tbody>
					</table>
					
			</div>

			<!-- End of Sales or Return Mode -->
						<!-- End of Store Account Payment Mode -->

		</div>
		<!-- /.Register Items -->
			<div class="register-box register-summary paper-cut">

		<!-- Tiers if its greater than 1 -->
		 

		<!-- Tiers if its greater than 1 -->
				<div class="tier-group">
			<a href="#" class="select-sales-person ">
				Sales person:  <span class="selected-sales-person"><?php echo $_SESSION['name']; ?></span>
			</a>					
		
		</div>
		 
		
		
		<div class="amount-block">
			<div class="total amount">
				<div class="side-heading">
					Total				</div>
				<div class="amount total-amount" data-speed="1000" data-currency="$" data-decimals="2">
					Rs. <span id="maintotal">0.00</span>				</div>
			</div>
			
			<div class="total amount" style="border-right: 1px dashed #ddd;">
				<div class="side-heading">
					Amount Received	/ Paid			</div>
				<div class="amount">
					<input type="text" id="received" class="xeditable editable editable-click editable-empty" placeholder="0.00" style="border: 0px solid;">				</div>
			</div>
			
			<div class="total amount-due">
				<div class="side-heading">
					Amount Due				</div>
				<div class="amount">
					Rs. <span id="dues">0.00</span>				</div>
			</div>

		</div>
		<!-- ./amount block -->

			<!-- Finish Sale Button Handler -->
			<div id="finish_sale" style="margin: 10px 15px;">
				<a href="#" style="margin-left :20px; " id="sn"><input type="button" class="btn btn-success" value="Save & New" style="display:none; height: 40px; width: 100px;"></a>
				<form target="_blank" action="invoice-print.php" style="float: left;">
					<input type="hidden" name="saleid" value="" id="saleinvocie">
					<input type="button" id="sp" class="btn btn-primary" value="Save & Print" style="height: 40px; width: 100px;">
				</form>
			</div>
								<div>&nbsp;</div>
			<ul class="list-group">
			<li class="list-group-item global-discount-group"  style="display:none;">
				<div class="key">Discount all Items by Percent: 	</div>
				<div class="value pull-right">

					<input type="text" id="discount_all_percent" class="xeditable editable editable-click editable-empty" placeholder="Set Discount" style="display:none; border: 1px solid;">				</div>
			</li>
			
			<li class="list-group-item global-discount-group" style="display:none; margin: 5px 0px;">
				<div class="key">Discount Entire Sale by RS: 	</div>
				<div class="value pull-right">
				 
				 <input type="text" id="discount_all_flat" class="xeditable editable editable-click editable-empty" placeholder="Set Discount" style="border: 1px solid;">

				</div>
			</li>

			<li class="list-group-item global-discount-group" style="display:none; margin: 5px 0px;">
				<div class="key">Transport Expense: 	</div>
				<div class="value pull-right">
				 
				 <input type="text" id="transport_expense" class="xeditable editable editable-click editable-empty" placeholder="Set Expense" style="border: 1px solid;">

				</div>
			</li>

			<li class="list-group-item global-discount-group" style="margin: 5px 0px;">
				<div class="key">Description: 	</div>
				<div class="value pull-right">
				 
				 <input type="text" id="sales_desc" class="xeditable editable editable-click editable-empty" placeholder="Description" style="border: 1px solid;">

				</div>
			</li>

			<li class="sub-total list-group-item">
				<span class="key">Sub Total:</span>
				<span class="value">Rs. <span id="subtotal">0.00</span></span>
			</li>
			
			
		
				</ul>

			
						
						<!-- End of complete sale button -->
	</div>
	</div>
	<!-- /.Col-lg-8 @end of left Column -->


</div>

  
</div>
</div>

</div>
 
</div>

<?php include 'dfooter.php'; ?>
<script>
	$(document).ready(function(){
		$('#lst').hide();
		$('#chkp').click(function(){
			if($("#chkp").prop('checked')){
				$('#lst').show();
				//alert("checked");
			}else{
				$('#lst').hide();
				//alert("unchecked");
			}
		});
		
		//$('#saleinvocie').val(invoice);
		$('#nc').on('click', function(e){
			$('#cusform').show();
		});
		$('#cussubmit').on('click', function(e){
			e.preventDefault();
			var date = $('#date').val();
			var fullname = $('#fullname').val();
			var email = $('#email').val();
			var phone_number = $('#phone_number').val();
			var address = $('#address').val();
			var city = $('#city').val();
			var ntn = $('#ntn').val();
			var gst = $('#gst').val();
			var comments = $('#comments').val();
			var balance = $('#balance').val();
			var credit_limit = $('#credit_limit').val();
			var company_name = $('#company_name').val();
			var account_number = $('#account_number').val();
			$.ajax({
			  method: "POST",
			  url: "addnewcusfromsale.php",
			  async:false,
			  data: { date: date, fullname: fullname, email: email, phone_number: phone_number, address: address, city: city, ntn: ntn, gst: gst, comments: comments, balance: balance, credit_limit: credit_limit, company_name: company_name, account_number: account_number },
			  dataType: 'html',
			  success: function( data ) {
			  	$('#nameLists').prepend(data);
			  	$('#customer').focus();
			  	/*$.each(data, function(i, v) {
			  		alert(i + v);
					if(i == 0){
						
					}
				});*/
			  }
		});
			$('#cusform').hide();
		});
		$('#closecusform').on('click', function(e){
			$('#cusform').hide();
		});

		$('#ns').on('click', function(e){
			$('#supform').show();
		});
		$('#supsubmit').on('click', function(e){
			e.preventDefault();
			var date = $('#date2').val();
			var fullname = $('#fullname2').val();
			var email = $('#email2').val();
			var phone_number = $('#phone_number2').val();
			var address = $('#address2').val();
			var city = $('#city2').val();
			var ntn = $('#ntn2').val();
			var gst = $('#gst2').val();
			var comments = $('#comments2').val();
			var balance = $('#balance2').val();
			var company_name = $('#company_name2').val();
			var account_number = $('#account_number2').val();
			$.ajax({
			  method: "POST",
			  url: "addnewsupfromsale.php",
			  async:false,
			  data: { date: date, fullname: fullname, email: email, phone_number: phone_number, address: address, city: city, ntn: ntn, gst: gst, comments: comments, balance: balance, company_name: company_name, account_number: account_number },
			  dataType: 'html',
			  success: function( data ) {
			  	$('#nameLists').prepend(data);
			  	$('#customer').focus();
			  	/*$.each(data, function(i, v) {
			  		alert(i + v);
					if(i == 0){
						
					}
				});*/
			  }
		});
			$('#supform').hide();
		});

		$('#closesupform').on('click', function(e){
			$('#supform').hide();
		});

		$('#ni').on('click', function(e){
			$('#itemform').show();
		});
		$('#itemsubmit').on('click', function(e){
			e.preventDefault();
			//var date = $('#date3').val();
			var item_number3 = $('#item_number3').val();
			var product_id3 = $('#product_id3').val();
			var name3 = $('#name3').val();
			var pcs = $('#pcs').val();
			var dzn = $('#dzn').val();
			var ctn = $('#ctn').val();
			var category3 = $('#category3').val();
			var cost_price3 = $('#cost_price3').val();
			var selling_price3 = $('#selling_price3').val();
			var quantity3 = $('#quantity3').val();
			var opening3 = $('#opening3').val();
			var reorder_level3 = $('#reorder_level3').val();
			$.ajax({
			  method: "POST",
			  url: "addnewitemfromsale.php",
			  async:false,
			  data: { /*date: date,*/ item_number3: item_number3, product_id3: product_id3, name3: name3, pcs: pcs, dzn: dzn, ctn: ctn, category3: category3, cost_price3: cost_price3, selling_price3: selling_price3, quantity3: quantity3, opening3: opening3, reorder_level3: reorder_level3 },
			  dataType: 'html',
			  success: function( data ) {
			  	$('#itemList').prepend(data);
			  	$('#item_name').focus();
			  	/*$.each(data, function(i, v) {
			  		alert(i + v);
					if(i == 0){
						
					}
				});*/
			  }
		});
			$('#itemform').hide();
		});
		$('#closeitemform').on('click', function(e){
			$('#itemform').hide();
		});

		$('#sn').on('click', function(e){
			e.preventDefault();
				var saleType;
		if($('#sale:checked').val()){
			saleType = $('#sale').val();	
		}else if($('#purchase:checked').val()){
			saleType = $('#purchase').val();
		}else if($('#return:checked').val()){
			saleType = $('#return').val();
		}else if($('#misc:checked').val()){
			saleType = $('#misc').val();
		}

		var paymentType;
		if($('#cash:checked').val()){
			paymentType = $('#cash').val();	
		}else if($('#credit:checked').val()){
			paymentType = $('#credit').val();
		}else if($('#misc2:checked').val()){
			paymentType = $('#misc2').val();
		}

		var date = $('#datesale').val();
		var name = $('#customer').val();
		var bilty = $('#bilty').val();

			var invoice = $('#invoice').val();
			$.ajax({
			  method: "POST",
			  url: "updatestock.php",
			  async:false,
			  data: { saleType:saleType, paymentType:paymentType, date:date, name:name, bilty:bilty,  invoice:invoice, from_page: 'sale' },
			  success: function( data ) {
				//alert(data);
			  }
			});
			window.location = 'sales.php';
		});
		$('#sp').on('click', function(e){
		
		e.preventDefault();
		var saleType;
		if($('#sale:checked').val()){
			saleType = $('#sale').val();	
		}else if($('#purchase:checked').val()){
			saleType = $('#purchase').val();
		}else if($('#return:checked').val()){
			saleType = $('#return').val();
		}else if($('#misc:checked').val()){
			saleType = $('#misc').val();
		}

		var paymentType;
		if($('#cash:checked').val()){
			paymentType = $('#cash').val();	
		}else if($('#credit:checked').val()){
			paymentType = $('#credit').val();
		}else if($('#misc2:checked').val()){
			paymentType = $('#misc2').val();
		}

		var date = $('#datesale').val();
		var name = $('#customer').val();
		var bilty = $('#bilty').val();
		var prebal = 0;

		$('#pre_bal').change(function() {
	        if ($(this).prop('checked')) {
	            prebal = 1;
	            //alert("You have elected to show your checkout history."); //checked
	        }
	        else {
	            prebal = 0; 
	            //alert("You have elected to turn off checkout history."); //not checked
	        }
	    });
		var invoice = $('#invoice').val();
		
		window.open('invoice-printq.php?saleid='+invoice+'&prebal='+prebal);

		});
	});

	$(document).on('click', '.deleteItem', function(){
		var invoice = $('#invoice').val();
		var discount_all_percent = $('#discount_all_percent').val();
		var discount_all_flat = $('#discount_all_flat').val();
		var transport_expense = $('#transport_expense').val();
		var received = $('#received').val();
		var sales_desc = $('#sales_desc').val();

		var id = $(this).val();
		$(this).closest('tr').css('display', 'none');
		
		var sin = $('#search').val(); // Search Input field

		// $.ajax({
		//   method: "POST",
		//   url: "deleteinvoice.php",
		//   async:false,
		//   data: { id: id },
		//   dataType: 'json',
		//   success: function( data ) {
		// 	$(this).closest('tbody').css('display', 'none');
		// 	//return false;
		//   }
		// });

		$.ajax({
			  method: "POST",
			  url: "invoicecalculationq.php",
			  async:false,
			  data: { invoice:invoice },
			  dataType: 'json',
			  success: function( data ) {
				$.each(data, function(i, v) {
					if(i == "subtotal"){
						$('#subtotal').html(v);
					}
					if(i == "grand_total"){
						$('#maintotal').html(v);
					}
					if(i == "remaining"){
						$('#dues').html(v);
					}
				});
				
			  }
		});

		$.ajax({
			  method: "POST",
			  url: "invoicecalculation2q.php",
			  async:false,
			  data: { invoice:invoice, discount_all_percent:discount_all_percent, discount_all_flat:discount_all_flat, transport_expense:transport_expense, received:received, sales_desc:sales_desc },
			  dataType: 'json',
			  success: function( data ) {
				$.each(data, function(i, v) {
					//alert(i + ': ' + v);
					if(i == "grand_total"){
						$('#maintotal').html(v);
					}
					if(i == "remaining"){
						$('#dues').html(v);
					}
				});
				
			  }
		});

	});	

	$(document).ready(function(){
		$('#search').on('change', function(){
			
			var invoice = $('#search').val();
			$('#saleinvocie').val(invoice);

			$.ajax({
			  method: "POST",
			  url: "searchinvoiceq.php",
	          async:false,
			  data: { invoice:invoice },
			  success: function( data ) {
			  	$('#addItems tr').remove();
			  	$('#addItems').prepend(data);
			  }
			});
			
			$.ajax({
			  method: "POST",
			  url: "search_calculationq.php",
			  async:false,
			  data: { invoice:invoice },
			  dataType: 'json',
			  success: function( data ) {
				$.each(data, function(i, v) {
					if(i == "subtotal"){
						$('#subtotal').html(v);
					}
					if(i == "grand_total"){
						$('#maintotal').html(v);
					}
					if(i == "remaining"){
						$('#dues').html(v);
					}
					if(i == "received"){
						$('#received').val(v);
					}
					if(i == "entire_discount_rs"){
						$('#discount_all_flat').val(v);
					}
					if(i == "entire_discount_pr"){
						$('#discount_all_percent').val(v);
					}
					if(i == "transport_expense"){
						$('#transport_expense').val(v);
					}
					if(i == "invoiceNo"){
						$('#invoice').val(v);
					}
					if(i == "date"){
						$('#datesale').val(v);
					}
					if(i == "name_word"){
						$('#customer').val(v);
					}
					if(i == "bilty"){
						$('#bilty').val(v);
					}
					if(i == "description"){
						$('#sales_desc').val(v);
					}
					if(i == "sale_type"){
						$ftype = v.toLowerCase();
						$("#"+$ftype).attr("checked", "checked");
					}
					if(i == "payment_type"){
						$ptype = v.toLowerCase();
						$("#"+$ptype).attr("checked", "checked");
					}
				});
				
			  }
			});

			
		});
	});

	$(document).ready(function(){
		$('#sales_desc').on('change', function(){
			var invoice = $('#invoice').val();
			var discount_all_percent = $('#discount_all_percent').val();
			var discount_all_flat = $('#discount_all_flat').val();
			var transport_expense = $('#transport_expense').val();
			var received = $('#received').val();
			var sales_desc = $('#sales_desc').val();

			$.ajax({
			  method: "POST",
			  url: "invoicecalculation2q.php",
			  async:false,
			  data: { invoice:invoice, discount_all_percent:discount_all_percent, discount_all_flat:discount_all_flat, transport_expense:transport_expense, received:received, sales_desc:sales_desc },
			  dataType: 'json',
			  success: function( data ) {
				$.each(data, function(i, v) {
					//alert(i + ': ' + v);
					if(i == "grand_total"){
						$('#maintotal').html(v);
					}
					if(i == "remaining"){
						$('#dues').html(v);
					}
				});
				
			  }
			});
		});
	});

	$(document).ready(function(){
		$('#unit').on('change', function(){
			var ut = $(this).val();
			var item_name = $('#item_name').val();
			
			$.ajax({
			  method: "POST",
			  url: "showPrice.php",
			  data: { ut: ut, item_name: item_name },
			  dataType: 'json',
			  success: function( data ) {
				$.each(data, function(i, v) {
					if(i == 'p'){
						$('#item_price').val(v);
					}
					if(i == 'a'){
						$('#actual_price').val(v);
						$('#lst').text('/'+v);
					}
				});
			  }
			});
		});	
	});

	$(document).ready(function(){
		$('#addItem').on('click', function(){

		var saleType;
		if($('#sale:checked').val()){
			saleType = $('#sale').val();	
		}else if($('#purchase:checked').val()){
			saleType = $('#purchase').val();
		}else if($('#return:checked').val()){
			saleType = $('#return').val();
		}else if($('#misc:checked').val()){
			saleType = $('#misc').val();
		}

		var paymentType;
		if($('#cash:checked').val()){
			paymentType = $('#cash').val();	
		}else if($('#credit:checked').val()){
			paymentType = $('#credit').val();
		}else if($('#misc2:checked').val()){
			paymentType = $('#misc2').val();
		}

		var invoice = $('#invoice').val();
		var date = $('#datesale').val();
		var name = $('#customer').val();
		var bilty = $('#bilty').val();
		var item_code = $('#item_code').val();
		var item_unit = $('#unit').val();
		var item_name = $('#item_name').val();
		var item_cost = $('#actual_price').val();
		var item_price = $('#item_price').val();
		var item_qty = $('#item_qty').val();
		var item_dispr = $('#item_dispr').val();
		var item_disrs = $('#item_disrs').val();
		var item_total = $('#item_total').val();
		var discount_all_percent = $('#discount_all_percent').val();
		var discount_all_flat = $('#discount_all_flat').val();
		var transport_expense = $('#transport_expense').val();
		var received = $('#received').val();


			$.ajax({
			  method: "POST",
			  url: "addinvoiceq.php",
	          async:false,
			  data: { saleType:saleType, paymentType:paymentType, invoice:invoice, date:date, name:name, bilty:bilty, item_code:item_code, item_unit:item_unit, item_name:item_name, item_cost:item_cost, item_price: item_price, item_qty:item_qty, item_dispr:item_dispr, item_disrs:item_disrs, item_total:item_total },
			  success: function( data ) {
			  	$('#addItems').prepend(data);
				$('#item_code').val("").focus();
				$('#item_name').val("");
				$('#item_price').val("");
				$('#item_qty').val("");
				$('#item_dispr').val("");
				$('#item_disrs').val("");
			  }
			});

			
			$.ajax({
			  method: "POST",
			  url: "invoicecalculationq.php",
			  async:false,
			  data: { invoice:invoice },
			  dataType: 'json',
			  success: function( data ) {
				$.each(data, function(i, v) {
					if(i == "subtotal"){
						$('#subtotal').html(v);
					}
					if(i == "grand_total"){
						$('#maintotal').html(v);
					}
					if(i == "remaining"){
						$('#dues').html(v);
					}
				});
				
			  }
			});

			$.ajax({
			  method: "POST",
			  url: "invoicecalculation2q.php",
			  async:false,
			  data: { invoice:invoice, discount_all_percent:discount_all_percent, discount_all_flat:discount_all_flat, transport_expense:transport_expense, received:received, sales_desc:sales_desc },
			  dataType: 'json',
			  success: function( data ) {
				$.each(data, function(i, v) {
					//alert(i + ': ' + v);
					if(i == "grand_total"){
						$('#maintotal').html(v);
					}
					if(i == "remaining"){
						$('#dues').html(v);
					}
				});
				
			  }
			});
		});
	});

	$(document).ready(function(){


		$('#item_code').on('change',function(){
			//alert("run");
			$('#item_qty').val(1);
			$('#addItem').focus();
		});

		$('#item_qty').on('change',function(){
			var qty=$('#item_qty').val();
			var price=$('#item_price').val();
			var multi=price*qty;
			$('#item_total').val(parseInt(multi));
		});

		$('#item_disrs').on('change',function(){
			var total=$('#item_total').val();
			var discount=$('#item_disrs').val();
			var dis_count=total-discount;
			$('#item_total').val(parseInt(dis_count));
		});
       

		$('#item_code, #item_name').on('change', function(){
		var code = $(this).val();
		
		$.ajax({
		  method: "POST",
		  url: "fetchItemCode.php",
		  data: { code:code },
		  dataType: 'json',
		  success: function( data ) {
			$.each(data, function(i, v) {
				//alert(i + ': ' + v);
				if(i == "cost_price"){
					$('#actual_price').val(v);
				}
				if(i == "purchase_price"){
					$('#actual_price').val(v);
				}
				if(i == "kit_code"){
					$('#item_code').val(v);
				}
				if(i == "kit_name"){
					$('#item_name').val(v);
				}
				if(i == "selling_price"){
					$('#item_price').val(v);
				}
				if(i == "cost_price"){
					$('#lst').text("/" + v);
				}
				
				if(i == "uei"){
					$('#item_code').val(v);
				}
				if(i == "item_name"){
					$('#item_name').val(v);
				}
				if(i == "selling_price"){
					$('#item_price').val(v);
				}
				if(i == "purchase_price"){
					$('#lst').text("/" + v);
				}
				if(i == "stock"){
					$('#stShow').text("/" + v);
				}
			});
			
		  }
		});
	});


});

	$(document).ready(function(){
		$('#manage_cat').on('click', function(e){
			e.preventDefault();
			$('#cat_form').show();
		});

		$('#closecatform').on('click', function(){
			$('#cat_form').hide();
		});

		$('#catsubmit').on('click', function(e){
			e.preventDefault();

			var cat_name = $('#cname').val();
			$.ajax({
			  method: "POST",
			  url: "addnewcatfromitem.php",
			  async:false,
			  data: { cat_name: cat_name },
			  dataType: 'html',
			  success: function( data ) {
			  	$('#category_id').append(data);
			  	$('#category_id').focus();
			  }
		});
			$('#cat_form').hide();
		});
	});

	$(document).ready(function(){
		$('#cost_price3, #item_discount').on('change', function(){
			var cost_price = parseInt($('#cost_price3').val());
			var item_discount = parseInt($('#item_discount').val());

			var selling_price = ((cost_price * item_discount) / 100) + cost_price;
			$('#selling_price3').val(selling_price);

		});
	});
</script>