<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>New Invoice</h1>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <?php $cid = $id ?>
        
                <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">
                   <form id="myForm">
                       <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;"/>
                                 
                               <input type="hidden" id="coid"  name="coid" placeholder="Total" value="0" class="col-xs-12 col-sm-12 form-control" style="text-align: right;"/>
                       
                    <!--div class="row">
                        
                                                    
                                                    <div class="col-sm-5">
                                                           Customer
                                                            <select class="form-control standardSelect choosen-select" id="supselect" data-placeholder="Choose a Supplier..." name="supselect">
    							                                <option value="0"> Select Customer </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">  
                                                        <br>
                                                            <select class="form-control standardSelect choosen-select" id="poselect" data-placeholder="Choose a Supplier..." name="poselect">
    							                                <option value="0"> Select Quotation </option>
                                                                                                                    
                                                            </select> 
                                                                                  
                                                    </div>
                                                    <div class="col-sm-4">  
                                                        <br>
                                                           <input type="text" id="all_qt"  name="all_qt" placeholder="Total" value="" class="col-xs-12 col-sm-12 form-control" style="text-align: right;"/>
                                                                         
                                                                                  
                                                    </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-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">QTN</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">Order Quantity</span> </th>
                                    													<th style="text-align:right"><span id="cr_chng">Invoice 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="row">
                                    								    <div class="col-lg-6">
                                    								        
                                    								       <div class="row">
                                    								           
                                    							        
                                    						<div class="col-lg-6">   
                                    						<form id="calc">
                                                     
                                                        T & C Catagory 
                                                            <select class="form-control" id="tccat" data-placeholder="" name="tccat">
    							                                <option value="0"> Select T & C Catagory </option>
                                                             <?php 
                                                             foreach ($tccatdetails as $dbagent) {            
                                                              ?>
    							                            <option value="<?php echo $dbagent->tcat_id; ?>"><?php echo $dbagent->tcat_name; ?></option>
                                                              <?php }?>                                                           
                                                            </select>                        
                                                    
                                                                </div>
                                                    
                                                    		 <div class="col-lg-6">       
                                                     
                                                       TC Description
                                                            <select class="form-control" id="tcdes" data-placeholder="Choose a Supplier..." name="tcdes">
    							                                <option value="0"> Select Conditions</option>
    							                               
                                                             <?php 
                                                             foreach ($tc as $dbagent) {            
                                                              ?>
    							                            <option value="<?php echo $dbagent->tmdes_id; ?>"><?php echo $dbagent->tmdes_des; ?></option>
                                                              <?php }?>                                                           
                                                            </select>                         
                                                    
                                                    
                                                                
                                                            </div>
                                                            
                                                            <div class="col-lg-12"> Terms & Conditions
                                                            
                                                              <table  id="data">
                                                          <thead>
                                                        
                                                          <tr id="finalrow">
                                                            <td></td>
                                                            <td></td>
                                                           
                                                            <td id="total"></td>
                                                          </tr>
                                                          </thead>
                                                          
                                                          <tbody></tbody>
                                                        
                                                        </table></div>
                                                            
                                                          
                                                             </form >   
                                                             <br>
                                                            <div class="col-lg-12">
                                                                 Remarks
                                                                 <textarea id="remark" name="remark" rows="2" cols="60"></textarea>
                                                            </div> 
                                                    
                                                    </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" 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" 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" 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" 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" 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 id="tx" style="display: none;">
                                                                             <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">
                                                                                 Tax amount
                                                                            <input type="text" id="tax_total"  name="tax_total" placeholder="Dis_amount" value="0" class="col-xs-12 col-sm-12 form-control" onclick="this.select();" onkeyup="cal_tax_amt();" style="text-align: right;"/>
                                                                        </div>
                                                                        <div class="col-lg-6 float-end">
                                                                              Tax %
                                                                              <input type="text" id="tax_pc"  name="tax_pc" placeholder="%" value="0" class="col-xs-12 col-sm-12 form-control" onclick="this.select();" onkeyup="cal_tax_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">
                                                                              Total
                                                                              <input type="text" id="ttot"  name="ttot" placeholder="Total" value="0" class="col-xs-12 col-sm-12 form-control" 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>
                                                                      
                                                                         <div id="for_tax" style="display: none;">
                                                                         
                                                                         <div class="row" >
                                                                         <div class="col-lg-12" style="padding-bottom: 2px;">
                                    									        <div class="row" style="float: right;">
                                    									    <div class="col-lg-12 d-inline">
                                    									        NBT <?php echo $tax[1]->tax_value  ?>% <input type="checkbox" id="chk_nbt" name="chk_nbt" value="10"  onclick="mynbt()">
                                                                                  <input type="text" id="nbt"  name="nbt" placeholder="nbt" value="0.00" class="col-xs-12 col-sm-12 form-control" onkeyup="cal_total();" style="text-align: right;"/>
                                                                            <div class="help-block" id="input-span-slider"></div>
                                    									         </div>
                                    									         </div>
                                    									    
                                    									    </div>     
                                                                             
                                    									<div class="col-lg-12" style="padding-bottom: 2px;">
                                    									    <div class="row" style="float: right;">
                                    									    
                                    									    
                                    									    <div class="col-lg-12 d-inline">
                                    									        VAT <?php echo $tax[0]->tax_value  ?>% <input type="checkbox" id="chk_vat" name="chk_vat" value="10" onclick="myvat()">
                                                                                  <input type="text" id="vat"  name="vat" placeholder="vat" value="0.00" class="col-xs-12 col-sm-12 form-control" onkeyup="cal_total();" 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">
                                                                              Total
                                                                              <input type="text" id="txtot"  name="txtot" placeholder="Total" value="0" class="col-xs-12 col-sm-12 form-control" onkeyup="cal_dis_pc();" style="text-align: right;"/>
                                                                        <div class="help-block" id="input-span-slider"></div>
                                                                        </div>
                                                                </div>
                                    									    
                                    									    </div>
                                                                         </div>
                                                                         
                                                                         </div>   
                                                                        
                                                                       <div id="trafee" style="display: block;">  
                                                                          <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">
                                                                              Transport Fee
                                                                              <input type="text" id="trfee"  name="trfee" placeholder="Transport Fee" value="0" class="col-xs-12 col-sm-12 form-control" onclick="this.select();" onkeyup="cal_total();" style="text-align: right;"/>
                                                                        <div class="help-block" id="input-span-slider"></div>
                                                                        </div>
                                                                </div>
                                    									    
                                    									    </div>
                                                                         </div>
                                                                         </div>
                                                                         
                                                                         
                                                                         
                                                                          <div id="for_foreign2" style="display: none;">
                                                                              
                                                                              
                                                                         <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">
                                                                                 Shipping Fee 
                                                                                  <input type="text" id="ship"  name="ship" placeholder="ship" value="0.00" class="col-xs-12 col-sm-12 form-control" onclick="this.select();" onkeyup="cal_total();" style="text-align: right;"/>
                                                                            <div class="help-block" id="input-span-slider"></div>
                                                                            </div>
                                                                            </div>
                                                                            
                                    									     </div>
                                    									<div class="col-lg-12" style="padding-bottom: 2px;">
                                    									    <div class="row" style="float: right;">
                                    									    <div class="col-lg-12 float-end">
                                                                                 Bank Fee
                                                                                  <input type="text" id="bank_fee"  name="bank_fee" placeholder="bank_fee" value="0.00" class="col-xs-12 col-sm-12 form-control" onclick="this.select();" onkeyup="cal_total();" style="text-align: right;"/>
                                                                            <div class="help-block" id="input-span-slider"></div>
                                                                            </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-6 float-end">
                                                                              Other - Reason
                                                                              <input type="text" id="other_reason"  name="other_reason" placeholder="Total" value="Other" class="col-xs-12 col-sm-12 form-control" />
                                                                        <div class="help-block" id="input-span-slider"></div>
                                                                        </div-->
                                                                                                                                               <div class="col-lg-12 d-inline">
                                                                              Other Fee
                                                                            <input type="text" id="other_amnt"  name="other_amnt" placeholder="Other" value="0.00" class="col-xs-12 col-sm-12 form-control" onclick="this.select();" onkeyup="cal_total();" 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">
                                                                            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" 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 New Invoice
                                            </button>
                                           
                                            <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>
                   
               
               </form>
               </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>


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

    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_ttot();
      //cal_total();
    }
    
    
    function cal_tax_pc(){
        var pc = $('#tax_pc').val();
        var dis = ($('#net').val() * pc)/100;
        //alert(dis);
      $('#tax_total').val(dis.toFixed(2));
      cal_ttot();
      cal_total();
    }
    
    function cal_tax_amt(){
        var tax_total = $('#tax_total').val();
      var pc = (tax_total/$('#net').val())*100;
      $('#tax_pc').val(pc);
      cal_total();
      cal_ttot();
    }

    function cal_ttot(){
        
        var ttot = parseFloat($('#tax_total').val())+parseFloat($('#net').val());
        $('#ttot').val(ttot.toFixed(2));
        
        var txtot = parseFloat($('#net').val())+parseFloat($('#vat').val())+parseFloat($('#nbt').val());
        $('#txtot').val(txtot.toFixed(2));
    }

    function myvat(){
      var checkBox = document.getElementById("chk_vat");
            
      cal_total();
    }

    function mynbt(){
      var checkBox = document.getElementById("chk_nbt");
           
      $('[name="nbt"]').val(nbt.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 =  parseInt($('#inv_total').val())-parseInt(dis);
      $('#net').val(nett);
      
        var other = $('#other_amnt').val();
        var vat = $('#vat').val();
        var nbt = $('#nbt').val();
        var ship = $('#ship').val();
        var bank = $('#bank_fee').val();
        var tax_total = $('#tax_total').val();
        var trfee = $('#trfee').val();

        var total = parseInt($('#inv_total').val()) - parseInt(dis) + parseInt(other)+ parseInt(vat)+ parseInt(nbt)+ parseInt(tax_total)+parseInt(ship)+ parseInt(bank)+ parseInt(trfee);
        $('[name="final1"]').val(total.toFixed(2));
        cal_ttot();
    }

           $('#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(){
       var coid =  "<?php echo $cid ;?>";
        $.ajax({
                 
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('getco_product')?>',
                dataType : 'json',
                data : {po_id:coid},
                success : function(data){
                    $("#show_data").after(data['item']);
                           //$('#show_data').html(data);
                            //$('#posup_do').val(data[2]);
                            
                       var totdis = (data['customer'][0].podis/data['customer'][0].total)*100; 
                       $('#dis_pc').val(totdis.toFixed(2));
                       $('#inv_total').val(data['customer'][0].total);
                       $('#dis_total').val(data['customer'][0].podis);
                       //alert(data['customer'][0].pre_inv);
                       
                       if(data['customer'][0].pre_inv<1){
                                var totbnk =   parseInt($('#bank_fee').val()) + parseInt(data['customer'][0].bank); 
                               $('#bank_fee').val(totbnk.toFixed(2));
                               
                               var totship =   parseInt($('#ship').val()) + parseInt(data['customer'][0].ship); 
                               $('#ship').val(totship.toFixed(2));
                               
                               var totship =   parseInt($('#trfee').val()) + parseInt(data['customer'][0].trans); 
                               $('#trfee').val(totship.toFixed(2));
                               
                               var tax =   parseInt($('#tax_total').val()) + parseInt(data['customer'][0].tax); 
                               $('#tax_total').val(tax.toFixed(2));
                               
                               var other =   parseInt($('#other_amnt').val()) + parseInt(data['customer'][0].other); 
                               $('#other_amnt').val(other.toFixed(2));
                       }else{
                          
                           $('#bank_fee').attr('readonly', true);
                           $('#ship').attr('readonly', true);
                           $('#trfee').attr('readonly', true);
                           $('#tax_total').attr('readonly', true);
                           $('#other_amnt').attr('readonly', true);
                       }
                       
                       
                         document.getElementById("dis_cur").textContent=data['customer'][0].curr;
  
                       $("#cus_id").val(data['customer'][0].customer_id);
                       $("#curid").val(data['customer'][0].cur_id);
                       $("#coid").val(coid);
                      // document.getElementById("cur").disabled = true;
               
                   // $("#cus_id").val(cid);
                    
                            document.getElementById("load").style.display = "none";
                           
                            cal_dis_pc();
                           get_cal();
                           cal_total();
                }
                
            }); 
            
            cal_total();
      
      
        
      $('#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('getqtn_product')?>',
                dataType : 'json',
                data : {po_id:id},
                success : function(data){
                    $("#show_data").after(data['item']);
                           // $('#show_data').html(data);
                            //$('#posup_do').val(data[2]);
                            
                       var totdis =   parseInt($('#dis_total').val()) + parseInt(data['customer'][0].podis); 
                       $('#dis_total').val(totdis.toFixed(2));
                       
                      var totbnk =   parseInt($('#bank_fee').val()) + parseInt(data['customer'][0].bank); 
                       $('#bank_fee').val(totbnk.toFixed(2));
                       
                       var totship =   parseInt($('#ship').val()) + parseInt(data['customer'][0].ship); 
                       $('#ship').val(totship.toFixed(2));
                       
                       var totship =   parseInt($('#trfee').val()) + parseInt(data['customer'][0].trans); 
                       $('#trfee').val(totship.toFixed(2));
                       
                       var tax =   parseInt($('#tax_total').val()) + parseInt(data['customer'][0].tax); 
                       $('#tax_total').val(tax.toFixed(2));
                       
                       var other =   parseInt($('#other_amnt').val()) + parseInt(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;
  
                       $("#cus_id").val(data['customer'][0].customer_ref);
                       $("#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() {
        
            check_qty();
         var res = document.getElementById("all_qt");
        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 x = 0;
                for (let i = 0; i < ids.length; i++) {
                    if((values[i]=="")||(values[i]=="0")){
                         swal("Please enter a value");
                        return;
                    }
                    
                }
                
                if(ids.length===x){
                    swal("No Values");
                        return;
                } 
               
             var item_id = $('input[name="item_item_id[]"]').map(function(){
                   return this.value
            }).get() 
                for (let i = 0; i < item_id.length; i++) {
                    $.ajax({
                            type : "POST",
                            url  : "<?php echo site_url('chk_stock')?>",
                            dataType : "JSON",
                            data : {item:item_id[i],qty:values[i]},
                            success: function(data){
                                if(data!=true){
                                    swal('No Stock available');
                                    return;
                                }
                                
                            }
                        });
                }
              
              
                
             //$(function(){
                var tcvalues = $("input[name='term\\[\\]']")
              .map(function(){return $(this).val();}).get();
                //});
                
                if(tcvalues.length != 0){
                    $.ajax({
                            type : "POST",
                            url  : "<?php echo site_url('save_new_invoice')?>",
                            dataType : "JSON",
                            data : $('#myForm').serialize(),
                            success: function(data){
                                
                                //show_product()
                                swal('Data Saved');
                               
                                location.href = "<?php echo site_url('view_allco')?>/";
                            }
                        });



                }else{ 
                    swal("please add terms and condition");
                }
           
            
            
    });
    
    function check_qty(){
        
        var itm_id = $('input[name="item_item_id[]"]').map(function(){
                   return this.value
            }).get()
            
            
        var nqty = $('input[name="po_val[]"]').map(function(){
                   return this.value
            }).get()
            
    }
    
   $("#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)
                    });
                muby = parseFloat(table.rows[i].cells[6].children[0].value); 
                qqt = parseFloat(table.rows[i].cells[5].innerHTML);
                sumqty = sumqty +  muby;
                sumamnt = sumamnt + (mul*muby);
                
                if(muby > qqt){
                swal("Please enter a correct value");
                return;
                }
            }
       
            $('#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('customer_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][0],
                                                            text: 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("");
					//document.getElementById("curid").selectedIndex = "0";
					 
          var id = $(this).val();
          if(id!=""){
               $.ajax({
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('customer_data')?>',
                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].id,
                                                            text: data['qtn'][i].code +data['qtn'][i].id
                                                        }));
                        
                                                
                    }$("#poselect").trigger("chosen:updated");
                    
                    $('[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";
                      var txt2 = document.getElementById("trafee");                      
                      txt2.style.display = "none";
                      
                      document.getElementById("for_foreign2").style.display = "block";
                      $('#supselect_chosen').css('border', '3px solid rgb(247, 166, 126)');
                      
                      
                    }else{
                      //var txt1 = document.getElementById("for_foreign1");                      
                      //txt1.style.display = "none";
                      document.getElementById("for_foreign2").style.display = "none";
                      document.getElementById("trafee").style.display = "block";
                      $('#supselect_chosen').css('border', '0px solid #ced4da');
                    }
                    
                    if(data['sup'][0].custo_tax == 1){
                        if(data['sup'][0].custo_type=="Foreign"){
                            var txt = document.getElementById("tx");
                            txt.style.display = "block";
                        }else{
                      var txt = document.getElementById("for_tax");
                      txt.style.display = "block";
                        }
                    }else{
                      var txt = document.getElementById("for_tax");
                      txt.style.display = "none";
                    }
                    document.getElementById("load").style.display = "none";
                    
                }
 
            }); 
          }else{
             document.getElementById("load").style.display = "none"; 
          }
            
      });
    
</script>