	<?php $this->load->view("common/header"); ?>
	<!-- end: HEAD -->
       
		<!-- start: CSS REQUIRED FOR THIS PAGE ONLY -->

		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/select2/select2.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/datepicker/css/datepicker.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-timepicker/css/bootstrap-timepicker.min.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-daterangepicker/daterangepicker-bs3.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-colorpicker/css/bootstrap-colorpicker.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/jQuery-Tags-Input/jquery.tagsinput.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-fileupload/bootstrap-fileupload.min.css">
		<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/summernote/build/summernote.css">
        <link rel="stylesheet" href="<?php echo asset_url(); ?>css/jquery-ui.css">
        <link rel="stylesheet" href="<?php echo asset_url(); ?>css/pos.css">
		
		<style type="text/css">
			.text_box_7_input {
    height: 40px;
    font-size: 16px !important;
}
		</style>

		<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
	<!-- end: HEAD -->
	<!-- start: BODY -->
	<body>
    
		<!-- start: HEADER -->
		<div class="navbar navbar-inverse navbar-fixed-top">
			<!-- start: TOP NAVIGATION CONTAINER -->
			
			<!-- end: TOP NAVIGATION CONTAINER -->
		</div>
		<!-- end: HEADER -->
		<!-- start: MAIN CONTAINER -->
		<div class="">
			
			<!-- start: PAGE -->
			<div class="">
				<!-- end: SPANEL CONFIGURATION MODAL FORM -->
				<div class="">
					<!-- start: PAGE HEADER -->
					<div class="row">
						<div class="col-sm-12">
							<!-- start: PAGE TITLE & BREADCRUMB -->
							
					
                                                                
<!-- start return box-->                                    
<div id="return_wapper" class="return_wapper" style="display:none">
    
    <div class="panel panel-default">
    <div class="panel-body">
        <div class="panel-heading">
            <i class="fa fa-plus"></i>Add Sales Return
        </div>
    
    	<p style="margin-top:20px" onClick="hide_return_wapper()" class="pull-right"><i class="clip-close-3" ></i></p>
    
<!--    	<div id="class_middle_wappert">
            <div class="col-md-3">
            <div class="form-group">
            
            <br>
            <label>Invocie No  </label><?php //print_r($suppliers);?>
            <input type="hidden" id="sale_id_search"  name="sale_id_search" class="js-data-sales-list-ajax form-control text_box_height" style="padding:0px 0px; border: none; box-shadow: none" placeholder="-Select Item-" onChange="setitemselect()"/>
            </div>
            </div>    
            
            <div class="col-md-1">
            <div class="form-group">
            <br><br>
            <input name="search_invoice" value="Search Invoice" class="btn btn-primary text_box_height" onClick="search_invoice_data()" style="width:200px; margin-top:7px;" id="add_customer" type="">
            </div>
            </div>
        </div>-->
        
        <!--class_middle_wappert-->
        
        <!--start classreturn_invocie_data-->
        <div id="classreturn_invocie_data" class="col-md-12">
        	<!--classreturn_invocie_data-->
        </div> <!--classreturn_invocie_data-->
         
    </div> <!--panel-body-->

    </div> <!--panel panel-default-->
