<?php require_once('../auth/auth.php');require_once('../auth/function.php');require_once('../auth/acid_balance.php');?>
<?php 
$invoice=$_GET['invoice']; 
$g_acid = (isset($_GET['g_acid']))?$_GET['g_acid']:0;

mysqli_query($conn,"DELETE FROM order_list_edit   WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice' AND active=0") or die(mysqli_error($conn));
mysqli_query($conn,"DELETE FROM imei_tracker_edit WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice' AND active=0") or die(mysqli_error($conn));

mysqli_query($conn,"INSERT INTO `order_list_edit`(`coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `item_code`, `pro_type`, `plus`, `qty`, `pro_unit`, `unit_val`, `out_qty`, `run_qty`, `pu_price`, `avg_price`, `sale_price`, `change_slprice` , `discount`, `dis_amount`, `amount`, `t_amount`, `net_amount`, `tax`, `profit`, `details`, `d_man`, `userid`, `datetime`, `active`) SELECT `coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `item_code`, `pro_type`, `plus`, (qty/unit_val), `pro_unit`, `unit_val`, `out_qty`, `run_qty`, `pu_price`, `avg_price`, `sale_price`, `change_slprice`, `discount`, `dis_amount`, `amount`, `t_amount`, `net_amount`, `tax`, `profit`, `details`, `d_man`, `userid`, `datetime`, 0 FROM `order_list` WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice' ORDER BY oid ASC ") or die(mysqli_error($conn));

mysqli_query($conn,"INSERT INTO `imei_tracker_edit`(`coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `imei_code`, `imei_color`, `mfg_date`, `exp_date`, `plus`, `pu_price`, `sale_price`, `date`, `active`) SELECT `coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `imei_code`, `imei_color`,`mfg_date`,`exp_date`, `plus`, `pu_price`, `sale_price`, `date`, 0 FROM `imei_tracker` WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice'") or die(mysqli_error($conn));


    $pinvoice_query= mysqli_query($conn,"SELECT t.tid,t.em_id,t.invoice,t.m_invoice,date(t.date) tdate,t.date date_time,t.amount,t.details,t.ttype,a.acid,a.ac_name,a.ac_number,a.balance,cs.c_name,cs.c_gsm,cs.c_address,o.d_man,o.wh_id
      FROM transaction t 
      LEFT JOIN accounts a ON (a.acid=t.dr_acid)
      LEFT JOIN c_customer cs ON(cs.c_id=t.e_acid)
      LEFT JOIN order_list o ON(t.invoice=o.invoice)
      WHERE t.coid='$coid' AND t.invoice ='$invoice' AND (ttype='ERSL' || ttype='ECSL') LIMIT 1") or die(mysqli_error($conn));
    $pin_row= mysqli_fetch_assoc($pinvoice_query);

    $cacid          = $pin_row['acid'];
     $em_id          = $pin_row['em_id'];
    $ac_name        = $pin_row['ac_name'];
    $ac_number      = $pin_row['ac_number'];
    $ac_balance     = $pin_row['balance'];

    $c_name         = $pin_row['c_name'];
    $c_gsm          = $pin_row['c_gsm'];
    $c_address      = $pin_row['c_address'];
 

    $tid            = $pin_row['tid'];
    $tdate          = $pin_row['tdate'];
    $m_invoice      = $pin_row['m_invoice'];
    $date_time      = $pin_row['date_time'];
    $details        = $pin_row['details'];
    $sltype         = $pin_row['ttype'];

    $d_man          = $pin_row['d_man'];
    $wh_id          = $pin_row['wh_id'];

    $chbll = invbalance($cacid,$tdate,$tid);
    $pr_due = ($chbll)?$chbll['pr_due']:0;
    
    $delivery  = 0;
    $discount = 0;
    $payment  = 0;




    $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;} 
    }
