<div class="breadcrumbs">
            <div class="breadcrumbs-inner">
                <div class="row m-0">
                    <div class="col-sm-4">
                        <div class="page-header float-left">
                            <div class="page-title">
                                <h1>Invoice Return</h1>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
                <style>
.content{position: relative;}
.overlay{position: absolute;left: 0; top: 0; right: 0; bottom: 0;z-index: 2;background-color: rgba(255,255,255,0.8);position: fixed;   overflow-y: scroll;}
.overlay-content {
    position: absolute;
    transform: translateY(-50%);
     -webkit-transform: translateY(-50%);
     -ms-transform: translateY(-50%);
    top: 50%;
    left: 15%;
    right: 0;
    text-align: center;
    color: #555; 
    position: fixed; 
   overflow-y: scroll;
}

.tableFixHead          { overflow: auto; }
.tableFixHead thead th { position: sticky; top: 0; z-index: 1; }
</style>  
                                                                
    <div class="overlay" style="display:none" id="load"><div class="overlay-content"><img src="<?php echo base_url(); ?>assets/images/loader.gif" alt="Loading..." style="width: 60px;"/></div></div>
                    
      
          <link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/lib/chosen/chosen.min.css">
        
        <div class="content">
            <div class="animated fadeIn">
                
                
                
                 <div class="space" style="padding-bottom: 10px;"></div>
                
               <div id="po" style="display:block">
                   
                    <div class="row">
                                                    <div class="col-sm-3">  
                                                        <br>
                                                            <select class="form-control standardSelect choosen-select" id="poselect" data-placeholder="" name="poselect">
    							                                <option value="0"> Select Invoice </option>
                                                             <?php 
                                                             //echo $agentdetail[0];
                                                             foreach ($agentdetail as $dbagent) { 
                                                                 
                                                              ?>
    							                            <option value="<?php echo $dbagent[1]; ?>"><?php echo $dbagent[1]; ?></option>
                                                              <?php }?>                                                           
                                                            </select> 
                                                                                  
                                                    </div>
                                                    
                                                     <!--div class="col-sm-3">
                                                           Reason for Return
                                                           <select class="form-control standardSelect choosen-select" id="posup_invoice" data-placeholder="Choose a Supplier..." name="posup_invoice">
    							                                <option value="0"> Select Reason </option>
                                                             <?php 
                                                             foreach ($ret_res as $dbagent) {            
                                                              ?>
    							                            <option value="<?php echo $dbagent->ret_reason_id; ?>"><?php echo $dbagent->ret_reason; ?></option>
                                                              <?php }?>                                                           
                                                            </select>
                                                    </div-->
                                                    
                                                    <div class="col-sm-6">
                                                           <div class="checkbox"><br>
                                                                <label for="checkbox1" class="form-check-label ">
                                                                    <input type="checkbox" id="checkbox1" name="checkbox1" value="1" class="form-check-input">Total Invoice Return
                                                                </label>
                                                            </div>
                                                    </div>
                                                    
                                                    
                                                    <!--div class="col-sm-3" style="text-align: center;">Add New<br>
                                                         
                                                            <button class="btn btn-secondary btn-sm" name="tax" id="new_product" onclick ="add_data(this.id);" data-backdrop='static' data-keyboard='false'>Product</button> 
                                                            <button class="btn btn-secondary btn-sm" name="Ser" id="add_services" onclick ="add_data(this.id);" data-backdrop='static' data-keyboard='false'>Services</button>
                                                            <button class="btn btn-secondary btn-sm" name="Ser" id="new_supplier" onclick ="add_data(this.id);" data-backdrop='static' data-keyboard='false'>Supplier</button>
                                                    </div-->
                                                    
                                                    
                                                  
                                                   
                                                   
                                                    
                                                   
                                                         
                                                     
                                               </div>
                                               <div class="space" style="padding-bottom: 10px;"></div>
                                               
                                                 <div class="row">
                                    									<div class="col-lg-12" style="padding-bottom: 10px;overflow:scroll;height:280px;width:100%;overflow:auto">
                                    										<table id="po_tbl" class="table table-bordered tableFixHead">
                                    											<thead class="thead-dark">
                                    												<tr>
                                    													
                                    													<th scope="col">ID</th>
                                    													<th>Item Code</th>
                                    													<th>Item Description</th>
                                    													<th style="text-align:right">Unit Price</th>
                                    													<th style="text-align:right"><span id="cr_chng">INV Quantity</span> </th>
                                    													<th style="text-align:right"><span id="cr_chng">Already Returned</span> </th>
                                                                                        <th style="text-align:right; width: 80px"><span id="cr2_chng">Return Quantity</span>  </th>
                                                                                        <th style="text-align:right; width: 100px"><span id="cr2_chng">Return Price</span>  </th>
                                                                                        <th style="text-align:right; width: 70px"><span id="cr2_chng">Free Item</span>  </th>
                                                                                        <th style="text-align:right; width: 70px"><span id="cr2_chng">Already returned</span>  </th>
                                                                                        <th style="text-align:right; width: 70px"><span id="cr2_chng">Return Free</span>  </th>
                                    													
                                    													
                                    
                                    													
                                    												</tr>
                                    											</thead>
                                            
                                    											<tbody id="show_data" >
                                    									
                                    								
                                    											</tbody>
                                    										</table>
                                    									</div><!-- /.span -->
                                                                          
                                                                           <div class="col-sm-6">
                                                           Reason for Return
                                                           <select class="form-control standardSelect choosen-select" id="posup_invoice" data-placeholder="Choose a Supplier..." name="posup_invoice">
    							                                <option value="0"> Select Reason </option>
                                                             <?php 
                                                             foreach ($ret_res as $dbagent) {            
                                                              ?>
    							                            <option value="<?php echo $dbagent->ret_reason_id; ?>"><?php echo $dbagent->ret_reason; ?></option>
                                                              <?php }?>                                                           
                                                            </select>
                                                            
                                                            Remark
                                                           <input type="text" id="posup_do"  name="posup_do" placeholder="" value=""class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" /> 
                                                    </div>     
                                                    <div class="col-sm-6">
                                    								        <div class="row">
                                            									<div class="col-lg-12" style="padding-bottom: 15px;padding-top: 15px;">
                                            									    <div class="row" style="float: right;">
                                            									    <div class="col-lg-12 d-inline">
                                                                                    Discount
                                                                                    <input type="text" id="disc"  name="disc" placeholder="Total" value="0" class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" style="text-align: right;"/>
                                                                                </div>
                                                                                </div>
                                                                                 </div>
                                                                                 </div>
                                                                                 
                                                                            <div class="row">
                                            									<div class="col-lg-12" style="padding-bottom: 15px;padding-top: 15px;">
                                            									    <div class="row" style="float: right;">
                                            									    <div class="col-lg-12 d-inline">
                                                                                    Total
                                                                                    <input type="text" id="final"  name="final" placeholder="Total" value="0" class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" style="text-align: right;"/>
                                                                                </div>
                                                                                </div>
                                                                                 </div>
                                                                                 </div>     
                                    								    </div>
                                                                                                            
                                    								</div><!-- /.row -->
                                    								
                                    								
                                               
                <div class="row">
                    
                     <div class="col-lg-12" style="padding-bottom: 10px;">
                         
                         <table class="table table-bordered">
                                
                                <thead class="thead-dark" id="thed"></thead>
                               
                             <tbody id="tb_bdy">
                            
                        </tbody>
                        </table>
                         
                         </div>
                    
               </div>
               
                    <div class="row">
                                    									<div class="col-lg-12" style="padding-bottom: 10px;">      
                                    
                                                                <div class="row" style="float: right; text-align: right;">
                                                                        
                                        <div class="col-md-12 center-block">
                                            <button class="btn btn-secondary btn-sm" id="btn_posave">
                                                    <i class="ace-icon fa fa-floppy-o bigger-120 blue"></i>
                                                   Save Return Note
                                            </button>
                                           
                                            <button class="btn btn-secondary btn-sm" id="btn_cancel">
                                                    <i class="ace-icon fa fa-times red2"></i>
                                                    Cancel Return Note
                                            </button>
                                        </div>
                                        
                                    </div>
                                     </div>
                                    </div>
                   
               </div>
              
            </div>
        </div>
        
        <div class="clearfix"></div>