</div>   <!--return_wapper-->                              
                                
                               
<!-- end return box-->

						</div>
					</div>
					<!-- end: PAGE HEADER -->
					<!-- start: PAGE CONTENT 
                    <!-- start grid -->



                    <div class="row">
                    
						<div class="col-md-12" id="classreturn_invocie_data">
							<!-- start: DYNAMIC TABLE PANEL -->
							<div class="panel panel-default">
								
								<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-2">
                            <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 text_box_height" value="" data-bv-field="date"/>
                                    
            
                            </div>
                        </div>
                        <div class="col-md-2">
                            <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','sales_return','');
								?>
                                <input type='text' class="form-control text_box_height" id="sl_rtn_reference_no" name="sl_rtn_reference_no" value="<?php echo $sl_rtn_reference_no ?>" readonly/>
                            </div>
                        </div>
                        
                        <div class="col-md-2">
                            <div class="form-group">
                                <label>Returned By</label>
                                <select id="return_by" class="form-control search-select" name="return_by">
                        <option value="">-- Select --</option>
                        <?php 

															

															foreach ($rep_list as $row)

															{

															 $sel='';

																  if($ss_warehouse_id==$row->warehouse_id)

																  {

																	//  $sel=' selected="selected"';

																  }
															{

															?>
                        <option value="<?php echo $row->user_id;?>"  <?php echo $sel ?>>
                        <?php //echo $row->id.',' ?>
                        <?php echo $row->user_username; ?></option>
                        <?php } }  ?>
                      </select>
                                 </div>
                        </div>
                                
                                
										</div> <!--col-md-8-->
                                        

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

											<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-1">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 class="" name="row[<?php echo $tmpcount ?>][product_sn][]" value="" id="product_sn_<?php echo $tmpcount ?>" type="hidden">
                <input class="" name="row[<?php echo $tmpcount ?>][product_sn_exissts][]" readonly value="<?php echo $row['product_sn_exissts']; ?>" id="product_sn_exissts_<?php echo $tmpcount ?>" type="hidden">
                
                <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;">
                
                <input type="hidden" value="<?php echo $row['item_cost'] ?>" id="item_cost_<?php echo $tmpcount ?>" name="row[<?php echo $tmpcount ?>][item_cost][]">
                </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">
                
                <?php //echo "product_sn_exissts:".$row['product_sn_exissts']; ?>
                <?php if ($row['product_sn_exissts']){ ?>
                <i style="cursor: pointer; margin-top: 3px;" id="addIcon" class="fa fa-2x fa-plus-circle addIcon" onClick="pop_up_add_serial_no('<?php echo $tmpcount;?>')" title="Add Serial No."></i>
                <?php }?>
                </td>
                
                <td class="text-right"><span id="sdiscount_1446800197032" class="text-right sdiscount text-danger">
                    <?php
                    $each_dis_val=0;
                    $each_dis_val=($row['discount_val']/$row['quantity']);
                    //echo $each_dis_val;
                    ?>
                <input type="text" style="width:100px;" id="discount_<?php echo $tmpcount ?>" value="<?php echo $each_dis_val;//echo $row['discount']; ?>" name="row[<?php echo $tmpcount ?>][discount][]" onChange="changeDiscountByProductID(this.value,1);" readonly>
                 </span>
                 <input type="hidden" id="discount_val_<?php echo $tmpcount ?>" value="<?php  echo $each_dis_val;//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">
											
					                           <input type="hidden" title="" data-original-title="" value="<?php echo $sale_details['sale_inv_discount'] ?>" class="form-control input-tip" id="sl_rtn_inv_discount" name="sl_rtn_inv_discount" readonly>
											</div>
										</div>

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



<!--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 payment details -->
   
   <!-- start cash payment -->
 <div class="row">
     <div class="col-md-12">
         
       
    <div>
    <div>
        <div id="div1" class="well">Payment Details</div>
        
    </div>
    <div class="col-sm-2">
<div class="payment">
<div class="form-group">
<label for="amount_1">Return Amount *</label> 
<input required type="text" class="pa form-control kb-pad amount text_box_height text_box_7_input" id="sale_pymnt_amount" name="sale_pymnt_amount" value="<?php echo number_format($total_paid_amount, 2, '.', '') ?>">
</div>
</div>
</div>


<div class="col-sm-2">
<div class="payment">
<div class="form-group">
<label for="amount_1">Paying by</label>
<select class="form-control paid_by text_box_height text_box_7_input" 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="Return Payment">Return Payment</option>
<option value="Cash">Cash</option>

<!-- 
<option value="Credit Card">Credit Card</option>
<option value="Return Payment">Return Payment</option>
-->
</select>

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

