<?php $this->load->view("common/header"); ?>
<!-- end: HEAD -->

<!-- start: CSS REQUIRED FOR THIS PAGE ONLY -->

<style type="text/css">
	label {
		font-weight: 700;
	}

	.table>thead:first-child>tr:first-child>th,
	.table>thead:first-child>tr:first-child>td,
	.table-striped thead tr.primary:nth-child(2n+1) th {
		background-color: #428bca;
		border-color: #357ebd;
		border-top: 1px solid #357ebd;
		color: white;
		text-align: center;
	}

	.form-horizontal .form-group {
		margin-left: 0;
		margin-right: 0;
	}
 
	.main-container {
		margin-top: unset !important;
	}
	.main-content {
        margin-left: unset !important;
    }
</style>

<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
<!-- end: HEAD -->
<!-- start: BODY -->

<body>
	<div class="main-container">
		<div class="navbar-content">
			<!-- start: SIDEBAR -->
			<?php //$this->load->view("common/navigation"); 
			?>
			<!-- end: SIDEBAR -->
		</div>
		<!-- start: PAGE -->
		<div class="main-content">
			<!-- end: SPANEL CONFIGURATION MODAL FORM -->
			<div class="container">
				<!-- start: PAGE HEADER -->
				<div class="row">
					<div class="col-sm-12">
						<!-- start: PAGE TITLE & BREADCRUMB -->
						<ol class="breadcrumb collapse">
							<li>
								<a href="#">
									Dashboard
								</a>
							</li>

							<li>
								<a href="<?php echo base_url('sales'); ?>">
									Sales Return
								</a>
							</li>

							<li class="active">
								Add
							</li>

							<li class="search-box">
								<form class="sidebar-search">
									<div class="form-group">
										<input type="text" placeholder="Start Searching...">
										<button class="submit">
											<i class="clip-search-3"></i>
										</button>
									</div>
								</form>
							</li>
						</ol>
						<div class="page-header">
							<h1>Add Sales Return</h1>
						</div>

						<p>Please fill in the information below. The field labels marked with * are required input fields.</p>
					</div>
				</div>
				<!-- end: PAGE HEADER -->
				<!-- start: PAGE CONTENT 
                    <!-- start grid -->



				<div class="row">

					<div class="col-md-12">
						<!-- start: DYNAMIC TABLE PANEL -->
						<div class="panel panel-default">
							<div class="panel-heading">
								<i class="fa fa-plus"></i>
								Add Sales Return
							</div>
							<div class="panel-body">
								<div class="alert alert-danger" style="display:none;">
									<h4 class="alert-heading"><i class="fa fa-times-circle"></i> Error!</h4>

									<strong></strong> <span class="errortxt"></span>
								</div>
								<div class="alert alert-success" style="display:none;">
									<button class="close" data-dismiss="alert">
										×
									</button>
									<i class="fa fa-check-circle"></i>
									<strong></strong><span class="succetxt"></span>
								</div>




								<form role="form" class="form-horizontal" id="create_sales_return_form" action="#" method="post">

									<div class="col-md-12"></div>
									<!--col-md-12-->
									<div class="col-md-12">


										<div class="col-md-3">
											<div class="form-group">
												<label>Date *</label>

												<?php $nowdate = date("Y-m-d H:i:s"); ?>
												<input id="sl_rtn_datetime" name="sl_rtn_datetime" type='text' class="form-control date" value="" data-bv-field="date" />
												<input id="base_url" type='hidden' value="<?php echo base_url(); ?>"  />
											</div>
										</div>
										<div class="col-md-3">
											<div class="form-group">
												<label>Reference No</label>
												<?php
												$ref_id = new Common_Model();
												$sl_rtn_reference_no = $ref_id->gen_ref_number('sl_rtn_id', 'hw_sales_return', '');
												?>
												<input type='text' class="form-control" id="sl_rtn_reference_no" name="sl_rtn_reference_no" value="<?php echo $sl_rtn_reference_no ?>" readonly />
											</div>
										</div>

										<div class="col-sm-3">
											<div class="form-group">
												<label for="customer_id">Sale Terms * </label>
												<?php
												$ref_usr = new User_Model;
												$user_details = $ref_usr->get_user_info($this->session->userdata('ss_user_id'));
												//print_r($user_details);
												$invoice_name = '';
												$invoice_name = $user_details['invoice_name'];
												//echo $invoice_name;
												?>

												<select id="in_type" class="form-control" name="in_type" onChange="addCrInterest()">
													<?php
													$sel_w = '';
													$sel_s = '';
													$sel_c = '';



													if (isset($sale_details['in_type']) && $sale_details['in_type'] == 'Credit') {
														$sel_c = ' selected';
													}
													if (isset($sale_details['in_type']) && $sale_details['in_type'] == 'Wholesale') {
														$sel_w = ' selected';
													}
													if (isset($sale_details['in_type']) && $sale_details['in_type'] == 'Cash') {
														$sel_s = ' selected';
													}
													/*
					if(isset($hdr_data->bkng_status) && $hdr_data->bkng_status=='Removed'){
						$sel_r=' selected';
					}*/

													?>
													<option value="Cash" <?php echo $sel_s; ?>>Cash</option>
													<option value="Wholesale" <?php echo $sel_w; ?>>Wholesale</option>
													<option value="Credit" <?php echo $sel_c; ?>>Wholesale Credit</option>


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



										<div class="col-sm-3">
											<div class="form-group">
												<label>Sift </label>
												<select id="float_id" class="form-control" name="float_id">
													<option value="0">-please select-</option>
													<?php foreach ($sfilt_list as $row) { ?>
														<option value="<?php echo $row['c_float_mstr_id']; ?>"> <?php echo $row['user_first_name'] . " [ " . $row['c_f_m_date_time'] . " ]"; ?> </option>
													<?php } ?>
												</select>
											</div>
										</div>



										<div class="col-sm-4" id="cr_interest_div">
											<div class="form-group">
												<label for="cr_interest">Credit Interest</label>

												<select class="form-control" name="cr_interest" id="cr_interest" onChange="calculateTotal()">
													<!--  <option value="">-- Select Credit Interest --</option>-->
													<?php

													foreach ($cr_limit_list as $row) { ?> <option value="<?php echo $row['cr_limit_value']; ?>"><?php echo $row['cr_limit_name'];
																																				echo " / ";
																																				echo $row['cr_limit_value'];
																																				echo "% "; ?> </option>
													<?php }  ?>
												</select>
											</div>
										</div>

									</div>
									<!--col-md-8-->



									<!-- item add box-->
									<div id="sticker" class="col-md-12">
										<div class="">


											<!-- auto complete start -->
											<div class="input-group wide-tip">
												<div style="padding-left: 10px; padding-right: 10px;" class="input-group-addon">
													<i class="fa fa-2x fa-barcode addIcon"></i>
												</div>
												<input type="text" placeholder="Please add products to order list" id="add_item" class="form-control input-lg" value="" name="add_item" onClick="add_items()" style="border-radius: 6px;font-size: 18px;height: 46px;line-height: 1.33;padding: 10px 16px;">
												<div style="padding-left: 10px; padding-right: 10px;" class="input-group-addon">
													<i id="addIcon" class="fa fa-2x fa-plus-circle addIcon"></i>

												</div>
											</div>
											<!-- end auto complete end -->


											<div class="clearfix"></div>
											<div class="control-group table-group">
												<label class="table-label">Order Items</label>
												<div class="controls table-controls">
													<table class="table items table-striped table-bordered table-condensed table-hover" id="soTable">
														<thead>
															<tr>
																<th class="col-md-6">Product Name (Product Code)</th>
																<th class="col-md-2">Selling Price</th>
																<th class="col-md-1">Quantity</th>
																<th class="col-md-2 text-right">Discount</th>

																<th class="text-right">Subtotal </th>
																<th style="width: 30px !important; text-align: center;"><i class="fa fa-trash-o"></i></th>
															</tr>
														</thead>
														<tbody>
															<?php
															//print_r($sale_item_list);
															$tmpcount = 0;
															$Subtotal = 0;
															foreach ($sale_item_list as $row) {
																$tmpcount++;
																$product_id = $row['product_id'];
																$Subtotal += $row['gross_total'];

																//calculate qty: returnqty=soldqty-alredy returned qty
																$alredyReturnedQty = 0;
																$modelSR = new Sales_Return_Model();
																$alredyReturnedQty = $modelSR->get_sales_return_product_qty($product_id, $sale_details['warehouse_id'], $sale_id);
																$remaintQty = $row['quantity'] - $alredyReturnedQty;
															?>
																<tr id="row_<?php echo $tmpcount; ?>" class="child">
																	<td><?php echo $row['product_name']; ?> (<?php echo $row['product_code']; ?>)<?php if ($row['product_part_no']) echo ", Part No.:" . $row['product_part_no']; ?>
																		<?php if ($row['product_oem_part_number']) echo ", OEM Part No.:" . $row['product_oem_part_number']; ?>
																		<input type="hidden" id="product_id_<?php echo $tmpcount ?>" value="<?php echo $product_id ?>" name="row[<?php echo $tmpcount ?>][product_id][]" class="form-control text-center rquantity">
																	</td>
																	<td class="text-right"><input type="hidden" value="<?php echo $row['unit_price'] ?>" id="product_price_<?php echo $tmpcount ?>" name="row[<?php echo $tmpcount ?>][unit_price][]"><?php echo number_format($row['unit_price'], 2, '.', ',') ?>

																		<input type="hidden" value="0" id="item_price_p_<?php echo $tmpcount ?>" name="row[<?php echo $tmpcount ?>][item_price_p][]" style="width:50px;">
																	</td>

																	<td><?php //echo $alredyReturnedQty 
																		?>
																		<input type="text" onChange="changeQtyByProductID(this.value,<?php echo $tmpcount ?>);" onClick="this.select(); setTmpVal(this.value);" id="quantity_<?php echo $tmpcount ?>" value="<?php echo $remaintQty ?>" name="row[<?php echo $tmpcount ?>][qty][]" class="form-control text-center rquantity">
																	</td>

																	<td class="text-right"><span id="sdiscount_1446800197032" class="text-right sdiscount text-danger">
																			<input type="text" style="width:50px;" id="discount_<?php echo $tmpcount ?>" value="<?php echo $row['discount']; ?>" name="row[<?php echo $tmpcount ?>][discount][]" onChange="changeDiscountByProductID(this.value,1);">
																		</span>
																		<input type="hidden" id="discount_val_<?php echo $tmpcount ?>" value="<?php echo $row['discount_val']; ?>" name="row[<?php echo $tmpcount ?>][discount_val][]">
																		<input type="hidden" id="discount_val_tot_<?php echo $tmpcount ?>" value="0" name="row[<?php echo $tmpcount ?>][discount_val_tot][]">
																	</td>

																	<td class="text-right">
																		<span id="subtotal_<?php echo $tmpcount ?>" class="text-right ssubtotal">

																			<?php echo number_format($row['gross_total'], 2, '.', ',') ?></span>
																		<input type="hidden" id="gross_total_<?php echo $tmpcount ?>" value="<?php echo $row['gross_total'] ?>" name="row[<?php echo $tmpcount ?>][gross_total][]">

																	</td>
																	<td><a onClick="deleteSalesItem(<?php echo $tmpcount ?>)"><i style="cursor:pointer;" title="Remove" id="1446800197032" class="fa fa-times tip podel"></i></a></td>
																</tr>
															<?php } ?>
														</tbody>
														<tfoot>
															<tr class="tfoot active" id="tfoot">
																<th colspan="4" class="text-right">Total Amount</th>
																<th class="text-right"><span id="Subtotal"><?php echo number_format($Subtotal, 2, '.', ',') ?></span></th>

															</tr>

														</tfoot>
													</table>


													<!-- start list -->
													<!-- end list -->
												</div>
											</div>
										</div>
										<!-- end item add box-->



										<div id="extras-con" class="row">

											<div class="col-md-4">
												<div class="form-group">


												</div>
											</div>


										</div>

										<div class="col-md-4">
											<div class="form-group">
												<label>Order Discount</label><br>

												<input type="text" title="" data-original-title="" value="<?php echo $sale_details['sale_inv_discount'] ?>" class="form-control input-tip" id="sale_inv_discount" name="sl_rtn_inv_discount" onChange="changeMainDiscount(this.value)">
											</div>
										</div>


										<div class="col-md-4">
											<div class="form-group">
												<label>Credit Sur Charge RS</label><br>

												<input readonly align="right" name="cr_interest_amt" type="text" id="cr_interest_amt" value="0" class="text-right">
											</div>
										</div>

									</div> <!-- end: col-md-12-->



									<!-- footer amount details -->
									<div class="col-md-12">
										<div style="margin-bottom: 0;" class="well well-sm" id="bottom-total">
											<div style="margin-bottom: 0px; z-index: 50000;" id="bottom-total">
												<table style="margin-bottom:0;" class="table table-bordered table-condensed totals">
													<tbody>
														<tr class="warning">
															<td width="30%">Total <span id="f_total" class="totals_val pull-right">0.00</span></td>
															<td width="30%">Order Discount <span id="tds" class="totals_val pull-right"><?php echo number_format($sale_details['sale_inv_discount_amount'], 2, '.', ',') ?></span></td>
															<td>Return Amount <span id="gtotal" class="totals_val pull-right"><?php echo number_format($sale_details['sale_total'], 2, '.', ',') ?></span></td>
														</tr>
													</tbody>
												</table>
											</div>
										</div>
									</div>
									<div style="height:15px; clear: both;"></div>
									<!-- end footer amount details -->


									<!--error msg start-->
									<div class="col-md-12">
										<?php
										$obj = new Sales_Model();
										$total_paid_amount = $this->Sales_Model->get_total_paid_by_sale_id($sale_id);
										if (empty($total_paid_amount)) {
											$pay_st = 'Pending';
										} else {
											if ($total_paid_amount >= $sale_details['sale_total']) {
												$pay_st = 'Paid';
											} else {
												$pay_st = 'Partial';
											}
										}
										?>
										<!--<div class="alert alert-warning">