?>
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <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); } ?>
          </h3>
          <div class="card-tools">
            <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice;?></span>
            <div class="btn-group">
              <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%">
                    <select onchange="ebalancex(this.value,'#n_show','#b_show')" class="form-control select2" style="width: 100%;" required="true">
                      <?php
                      echo '<option value='.$cacid.','.$ac_number.','.$ac_balance.'>'.$ac_name.'</option>';
                      $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('su-deb') AND ac_type IS NULL") or die(mysqli_error($conn));
                      while($acat= mysqli_fetch_assoc($accat)){
                      echo '<option value='.$acat["acid"].','.$acat["ac_number"].','.$acat["balance"].'>'.$acat["ac_name"].'</option>';
                      }?>
                    </select>
                  </th>
                  <th class="text-center" width="12%"><?php echo language(111);?></th>
                  <th class="text-center" width="15%" id="n_show"><?php echo $ac_number;?></th>
                  <th class="text-center" width="13%"><?php echo language(137);?></th>
                  <th class="text-center" width="15%" id="b_show"><?php echo $ac_balance;?></th>
                </tr>
              </tbody>
            </table>
          <?php if(input_check('item_scan_status')==1){ ?>
            <form id="esal_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="eitem_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(input_check('item_scan_status')==1){ ?>
            </form>
          <?php } ?>
          </div>
          <form id="esal_form" action="#" method="post" autocomplete="off">
            <input type="hidden" name="ec_acid" value="<?php echo $cacid;?>">
            <input type="hidden" name="esal_invoice" value="<?php echo $invoice;?>">
            <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="ec_name" value="<?php echo $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="ec_gsm" value="<?php echo $c_gsm;?>" class="form-control form-control-sm" value="01" required="true">
                    </div>
                  </div>
                  <div class="col-md-5">
                    <div class="form-group">
                      <label><?php echo language(116);?></label>
                      <input type="text" name="ec_address" value="<?php echo $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 } ?>
                  
                  
                  <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>Narration</label>
                      <textarea name="edetails" class="form-control form-control-sm inputselect" 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>Invoice No</label>
                      <input type="text" name="em_invoice" value="<?php echo $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>Delivery Man</label>
                      <input type="text" name="ed_man" value="<?php echo $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="ewh_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 bwid='$wh_id' 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>Invoice Date</label>
                      <input type="text" name="etdate" class="form-control form-control-sm text-center" onmouseover="lcdate(this)" readonly="true" required="true" value="<?php echo $tdate;?>">
                    </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="eemail" class="custom-control-input" type="checkbox" id="customCheckbox1" value="yes">
                        <label for="customCheckbox1" class="custom-control-label float-right">E-Mail</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="">
                        <input name="esms"  type="checkbox" id="customeedit" value="yes">
                        <label for="customeedit" class="float-right">SMS</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="ef_show" style="float: right;"></dt>
                      <input type="hidden" name="ef_acid">
                      <select id="ef_acid" onchange="balance(this.value,'#ef_show')" class="form-control select2" style="width: 100%;" required="true">
                        <?php
                        $freight = 0;
                        $fr= mysqli_fetch_assoc(mysqli_query($conn,"SELECT cr_acid,amount FROM transaction WHERE coid='$coid' AND invoice ='$invoice' AND ttype='R' AND note = 'esale_delivary' LIMIT 1"));
                        if ($fr) {
                        $freight = $fr['amount'];
                        $acnd= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,ac_name,ac_alise,ac_number,balance FROM accounts WHERE coid='$coid' AND acid=".$fr['cr_acid']." LIMIT 1"));
                        echo '<option value='.$acnd["acid"].','.$acnd["balance"].'>'.$acnd["ac_name"]."-".$acnd["ac_number"].' '.'('.$acnd["ac_alise"].')</option>';
                        }
                        $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>Discount A/C</label>
                      <dt id="ed_show" style="float: right;"></dt>
                      <input type="hidden" name="ed_acid">
                      <select id="ed_acid" onchange="balance(this.value,'#ed_show')" class="form-control select2" style="width: 100%;" required="true">
                        <?php
                        $discount = 0;
                        $dis= mysqli_fetch_assoc(mysqli_query($conn,"SELECT dr_acid,amount FROM transaction WHERE coid='$coid' AND invoice ='$invoice' AND ttype='P' AND note = 'esale_discount' LIMIT 1"));
                        if ($dis) {
                        $discount = $dis['amount'];
                        $dnd= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,ac_name,ac_alise,ac_number,balance FROM accounts WHERE coid='$coid' AND acid=".$dis['dr_acid']." LIMIT 1"));
                        echo '<option value='.$dnd["acid"].','.$dnd["balance"].'>'.$dnd["ac_name"]."-".$dnd["ac_number"].' '.'('.$dnd["ac_alise"].')</option>';
                        }
                        $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>
                  
                  
                </div>
              </div>
              <div class="col-md-1"></div>
              <div class="col-md-4">
                <input type="hidden" id="sl_totalc<?php echo $invoice;?>" name="esl_totalc">
                <table class="ttable" cellpadding="0" cellspacing="0">
                  <tbody>
                    <tr>
                      <td class="text-right w-45"><dt><?php echo language(142);?></dt></td>
                      <td class="w-50"><dt>
                        <input type="number" oninput="slblcal();" name="edelivary_c" id="edelivary_c" class="form-control form-control-sm inputselect text-right" value="<?php echo $freight;?>" style="text-align:center;" required="true">
                      </dt></td>
                      <td class="text-center w-5" title="Delete This value..?" onclick="$('#edelivary_c').val(0);slblcal();"><dt><i class="fas fa-trash"></i></dt></td>
                    </tr>
                    <tr>
                      <td class="text-right w-45"><dt><?php echo language(88);?></dt></td>
                      <td class="w-50"><dt>
                        <input type="number" oninput="slblcal();" name="esldiscount" id="esldiscount" class="form-control form-control-sm inputselect text-right" value="<?php echo $discount;?>" style="text-align:center;" required="true">
                      </dt></td>
                      <td class="text-center w-5" title="Delete This value..?" onclick="$('#esldiscount').val(0);slblcal();"><dt><i class="fas fa-trash"></i></dt></td>
                    </tr>
                    <?php if ($sltype=='ERSL') { ?>
                    <tr>
                      <td class="text-right w-45"><dt><?php echo language(143);?></dt></td>
                      <td class="w-50"><dt>
                        <input type="number" id="eslsub_total" class="form-control form-control-sm inputselect text-right" value="0" required="true" readonly="true">
                      </dt></td>
                      <td class="text-center w-5"><dt></dt></td>
                    </tr>
                    <tr>
                      <td class="text-right w-45"><dt>Previous Due</dt></td>
                      <td class="w-50"><dt>
                        <input type="number" id="eslpre_due" class="form-control form-control-sm inputselect text-right" value="<?php echo $balance;?>" required="true" readonly="true">
                      </dt></td>
                      <td class="text-center w-5"><dt></dt></td>
                    </tr>
                    <?php } else { ?>
                    <input type="hidden" id="eslsub_total" value="0">
                    <input type="hidden" id="eslpre_due" value="0">
                    <?php } ?>
                    <tr>
                      <td class="text-right w-45"><dt><?php echo language(145);?> </dt></td>
                      <td class="w-50"><dt>
                        <input type="number" id="eslgrd_total" class="form-control form-control-sm inputselect text-right" value="0" required="true" readonly="true">
                      </dt></td>
                      <td class="text-center w-5"><dt></dt></td>
                    </tr>
                    <tr>
                      <td class="text-right w-45"><dt><?php echo language(146);?></dt></td>
                      <td class="w-50"><dt>
                        <?php
                        
  $sltype=mysqli_fetch_assoc(mysqli_query($conn,"SELECT order_type FROM order_list WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice' LIMIT 1"))['order_type'];

  
    if ($sltype=='ERSL'){ 
       $rec_type = 'R_R';
   } else if($sltype=='ECSL'){
       $rec_type = 'W_R';
   } else if($sltype=='INSL'){ 
       $rec_type = 'R_I';
   }
                        
                        $payment = 0;
                        $pay= mysqli_fetch_assoc(mysqli_query($conn,"SELECT dr_acid,amount FROM transaction t WHERE t.coid='$coid' AND t.invoice ='$invoice' AND t.ttype='$rec_type' AND t.cr_acid='$cacid' AND note='esale_payment' LIMIT 1"));
                        if ($pay) {
                        $payment = $pay['amount'];
                        }
                        ?>
                        <input type="number" step="0.001" oninput="slblcal();" name="eslpayment" id="eslpayment" class="form-control form-control-sm inputselect text-right" value="<?php echo $payment;?>" required="true">
                      </dt></td>
                      <td class="text-center w-5" title="Delete This value..?" onclick="$('#eslpayment').val(0);slblcal();"><dt><i class="fas fa-trash"></i></dt></td>
                    </tr>
                    <?php if ($sltype=='ERSL') { ?>
                    <tr>
                      <td class="text-right w-45"><dt>Current Due</dt></td>
                      <td class="w-50"><dt>
                        <input type="number" id="eslcur_due" class="form-control form-control-sm inputselect text-right" value="0" required="true" readonly="true">
                      </dt></td>
                      <td class="text-center w-5"><dt></dt></td>
                    </tr>
                    <?php } else { ?>
                    <input type="hidden" id="eslcur_due" value="0">
                    <?php } if ($sltype=='ECSL') { ?>
                    <tr>
                      <td class="text-right w-45"><dt><?php echo language(165);?></dt></td>
                      <td class="w-50"><dt>
                        <input type="number" id="eslrefund" class="form-control form-control-sm inputselect text-right" value="0" readonly="true">
                      </dt></td>
                      <td class="text-center w-5"><dt></dt></td>
                    </tr>
                    <?php } else { ?>
                    <input type="hidden" id="eslrefund" value="0">
                    <?php } ?>
                    <tr>
                      <th colspan="2">
                        <div class="form-group">
                          <label><?php echo language(161);?></label>
                          <dt id="em_show" style="float: right;"></dt>
                          <input type="hidden" name="em_acid">
                          <select id="em_acid" onchange="balance(this.value,'#em_show')" class="form-control select2" style="width: 100%;" required="true">
                            <?php
                            if ($pay) {
                            $paid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,ac_name,ac_alise,ac_number,balance FROM accounts WHERE coid='$coid' AND acid=".$pay['dr_acid']." LIMIT 1"));
                            echo '<option value='.$paid["acid"].','.$paid["balance"].'>'.$paid["ac_name"]."-".$paid["ac_number"].' '.'('.$paid["ac_alise"].')</option>';
                            }
                            $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>';
                            }?>
                          </select>
                        </div>
                      </th>
                    </tr>
                    
                    
                          <?php  if ($administrator == 'primary'){  ?>
                    <div class="col-md-4"> 
                    <div class="form-group">
                         <label><?php echo language(239); ?><b class="text-danger"> *</b></label> 
                        <select  name="em_idd"  class="form-control" >
                              <option value="<?php echo $em_id;?>">Change</option>
 
                        
                        
                             <?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="em_idd"  value="<?php echo $em_id;?>"  required="true">
<?php }  ?>

                    
                  </tbody>
                </table>
              </div>
            </div>
          </div>
          <div class="card-footer mt-1" align="center" style="padding: 8px 5px;">
            <button type="submit" id="esave" class="btn btn-success"><?php echo language(25);?></button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