<div class="col-sm-12">&nbsp;</div>
</div>
    
   
     
     </div>
     
      <!-- end cash payment -->
      
      <!-- credit card -->
      
     <div class="col-md-12">
                  
                    <div class="col-sm-2">
                      <div class="form-group">
                        <label for="amount_1">Credit Card Amount *</label>
                        <input required type="text" class="pa form-control kb-pad amount text_box_height text_box_7_input" id="sale_pymnt_amount_2" name="sale_pymnt_amount_2" value="0" style="text-align:right" onClick="this.select()">
                      </div>
                    </div>
                    <div class="col-sm-2" style="display:">
                      <div class="form-group">
                        <label for="amount_1">Paying by</label>
                        <select class="form-control paid_by text_box_height  text_box_7_input" id="sale_pymnt_paying_by_2" name="sale_pymnt_paying_by_2" data-bv-field="paid_by" tabindex="-1" title="Paying by *" onChange="changePayingby(this.value)">
                          <!--<option value="Cash">Cash</option>-->
                          <option value="Credit Card" selected>Credit Card</option>
                          <!--<option value="Cheque">Cheque</option>-->
                        </select>
                      </div>
                    </div>
                    <div class="col-sm-2">
                      <div class="form-group">
                        <label for="amount_1">Type</label>
                        <select class="form-control paid_by text_box_7_input" id="sale_pymnt_crdt_card_type" name="sale_pymnt_crdt_card_type" data-bv-field="paid_by">
                          <option value="">-Select-</option> 
                          <option value="Visa">Visa</option>
                          <option value="MasterCard">MasterCard</option>
                          <option value="Amex">Amex</option>
                          <option value="Discover">Discover</option>
                        </select>
                      </div>
                    </div>
                    <div class="col-sm-2">
                      <div class="form-group">
                        <label for="amount_1">Last 4 Digits</label>
                        <input required type="text" class="pa form-control kb-pad amount text_box_height text_box_7_input" id="credit_card_last_digits" name="credit_card_last_digits" value="0" style="text-align:right" onClick="this.select()">
                      </div>
                    </div>
                    <div class="col-sm-12">&nbsp;</div>
                    </div>
                    
                    <!-- end credit card -->
                    
                    
                    <!-- cheque start -->
      
     <div class="col-md-12">
         <div class="col-sm-2">
                      <div class="form-group">
                        <label for="amount_1">Cheque Amount *</label>
                        <input required type="text" class="pa form-control kb-pad amount text_box_height text_box_7_input" id="sale_pymnt_amount_3" name="sale_pymnt_amount_3" value="0" style="text-align:right" onClick="this.select()">
                      </div>
                    </div>
                    
                    <div class="col-sm-2 " style="display:">
                      <div class="form-group">
                        <label for="amount_1">Paying by</label>
                        <select class="form-control paid_by text_box_height text_box_7_input" id="sale_pymnt_paying_by_3" name="sale_pymnt_paying_by_3" data-bv-field="paid_by" tabindex="-1" title="Paying by *" onChange="changePayingby(this.value)">
                          <!--<option value="Cash">Cash</option>
<option value="Credit Card" selected>Credit Card</option>-->
                          <option value="Cheque">Cheque</option>
                        </select>
                      </div>
                    </div>
                    <div class="col-sm-2">
                      <div class="form-group">
                        <label for="amount_1">Cheque No</label>
                        <input required type="text" class="pa form-control kb-pad amount text_box_height text_box_7_input" id="sale_pymnt_cheque_no" name="sale_pymnt_cheque_no" value="0" style="text-align:right" onClick="this.select()">
                      </div>
                    </div>
                    <div class="col-sm-2">
                      <div class="form-group">
                        <?php
//print_r($bank_list);

?>
                        <label for="amount_1">Bank</label>
                        <br/>
                        <select id="bank_id" class="form-control text_box_7_input" name="bank_id">
                          <option value="">-Select-</option>
                          <?php
 foreach ($bank_list as $row){
	 
	 ?>
                          <option value="<?php echo $row->bank_id; ?>" <?php //echo $sel ?> > <?php echo $row->bank_name; ?> </option>
                          <?php }?>
                        </select>
                        <!-- 
<input required type="text" class="pa form-control kb-pad amount text_box_height" id="bank_id" name="bank_id" value="0" style="text-align:right" onClick="this.select()"> --> 
                        
                      </div>
                    </div>
                    
                    <div class="col-sm-2">
                      <div class="form-group">
                        <label for="amount_1">Cheque Realized Date</label>
                        <input style="margin-top:5px" class="form-control paid_by text_box_7_input" name="rea_date" value="" id="rea_date" type="text">
                      </div>
                    </div>
         
         </div>
         <!-- end cheque -->
      
   <!-- end payment details -->
  
  
<!-- start payments-->
<div class="col-md-12">
    
    
   
    
    
