<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>Credit Note</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> 
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">
                                                                
    <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">
                   <form id="myForm">
                    <div class="row">
                        
                                                    
                                                    <div class="col-sm-3">
                                                           Customer Oder
                                                            <select class="form-control standardSelect choosen-select" id="supselect" data-placeholder="Choose a Supplier..." name="supselect">
    							                                <option value="0">Select</option>
                                                                                                                       
                                                            </select>  </div>
                                                            <input type="hidden" id="cus_id"  name="cus_id" placeholder="Total" value="0" class="col-xs-12 col-sm-12 form-control" style="text-align: right;"/>
                                                           
                                                    <div class="col-sm-3">  
                                                        Quotation
                                                            <select class="form-control standardSelect choosen-select" id="poselect" data-placeholder="Choose a Supplier..." name="poselect">
    							                                <option value="0"> Select  </option>
                                                                                                                    
                                                            </select> 
                                                                                  
                                                    </div>
                                                    <div class="col-sm-6">  
                                                       Customer
                                                           <input type="text" id="cust"  name="cust" placeholder="" value="" class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" />
                                                                         
                                                                                  
                                                    </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</th>
                                    													<th style="text-align:right">Unit Price</th>
                                    													<th style="text-align:right"><span id="cr_chng">Invoice Quantity</span> </th>
                                    													<th style="text-align:right"><span id="cr_chng">Return Quantity</span> </th>  
                                    													<th style="text-align:right"><span id="cr_chng"></span> </th>
                                    													<th style="text-align:right">Remove</th>
                                    
                                    
                                    													
                                    												</tr>
                                    											</thead>
                                            
                                    											<tbody id="show_data" >
                                    									
                                    								
                                    											</tbody>
                                    										</table>
                                    									</div><!-- /.span -->
                                                                          
                                                                                                        
                                                                                                            
                                    								</div><!-- /.row -->
                                   <div class="space" style="padding-bottom: 10px;"></div>
                                      <div class="row">
                                    								    <div class="col-lg-6">
                                    								        
                                    							<div class="col-lg-12">
                                                                 Remarks
                                                                 <textarea id="remark" name="remark" rows="2" cols="60" style="width: 100%;"></textarea>
                                                            </div> 
                                                 
                                                    
                                               
                                                   
                                    								    </div>
                                    								    
                                    								    
                                    								    
                                    								    <div class="col-lg-6">
                                    								        <div class="row">
                                    									<div class="col-lg-12" style="padding-bottom: 2px;">
                                    									     <div class="row" style="float: right;">
                                    									         <div class="col-lg-6 d-inline">
                                                                              Total Quantity
                                                                            <input type="text" id="inv_tot_item"  name="inv_tot_item" placeholder="Total" value="0" class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" style="text-align: right; "/>
                                                                        </div>
                                                                       <div class="col-lg-6 float-end">
                                                                              Sub Total
                                                                            <input type="text" id="inv_total"  name="inv_total" 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: 2px;">
                                    									     <div class="row" style="float: right;">
                                                                        
                                                                       <div class="col-lg-6 d-inline">  
                                                                             Discount(Amount)
                                                                            <input type="text" id="dis_total"  name="dis_total" placeholder="%" value="0" class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" onclick="this.select();" onkeyup="cal_dis_amt();" style="text-align: right;"/>
                                                                        </div>
                                                                        <div class="col-lg-6 float-end">
                                                                              Discount(%)
                                                                              <input type="text" id="dis_pc"  name="dis_pc" placeholder="Dis_amount" value="0" class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" onclick="this.select();" onkeyup="cal_dis_pc();" style="text-align: right;"/>
                                                                        <div class="help-block" id="input-span-slider"></div>
                                                                        </div>
                                                                </div>
                                    									    
                                    									    </div>
                                                                         </div>
                                                                         <div class="row">
                                    									<div class="col-lg-12" style="padding-bottom: 2px;">
                                    									     <div class="row" style="float: right;">
                                                                        
                                                                        <div class="col-lg-12 float-end">
                                                                              Net Total
                                                                              <input type="text" id="net"  name="net" placeholder="Net Total" value="0" class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" onclick="this.select();" onkeyup="cal_dis_pc();" style="text-align: right;"/>
                                                                        <div class="help-block" id="input-span-slider"></div>
                                                                        </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">
                                                                            Grand Total (<span id="dis_cur"></span>)<input type="hidden" id="curid"  name="curid"/>
                                                                       
                                                                            <input type="text" id="final1"  name="final1" 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>
               
                    <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>
                                                   Create Credit Note
                                            </button>
                                            
                                             </form>
                                           
                                            <button class="btn btn-secondary btn-sm" id="btn_cancel">
                                                    <i class="ace-icon fa fa-times red2"></i>
                                                    Cancel
                                            </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="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <script src="<?php echo base_url(); ?>assets/js/jquery-ui.js"></script>
  <script src="<?php echo base_url(); ?>assets/js/lib/chosen/chosen.jquery.min.js"></script>

