<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<?php
if (isset($_GET['c_acid'])) {
  $c_acid = $_GET['c_acid'];
  $sltype = $_GET['sltype'];
  $acv = mysqli_query($conn,"SELECT ac_name,balance,ac_number,cr_limit FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$c_acid' LIMIT 1") or die (mysqli_error($conn));
  $acr = mysqli_fetch_assoc($acv);

  $c_name    = $acr['ac_name'];
  $balance   = $acr['balance'];
  $ac_number = $acr['ac_number'];
  $cr_limit = $acr['cr_limit'];
}

      $custom_field= mysqli_query($conn,"SELECT sales FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
      $rowcf= mysqli_fetch_assoc ($custom_field);
   
      $p=$rowcf['sales'];
      function input_check($ap)
      {
       if (strpos($GLOBALS['p'], $ap) !== false) { return 1;} 
      }
      
 //   mysqli_query($conn,"DELETE FROM imei_tracker_edit WHERE  coid='$coid' AND bid='$bid' AND brid='$brid' AND active=0 ") or die(mysqli_error($conn));
    // mysqli_query($conn,"DELETE FROM order_list_edit WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND  active=0 ") or die(mysqli_error($conn));
 //   mysqli_query($conn,"DELETE FROM order_list_edit WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND order_type=('ERSL' || 'ECSL' || 'INSL')   AND  active=0 AND userid='$uid'  ") or die(mysqli_error($conn));

?>
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-1"></div>
      <div class="col-md-10">
        <div class="card card-success card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
          <div class="card-header" style="padding: 5px 8px;">
            <h3 class="card-title">
            <?php if ($sltype=='ERSL') { echo language(166);} else if($sltype=='ECSL'){ echo language(62); } else if($sltype=='INSL'){ echo language(376); } ?>   
            </h3>
            <div class="card-tools">
              <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = invoice($sltype);?></span>
              <div class="btn-group">
                <button type="button" class="btn btn-tool" data-toggle="dropdown"  title="More Tools"><i class="fa-solid fa-ellipsis-vertical"></i>
                </button>
                <ul class="dropdown-menu dropdown-menu-right">
                  <?php if(input_check('esale_draft')==1){ ?>
                  <li><a class="dropdown-item" onclick="draft(this);"><i class="fas fa-inbox"></i> <?php echo language(148);?></a></li>
                  <div class="dropdown-divider m-0"></div>
                  <?php } ?>
                  <li><a class="dropdown-item"><i class="fas fa-inbox"></i> Others</a></li>
                </ul>
                <button type="button" class="btn btn-tool" data-card-widget="remove" data-dismiss="modal" onclick="$.post('../sales/order_list_delete.php',{cinvoice:'<?php echo $invoice;?>'});">
                <i class="fas fa-times"></i>
                </button>
              </div>
            </div>
          </div>
          <div class="card-body">
            <div class="col-md-12">
              <table class="ttable mt-1 <?php if($sltype=='ECSL'){echo 'd-none';}?>" cellpadding="0" cellspacing="0">
                <tbody>
                  <tr>
                    <th class="text-center" width="15%"><?php echo language(164);?></th>
                    <th width="30%"><?php echo $c_name;?></th>
                    <th class="text-center" width="12%"><?php echo language(111);?></th>
                    <th class="text-center" width="15%"><?php echo $ac_number;?></th>
                    <th class="text-center" width="13%"><?php echo language(137);?></th>
                    <th class="text-center" width="15%">
                    <?php if( $cr_limit > $balance ){   ?>
                      <span title='<?php echo $cr_limit?>'> <?php echo $balance  ?>   </span>  
                   <?php }else{  ?> 
                          <span class='bg-danger' title='<?php echo $cr_limit?>'> <?php echo $balance  ?>  Limit End </span>  
                    <?php  } ?></th>
                  </tr>
                </tbody>
              </table>
            <?php if(per('item_scan_status')==1){ ?>
              <form id="sal_sub" class="mt-3" action="#" method="post" autocomplete="off">
            <?php } ?>
                <div class="row">
                  <div class="col-md-3"></div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <label><?php echo language(138);?></label>  
                      <input type="text" id="item_details" class="form-control form-control-sm inputselect" required="true" autofocus="true">
                    </div>
                  </div>
                  <div class="col-md-3"></div>
                  <div class="col-md-12">
                    <hr style="margin-top:-5px;margin-bottom: 7px;">
                  </div>
                </div>
            <?php if(per('item_scan_status')==1){ ?>
              </form>
            <?php } ?>
            </div>
            <form id="sal_form" action="#" method="post" autocomplete="off">
              <input type="hidden" name="c_acid" value="<?php echo $c_acid;?>">
              <input type="hidden" name="sal_invoice" value="<?php echo $invoice;?>">
              <input type="hidden" name="sltype" value="<?php echo $sltype;?>">
              
              
              
              <div class="col-md-12">
                <div class="row">
                  <div class="col-md-12">
                    <div class="card-body table-responsive cscroll" style="max-height: 450px;">
                      <table class="ttable" cellpadding="0" cellspacing="0">
                        <thead>
                          <tr class="sticky-top">
                            <th class="text-center" style="width:2%;"><?php echo language(15);?></th>
                            <th style="width:40%;"><?php echo language(77);?></th>
                            <th class="text-center" style="width:10%;"><?php echo language(139);?></th>
                            <th class="text-center <?php if(input_check('u_type') !==1){echo 'd-none';}?>" style="width:10%;"><?php echo language(98);?></th>
                            <th class="text-center" style="width:15%;"><?php echo language(84);?></th>
                            <th class="text-center" style="width:10%;"><?php echo language(88);?></th>
                            <!--<th class="text-center" style="width:10%;">NET PRICE</th>-->
                            <th class="text-center" style="width:10%;"><?php echo language(140);?></th>
                            <th class="text-center" style="width:2%;">#</th>
                          </tr>
                        </thead>
                      <tbody id="row_show<?php echo $invoice;?>"></tbody>
                    </table>
                  </div>
                </div>
                <div class="col-md-12"><hr style="margin-top: 7px;margin-bottom: 7px;"></div>
                <div class="col-md-7">
                  <div class="form-row">
                    <?php if ($sltype=='ECSL') { ?>
                        <div class="col-md-4">
                          <div class="form-group">
                            <label><?php echo language(164);?></label>
                            <input type="text" name="c_name" class="form-control form-control-sm inputselect">
                          </div>
                        </div>
                        <div class="col-md-3">
                          <div class="form-group">
                            <label><?php echo language(111);?> <b class="text-danger">*</b></label>
                            <input type="number" name="c_gsm" class="form-control form-control-sm" value="" required="true">
                          </div>
                        </div>
                        <div class="col-md-5">
                          <div class="form-group">
                            <label><?php echo language(116);?></label>
                            <input type="text" name="c_address" class="form-control form-control-sm inputselect">
                          </div>
                        </div>
                        <div class="col-md-12"><hr style="margin-top:0px;margin-bottom:5px;"></div>
                    <?php } else if ($sltype=='INSL') { ?>
                        <div class="col-md-3">
                          <div class="form-group">
                             <label><?php echo language(349); ?></label>
                             <select name="ins_type" class="form-control select2" style="width: 100%;" required="true">
                               <option value="Monthly"> <?php echo language(352);?></option>
                               <option value="Weekly">  <?php echo language(351);?></option>
                               <option value="Daily">   <?php echo language(350);?></option>
                             </select>
                          </div>
                        </div>                      
                        <div class="col-md-3">
                          <div class="form-group">
                             <label><?php echo language(379);?></label>
                             <input type="number" name="ins_qty" class="form-control form-control-sm text-center inputselect" required="true" oninput="inscal();" min="2" max="60">
                          </div>
                        </div>
                        <div class="col-md-3">
                          <div class="form-group">
                             <label><?php echo language(357);?></label>
                             <input type="text" id="per_ins" class="form-control form-control-sm text-center" readonly="true">
                          </div>
                        </div>
                        <div class="col-md-3">
                          <div class="form-group">
                             <label><?php echo language(358);?></label>
                             <input type="text" name="first_ins_date" class="form-control form-control-sm text-center" onmouseover="stdate(this)" readonly="true" required="true" value="<?php echo $date;?>">
                          </div>
                        </div>
                        <div class="col-md-4">
                          <div class="form-group">
                             <label><?php echo language(354);?></label>
                             <input type="text" name="reference_name" class="form-control form-control-sm" value="N/A"  required="true">
                          </div>
                        </div>
                        <div class="col-md-4">
                          <div class="form-group">
                             <label><?php echo language(355);?></label>
                             <input type="number" name="reference_number" class="form-control form-control-sm text-center" value="01" required="true">
                          </div>
                       </div>
                       <div class="col-md-4">
                          <div class="form-group">
                             <label><?php echo language(356); ?></label>
                             <input type="text" name="reference_address" class="form-control form-control-sm" value="N/A"  required="true">
                          </div>
                       </div>
                       <div class="col-md-12"><hr style="margin-top:0px;margin-bottom:5px;"></div>
                    <?php } ?>
                                        
                     <div class="col-md-12 <?php if((input_check('casnarration') !==1 && $sltype=='ECSL') || (input_check('narration') !==1 && $sltype=='ERSL')){echo 'd-none';}?>">
                      <div class="form-group">
                        <label><?php echo language(150);?></label>
                        <textarea name="details" class="form-control form-control-sm " rows="2" placeholder="Enter ...">N/A</textarea>
                      </div>
                    </div>
                    
                    
                    <div class="col-md-6 <?php if(input_check('m_invoice') !==1){echo 'd-none';}?>">
                      <div class="form-group">
                        <label> <?php echo language(151);?></label>
                        <input type="text" name="m_invoice" class="form-control form-control-sm text-center inputselect">
                      </div>
                    </div>

                    <div class="col-md-6 <?php if(input_check('d_man') !==1){echo 'd-none';}?>">
                      <div class="form-group">
                        <label><?php echo language(152);?> </label>
                        <input type="text" name="d_man" class="form-control form-control-sm inputselect">
                      </div>
                    </div>
                    <div class="col-md-12"><hr style="margin-top:0px;margin-bottom:5px;"></div>
                    
                    
                    <div class="col-md-4 <?php if(input_check('wharehouse')!==1){ echo 'd-none';}?>">
                      <div class="form-group">
                        <label><?php echo language(104);?></label>
                        <select name="wh_id" class="form-control select2" style="width: 100%;" required="true">
                        <?php
                          $swh = mysqli_fetch_assoc(mysqli_query($conn,"SELECT bwid,war_location FROM business_w_house WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 AND default_set='primary' LIMIT 1"));
                          ?>
                          <option value="<?php echo $swh['bwid'];?>"><?php echo $swh['war_location'];?></option>
                          <?php
                          $bw= mysqli_query($conn,"SELECT bwid,war_location FROM business_w_house WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 ORDER BY bwid ASC") or die(mysqli_error($conn));
                          while($wh= mysqli_fetch_assoc($bw)){
                          echo '<option value='.$wh["bwid"].'>'.$wh["war_location"].'</option>';
                          }?>
                        </select>
                      </div>
                    </div>
                    
                    
                    <div class="col-md-4 <?php if(input_check('invoice_date') !==1){echo 'd-none';}?>">
                      <div class="form-group">
                        <label> <?php echo language(131);?> </label>
                        <input type="text" name="tdate" class="form-control form-control-sm text-center" onmouseover="lcdate(this)" readonly="true" required="true" value="<?php echo $date;?>">
                      </div>
                    </div>

                    <div class="col-md-2 <?php if(input_check('email') !==1){echo 'd-none';}?>">
                      <div class="form-group mt-3">
                        <div class="custom-control custom-checkbox">
                          <input name="email" class="custom-control-input" type="checkbox" id="customCheckbox1" value="yes">
                          <label for="customCheckbox1" class="custom-control-label float-right"><?php echo language(155);?></label>
                        </div>
                      </div>
                    </div>

                    <div class="col-md-2 <?php if(input_check('sms') !==1){echo 'd-none';}?>">
                      <div class="form-group mt-3">
                        <div class="custom-control custom-checkbox">
                          <input name="sms" class="custom-control-input" type="checkbox" id="customCheckboxsmscash" value="yes">
                          <label for="customCheckboxsmscash" class="custom-control-label float-right"><?php echo language(132);?></label>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-12"><hr style="margin-top:0px;margin-bottom: 5px;"></div>
                    
                    <div class="col-md-6 <?php if(input_check('f_charge')!==1){echo 'd-none';} ?>">
                      <div class="form-group">
                        <label><?php echo language(153);?></label>
                        <dt id="f_show" style="float: right;"></dt>
                        <input type="hidden" name="f_acid">
                        <select id="f_acid" onchange="balance(this.value,'#f_show')" class="form-control select2" style="width: 100%;" required="true">
                          <?php
                          $accat= mysqli_query($conn,"SELECT acid,ac_name,ac_alise,ac_number,balance FROM accounts LEFT JOIN ac_sub_g_category ON(accounts.gcatid=ac_sub_g_category.ac_g_catid) WHERE accounts.coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 AND g_type IN('dr-inc') ORDER BY acid DESC ") or die(mysqli_error($conn));
                          while($acat= mysqli_fetch_assoc($accat)){
                          echo '<option value='.$acat["acid"].','.$acat["balance"].'>'.$acat["ac_name"]."-".$acat["ac_number"].' '.'('.$acat["ac_alise"].')</option>';
                          }?>
                        </select>
                      </div>
                    </div>
                    
                    
                    
                    <div class="col-md-6 <?php if(input_check('d_account')!==1){echo 'd-none';} ?>">
                      <div class="form-group">
                        <label><?php echo language(154);?></label>
                        <dt id="d_show" style="float: right;"></dt>
                        <input type="hidden" name="d_acid">
                        <select id="d_acid" onchange="balance(this.value,'#d_show')" class="form-control select2" style="width: 100%;" required="true">
                          <?php
                          $accat= mysqli_query($conn,"SELECT acid,ac_name,ac_alise,ac_number,balance FROM accounts LEFT JOIN ac_sub_g_category ON(accounts.gcatid=ac_sub_g_category.ac_g_catid) WHERE accounts.coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 AND g_type IN('dr-exp')") or die(mysqli_error($conn));
                          while($acat= mysqli_fetch_assoc($accat)){
                          echo '<option value='.$acat["acid"].','.$acat["balance"].'>'.$acat["ac_name"]."-".$acat["ac_number"].' '.'('.$acat["ac_alise"].')</option>';
                          }?>
                        </select>
                      </div>
                    </div>
                    
                    <?php  if ($administrator == 'primary'){  ?>
                    <div class="col-md-12"> 
                    <div class="form-group">
                         <label><?php echo language(239); ?><b class="text-danger"> *</b></label> 
                        <select  name="us_em_id"  class="form-control" > 
                             <?php $emdd_query= mysqli_query($conn,"SELECT uid,us_em_id FROM user 
                             WHERE user.us_coid='$coid'  AND user.us_bid='$bid'  AND  user.us_brid='$brid'  AND user.us_status=1  ") or die (mysqli_error($conn)); 
                             
                             
                             while($rouid= mysqli_fetch_assoc($emdd_query)){ $get_emdata=$rouid["us_em_id"]; 
                             $em_acname= mysqli_fetch_assoc(mysqli_query($conn,"SELECT em_acname FROM em_employee WHERE em_coid='$coid' AND em_bid='$bid' AND em_brid='$brid' AND em_id='$get_emdata' LIMIT 1"))['em_acname'];
                             $id_card = mysqli_fetch_assoc(mysqli_query($conn,"SELECT id_card FROM em_employee WHERE em_coid='$coid' AND em_bid='$bid' AND em_brid='$brid' AND em_id='$get_emdata' LIMIT 1"))['id_card'];

                             echo '<option value='.$rouid["us_em_id"].'>'.$em_acname.'  ('.$id_card.') </option>'; }?> 
                        </select> 
                    </div>
                    </div>

                    <?php } else{  ?>  
   <input type="hidden" name="us_em_id"  value="<?php echo $us_em_id;?>"  required="true">
<?php }  ?>
                    
                  </div>
                </div>
                <div class="col-md-1"></div>
                <div class="col-md-4">
                  <input type="hidden" id="sl_totalc<?php echo $invoice;?>" name="sl_totalc">
                  <table class="ttable" cellpadding="0" cellspacing="0">
                    <tbody>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(142);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" oninput="slblcal();" name="delivary_c" step="0.001" id="delivary_c" class="form-control form-control-sm inputselect text-right" value="0" style="text-align:center;" required="true">
                        </dt></td>
                      </tr>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(88);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" oninput="slblcal();" step="0.001" name="sldiscount" id="sldiscount" class="form-control form-control-sm inputselect text-right" value="0" style="text-align:center;" required="true">
                        </dt></td>
                      </tr>
                      <?php if ($sltype=='ERSL' || $sltype=='INSL') { ?>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(143);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" id="slsub_total" step="0.001" class="form-control form-control-sm inputselect text-right" value="0" required="true" readonly="true">
                        </dt></td>
                      </tr>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(236);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" id="slpre_due" step="0.001" class="form-control form-control-sm inputselect text-right" value="<?php echo $balance;?>" required="true" readonly="true">
                        </dt></td>
                      </tr>
                      <?php } else { ?>
                      <input type="hidden" id="slsub_total" value="0">
                      <input type="hidden" id="slpre_due" value="0">
                      <?php } ?>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(145);?> </dt></td>
                        <td class="w-50"><dt>
                          <input type="number" id="slgrd_total" class="form-control form-control-sm inputselect text-right" value="0" required="true" readonly="true">
                        </dt></td>
                      </tr>
                      <tr>
                        <td class="text-right w-50"> <?php if ($sltype=='ERSL' || $sltype=='INSL') { ?>
                            <div class="custom-control custom-switch custom-switch-off-danger custom-switch-on-success">
                             <input type="checkbox" class="custom-control-input " oninput="slpay();" name="total_bil_invoice" id="total_bil_invoice" value="0"> 
                                    <label class="custom-control-label" for="total_bil_invoice"> <dt style="margin-top: 3px;"> <?php echo language(146);?> </dt>   </label>
                          </div>
<?php } else{ ?>
<dt><?php echo language(146);?></dt>

<?php } ?></td>
                        <td class="w-50"><dt>
                          <input type="number" step="0.001" oninput="slpay();inscal();" name="slpayment" id="slpayment" class="form-control form-control-sm inputselect text-right text-bold" value="0" required="true">
                        </dt></td>
                      </tr>
                      <?php if ($sltype=='ERSL' || $sltype=='INSL') { ?>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(237);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" id="slcur_due"  class="form-control form-control-sm inputselect text-right" value="0" required="true" readonly="true">
                        </dt></td>
                      </tr>
                      <?php } else { ?>
                      <input type="hidden" id="slcur_due" value="0">
                      <?php } if ($sltype=='ECSL') { ?>
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(165);?></dt></td>
                        <td class="w-50"><dt>
                          <input type="number" id="slrefund" class="form-control form-control-sm inputselect text-right" value="0" readonly="true">
                        </dt></td>
                      </tr>
                      <?php } else { ?>
                      <input type="hidden" id="slrefund" value="0">
                      <?php } ?>
                      <tr>
                        <th colspan="2">
                          <div class="form-group">
                            <label><?php echo language(161);?></label>
                            <dt id="m_show" style="float: right;"></dt>
                            <input type="hidden" name="m_acid">
                            <select id="m_acid" onchange="balance(this.value,'#m_show');yesnoCheck(this);" class="form-control select2" style="width: 100%;" required="true">
                              <?php
                              $accat= mysqli_query($conn,"SELECT acid,ac_name,ac_alise,ac_number,balance FROM accounts LEFT JOIN ac_sub_g_category ON(accounts.gcatid=ac_sub_g_category.ac_g_catid) WHERE accounts.coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 AND g_type IN('mcash','bank')") or die(mysqli_error($conn));
                              while($acat= mysqli_fetch_assoc($accat)){
                              echo '<option value='.$acat["acid"].','.$acat["balance"].'>'.$acat["ac_name"]."-".$acat["ac_number"].' '.'('.$acat["ac_alise"].')</option>';
                              }?>
                             <?php if ($sltype=='ERSL' || $sltype=='INSL') { ?>
                              <option value="Bank_check"> Cheque Payment</option>
                            <?php } ?>
                            </select>
 <div class="form-group" id="ifYes" style="display: none;">
                               <label > <i class="fa fa-credit-card" aria-hidden="true"></i>  <?php echo language(408);?> :</label>
                               <input type="text" class="form-control form-control-sm inputselect text-left inp"  name="Bank_check" placeholder=" XXX XXX XXX" />
                            </div>

<style>
  .inp {
    border:none;
    border-bottom: dotted #4d5256;
    padding: 5px 10px;
    outline: none;
 }

[placeholder]:focus::-webkit-input-placeholder {
    transition: text-indent 0.4s 0.4s ease; 
    text-indent: -100%;
    opacity: 1;
 }
</style>
      <script>
        function yesnoCheck(that) {
          if (that.value == "Bank_check") {
            document.getElementById("ifYes").style.display = "block";
            } else {
            document.getElementById("ifYes").style.display = "none";
         }
        }
      </script>

                          </div>
                        </th>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </div>
            </div>
            <div class="card-footer mt-1" align="center" style="padding: 8px 5px;">
              <button type="submit" id="save" class="btn btn-success"><?php echo language(23);?></button>
            </div>
          </form>
        </div>
      </div>
    </div>
    <div class="col-md-1"></div>
  </div>
</div>
</section>
<script>
  $("select").select2(); 
  pagecall('../sales/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice?>',0);  
  function balance (value,sid){
      var vvvv = value.split(",");
      $(sid).next().val(Number(vvvv[0]));
      var bl  = Number(vvvv[1]);
      if (bl >=0) {
        $(sid).css("color","green");
        $(sid).text(bden.format(bl) + ' Dr');
      } else {
        $(sid).css("color","red");
        $(sid).text(bden.format(-bl) + ' Cr');
      }
  }
balance ($("#f_acid").val(),"#f_show");
balance ($("#d_acid").val(),"#d_show");
balance ($("#m_acid").val(),"#m_show");
function slblcal(){
  let sl_totalc   = Number($('[name=sl_totalc]').val());
  let delivary_c  = Number($('#delivary_c').val());
  let sldiscount  = Number($('#sldiscount').val());
  let slsub_total = Number((sl_totalc + delivary_c)-sldiscount);
  let slpre_due   = Number($('#slpre_due').val());
  let slgrd_total = Number(slsub_total + slpre_due);
  <?php if ($sltype=='ECSL') { ?>
    $("#slpayment").attr("min",slgrd_total);
    $("#slpayment").val(slgrd_total);
  <?php } ?>
  $('#slsub_total').val(slsub_total);
  $('#slgrd_total').val(slgrd_total);
<?php if ($sltype=='INSL') { ?>
  inscal();
<?php } ?>

$('input[id="total_bil_invoice"]').click(function(){
  if($(this).is(":checked")){
    $("#slpayment").val(slsub_total);
    }else if($(this).is(":not(:checked)")){
   $("#slpayment").val(0);
   }
        });
} 

function slpay(){
  let slpayment   = Number($('#slpayment').val());
  let slcur_due   = Number($('#slgrd_total').val()-slpayment);
  let slrefund    = Number(slcur_due<0?-slcur_due:0);
  
  $('#slcur_due').val(slcur_due);
  $('#slrefund').val(slrefund);
}

function inscal(){
  let ins_total   = Number($('#slsub_total').val()) - Number($('#slpayment').val());
  let per_ins     = (ins_total / Number($('[name=ins_qty]').val()));
 
  $('#per_ins').val(per_ins.toFixed(0));
}
</script>


<script>
   $(document).ready(function() {
   $('#sal_form').on('submit', function(e){
   e.preventDefault(); 
   if (Number($('[name=sl_totalc]').val()) >0) {
   $('#save').prop('disabled', true);
   $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
        $.post("../sales/sales_db.php",$(this).serialize(),function(data){
        toastr.success(Successfully, yourdata);
    <?php if ($sltype=='ECSL' && $printer_t=='P') { ?>
        $("#modal-sm").modal({backdrop: "static"});
        pagecall('../sales/c_sales_invoice_pos.php?invoice=<?php echo $invoice?>','#smdview',0);
    <?php } elseif ($sltype=='ERSL' && $printer_t=='P') { ?>    
        $("#modal-sm").modal({backdrop: "static"});
        pagecall('../sales/sales_invoice_pos.php?invoice=<?php echo $invoice?>','#smdview',0);
    <?php } elseif ($sltype=='ECSL' && $printer_t=='R') { ?>  
        $("#modal-lg").modal({backdrop: "static"});
        pagecall('../sales/c_sales_invoice.php?invoice=<?php echo $invoice?>','#mdview',0);
    <?php } elseif ($sltype=='ERSL' && $printer_t=='R') { ?> 
        $("#modal-lg").modal({backdrop: "static"});
        pagecall('../sales/sales_invoice.php?invoice=<?php echo $invoice?>','#mdview',0);
    <?php } elseif ($sltype=='INSL') { ?> 
        $("#modal-lg").modal({backdrop: "static"});
        pagecall('../sales/ins_sales_invoice.php?invoice=<?php echo $invoice?>','#mdview',0);
    <?php } ?>
        pagecall('../sales/sales_index.php?c_acid=<?php echo $c_acid?>'+'&sltype=<?php echo $sltype?>','#salmview',0);
      });
      } else {
        alert('Please Select Item...??');
      }
     });
   });  
</script>

<script>
  $(function() {
    $( "#item_details" ).autocomplete({
       minLength: 2,
       source: '../sales/item.php',
autoFocus: true,
       delay:500,
   // scroll: true,
       // focus: function( event, ui ) {
       //    $( "#item_details" ).val( ui.item.label );
       //       return false;
       // },
       select: function( event, ui ) {
          $( "#item_details" ).val( ui.item.label );
          $.post("../sales/order_list_db.php",
            {item_id : ui.item.value,oinvoice:'<?php echo $invoice;?>'},
         function(data){
          toastr.success(Successfully, yourdata);
          pagecall('../sales/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice?>',0);
        });  
        $( "#item_details" ).val('');        
          return false;
       }
    });
  });
</script>

<script>
   $(document).ready(function() {
   $('#sal_sub').on('submit', function(e){
   e.preventDefault();
      let item_code = $('#item_details').val();
        $.post("../sales/order_list_db.php",{item_code : item_code,sinvoice:'<?php echo $invoice;?>'},function(data){
          toastr.success(Successfully, yourdata);
          pagecall('../sales/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice?>',0);
          $("#sal_sub")[0].reset();
          $("#item_details").val('');
          return false;
        });
      });
   });  
</script>

<script>
  $(function() {
    $( "[name=c_name],[name=c_gsm]").autocomplete({
   minLength: 0,
       source: '../sales/c_cus.php',
autoFocus: true,
   //  delay:500,
   // scroll: true,
       // focus: function( event, ui ) {
       //    $( "#item_details" ).val( ui.item.label );
       //       return false;
       // },
       select: function( event, ui ) {    
       $( "[name=c_gsm]" ).val( ui.item.value );
       $( "[name=c_name]" ).val( ui.item.name );
       $( "[name=c_address]" ).val( ui.item.addr );
          return false;
       }
    });
  });
</script>
<script>
   function draft(edd){
      Swal.fire({
        title: 'Are you sure?',
        text: "You won't Save Invoice In Draft",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#6c757d',
        cancelButtonColor: '#3085d6',
        confirmButtonText: 'Yes, Save it!'
      }).then((result) => {
        if (result.isConfirmed) {
          $.post("../sales/sales_draft.php",$('#sal_form').serialize(),function(data){
            pagecall('../sales/sales_index.php?c_acid=<?php echo $c_acid?>'+'&sltype=<?php echo $sltype?>','#salmview',0);
            toastr.success(Successfully, yourdata);
        });          
      }
   }) 
  }  
</script>