<div class="well well-sm color-green">
<div class="row">
<div class="col-md-2" style="margin-left:1%">
<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 text_box_height" value="<?php echo "SRP/".$sl_rtn_reference_no ?>" name="sale_pymnt_ref_no" data-bv-field="payment_reference_no" data-original-title="" title="">
<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-2">
<div class="payment">
<div class="form-group">
<label for="amount_1">Return Note</label><br />

  
  <textarea name="sl_rtn_note" rows="1" cols="10" class="ckeditor form-control" id="sl_rtn_note" style="height:58px"></textarea>


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


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


 <!-- <label class="checkbox-inline">
  <input id="add_new_sale" type="checkbox" class="square-teal" value="1"  name="add_new_sale">  
  </label>-->
   Add New Sale
  <br>

<!--   <label style="width:50px;" class="checkbox-inline"><input id="add_new_sale" type="checkbox" class="square-teal" value="1"  name="add_new_sale"><br/>&nbsp;&nbsp;</label>-->

<select class="form-control paid_by text_box_height" id="add_new_sale" name="add_new_sale" data-bv-field="paid_by" tabindex="-1" title="Paying by *" onChange="changePayingby(this.value)">
    <option value="1">Yes</option>
<option value="">No</option>


</select>
 
   
   <!--<input id="keep_data" type="checkbox" class="square-teal" value="1"  name="keep_data">-->

</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> 
<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>

<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> 

<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> 

<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> 
</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> 


<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'] ?>">
                         <input name="sl_rtn_total" type="hidden" id="sale_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="total_paid_amount" type="hidden" id="total_paid_amount" value="<?php echo $total_paid_amount ?>">
                           
                           <input name="cost_total" type="hidden" id="cost_total" value="<?php echo $sale_details['cost_total']; ?>">
                           
                           
                         
                   		 </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 -->
		
		<!-- 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>
		
		<!--<script type="text/javascript" src="<?php echo asset_url(); ?>js/perches.js"></script>-->
		<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.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/pos_sales_return.js"></script>

		<!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY  VALIDATION-->

  <a id="modal_ajax_serial_no" data-toggle="modal" href="#"></a> 
  <a id="modal_ajax_serial_no_view" data-toggle="modal" href="#"></a>     
     <input name="" type="hidden" id="curr_row_no" value="0">       
<script>

$(document).ready(function() {
 $('#rea_date').datetimepicker({
      //  defaultDate: new Date()
    });
    
    $("#return_by").select2();
});
function pop_up_add_serial_no(row_no){
	$("#curr_row_no").val(row_no);
	document.getElementById("modal_ajax_serial_no").click();
}

$('a#modal_ajax_serial_no').on('click', function () {

	var curr_row_no=$("#curr_row_no").val();
	var sale_id=$("#sale_id").val();

	$("#curr_row_no").val('');

	var qtyfld='#quantity_'+curr_row_no;

	var productfld='#product_id_'+curr_row_no;

	//alert(qtyfld);

	var qty=$(qtyfld).val();

	var product_id=$(productfld).val();

	//alert(curr_row_no);

	// create the backdrop and wait for next modal to be triggered

	$('body').modalmanager('loading');

	setTimeout(function () {

		var url="<?php echo base_url("sales/sales_rtn_add_serial_no/");?>/"+curr_row_no+'/'+qty+'/'+product_id+'/'+sale_id;

		$modal.load(url, '', function () {

			$modal.modal();

		});

	}, 1000);

});



calculateTotal();


	$('#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');
		
		
		//alert(quantity_fld);
		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 warehouse_id=$('#warehouse_id').val();
		$.get( "<?php echo base_url();?>sales_return/get_avalable_product_qty_for_return", { product_id: product_id, warehouse_id: warehouse_id,sale_id:sale_id,qty:qty } )
			.done(function( data ) {
				var obj = jQuery.parseJSON(data);
				if(obj.remmnaingQty){
					calculateTotal();
				}else{
					 bootbox.alert('Unexpected value provided!', function () { });
					
					var oldVal=$('#tmpVal').val();
					$(quantity_fld).val(oldVal); //set last val
				}
				
	   });
	   
		//end getavalable product count
		
		
		//displayNotice('page','Product quantity successfully updated!');
	}
}

function getNextRefNo(){
	//return 'SALE/2015/11/0001';
	//alert();
	
	$.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);
	  });
	//return false;
}