<script>
  $( function() {
    $( ".datepicker" ).datepicker();
    $( ".datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd");
  } );

        
               function cal_dis_amt(){
      $('#dis_pc').val("0.00");
      cal_total(); 
     
    }

    function cal_dis_pc(){
        var pc = $('#dis_pc').val();
        var dis = ($('#inv_total').val() * pc)/100;
      $('#dis_total').val(dis.toFixed(2));
      var nett = parseFloat($('#inv_total').val()) - dis;
      $('#net').val(nett.toFixed(2));
     
      cal_total();
    }
    
    


    function cal_total(){
        if($('#dis_pc').val()!=""){
            var dtot = (parseFloat($('#dis_pc').val())/100) *parseFloat($('#inv_total').val());
            $('#dis_total').val(dtot.toFixed(2));
        }
    
      if($('#dis_total').val()!=""){
        var dis =  $('#dis_total').val();
      }else{ var dis = 0;}
      
      var nett =  parseFloat($('#inv_total').val())-parseFloat(dis);
      $('#net').val(nett);
      
     

        var total = parseFloat($('#inv_total').val()) - parseFloat(dis) ;
        $('[name="final1"]').val(total.toFixed(2));
       
    }

           $('#tccat').change(function(){ 
            document.getElementById("load").style.display = "block";
            $('#tcdes option').remove();
             var f = document.getElementById("tccat");
            var cat_id = f.options[f.selectedIndex].value;
           
              $.ajax({
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('Po_Controller/gettc')?>',
                dataType : 'json',
                data : {cat_id:cat_id},
                success : function(data){
                    console.log(data);
                    $('#tcdes').append($('<option>', {
                                                            value: "0",
                                                            text: "select"
                                                        }));
                                                        
                                                         var i;
                            for (var i = 0; i < data['tc'].length; i++) {
                               $('#tcdes').append($('<option>', {
                                                            value: data['tc'][i].tmdes_id,
                                                            text: data['tc'][i].tmdes_des
                                                        }));
                            }
                    
          document.getElementById("load").style.display = "none";
					
                }
 
            });
        });
        
        function addtcrow(){
            
            var f = document.getElementById("tccat");
            var tc_cat = f.options[f.selectedIndex].value;
            var d = document.getElementById("tcdes");
            var tc_des = d.options[d.selectedIndex].value;
            var tempid = $('#temp_id').val();
            
              $.ajax({
                type : "POST",
                url  : "<?php echo site_url('Po_Controller/addtc')?>",
                dataType : "JSON",
                data : {temp_id:tempid, tc_cat:tc_cat , tc_des:tc_des},
                success: function(data){
                    
                       alert(data);
                    
                    //$("#agentselect").prop("disabled", false);
                   // $('#agentselect').prop('disabled', 'disabled');
                    //show_product();
                    show_term();
                    
                }
            });
            return false;
            
        }
    

    $(document).ready(function(){
      $('#tcdes').change(function() {
    //debugger;
    $("#supselect").trigger("chosen:updated");
    let tr = $("<tr/>").appendTo("#data tbody");

    $('#tcdes, #tccat').each(function(index) {
      var input = $(this);
      $(tr).insertBefore("#finalrow").append('<td class=row-' + $(input).attr("id") + '><input type="hidden" id="term[]" name="term[]" value="' + $(input).find('option:selected').val() + '">' + $(input).find('option:selected').text() + '  </td>');
    });
    

    $(tr).append('<td class="row-time"></td>');
    //$("#calc")[0].reset();

    var removeBtn = $('<button class="removeBtn btn btn-outline-danger btn-sm" id="removeBtn">X</button>');
    $('.row-time:last').append(removeBtn);

  });
    });
    
      jQuery(document).ready(function() {
          get_sup();
        jQuery(".standardSelect").chosen({
            disable_search_threshold: 5,
            no_results_text: "Oops, nothing found!",
            width: "100%"
        });
    });
    
        $('#poselect').change(function(){ 
            
            
       
                var g = document.getElementById("poselect");
                var id = g.options[g.selectedIndex].value;
                var id_txt = g.options[g.selectedIndex].text;
    
             if(id==0) {
                 swal("Select Quotation"); 
                // $('#show_data').html("");
                          //  $('#posup_do').val("");
                            return;} 
         
            document.getElementById("load").style.display = "block";
             $.ajax({
                 
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('getinv_product')?>',
                dataType : 'json',
                data : {po_id:id},
                success : function(data){
                    //alert(data.customer);
                   // $("#show_data").after(data['item']);
                           $('#show_data').html(data['item']);
                            //$('#posup_do').val(data[2]);
                            
                       var totdis =   parseFloat($('#dis_total').val()) + parseFloat(data['customer'][0].co_discount); 
                       $('#dis_total').val(totdis.toFixed(2));
                       var prc = (parseFloat(data['customer'][0].co_discount)/parseFloat(data['customer'][0].co_tot_price))*100;
                       $('#dis_pc').val(prc.toFixed(2));
                       
                      var totbnk =   parseFloat($('#bank_fee').val()) + parseFloat(data['customer'][0].bank); 
                       $('#bank_fee').val(totbnk.toFixed(2));
                       
                       var totship =   parseFloat($('#ship').val()) + parseFloat(data['customer'][0].ship); 
                       $('#ship').val(totship.toFixed(2));
                       
                       var totship =   parseFloat($('#trfee').val()) + parseFloat(data['customer'][0].trans); 
                       $('#trfee').val(totship.toFixed(2));
                       
                       var tax =   parseFloat($('#tax_total').val()) + parseFloat(data['customer'][0].tax); 
                       $('#tax_total').val(tax.toFixed(2));
                       
                       var other =   parseFloat($('#other_amnt').val()) + parseFloat(data['customer'][0].other); 
                       $('#other_amnt').val(other.toFixed(2));
                       
                       var allqtn =   id_txt + ","+($('#all_qt').val()); 
                       $('#all_qt').val(allqtn);
                       
                         document.getElementById("dis_cur").textContent=data['customer'][0].curr;
  
                       //$("#cur").val(data['customer'][0].curr);
                       $("#curid").val(data['customer'][0].cur_id);
                      // document.getElementById("cur").disabled = true;
                      
                    document.getElementById("supselect").setAttribute("disabled", "disabled");
                    var g = document.getElementById("supselect");
                    $("#supselect").trigger("chosen:updated");
                    var cid = g.options[g.selectedIndex].value;
                    $("#cus_id").val(cid);
                    
                            document.getElementById("load").style.display = "none";
                            
        
                           get_cal();
                           cal_total();
                }
                
            }); 
            
            cal_total();
            

      }); 
      
      
      
      function deleteRow(btn) {
            var row = btn.parentNode.parentNode.parentNode;
  row.parentNode.removeChild(row);
  get_cal();
        }
      
    $("#btn_posave").on('click',function() {
        
         var res = document.getElementById("all_qt");
            //var inv = res.options[res.selectedIndex].value;
        //var inv = $('#posup_invoice').val();
        var podo = $('#posup_do').val();
                var g = document.getElementById("supselect");
                var id = g.options[g.selectedIndex].value;
        
       
        
            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 x = 0;
                for (let i = 0; i < ids.length; i++) {
                    
                    if((values[i]=="")||(values[i]=="0")){
                         swal("Please enter a value");
                        event.preventDefault();
                        return;
                    } 
                    
                }
                
                if(ids.length===x){
                    swal("No Values");
                       event.preventDefault();
                        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_creditnote')?>",
                            dataType : "JSON",
                            data : $('#myForm').serialize(),
                            success: function(data){
                                
                                //show_product()
                                swal('Data Saved');
                               
                                location.href = "<?php echo site_url('view_crn')?>/";
                            }
                                 });

                     } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                  
                });
           event.preventDefault();
            return false;
    });
    
 
    
   $("#po_tbl").on('onkeyup','.txt_val',function(){
       var currentRow=$(this).closest("tr");  
         var col1=currentRow.find("td:eq(0)").html();
         alert(crw);
   })

    function get_cal(){
        var table = document.getElementById("po_tbl"), sumqty =sumamnt= mul=0;
         
            
            for(var i = 1; i < table.rows.length; i++)
            {
                mul = parseFloat(table.rows[i].cells[4].innerHTML);
                    $(this).closest('tr').find("input").each(function() {
                        alert(this.value)
                    });
                chk = parseFloat(table.rows[i].cells[5].innerHTML);
                muby = parseFloat(table.rows[i].cells[6].children[0].value); 
                sumqty = sumqty + parseFloat(table.rows[i].cells[6].children[0].value);
                if(chk < muby){
                    
                   swal("exceed value"); 
                }else{sumamnt = sumamnt + (mul*muby);}
                
                
                
                
            }
        
            $('#inv_tot_item').val(sumqty.toFixed(2));
            $('#inv_total').val(sumamnt.toFixed(2));
            
            
            
            cal_total();
    }
    
    function get_sup(){
        $('#supselect option').remove();
        $("#supselect").trigger("chosen:updated");
        $.ajax({
                        type  : 'ajax',
                        method : "POST",
                        url   : "<?php echo site_url('cod_list')?>",
                        async : true,
                        dataType : 'json',
                        success : function(data){
                            
                              $('#supselect').append($('<option>', {
                                                            value: "",
                                                            text: "select"
                                                        }));
                                                        
                           var i;
                    for(i=0; i<data.length; i++){
                            $('#supselect').append($('<option>', {
                                                            value: data[i][1],
                                                            text: data[i][0]+data[i][1]
                                                        }));
                        
                                                
                    }                              
               
                        
                                            
                    
                       $("#supselect").trigger("chosen:updated");
                        }
         
                    });
                    
                   
    }
    
    $('#supselect').change(function(){ 
          document.getElementById("load").style.display = "block";
                    $('[name="price"]').val("");
                    $('[name="pricelkr"]').val("");
					$('[name="qty"]').val("");
					$('[name="total"]').val("");
					$('[name="all_qt"]').val("");
					//document.getElementById("curid").selectedIndex = "0";
					 
          var id = $(this).val();
          if(id!=""){
               $.ajax({
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('inv_list')?>',
                dataType : 'json',
                data : {sup_id:id},
                success : function(data){
                    $('#poselect option').remove();
                    $('#poselect').append($('<option>', {
                                                            value: "",
                                                            text: "select"
                                                        }));
                    var i;
                    for(i=0; i<data['qtn'].length; i++){
                            $('#poselect').append($('<option>', {
                                                            value: data['qtn'][i].co_id,
                                                            text: data['qtn'][i].co_code +data['qtn'][i].co_id
                                                        }));
                        
                                                
                    }$("#poselect").trigger("chosen:updated");
                    
                     $('[name="cust"]').val(data['sup'][0].cust_company);    
                    
                    $('[name="cur"]').val(data['sup'][0].cu_symbol);
                   $("#curid").val(data['sup'][0].cur_id);
                    $('[name="sup_type"]').val(data['sup'][0].custo_type);
                    //document.getElementById("cr_chng").innerHTML = "Rate("+data['sup'][0].cu_symbol+")"; 
                    //document.getElementById("cr2_chng").innerHTML = "Total("+data['sup'][0].cu_symbol+")"; 
                    
                    
                    if(data['sup'][0].custo_type=="Foreign"){
                     // var txt1 = document.getElementById("for_foreign1");                      
                      //txt1.style.display = "none";
                      
                      
                    
                      $('#supselect_chosen').css('border', '3px solid rgb(247, 166, 126)');
                      
                      
                    }else{
                      //var txt1 = document.getElementById("for_foreign1");                      
                      //txt1.style.display = "none";
                     
                      $('#supselect_chosen').css('border', '0px solid #ced4da');
                    }
                    
                 
                    document.getElementById("load").style.display = "none";
                    
                }
 
            }); 
          }else{
             document.getElementById("load").style.display = "none"; 
          }
            
      });
      
              
    
</script>