Please be informed that this sale payment status is <?php echo $pay_st ?>. Payment Status: 
<strong><?php echo $pay_st ?></strong> 
&amp; Paid Amount <strong><?php echo number_format($total_paid_amount, 2, '.', ',') ?></strong>
</div> -->

									</div>
									<div style="height:15px; clear: both;"></div>
									<!--end error msg -->


									<!-- start payments-->
									<div class="col-md-12">
										<div class="well well-sm">
											<div class="row">
												<div class="col-md-4">
													<div class="form-group">

														<label for="payment_reference_no">Payment Reference No *</label> <input type="text" required id="payment_reference_no" class="form-control tip" value="<?php echo "SRP/" . $sl_rtn_reference_no ?>" name="sale_pymnt_ref_no" data-bv-field="payment_reference_no" data-original-title="" title="" readonly>
														<small style="display: none;" class="help-block" data-bv-validator="notEmpty" data-bv-for="payment_reference_no" data-bv-result="NOT_VALIDATED">Please enter/select a value</small>
													</div>
												</div>

												<div class="col-sm-4">
													<div class="payment">
														<div class="form-group">

															<?php if (is_null($total_paiment['sale_pymnt_amount'])) {
																$payment_tot_by_cus = 0;
															} else {
																$payment_tot_by_cus = $total_paiment['sale_pymnt_amount'];
															}
															?>
															<input type="hidden" id="max_amount" name="max_amount" value="<?php echo $payment_tot_by_cus; ?>"/>
															<label for="amount_1">Return Amount * (MAX : Rs <?php echo number_format($payment_tot_by_cus,2,".",",");?>)</label> <input required type="text" class="pa form-control kb-pad amount" id="sale_pymnt_amount" name="sale_pymnt_amount" <?php echo ($payment_tot_by_cus < $sale_details['sale_total'])?"":""; ?> value="<?php echo ($payment_tot_by_cus < $sale_details['sale_total'])?$payment_tot_by_cus:""; ?>">
														</div>
													</div>
												</div>

												<div class="col-sm-4">
													<div class="payment">
														<div class="form-group">
															<label for="amount_1">Paying by</label>
															<select class="form-control paid_by" id="sale_pymnt_paying_by" name="sale_pymnt_paying_by" data-bv-field="paid_by" tabindex="-1" title="Paying by *" onChange="changePayingby(this.value)">
															    <option value="invoice_cash">INVOICE CASH</option>
															    <option value="cash">CASH</option>
															</select>
														</div>
													</div>
												</div>

												<!-- payment options start-->
												<div class="clearfix"></div>


												<div id="cheque_dtls" style="display:none;" class="paying_by_details col-sm-12">
													<label for="date">Cheque No *</label> <input type="text" id="sale_pymnt_cheque_no" class="form-control" value="" name="sale_pymnt_cheque_no" data-bv-field="date">
												</div>
												<!--cheque-->

												<div id="credit_card" style="display:none;" class="paying_by_details">
													<div class="col-sm-5">
														<div class="form-group">
															<input type="text" id="sale_pymnt_crdt_card_no" class="form-control tip" value="" name="sale_pymnt_crdt_card_no" placeholder="Credit Card No *">
														</div>
													</div>
													<!--col-sm-5-->
													<div class="col-sm-5 pull-right">
														<div class="form-group">
															<input type="text" id="sale_pymnt_crdt_card_holder_name" class="form-control tip" value="" name="sale_pymnt_crdt_card_holder_name" placeholder="Holder Name *">
														</div>
													</div>
													<!--col-sm-5-->

													<div class="col-sm-3" style="margin-right:60px;">
														<div class="form-group">
															<select class="form-control paid_by" id="sale_pymnt_crdt_card_type" name="sale_pymnt_crdt_card_type" data-bv-field="paid_by">
																<option value="">-- Select Credit Card Type --</option>
																<option value="Visa">Visa</option>
																<option value="MasterCard">MasterCard</option>
																<option value="Amex">Amex</option>
																<option value="Discover">Discover</option>
															</select>
														</div>
													</div>
													<!--col-sm-3-->

													<div class="col-sm-3">
														<div class="form-group">
															<input type="text" id="sale_pymnt_crdt_card_month" class="form-control tip" value="" name="sale_pymnt_crdt_card_month" placeholder="Month *">
														</div>
													</div>
													<!--col-sm-3-->

													<div class="col-sm-3 pull-right">
														<div class="form-group">
															<input type="text" id="sale_pymnt_crdt_card_year" class="form-control tip" value="" name="sale_pymnt_crdt_card_year" placeholder="Year *">
														</div>
													</div>
													<!--col-sm-3-->
												</div>
												<!--credit_card-->
												<div class="clearfix"></div>
												<!-- end payment optiones -->

											</div>



										</div>
									</div>
									<!-- end payments-->

									<!--start return note-->
									<div class="col-md-12">
										<label for="payment_reference_no">Return Note</label>
										<textarea name="sl_rtn_note" rows="2" cols="10" class="ckeditor form-control" id="sl_rtn_note"></textarea>

										<div style="height:15px; clear: both;"></div>
									</div>

									<!--end start return note-->

									<div class="col-md-12">
										<div class="modal-footer" style="margin-bottom:10px;">
											<input type="submit" class="btn btn-primary" value="Return Sales" name="return_sale" id="return_sale"> <button id="reset" class="btn btn-danger" type="button">Reset</button>
										</div>
									</div>
									<input name="sl_rtn_inv_discount_amount" type="hidden" id="sl_rtn_inv_discount_amount" value="<?php echo $sale_details['sale_inv_discount_amount'] ?>">
									<?php //print_r($sale_details);
									// echo $sale_details['sale_total']

									?>

									<!--<input name="cr_interest" type="hidden" id="cr_interest" value="0">-->
									<input name="sl_rtn_total" type="hidden" id="sl_rtn_total" value="<?php echo $sale_details['sale_total'] ?>">
									<input name="sale_paid" type="hidden" id="sale_paid" value="0">
									<input name="sale_balance" type="hidden" id="sale_balance" value="0">

									<input name="warehouse_id" type="hidden" id="warehouse_id" value="<?php echo $sale_details['warehouse_id']; ?>">
									<input name="customer_id" type="hidden" id="customer_id" value="<?php echo $sale_details['customer_id']; ?>">
									<input name="sale_id" type="hidden" id="sale_id" value="<?php echo $sale_id; ?>">
									<input name="rowCount" type="hidden" id="rowCount" value="<?php echo $tmpcount ?>">
									<input name="sales_return_cost_total" type="hidden" id="sales_return_cost_total" value="0">

								</form>

								<!-- end: DYNAMIC TABLE PANEL -->
								<div style="height:15px; clear: both;"></div>
							</div>
						</div>


						<!-- end grid -->


					</div>
					<!-- end: PAGE -->
				</div>
				<!-- end: MAIN CONTAINER -->
				<!-- start: FOOTER -->
				<div class="footer clearfix">
					<div class="footer-inner">
						2014 &copy; clip-one by cliptheme.
					</div>
					<div class="footer-items">
						<span class="go-top"><i class="clip-chevron-up"></i></span>
					</div>
				</div>
				<!-- end: FOOTER -->
				<!-- start: RIGHT SIDEBAR -->
				<!-- end: RIGHT SIDEBAR -->
				<div id="event-management" class="modal fade" tabindex="-1" data-width="760" style="display: none;">
					<div class="modal-dialog">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
									&times;
								</button>
								<h4 class="modal-title">Event Management</h4>
							</div>
							<div class="modal-body"></div>
							<div class="modal-footer">
								<button type="button" data-dismiss="modal" class="btn btn-light-grey">
									Close
								</button>
								<button type="button" class="btn btn-danger remove-event no-display">
									<i class='fa fa-trash-o'></i> Delete Event
								</button>
								<button type='submit' class='btn btn-success save-event'>
									<i class='fa fa-check'></i> Save
								</button>
							</div>
						</div>
					</div>
				</div>


				<input name="tmpVal" type="hidden" id="tmpVal" value="0">


				<!-- start ajax model -->
				<div id="ajax-modal" class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" style="display: none;"></div>
				<!-- end ajax model -->



				<!-- start: MAIN JAVASCRIPTS -->
				<?php $this->load->view("common/footer"); ?>
				<script src="<?php echo asset_url(); ?>js/jquery-ui.js"></script>

				<script src="<?php echo asset_url(); ?>js/moment-with-locales.js"></script>
				<?php /*?>
		<script type="text/javascript" src="<?php echo asset_url(); ?>js/perches.js"></script><?php */ ?>
				<script type="text/javascript" src="<?php echo asset_url(); ?>js/bootbox.min.js"></script>
				<script type="text/javascript" src="<?php echo asset_url(); ?>js/bootstrap-datetimepicker.js"></script>
				<script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.min.js"></script>


				<!-- end: MAIN JAVASCRIPTS -->

				<!-- start: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY VALIDATION-->
				<script src="<?php echo asset_url(); ?>js/form-validation-create_sales_return.js"></script>
				<script src="<?php echo asset_url(); ?>js/sales_return_pos.js"></script>
				<!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY  VALIDATION-->

				<script>
					//document.getElementById("cr_interest_div").style.visibility = "hidden";
					function addCrInterest() {
						//$('#soTable tr').remove();
						//$("#soTable > tbody").remove();
						var x = document.getElementById("in_type").value;
						if (x == "Credit") {
							document.getElementById("cr_interest_div").style.visibility = "visible";
						} else {
							document.getElementById("cr_interest_div").style.visibility = "hidden";
						}
					}

					function addextraprice() {
						calculateTotal();
					}
					calculateTotal();

					function add_items() {
						//ItemnTotals();
						$("#add_item").autocomplete({
							source: '<?php echo base_url(); ?>sales/return_suggestion_hw?t=' + $("#in_type").val() + '&page=sales_return_add&sale_id=<?php echo $sale_id ?>',
							minLength: 1,
							autoFocus: false,
							delay: 5,
							response: function(event, ui) {
								if (ui.content.length == 1 && ui.content[0].product_id != 0) {
									//ui.item = ui.content[0];
									//$(this).data('ui-autocomplete')._trigger('select', 'autocompleteselect', ui);
									//$(this).autocomplete('close');
									// $(this).removeClass('ui-autocomplete-loading');
								} else if (ui.content.length == 0) {
									var noResult = {
										value: "",
										label: "No matching result found! Product might be out of stock in the selected warehouse."
									};
									ui.content.push(noResult);
								} else {

								}
							},
							select: function(event, data) {
								//alert( "You selected: " + data.item.product_id );
								if (data.item.value) {
									//check is added prodcut
									rowCount = parseInt($('#rowCount').val());
									var tmp = '';
									if (rowCount) {
										tmp = isAddedProduct(data.item.product_id);
									}
									if (!tmp) {
										addProductToListByID(data.item.product_id, data.item.product_name, data.item.product_code, data.item.product_price, data.item.product_part_no, data.item.product_oem_part_number, data.item.item_cost, data.item.qty_in_stock, data.item.batch_id);
										//$('#in_type').attr("disabled", true); 
										$('#in_type option:not(:selected)').prop('disabled', true);
										$('#customer_id option:not(:selected)').prop('disabled', true);
									} else {
										calculateTotal();
									}
									$("#add_item").val('');
									return false;
								}
								$(this).autocomplete('close');
								$(this).removeClass('ui-autocomplete-loading');
							}
						});
					}

					$('#sl_rtn_datetime').datetimepicker({
						defaultDate: new Date()
					});
					$('#add_item').keypress(function(e) {
						if (e.which == 13) return false;
						//or...
						if (e.which == 13) e.preventDefault();
					});

					function changeQtyByProductID(qty, nxtCount) {
						//alert(qty+' '+nxtCount);
						var quantity_fld = '#quantity_' + nxtCount;
						var product_id_fld = '#product_id_' + nxtCount;
						if (isNaN(qty)) {
							displayNotice('page', 'Invalid Quantity');
							var oldVal = $('#tmpVal').val();
							$(quantity_fld).val(oldVal); //set last val
						} else {
							//getavalable product count
							var product_id_fld = '#product_id_' + nxtCount;
							var product_id = $(product_id_fld).val();
							var sale_id = $('#sale_id').val();
							var availble_qty_fld = '#qtyforcheck_' + nxtCount;
							var availble_qty = parseFloat($(availble_qty_fld).val());
							if (qty <= availble_qty && qty > 0) {
								calculateTotal();
							} else {
								if (qty > availble_qty) {
									bootbox.alert('Out of stock.', function() {
										$('#quantity_' + nxtCount).focus();
									});
								}
								if (qty <= 0) {
									bootbox.alert('System cannot accept your quntity requirement.', function() {
										$('#quantity_' + nxtCount).focus();
									});
								}
								$('#quantity_' + nxtCount).val(1);
								calculateTotal();
							}
							//end getavalable product count
							//displayNotice('page','Product quantity successfully updated!');
						}
					}

					function addextraprice() {
						calculateTotal();
					}

					function getNextRefNo() {
						$.post("<?php echo base_url(); ?>sales/get_next_ref_no")
							.done(function(data) {
								//  var obj = jQuery.parseJSON(data);
								// $('#sale_reference_no').val(obj.sale_reference_no);
							});
					}

					function insertSalesReturnData() {
					    if(parseFloat($('#sale_pymnt_amount').val()) > parseFloat($('#max_amount').val())){
					        bootbox.alert("ERROR! Invalid return amount.");return false;}
						var type = 'A';
						var fields = $("#create_sales_return_form").serialize();
						var rowCount = $('#rowCount').val();
						if (rowCount != 0) {
							$("#return_sale").prop("disabled", true);
							$("#return_sale").val('Please wait...');
							$.post("<?php echo base_url(); ?>sales_return/save_sales_return_pos", fields)
								.done(function(data) {
									var obj = $.parseJSON(data);
									if (obj.error == 1) {

										$('.alert-success').hide();
										$('.alert-danger').show();
										$(".errortxt").html(obj.disMsg);
										window.scrollTo(600, 200);
										//empty item table
										$("#return_sale").prop("disabled", false);
										$("#return_sale").val('Return Sale');

									}
									if (obj.error == 0) {
										//$('.alert-danger').hide();
										//$('.alert-success').show();
										//$( ".succetxt" ).text( obj.disMsg );
										window.scrollTo(500, 0);
										//$("#soTable tr:gt(0)").remove();
										$("#soTable > tbody").empty();
										displayNotice('page', 'Sale return successfully added!');
										sendUrl = 'sales_return/invoice_print_hw/' + obj.sale_id;
										setTimeout(function() {
											window.location.replace("<?php echo base_url(); ?>" + sendUrl);
										}, 1000);
									}
								});
							return false;
						} else {
							bootbox.alert('Please add products.', function() {
								$('#add_item').focus();
							});
						}
					}

					function changePayingby(val) {
						$('.paying_by_details').hide();
						if (val == 'Credit Card') {
							$('#credit_card').show();
						}
						if (val == 'Cheque') {
							$('#cheque_dtls').show();
						}
					}
                    $(document).ready(function(){
                        if($('#customer_id').val() != 1)
                            $('#sale_pymnt_paying_by').append($('<option>', {
                                value: 'store_cash',
                                text: 'Store Cash'
                            }));
                    });
					jQuery(window).load(function() {
						addCrInterest();
					});
					//add_invoice();
				</script>


</body>
<!-- end: BODY -->

</html>