<script src="<?php echo base_url(); ?>assets/js/jquery.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/popper.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/bootstrap.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/lib/chosen/chosen.jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

<script>
    
    

    $(document).ready(function(){
      
    });
    
      jQuery(document).ready(function() {
        jQuery(".standardSelect").chosen({
            disable_search_threshold: 1,
            search_contains: true,
            no_results_text: "Oops, nothing found!",
            width: "100%"
        });
    });
    
        $('#poselect').change(function(){ 
       
                var g = document.getElementById("poselect");
                var id = g.options[g.selectedIndex].value;
                $('#final').val(0);
    
             if(id==0) {
                 swal("Select Invoice"); 
                 $('#show_data').html("");
                            $('#posup_do').val("");
                            return;} 
         
            document.getElementById("load").style.display = "block";
             $.ajax({
                 
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('invret_product')?>',
                dataType : 'json',
                data : {po_id:id},
                success : function(data){
              
                            $('#show_data').html(data[0]);
                            $('#posup_do').val(data[2]);
                            $('#disc').val(data[1]);
                      
                            document.getElementById("load").style.display = "none";
                }
                
            }); 
      }); 
      
      
    $("#btn_posave").on('click',function() {
        
        document.getElementById("load").style.display = "block";
        
         var res = document.getElementById("posup_invoice");
            var inv = res.options[res.selectedIndex].value;
        //var inv = $('#posup_invoice').val();
        var podo = $('#posup_do').val();
       
        
            var values = $('input[name="po_val[]"]').map(function(){
                   return this.value
            }).get()
            var ids = $('input[name="po_val[]"]').map(function(){
                   return this.id
            }).get()
            
            var p_tot = $('input[name="po_tot[]"]').map(function(){
                   return this.value
            }).get()
            
            var free_val = $('input[name="free_val[]"]').map(function(){
                   return this.value
            }).get()
            
            var free_tot = $('input[name="free_tot[]"]').map(function(){
                   return this.value
            }).get()
            
            var po_prc = $('input[name="po_prc[]"]').map(function(){
                   return this.value
            }).get()
            
            
            var checkBox = document.getElementById("checkbox1");
             if (checkBox.checked == true){
                var allret = '1';
            }else{
                var x = 0; var allret = '0';
                for (let i = 0; i < ids.length; i++) {
                    if(values[i]==""){
                         x++;
                    }else if(parseInt(values[i])>parseInt(p_tot[i])){
                        swal("Cannot exceed Invoiced quantity");
                        return;
                    }
                    
                    if(free_tot[i]==""){
                         x++;
                    }else if(parseInt(free_val[i])>parseInt(free_tot[i])){
                        swal("Cannot exceed Free quantity");
                        return;
                    }
                    
                }
                
                if(ids.length===x){
                    swal("No Values");
                        return;
                } 
            }
            
               
               var curn = document.getElementById("poselect");
            var poid = curn.options[curn.selectedIndex].text;
            var poidval = curn.options[curn.selectedIndex].value;
                
              var final = $('#final').val();
            
             if(poidval==0) {swal("Select Invoice"); return;}     
             if(inv==0) {swal("Select Reason for Return"); return;}
                
                document.getElementById("load").style.display = "block";
            
            swal({
                  title: "Are you sure?",
                  text: "Save, Entered Details will be Saved",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                            $.ajax({
                            type : "POST",
                            url  : "<?php echo site_url('save_invreturn')?>",
                            dataType : "JSON",
                            data : {poid:poid, values:values, ids:ids,inv:inv,podo:podo,poidval:poidval,final:final,free_val,allret:allret,po_prc:po_prc},
                            success: function(data){
                                swal(data);
                                document.getElementById("load").style.display = "none";
                                location.href = "<?php echo site_url('allinv_return')?>/";
                               
                            }
                        });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
            
            
    });
    
    
   $("#po_tbl").on('onkeyup','.txt_val',function(){
       var currentRow=$(this).closest("tr");  
         var col1=currentRow.find("td:eq(0)").html();
         alert(crw);
   })
   
  function cal_price(){
   
           var table = document.getElementById("po_tbl"), sumqty =sumamnt= mul=0;
            
            for(var i = 1; i < table.rows.length; i++)
            {
                unit_val = parseFloat(table.rows[i].cells[3].innerHTML);
                    $(this).closest('tr').find("input").each(function() {
                        alert(this.value)
                    });
                
                qty = parseFloat(table.rows[i].cells[6].children[0].value); 
                sumqty = unit_val *  qty;
                table.rows[i].cells[7].children[0].value = sumqty;
                sumamnt = sumamnt + sumqty;
                
            }
            
             var disc = $('#disc').val();  
             var newval = (100-disc)*sumamnt.toFixed(2)/100;
        
            $('#final').val(newval.toFixed(2));
           // $('#inv_total').val(sumamnt.toFixed(2));
  }
  
 
        
    
</script>