function insertSalesReturnData(){
	
	
	
	var type='A';
	
	 var fields = $("#create_sales_return_form").serialize();
	  
	  var rowCount=$('#rowCount').val();
	  
	  
	   //check searial no added

	  var validationFailed=false;

    var invalid_sn='';
   
  // alert(1);
   
   for(k=1; k<=rowCount; k++){
		//alert(k);
		//var invalid_sn='';
		var fld_sn="#product_sn_"+k  
		var fld_product_sn_exissts='#product_sn_exissts_'+k;
		
		var fld_qty="#quantity_"+k   
		const sn_val=$(fld_sn).val();
		var qty_val=$(fld_qty).val();
		//alert(sn_val);
		if($(fld_product_sn_exissts).val()==1){
		if(sn_val){
			const splitString = sn_val.split(",");
			var sn_length=splitString.length;
		
			if($(fld_sn).val()==''){
				validationFailed = true;
			}else if(qty_val!=sn_length){
				//invalid_sn="Serial no qty does't match with product qty ."+qty_val;
				if(invalid_sn) invalid_sn=invalid_sn+' ,';
				invalid_sn=invalid_sn+qty_val;
			}
		}else if(qty_val){
			if(!sn_val) {
				validationFailed = true;
			}
		}
		}

		//console.log(splitString);   
   }
/**/


var sale_pymnt_amount_2=$("#sale_pymnt_amount_2").val();
var sale_pymnt_amount_3=$("#sale_pymnt_amount_3").val();
var sale_pymnt_crdt_card_type=$("#sale_pymnt_crdt_card_type").val();
var credit_card_last_digits=$("#credit_card_last_digits").val();
var sale_pymnt_cheque_no=$("#sale_pymnt_cheque_no").val();
var bank_id=$("#bank_id").val();
var rea_date=$("#rea_date").val();

var credit_card_error=false;
var cheque_card_error=false;
if(sale_pymnt_amount_3!=0){
     if(sale_pymnt_cheque_no=="0" || bank_id=="" || rea_date=="" ){
         cheque_card_error=true;
     
     }
} 
if(sale_pymnt_amount_2!=0){
    if(sale_pymnt_crdt_card_type=="" || credit_card_last_digits==0){
        credit_card_error=true;
    }
}


if(invalid_sn){
	alert("Serial no qty does't match with product qty : "+invalid_sn);
}else if(cheque_card_error){
    alert('Please add Cheque No , Bank , Cheque Realized Date');
}else if(credit_card_error){
    alert('Please add Credit card Type , Last 4 Digits');
}
else if (validationFailed) {

	   alert('Please add serial No.');

	 

     // e.preventDefault();

     // return false;

   }

 else if(rowCount!=0){
	//alert('no errors'); 
	// return false; //test
	 
	 
		  $("#return_sale").prop("disabled", true);
		  $("#return_sale").val('Please wait...');

		 // create_sales_form.add_sale.disabled = true;
   		// create_sales_form.add_sale.value = "Please wait...";
    	//	return true;
	
	//alert(fields);
	//type:type, sale_reference_no:sale_reference_no
	$.post( "<?php echo base_url();?>sales_return/save_sales_return", fields)
	  .done(function( data ) {
		  var obj = jQuery.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!');
				
				//clearForm();
				//empty footer details
				
				//alert(obj.sale_id);
				
				//alert(sendUrl);
				
				if(obj.add_new_sale){
					sendUrl='sales/add/'+obj.sale_id;
				}else{
					sendUrl='sales/add';
				}
				
				setTimeout(function(){
  window.location.href = "<?php echo base_url();?>"+sendUrl;
}, 500);

				
				
			}
			
			
	  });
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();	
		}
	 }	
	 
	 
	/* $(".js-data-sales-list-ajax").select2({
	   
	 
        ajax: {
			'type': 'POST',
				'url': '<?php echo base_url('sales_pos/js_data_sales_list_ajax?srh_warehouse_id=');?>'+$("#warehouse_id").val(),
                dataType: 'json',
                delay: 0,
			
                data: function (query) {
						
                if (!query)
                        query = '';
                return {
                search_string: query,
				 cat_srh: $("#cat_srh").val(),
				  srh_warehouse_id: $("#srh_warehouse_id").val(),
				service_item_avalable: $("#service_item_avalable").val(),
                        format: 'json'
                };
                },
                results: function (data) {
                return {
                results: $.map(data, function (item) {
                return {
					
                text: item.sale_reference_no,
                        slug: item.sale_id,
                        id: item.sale_id
                };
                })
                };
                },
                cache: true
         }
        });
		*/
		
		
	
</script>


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