</section>
<script>
  $("select").select2(); 
  pagecall('../sales/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);

  function ebalancex (evalue,nsid,bsid){
      var evvvv = evalue.split(",");
      $('[name=ec_acid]').val(Number(evvvv[0]));
      $(nsid).html(evvvv[1]);
      $(bsid).html(Number(evvvv[2]));
    if (Number(evvvv[0])=== Number(<?php echo $cacid;?>)){
     $('#eslpre_due').val(<?php echo $pr_due;?>);
    } else {
     $('#eslpre_due').val(Number(evvvv[2])); 
    }
      slblcal();
  }

  function ebalance (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');
      }
  }
ebalance ($("#ef_acid").val(),"#ef_show");
ebalance ($("#ed_acid").val(),"#ed_show");
ebalance ($("#em_acid").val(),"#em_show");

function slblcal(){
  let esl_totalc   = Number($('[name=esl_totalc]').val());
  let edelivary_c  = Number($('#edelivary_c').val());
  let esldiscount  = Number($('#esldiscount').val());
  let eslsub_total = Number((esl_totalc + edelivary_c)-esldiscount);
  let eslpre_due   = Number($('#eslpre_due').val());
  let eslgrd_total = Number(eslsub_total + eslpre_due);
  <?php if ($sltype=='ECSL') { ?>
    $("#eslpayment").attr("min",eslgrd_total);
  <?php } ?>
  let eslpayment   = Number($('#eslpayment').val());
  let eslcur_due   = Number(eslgrd_total-eslpayment);
  let eslrefund    = Number(eslcur_due<0?-eslcur_due:0);

  $('#eslsub_total').val(eslsub_total);
  $('#eslgrd_total').val(eslgrd_total);
  $('#eslcur_due').val(eslcur_due);
  $('#eslrefund').val(eslrefund);

}   
</script>


<script>
   $(document).ready(function() {
   $('#esal_form').on('submit', function(e){
   e.preventDefault(); 
   if (Number($('[name=esl_totalc]').val()) >0) {
   $('#esave').prop('disabled', true);
   $('#esave').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
        $.post("../sales/edit_sales_db.php",$(this).serialize(),function(data){
        toastr.success(Successfully, yourdata);
        $('.modal').modal('hide');
    <?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"}).hide();
        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 } ?>       
        if($('#ac_view').length){pagecall('../accounts/ac_profile.php?acid=<?php echo $g_acid?>','#ac_view',0);}
      });
      } else {
        alert('Please Select Item...??');
      }
     });
   });  
</script>

<script>
  $(function() {
    $( "#eitem_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 ) {
          $( "#eitem_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);
        });  
        $( "#eitem_details" ).val('');        
          return false;
       }
    });
  });
</script>

<script>
   $(document).ready(function() {
   $('#esal_sub').on('submit', function(e){
   e.preventDefault();
      let item_code = $('#eitem_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);
          $("#esal_sub")[0].reset();
          $("#eitem_details").val('');
          return false;
        });
      });
   });  
</script>

<script>
  $(function() {
    $( "[name=ec_name],[name=ec_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=ec_gsm]" ).val( ui.item.value );
       $( "[name=ec_name]" ).val( ui.item.name );
       $( "[name=ec_address]" ).val( ui.item.addr );
          return false;
       }
    });
  });
</script>