<!DOCTYPE html>
<html>

<head>
<!-- TABLES CSS CODE -->
<?php include"comman/code_css.php"; ?>
<!-- iCheck -->
  <link rel="stylesheet" href="<?php echo $theme_link; ?>plugins/iCheck/square/blue.css">
  <link rel="stylesheet" href="<?= base_url('theme/css/pos.css') ?>">
</head>

<!-- ADD THE CLASS layout-top-nav TO REMOVE THE SIDEBAR. -->
<body class="hold-transition skin-blue layout-top-nav">
  <?php $CI =& get_instance(); ?>
<div class="wrapper">
  
  
  <header class="main-header">
    <nav class="navbar navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <a href="<?php echo $base_url; ?>dashboard" class="navbar-brand" title="Go to Dashboard!"><b class="hidden-xs"><?= $this->session->userdata('store_name'); ?></b><b class="hidden-lg">POS</b></a>
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
            <i class="fa fa-bars"></i>
          </button>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse pull-left" id="navbar-collapse">
          <ul class="nav navbar-nav">
            <?php if($CI->permissions('sales_view')) { ?>
            <li class=""><a href="<?php echo $base_url; ?>sales" title="View Sales List!"><i class="fa fa-list text-yellow" ></i> <span><?= $this->lang->line('sales_list'); ?></span></a></li>
            <?php } ?>
            <?php if($CI->permissions('sales_add')) { ?>
            <li class=""><a href="<?php echo $base_url; ?>pos" title="Create New POS Invoice"><i class="fa fa-calculator text-yellow " ></i> <span><?= $this->lang->line('new_invoice'); ?></span></a></li>
            <?php } ?>
             <?php if($CI->permissions('expense_add')) { ?>
            <li class=""><a href="<?php echo $base_url; ?>expense/add" title="Add Expenses"><i class="fa  fa-money  text-yellow " ></i> <span><?= $this->lang->line('expenses'); ?></span></a></li>
            <?php } ?>
          </ul>
        </div>
        <!-- /.navbar-collapse -->
        <!-- Navbar Right Menu -->
        <div class="navbar-custom-menu">
          <ul class="nav navbar-nav">
            
            <!-- User Account Menu -->
            <li class="dropdown user user-menu">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" title="Click To View Hold Invoices">
             
              <span class=""><?= $this->lang->line('hold_list'); ?></span>
              <span class="label label-danger hold_invoice_list_count"><?=$tot_count?></span>
            </a>

            <ul class="dropdown-menu dropdown-width-lg">
              
              <!-- Menu Body -->
              <li class="user-body">
                <div class="row">
                  <div class="col-xs-12 text-center " style="max-height:300px;overflow-y: scroll;">
                    <table class="table table-bordered" width="100%">
                      <thead>
                      <tr>
                        <th>ID</th>
                        <th>Date</th>
                        <th>Ref.ID</th>
                        <th>Action</th>
                      </tr>
                      </thead>
                      <tbody id="hold_invoice_list" >
                       <?=$result?>
                      </tbody>
                    </table>
                  </div>
                </div>
                <!-- /.row -->
              <!--</li>-->
            </ul>
          </li>

            <!-- Connection status (Online/Offline) - POS has no sidebar; hidden on localhost -->
            <li id="connection-status-li-pos" style="position: relative; padding: 8px 10px;">
              <span id="connection-status-badge-pos" style="display: inline-block; width: 35px; height: 35px; border-radius: 50%; background: #28a745; color: white; text-align: center; line-height: 35px; font-weight: bold; cursor: pointer; box-shadow: 0 2px 8px rgba(0,0,0,0.2);" title="Online - Click to sync offline transactions" onclick="if(window.OfflineSyncManager) window.OfflineSyncManager.manualSync(); return false;">
                <i class="fa fa-wifi" style="font-size: 16px;"></i>
              </span>
              <span id="pending-count-pos" class="badge" style="display: none; position: absolute; top: 3px; right: 3px; background: #dc3545; min-width: 20px; height: 20px; line-height: 20px; padding: 0 5px; border: 2px solid white; border-radius: 10px; font-size: 11px; font-weight: bold;"></span>
            </li>

            <!-- Messages: style can be found in dropdown.less-->
            <li class="hidden-xs" id="fullscreen"><a title="Fullscreen On/Off"><i class="fa fa-tv text-white" ></i> </a></li>
            <li class="hidden-xs"><a href="<?php echo $base_url; ?>pos/customer_display" target="_blank" title="Open Customer Display (second screen)"><i class="fa fa-desktop text-white"></i> <span class="hidden-xs">Customer Display</span></a></li>
            <li class="text-center" id="">
            <a title="Dashboard" href="<?php echo $base_url; ?>dashboard"><i class="fa fa-dashboard text-yellow" ></i><b class="hidden-xs"><?= $this->lang->line('dashboard'); ?></b></a>
          </li>

            <!-- User Account Menu -->
            <li class="dropdown user user-menu">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
              <img src="<?php echo get_profile_picture(); ?>" class="user-image" alt="User Image">
              <span class="hidden-xs"><?php print ucfirst($this->session->userdata('inv_username')); ?></span>
            </a>

            <ul class="dropdown-menu">
              <!-- User image -->
              <li class="user-header">
                <img src="<?php echo get_profile_picture(); ?>" class="img-circle" alt="User Image">

                <p>
                 <?php print ($this->session->userdata('inv_username')); ?>
                  <small>Year <?=date("Y");?></small>
                  <small class='text-uppercase text-bold'>Role: <?=$this->session->userdata('role_name');?></small>
                </p>
              </li>
              <!-- Menu Body -->
              <!-- Menu Footer-->
              <li class="user-footer">
                <div class="pull-left">
                  <a href="<?php echo $base_url; ?>users/edit/<?= $this->session->userdata('inv_userid'); ?>" class="btn btn-default btn-flat">Profile</a>
                </div>
                <div class="pull-right">
                  <a href="<?php echo $base_url; ?>logout" class="btn btn-default btn-flat">Sign out</a>
                </div>
              </li>
            </ul>
          </li>
          </ul>
        </div>
        <!-- /.navbar-custom-menu -->
      </div>
      <!-- /.container-fluid -->
    </nav>
  </header>

  <?php $css = ($this->session->userdata('language')=='Arabic' || $this->session->userdata('language')=='Urdu') ? 'margin-right: 0 !important;': '';?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper" style="<?=$css;?>">
    <!-- Content Header (Page header) -->
   <!--  <section class="content-header">
      <h1>
        General Form Elements
        <small>Preview</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">Forms</a></li>
        <li class="active">General Elements</li>
      </ol>
    </section> -->

    <!-- **********************MODALS***************** -->
    <?php include"modals/modal_customer.php"; ?>
    <?php include"modals/modal_sales_item.php"; ?>
    <?php include"modals/modal_item.php"; ?>
    <?php include"modals/modal_item_or_service.php"; ?>
    <?php include"modals/modal_sales_batch_selection.php"; ?>
    
    <?php /*include"modals/modal_service.php";*/ ?>

    
    <!-- **********************MODALS END***************** -->
    <!-- Main content -->
    <section class="content pos-page">
      <div class="row pos-main-row">
        <!-- Mobile-only: CART (qty) | PRODUCT on one line; two separate screens -->
        <div class="pos-mobile-tab-bar visible-xs visible-sm">
          <ul class="nav nav-tabs pos-mobile-tabs-one-line" role="tablist">
            <li role="presentation" class="active"><a href="javascript:void(0)" id="pos-tab-cart" role="tab"><i class="fa fa-shopping-cart"></i> CART (<span id="pos-cart-tab-qty">0</span>)</a></li>
            <li role="presentation" class="pos-tab-sep">|</li>
            <li role="presentation"><a href="javascript:void(0)" id="pos-tab-products" role="tab"><i class="fa fa-cubes"></i> PRODUCT</a></li>
          </ul>
        </div>
        <!-- left column = Cart (on mobile: Cart Tab content only) -->
        <div class="col-md-7 pos-cart-panel pos-mobile-tab-pane pos-mobile-active" id="pos-mobile-cart">
         
          <!-- general form elements -->
          <div class="box box-primary">
            <!-- form start -->
            <form class="form-horizontal" id="pos-form" >
            <div class="box-header with-border" style="padding-bottom: 0px;">
              <div class="row" >
                <div class="col-md-12" >
               
                  
                <?php if(isset($sales_id)): ?>
                  <?php if($CI->permissions('sales_add')) { ?>
                  <div class="col-md-4 pull-right">
                    <a href='<?= $base_url;?>pos' class="btn btn-primary pull-right">New Invoice</a>
                  </div>
                  <?php } ?>
                <?php endif; ?>
                
              </div>
              </div>
               
            
            
            
          </div>
            <!-- /.box-header -->
            
              <input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
              <input type="hidden" value='0' id="hidden_rowcount" name="hidden_rowcount">
              <input type="hidden" value='' id="hidden_invoice_id" name="hidden_invoice_id">
              <input type="hidden" id="base_url" value="<?php echo $base_url; ?>">
              <input type="hidden" class="scroll_or_not" value="true">

              
              <input type="hidden" value='' id="walk_in_customer_name" value="<?=get_walk_in_customer_name();?>">
              
              <!-- **********************MODALS***************** -->
             <?php include"modals_pos_payment/modal_payments_multi.php"; ?>

             <?php include"modals/modal_terms.php"; ?>

              <!-- **********************MODALS END***************** -->
              <!-- **********************MODALS***************** -->
              <div class="modal fade" id="discount-modal" tabindex='-1'>
                <div class="modal-dialog">
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                      <h4 class="modal-title">Set Discount</h4>
                    </div>
                    <div class="modal-body">
                      
                        <div class="row">
                          <div class="col-md-6">
                            <div class="box-body">
                              <div class="form-group">
                                <label for="discount_input">Discount</label>
                                <input type="text" class="form-control" id="discount_input" name="discount_input" placeholder="" value="0">
                              </div>
                            </div>
                          </div>
                          <div class="col-md-6">
                            <div class="box-body">
                              <div class="form-group">
                                <label for="discount_type">Discount Type</label>
                                <select class="form-control" id='discount_type' name="discount_type">
                                  <option value='in_percentage'>Per%</option>
                                  <option value='in_fixed'>Fixed</option>
                                </select>
                              </div>
                            </div>
                          </div>
                        </div>
                     
                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
                      <button type="button" class="btn btn-primary discount_update">Update</button>
                    </div>
                  </div>
                  <!-- /.modal-content -->
                </div>
                <!-- /.modal-dialog -->
              </div>
              <!-- /.modal -->
              <!-- **********************MODALS END***************** -->
              <div class="box-body">                
              <!-- Store Code -->
              <?php $store_id=''; ?>
            <?php 
             /*if(store_module() && is_admin()) {$this->load->view('store/store_code',array('show_store_select_box_2'=>true,'store_id'=>$store_id)); }else{*/
                echo "<input type='hidden' name='store_id' id='store_id' value='".get_current_store_id()."'>";
              /*}*/
              ?>
            <!-- Store Code end -->


              <div class="row">
                <div class="col-md-6">
                  <div class="input-group" data-toggle="tooltip" title="Warehouse">
                    <span class="input-group-addon" ><i class="fa fa-building text-red"></i></span>
                     <select class="form-control select2" id="warehouse_id" name="warehouse_id"  style="width: 100%;"  >
                          <?= get_warehouse_select_list($warehouse_id,get_current_store_id()); ?>
                      </select>
                   
                  </div>
                  
                </div>
                <div class="col-md-4">
                  <div class="input-group" data-toggle="tooltip" title="Invoice Initial Code">
                    <span class="input-group-addon"><i class="fa fa-th-list"></i></span>
                     <input type="text" class="form-control" placeholder="Invioce Initial Code" id="init_code" name="init_code" value="<?= $init_code ?>">
                  </div>
                </div> 
                <div class="col-md-2">
                     <input type="text" class="form-control" data-toggle="tooltip" title="Invoice Count ID" placeholder="Invioce Number" id="count_id" name="count_id" value="<?= $count_id ?>">
                </div> 

              </div><!-- row end -->
              <br>

              <div class="row">
                <div class="col-md-6">
                  <div class="input-group" data-toggle="tooltip" title="Customer">
                    <span class="input-group-addon" ><i class="fa fa-user"></i></span>
                     <select class="form-control select2" id="customer_id" name="customer_id"  style="width: 100%;"  >
                      </select>
                    <span class="input-group-addon pointer" data-toggle="modal" data-target="#customer-modal" title="New Customer?"><i class="fa fa-user-plus text-primary fa-lg"></i></span>
                  </div>
                    <span class="customer_points text-success" style="display: none;"></span>
                    <lable><?= $this->lang->line('previous_due'); ?> :<label class="customer_previous_due text-red" style="font-size: 18px;"><?=store_number_format(0)?></label></lable>
                  
                  
                </div>
                <div class="col-md-6">
                  <div class="input-group" data-toggle="tooltip" title="Select Items">
                    <span class="input-group-addon" ><i class="fa fa-barcode"></i></span>
                     <input type="text" class="form-control" placeholder="Item name/Barcode/Itemcode" id="item_search">
                     <span class="input-group-addon pointer show_item_service" title="New Item?"><i class="fa fa-plus text-primary fa-lg"></i></span>
                  </div>
                </div>                
              </div><!-- row end -->
             
              <div class="row">
                <div class="col-md-12">
                  <div class="form-group">
                    <div class="col-sm-12" style="overflow-y:auto;height: 300px;border:1px solid #337ab7;" >
                      <table class="table table-condensed table-bordered  table-responsive items_table" style="">
                        <thead class="bg-gray">
                          <th width="30%"><?= $this->lang->line('item_name'); ?></th>
                          <th width="10%"><?= $this->lang->line('stock'); ?></th>
                          <th width="25%"><?= $this->lang->line('quantity'); ?></th>
                          <th width="15%"><?= $this->lang->line('price'); ?></th>
                          <th width="10%"><?= $this->lang->line('discount'); ?>(<?=$CI->currency()?>)</th>
                          <th width="10%"><?= $this->lang->line('tax'); ?></th>
                          <th width="15%"><?= $this->lang->line('subtotal'); ?></th>
                          <th width="5%"><i class="fa fa-close"></i></th>
                        </thead>
                        <tbody id="pos-form-tbody" style="font-size: 16px;font-weight: bold;overflow: scroll;">
                          <!-- body code -->
                        </tbody>        
                        <tfoot>
                          <!-- footer code -->
                        </tfoot>              
                      </table>
                    </div>
                  </div>
                </div>
              </div>

              <div class="row pos-sms-tc-row">
                 <!-- SMS + T&C on one line -->
                      <?php 
                         //Change Return
                          $send_sms_checkbox='disabled';
                          if($CI->is_sms_enabled()){
                            if(!isset($sales_id)){
                              $send_sms_checkbox='checked';  
                            }else{
                              $send_sms_checkbox='';
                            }
                          }

                    ?>
                    <div class="col-md-6 pos-sms-tc-left">
                           <div class="checkbox icheck">
                            <label>
                              <input type="checkbox" <?=$send_sms_checkbox;?> class="form-control" id="send_sms" name="send_sms" > <label for="sales_discount" class="control-label"><?= $this->lang->line('send_sms_to_customer'); ?>
                                <i class="hover-q" data-container="body" data-toggle="popover" data-placement="top" data-content="If checkbox is Disabled! You need to enable it from SMS -> SMS API <br><b>Note:<i>Walk-in Customer will not receive SMS!</i></b>" data-html="true" data-trigger="hover" data-original-title="" title="Do you wants to send SMS ?">
                                  <i class="fa fa-info-circle text-maroon text-black hover-q"></i>
                                </i>
                              </label>
                            </label>
                          </div>
                    </div>
                    <div class="col-md-6 pos-sms-tc-right text-right">
                      <label class="control-label text-blue pointer" style="margin:0;cursor:pointer;" toggle="tooltip" title="<?= $this->lang->line('edit_invoice_tc')?>" data-toggle="modal" data-target="#terms-modal">
                         <span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> T&C
                        </label>
                    </div>
                    <div class="col-md-12 hide div2 text-blue text-right">
                        <label class="control-label">
                          <?= $this->lang->line('couponApplied'); ?>: <span class="coupon_value">0.00 </span> <span class="coupon_type"></span>
                        </label>
                      </div>
                </div>
           
              </div>
              <!-- /.box-body -->

              <div class="box-footer bg-gray pos-totals-footer">
                <div class="row pos-totals-row">
                  <div class="col-md-3 col-xs-12 pos-total-item">
                          <label class="pos-total-label"><?= $this->lang->line('quantity'); ?></label>
                          <span class="text-bold tot_qty pos-total-value">0</span>
                  </div>
                  <div class="col-md-3 col-xs-12 pos-total-item">
                          <label class="pos-total-label"><?= $this->lang->line('total_amount'); ?></label>
                          <?= $CI->currency('<span style="font-size: 19px;" class="tot_amt text-bold pos-total-value"></span>');?>
                  </div>
                  <div class="col-md-3 col-xs-12 pos-total-item">
                          <label class="pos-total-label"><?= $this->lang->line('total_discount'); ?> <a class="fa fa-pencil-square-o cursor-pointer" data-toggle="modal" data-target="#discount-modal"></a></label>
                          <?= $CI->currency('<span style="font-size: 19px;" class="tot_disc text-bold pos-total-value"></span>');?>
                  </div>
                  <div class="col-md-3 col-xs-12 pos-total-item">
                          <label class="pos-total-label"><?= $this->lang->line('grand_total'); ?></label>
                          <?= $CI->currency('<span style="font-size: 19px;" class="tot_grand text-bold pos-total-value"></span>');?>
                  </div>
                </div>
                <!-- Tax Summary (collapsible) -->
                <div class="row" style="padding: 5px 15px;">
                  <div class="col-md-12">
                    <a data-toggle="collapse" href="#pos_tax_summary" class="text-info" style="font-size: 12px;">
                      <i class="fa fa-calculator"></i> Tax Summary <i class="fa fa-chevron-down pos-tax-summary-chevron"></i>
                    </a>
                    <div id="pos_tax_summary" class="collapse">
                      <div class="table-responsive">
                        <table class="table table-bordered table-condensed" style="margin-top:5px; margin-bottom:0; font-size:11px; width:100%;">
                          <thead class="bg-gray">
                            <tr>
                              <th style="text-align:center; padding:3px; width:20%;">Tax Rate</th>
                              <th style="text-align:right; padding:3px; width:27%;">Taxable</th>
                              <th style="text-align:right; padding:3px; width:26%;">Tax</th>
                              <th style="text-align:right; padding:3px; width:27%;">Total</th>
                            </tr>
                          </thead>
                          <tbody id="pos_tax_summary_body">
                            <tr><td colspan="4" class="text-center text-muted">No items</td></tr>
                          </tbody>
                          <tfoot class="bg-gray" id="pos_tax_summary_footer" style="display:none;">
                            <tr style="font-weight:bold;">
                              <td style="text-align:center; padding:3px;">Totals</td>
                              <td style="text-align:right; padding:3px;" id="pos_tax_taxable_total">0.00</td>
                              <td style="text-align:right; padding:3px;" id="pos_tax_tax_total">0.00</td>
                              <td style="text-align:right; padding:3px;" id="pos_tax_grand_total">0.00</td>
                            </tr>
                          </tfoot>
                        </table>
                      </div>
                    </div>
                  </div>
                </div>
               
               <div class="row pos-action-buttons-row">
                
                  <?php if(isset($sales_id)){ $btn_id='update';$btn_name="Cash"; ?>
                    <input type="hidden" name="sales_id" id="sales_id" value="<?php echo $sales_id;?>"/>
                  <?php } else{ $btn_id='save';$btn_name="Cash";} ?>

                  <div class="col-md-12 text-right pos-action-buttons">

                    <div class="col-sm-3 col-xs-6 pos-action-btn-wrap">
                      <button type="button" id="hold_invoice" name="" class="btn bg-maroon btn-block btn-flat btn-lg btnhold" title="Hold Invoice [Alt+H]" style="">
                      <i class="fa fa-hand-paper-o" aria-hidden="true"></i>
                       Hold
                    </button>
                    </div>
                    <div class="col-sm-3 col-xs-6 pos-action-btn-wrap">
                      <button type="button" id="" name="" class="btn btn-primary btnhold btn-block btn-flat btn-lg show_payments_modal" title="Multiple Payments [Alt+M]" style="">
                            <i class="fa fa-credit-card" aria-hidden="true"></i>
                             Multiple
                          </button>
                    </div>
                    <div class="col-sm-3 col-xs-6 pos-action-btn-wrap">
                      <button type="button" id="<?php echo "show_cash_modal";?>" name="" class="btn btnhold btn-success btn-block btn-flat btn-lg Alt_c" title="By Cash & Save [Alt+C]" style="">
                            <i class="fa fa-money" aria-hidden="true"></i>
                             <?php echo $btn_name;?>
                          </button>
                    </div>

                    <div class="col-sm-3 col-xs-6 pos-action-btn-wrap">
                      <button type="button" id="pay_all" name="" class="btn bg-purple btnhold btn-block btn-flat btn-lg Alt_a" title="By Cash & Save [Alt+A]" style="">
                            <i class="fa fa-money" aria-hidden="true"></i>
                             Pay All
                          </button>
                    </div>
                    

                          
                  </div>
                </div>
              </div>
            </form>
          </div>
          <!-- /.box -->
        </div>
        <!--/.col (left) -->
        <!-- right column = Products (on mobile: Product Tab content only) -->
        <div class="col-md-5 pos-products-panel pos-mobile-tab-pane" id="pos-mobile-products">
          <!-- Horizontal Form -->
          <div class="box box-info">
            <!-- form start -->
            
              <div class="box-body">
                
              <div class="row">

                <div class="col-md-6">
                  <div class="input-group input-group-md">
                      <select class="form-control select2" id="category_id" name="category_id"  style="width: 100%;"  >
                        <option value="">-All Categories-</option>
                        <?= get_categories_select_list();  ?>
                      </select>
                          <span class="input-group-btn">
                            <button type="button" class="btn text-blue btn-flat reset_categories" title="Reset Categories" data-toggle="tooltip" data-placement="top">
                              <i class="fa fa-undo"></i>
                            </button>
                          </span>
                    </div>
                </div>


                <div class="col-md-6">
                  <div class="input-group input-group-md">
                      <select class="form-control select2" id="brand_id" name="brand_id"  style="width: 100%;"  >
                        <option value="">-All Brands-</option>
                        <?= get_brands_select_list();  ?>
                      </select>
                          <span class="input-group-btn">
                            <button type="button" class="btn text-blue btn-flat reset_brands" title="Reset Brand" data-toggle="tooltip" data-placement="top">
                              <i class="fa fa-undo"></i>
                            </button>
                          </span>
                    </div>
                </div>                

              </div><!-- row end -->

              <br>
              <div class="row">

                <div class="col-md-12">
                  <div class="input-group input-group-md">
                   
                      <input type="text" class="form-control" data-toggle="tooltip" title="Enter Item Name" placeholder="Item Name" id="item_name" name="item_name">

                          <span class="input-group-btn">
                            <button type="button" class="btn text-blue btn-flat reset_item_name" title="Reset Item Name" data-toggle="tooltip" data-placement="top">
                              <i class="fa fa-undo"></i>
                            </button>
                          </span>
                    </div>
                </div>               

              </div><!-- row end -->


             
              <div class="row">
                <div class="col-md-12">
                  <!-- <div class="form-group"> -->
                   <!--  <div class="col-sm-12"> -->
                      <!-- <style type="text/css">
                        
                      </style> -->
                     

                            <section class="content .sec_div" >
                              <div class="row search_div" style="overflow-y: scroll;min-height: 100px;height: 550px">
                              </div>
                              <h4 class='text-danger text-center error_div' style="display: none;">No More Records Found</h4>
                            </section>
                            <div class="ajax-load text-center" style="display:none;">
                                <button type="button" class="btn btn-default btn-lrg ajax" title="Ajax Request">
                                <i class="fa fa-spin fa-refresh"></i>&nbsp; Loading More Data
                              </button>
                              </div>
                      
                         
                    <!-- </div> -->
                  <!-- </div> -->
                </div>
              </div>
           
              </div>
              <!-- /.box-body -->

              
           
          </div>
          <!-- /.box -->
          
          <!-- /.box -->
        </div>
        <!--/.col (right) -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

  <?php include"footer.php";?>
</div>
<!-- ./wrapper -->

<!-- SOUND CODE -->
<?php include"comman/code_js_sound.php"; ?>
<!-- GENERAL CODE -->
<?php include"comman/code_js.php"; ?>

<!-- iCheck -->
<script src="<?php echo $theme_link; ?>plugins/iCheck/icheck.min.js"></script>

<script src="<?php echo $theme_link; ?>js/fullscreen.js"></script>
<script src="<?php echo $theme_link; ?>js/modals.js"></script>
<script src="<?php echo $theme_link; ?>js/modals/modal_item.js"></script>
<script src="<?php echo $theme_link; ?>js/ajaxselect/customer_select_ajax.js"></script>  

<!-- DROP DOWN -->
<script src="<?php echo $theme_link; ?>dist/js/bootstrap3-typeahead.min.js"></script>  
<!-- DROP DOWN END-->

<script type="text/javascript">
  
  var warehouse_module=false;
  <?php if(warehouse_module() && warehouse_count()>1){ ?> 
    warehouse_module=true;
  <?php } ?>

  var store_module=false;
  <?php if(store_module()){ ?> 
    store_module=true;
  <?php } ?>
  <?php if(function_exists('get_base_currency')){ $bc=get_base_currency(); ?>
  window.baseCurrencySymbol = <?php echo json_encode(isset($bc->symbol) ? $bc->symbol : ''); ?>;
  <?php } ?>
  <?php
  $pos_store = function_exists('get_store_details') ? get_store_details() : null;
  $ps = $pos_store ?: (object)array('store_name'=>'','address'=>'','city'=>'','postcode'=>'','mobile'=>'','phone'=>'','email'=>'','gst_no'=>'','vat_no'=>'','store_website'=>'','store_logo'=>'');
  ?>
  window.posStoreName = <?php echo json_encode(trim((string)($ps->store_name ?? '')) ?: 'Store'); ?>;
  window.posStoreAddress = <?php echo json_encode(trim((string)($ps->address ?? ''))); ?>;
  window.posStoreCity = <?php echo json_encode(trim((string)($ps->city ?? ''))); ?>;
  window.posStorePostcode = <?php echo json_encode(trim((string)($ps->postcode ?? ''))); ?>;
  window.posStoreMobile = <?php echo json_encode(trim((string)($ps->mobile ?? ''))); ?>;
  window.posStorePhone = <?php echo json_encode(trim((string)($ps->phone ?? ''))); ?>;
  window.posStoreEmail = <?php echo json_encode(trim((string)($ps->email ?? ''))); ?>;
  window.posStoreGstNo = <?php echo json_encode(trim((string)($ps->gst_no ?? ''))); ?>;
  window.posStoreVatNo = <?php echo json_encode(trim((string)($ps->vat_no ?? ''))); ?>;
  window.posStoreWebsite = <?php echo json_encode(trim((string)($ps->store_website ?? ''))); ?>;
  window.posStoreLogo = <?php echo json_encode(trim((string)($ps->store_logo ?? ''))); ?>;
</script>
<?php $pos_js_ver = (file_exists(FCPATH.'theme/js/pos.js')) ? filemtime(FCPATH.'theme/js/pos.js') : time(); ?>
<script src="<?php echo $theme_link; ?>js/pos.js?v=<?php echo $pos_js_ver; ?>"></script>
<script>

        //Customer Selection Box Search
         function getCustomerSelectionId() {
           return '#customer_id';
         }

         $(document).ready(function () {
            var isLocal = (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1');
            if (isLocal) { $('#connection-status-li-pos').hide(); }
            $('#pos_tax_summary').removeClass('in').css('height','0');
            $('#pos_tax_summary').on('show.bs.collapse',function(){ $('.pos-tax-summary-chevron').removeClass('fa-chevron-down').addClass('fa-chevron-up'); }).on('hide.bs.collapse',function(){ $('.pos-tax-summary-chevron').removeClass('fa-chevron-up').addClass('fa-chevron-down'); });
            // Mobile: switch between Cart tab and Products tab (two separate screens)
            $('#pos-tab-cart').on('click', function(){ $('.pos-mobile-tab-bar .nav-tabs li').removeClass('active'); $(this).closest('li').addClass('active'); $('.pos-mobile-tab-pane').removeClass('pos-mobile-active'); $('#pos-mobile-cart').addClass('pos-mobile-active'); });
            $('#pos-tab-products').on('click', function(){ $('.pos-mobile-tab-bar .nav-tabs li').removeClass('active'); $(this).closest('li').addClass('active'); $('.pos-mobile-tab-pane').removeClass('pos-mobile-active'); $('#pos-mobile-products').addClass('pos-mobile-active'); });
         });
         //Customer Selection Box Search - END


    var base_url=$("#base_url").val();
    /*$("#store_id").on("change",function(){
      var store_id=$(this).val();
      $.post(base_url+"sales/get_customers_select_list",{store_id:store_id},function(result){
          $("#customer_id").html('').append(result).select2();
          <?php if(isset($sales_id) && empty($sales_id)){ ?>
            $(".items_table > tbody").empty();
          <?php } ?>
          final_total();
      });
    });*/

     $(".close_btn").on("click",function(){
       if(confirm('Are you sure you want to navigate away from this page?')){
           window.location='<?php echo $base_url; ?>dashboard';
         }
     });


  /*Warehouse*/
    $("#warehouse_id").on("change",function(){
      var warehouse_id=$(this).val();
      $(".items_table > tbody").empty();
      get_details(null,true);
      final_total();
    });
    /*Warehouse end*/

  //RIGHT SIT DIV:-> FILTER ITEM INTO THE ITEMS LIST
  function search_it(){
  
  /*var input = $("#search_it").val();
  var item_count=$(".search_div .search_item").length;
  var error_count=item_count;
  for(i=0; i<item_count; i++){
    console.log("item_count ->"+i+" =>"+$("#item_"+i).html());
    console.log($("#item_"+i).html().toUpperCase().indexOf(input.toUpperCase()));
    if($("#item_"+i).html().toUpperCase().indexOf(input.toUpperCase())>-1){
      console.log("found");
      $("#item_"+i).show();
      $("#item_parent_"+i).show();
    }
    else{
     console.log("not-found"); 
     $("#item_"+i).hide();
     $("#item_parent_"+i).hide();
     error_count--;
    }
    if(error_count==0){
      $(".error_div").show();
    }
    else{
      $(".error_div").hide();
    }
    
  }*/
  }




//LEFT SIDE: ON CLICK ITEM ADD TO INVOICE LIST
function addrow(id='',item_obj=''){


    var item_id = (item_obj=='') ? $('#div_'+id).attr('data-item-id') : item_obj.item_id;
    var has_dual_units = (item_obj=='') ? ($('#div_'+id).attr('data-has-dual-units')||'0') : (item_obj.has_dual_units||0);
    var sameItemResult = check_same_item(item_id, has_dual_units);
    if (!sameItemResult.addNewRow) return;
    var preferred_unit = sameItemResult.preferredUnit || 'main';
    var rowcount        =$("#hidden_rowcount").val();//0,1,2...
    var item_name = (item_obj=='') ? $('#div_'+id).attr('data-item-name') : item_obj.item_name; 

    var stock   =(item_obj=='') ? $('#div_'+id).attr('data-item-available-qty') : item_obj.stock;
        stock     =(parseFloat(stock)).toFixed(2);

    var tax_type   =(item_obj=='') ? $('#div_'+id).attr('data-item-tax-type') : item_obj.tax_type;  
    var tax_id   =(item_obj=='') ? $('#div_'+id).attr('data-item-tax-id') : item_obj.tax_id;  
    var tax_value   =(item_obj=='') ? $('#div_'+id).attr('data-item-tax-value') : item_obj.tax;
    
    var tax_name   =(item_obj=='') ? $('#div_'+id).attr('data-item-tax-name'):item_obj.tax_name;  
    var tax_amt   =(item_obj=='') ? $('#div_'+id).attr('data-item-tax-amt') : item_obj.item_tax_amt; 
    //var purchase_price   =(item_obj=='') ? $('#div_'+id).attr('data-purchase_price') : item_obj.purchase_price; 
    var discount_type   =(item_obj=='') ? $('#div_'+id).attr('data-discount_type') :item_obj.discount_type; 
    var discount   =(item_obj=='') ? $('#div_'+id).attr('data-discount') : item_obj.discount; 
 
    
    var service_bit   =(item_obj=='') ? $('#div_'+id).attr('data-service_bit') : item_obj.service_bit; 
    var batch_tracking_enabled = (item_obj=='') ? ($('#div_'+id).attr('data-batch-tracking-enabled') || '0') : (item_obj.batch_tracking_enabled || 0);
    var tax_name = (item_obj=='') ? ($('#div_'+id).attr('data-item-tax-name') || '') : (item_obj.tax_name || '');

    var item_cost     =(item_obj=='') ? $('#div_'+id).attr('data-item-cost') : item_obj.purchase_price;  
    var sales_price     =(item_obj=='') ? $('#div_'+id).attr('data-item-sales-price') : item_obj.sales_price ; 
    var sales_price_temp=sales_price;
        sales_price     =to_Fixed(sales_price);

    var main_unit_name = (item_obj=='') ? ($('#div_'+id).attr('data-main-unit-name')||'') : (item_obj.main_unit_name||'');
    var alternate_unit_name = (item_obj=='') ? ($('#div_'+id).attr('data-alternate-unit-name')||'') : (item_obj.alternate_unit_name||'');
    var conversion_factor = (item_obj=='') ? (parseFloat($('#div_'+id).attr('data-conversion-factor'))||0) : (parseFloat(item_obj.conversion_factor)||0);
    var sales_price_main = (item_obj=='') ? ($('#div_'+id).attr('data-sales-price-main')||sales_price) : (item_obj.sales_price_main||sales_price);
    var sales_price_alternate = (item_obj=='') ? ($('#div_'+id).attr('data-sales-price-alternate')||sales_price) : (item_obj.sales_price_alternate||sales_price);
    sales_price_main = to_Fixed(sales_price_main);
    sales_price_alternate = to_Fixed(sales_price_alternate);
    var stock_main = parseFloat(stock);
    var stock_alt = (conversion_factor > 0) ? (stock_main * conversion_factor) : stock_main;
    var stock_display = stock;
    if (has_dual_units=='1' && main_unit_name && alternate_unit_name) {
      if (preferred_unit === 'alternate') {
        sales_price = sales_price_alternate;
        sales_price_temp = sales_price_alternate;
        stock_display = stock_alt.toFixed(2)+' '+alternate_unit_name;
      } else {
        stock_display = stock_main.toFixed(2)+' '+main_unit_name+' ('+stock_alt.toFixed(2)+' '+alternate_unit_name+')';
      }
    }
    var unit_selector = '';
    if (has_dual_units=='1' && main_unit_name && alternate_unit_name) {
      var selMain = (preferred_unit === 'main') ? ' selected' : '';
      var selAlt = (preferred_unit === 'alternate') ? ' selected' : '';
      unit_selector = '<span class="pos-unit-select-wrap" style="margin-left:6px;"><select class="form-control input-sm pos-unit-type" id="unit_type_'+rowcount+'" name="unit_type_'+rowcount+'" data-row="'+rowcount+'" style="max-width:120px;display:inline-block;height:26px;padding:2px 6px;" onchange="pos_unit_type_change('+rowcount+')"><option value="main"'+selMain+'>Main ('+main_unit_name+')</option><option value="alternate"'+selAlt+'>Alt ('+alternate_unit_name+')</option></select></span>';
    }

    var quantity        ='<div class="input-group input-group-sm"><span class="input-group-btn"><button onclick="decrement_qty('+item_id+','+rowcount+')" type="button" class="btn btn-default btn-flat"><i class="fa fa-minus text-danger"></i></button></span>';
        quantity       +='<input typ="text" value="'+format_qty(1)+'" class="form-control no-padding text-center min_width" onchange="item_qty_input('+item_id+','+rowcount+')" id="item_qty_'+rowcount+'" name="item_qty_'+rowcount+'">';
        quantity       +='<span class="input-group-btn"><button onclick="increment_qty('+item_id+','+rowcount+')" type="button" class="btn btn-default btn-flat"><i class="fa fa-plus text-success"></i></button></span></div>';
    var sub_total       = parseFloat(1)*parseFloat(sales_price);
    var remove_btn      ='<a class="fa fa-fw fa-trash-o text-red" style="cursor: pointer;font-size: 20px;" onclick="removerow('+rowcount+')" title="Delete Item?"></a>';

    var batch_icon = (batch_tracking_enabled=='1' || batch_tracking_enabled==1) ? ' <i class="fa fa-tags text-info pointer" onclick="show_pos_batch_selection_modal('+item_id+','+rowcount+')" title="Select Batch" style="margin-left: 5px;"></i>' : '';
    var str=' <tr id="row_'+rowcount+'" data-row="0" data-item-id="'+item_id+'" data-stock-main="'+stock_main+'" data-conversion-factor="'+conversion_factor+'" data-sales-price-main="'+sales_price_main+'" data-sales-price-alternate="'+sales_price_alternate+'" data-main-unit-name="'+(main_unit_name||'').replace(/"/g,'&quot;')+'" data-alternate-unit-name="'+(alternate_unit_name||'').replace(/"/g,'&quot;')+'">';/*item id*/
        str+='<td id="td_'+rowcount+'_0"><a data-toggle="tooltip" title="Click to Change Tax" class="pointer" id="td_data_'+rowcount+'_0" onclick="show_sales_item_modal('+rowcount+')">'+ item_name     +'</a> <i onclick="show_sales_item_modal('+rowcount+')" class="fa fa-edit pointer"></i>'+batch_icon+unit_selector+'</td>';/* td_0_0 item name*/ 
        str+='<td id="td_'+rowcount+'_1">'+ stock_display +'</td>';/* td_0_1 item available qty*/
        str+='<td id="td_'+rowcount+'_2">'+ quantity      +'</td>';/* td_0_2 item available qty*/
            info='<input id="sales_price_'+rowcount+'" onblur="set_to_original('+rowcount+','+item_cost+')" onkeyup="update_price('+rowcount+','+item_cost+')" name="sales_price_'+rowcount+'" type="text" class="form-control no-padding min_width" value="'+sales_price+'">';
        str+='<td id="td_'+rowcount+'_3" class="text-right">'+ info   +'</td>';/* td_0_3 item sales price*/

        /*Discount*/
         info='<input data-toggle="tooltip" title="Click to Change" onclick="show_sales_item_modal('+rowcount+')" id="item_discount_'+rowcount+'" readonly name="item_discount_'+rowcount+'" type="text" class="form-control no-padding min_width pointer" value="0">';
         
        str+='<td id="td_'+rowcount+'_6" class="text-right">'+ info   +'</td>';

        /*Tax amt*/
        str+='<td id="td_'+rowcount+'_11"><input data-toggle="tooltip" title="Click to Change" id="td_data_'+rowcount+'_11" onclick="show_sales_item_modal('+rowcount+')" name="td_data_'+rowcount+'_11" type="text" class="form-control no-padding pointer min_width" readonly value="'+tax_amt+'"></td>';

        str+='<td id="td_'+rowcount+'_4" class="text-right"><input data-toggle="tooltip" title="Total" id="td_data_'+rowcount+'_4" name="td_data_'+rowcount+'_4" type="text" class="form-control no-padding pointer" readonly value="'+to_Fixed(sub_total)+'"></td>';/* td_0_4 item sub_total */
        str+='<td id="td_'+rowcount+'_5">'+ remove_btn    +'</td>';/* td_0_5 item gst_amt */

        str+='<input type="hidden" name="tr_item_id_'+rowcount+'" id="tr_item_id_'+rowcount+'" value="'+item_id+'">';
       // str+='<input type="hidden" id="tr_item_per_'+rowcount+'" name="tr_item_per_'+rowcount+'" value="'+gst_per+'">';
        str+='<input type="hidden" id="tr_sales_price_temp_'+rowcount+'" name="tr_sales_price_temp_'+rowcount+'" value="'+sales_price_temp+'">';
        str+='<input type="hidden" id="tr_tax_type_'+rowcount+'" name="tr_tax_type_'+rowcount+'" value="'+tax_type+'">';
        str+='<input type="hidden" id="tr_tax_id_'+rowcount+'" name="tr_tax_id_'+rowcount+'" value="'+tax_id+'">';
        str+='<input type="hidden" id="tr_tax_value_'+rowcount+'" name="tr_tax_value_'+rowcount+'" value="'+tax_value+'">';
        str+='<input type="hidden" id="description_'+rowcount+'" name="description_'+rowcount+'" value="">';
        str+='<input type="hidden" id="service_bit_'+rowcount+'" name="service_bit_'+rowcount+'" value="'+service_bit+'">';
        str+='<input id="item_discount_type_'+rowcount+'" name="item_discount_type_'+rowcount+'" type="hidden" value="'+discount_type+'">';
        str+='<input id="item_discount_input_'+rowcount+'" name="item_discount_input_'+rowcount+'" type="hidden" value="'+discount+'">';
        str+='<input type="hidden" id="tr_tax_name_'+rowcount+'" name="tr_tax_name_'+rowcount+'" value="'+(tax_name||'').replace(/"/g,'&quot;')+'">';
        str+='<input type="hidden" id="batch_tracking_enabled_'+rowcount+'" name="batch_tracking_enabled_'+rowcount+'" value="'+(batch_tracking_enabled==1||batch_tracking_enabled=='1'?'1':'0')+'">';
        str+='<input type="hidden" id="batch_number_'+rowcount+'" name="batch_number_'+rowcount+'" value="">';
        str+='<input type="hidden" id="manufacturing_date_'+rowcount+'" name="manufacturing_date_'+rowcount+'" value="">';
        str+='<input type="hidden" id="expiry_date_'+rowcount+'" name="expiry_date_'+rowcount+'" value="">';
        if (has_dual_units!='1') {
          str+='<input type="hidden" id="unit_type_'+rowcount+'" name="unit_type_'+rowcount+'" value="main">';
        }
        str+='<input type="hidden" id="sales_price_main_'+rowcount+'" name="sales_price_main_'+rowcount+'" value="'+sales_price_main+'">';
        str+='<input type="hidden" id="sales_price_alternate_'+rowcount+'" name="sales_price_alternate_'+rowcount+'" value="'+sales_price_alternate+'">';
        str+='<input type="hidden" id="conversion_factor_'+rowcount+'" name="conversion_factor_'+rowcount+'" value="'+conversion_factor+'">';
        str+='</tr>';   

    //LEFT SIDE: ADD OR APPEND TO SALES INVOICE TERMINAL
    $('#pos-form-tbody').append(str);

    //LEFT SIDE: INCREMANT ROW COUNT
    $("#hidden_rowcount").val(parseFloat($("#hidden_rowcount").val())+1);
    failed.currentTime = 0;
    failed.play();
    //CALCULATE FINAL TOTAL AND OTHER OPERATIONS
    make_subtotal(item_id,rowcount);
  }

function update_price(row_id,item_cost){
  /*Input*/
  /*var sales_price=$("#sales_price_"+row_id).val();
  if(sales_price!='' || sales_price==0) {sales_price = parseFloat(sales_price); }

  Default set from item master
  var item_price=parseFloat($("#tr_sales_price_temp_"+row_id).val());

  if(sales_price<item_cost){
    //toastr["warning"]("Minimum Sales Price is "+item_cost);
    $("#sales_price_"+row_id).parent().addClass('has-error');
  }else{
    $("#sales_price_"+row_id).parent().removeClass('has-error');
  }*/

  make_subtotal($("#tr_item_id_"+row_id).val(),row_id);
}

function set_to_original(row_id,item_cost) {
  return true;
  /*Input*/
  var sales_price=$("#sales_price_"+row_id).val();
  if(sales_price!='' || sales_price==0) {sales_price = parseFloat(sales_price); }

  /*Default set from item master*/
  var item_price=parseFloat($("#tr_sales_price_temp_"+row_id).val());

  if(sales_price<item_cost){
    toastr["success"]("Default Price Set "+item_price);
    $("#sales_price_"+row_id).parent().removeClass('has-error');
    $("#sales_price_"+row_id).val(item_price);
  }
  make_subtotal($("#tr_item_id_"+row_id).val(),row_id);
}


//INCREMENT ITEM (qty by rowcount so multiple rows of same item work)
function increment_qty(item_id,rowcount){
  var service_bit=$("#service_bit_"+rowcount).val();
  var item_qty=parseFormattedNumber($("#item_qty_"+rowcount).val());
  var unitType = ($('#unit_type_'+rowcount).length ? $('#unit_type_'+rowcount).val() : 'main');
  var row = $('#row_'+rowcount);
  var stockMain = parseFloat(row.attr('data-stock-main')) || 0;
  var batchAvailMain = parseFloat(row.attr('data-batch-available-main')) || 0;
  if (batchAvailMain > 0) stockMain = Math.min(stockMain, batchAvailMain);
  var conv = parseFloat(row.attr('data-conversion-factor')) || 0;
  var stock = (unitType==='alternate' && conv > 0) ? (stockMain * conv) : stockMain;
  if(service_bit==1 || item_qty<stock){
    item_qty=item_qty+1;
    $("#item_qty_"+rowcount).val(format_qty(item_qty));
  }
  make_subtotal(item_id,rowcount);
}
//DECREMENT ITEM
function decrement_qty(item_id,rowcount){
  var item_qty=parseFormattedNumber($("#item_qty_"+rowcount).val());
  if(item_qty<=1){
    $("#item_qty_"+rowcount).val(format_qty(1));
    return;
  }
  $("#item_qty_"+rowcount).val(format_qty(item_qty-1));
  make_subtotal(item_id,rowcount);
}
// POS: when unit type (main/alt) changes, update price and stock display
function pos_unit_type_change(rowcount){
  var row = $('#row_'+rowcount);
  var unitType = $('#unit_type_'+rowcount).length ? $('#unit_type_'+rowcount).val() : 'main';
  var stockMain = parseFloat(row.attr('data-stock-main')) || 0;
  var conv = parseFloat(row.attr('data-conversion-factor')) || 0;
  var priceMain = parseFormattedNumber(row.attr('data-sales-price-main'));
  var priceAlt = parseFormattedNumber(row.attr('data-sales-price-alternate'));
  var mainName = row.attr('data-main-unit-name') || '';
  var altName = row.attr('data-alternate-unit-name') || '';
  if (unitType==='alternate' && conv > 0) {
    $('#sales_price_'+rowcount).val(to_Fixed(priceAlt));
    $('#tr_sales_price_temp_'+rowcount).val(priceAlt);
    var stockAlt = stockMain * conv;
    $('#td_'+rowcount+'_1').html(stockAlt.toFixed(2)+' '+altName);
  } else {
    $('#sales_price_'+rowcount).val(to_Fixed(priceMain));
    $('#tr_sales_price_temp_'+rowcount).val(priceMain);
    if (mainName && altName && conv > 0)
      $('#td_'+rowcount+'_1').html(stockMain.toFixed(2)+' '+mainName+' ('+(stockMain*conv).toFixed(2)+' '+altName+')');
    else
      $('#td_'+rowcount+'_1').html(stockMain.toFixed(2)+(mainName ? ' '+mainName : ''));
  }
  make_subtotal($('#tr_item_id_'+rowcount).val(), rowcount);
}

//LEFT SIDE: IF ITEM QTY CHANGED MANUALLY
function item_qty_input(item_id,rowcount){
  var item_qty=parseFormattedNumber($("#item_qty_"+rowcount).val());
  var service_bit=$("#service_bit_"+rowcount).val();
  var unitType = ($('#unit_type_'+rowcount).length ? $('#unit_type_'+rowcount).val() : 'main');
  var row = $('#row_'+rowcount);
  var stockMain = parseFloat(row.attr('data-stock-main')) || 0;
  var batchAvailMain = parseFloat(row.attr('data-batch-available-main')) || 0;
  if (batchAvailMain > 0) stockMain = Math.min(stockMain, batchAvailMain);
  var conv = parseFloat(row.attr('data-conversion-factor')) || 0;
  var stock = (unitType==='alternate' && conv > 0) ? (stockMain * conv) : stockMain;

  if(service_bit!=1){
    if(stock==0){ toastr["warning"]("item Not Available in stock!"); }
    if(item_qty>stock){
      $("#item_qty_"+rowcount).val(format_qty(stock));
      toastr["warning"]("Oops! You have only "+stock+" items in Stock");
    }
    if(item_qty<=0){
      $("#item_qty_"+rowcount).val(format_qty(1));
      toastr["warning"]("You must have at least one Quantity");
    }
  }
  make_subtotal(item_id,rowcount);
}

function zero_stock(){
  toastr["error"]("Out of Stock!");
  return;
}
//LEFT SIDE: REMOVE ROW 
function removerow(id){//id=Rowid  
    $("#row_"+id).remove();
    failed.currentTime = 0;
    failed.play();
    final_total();
}

// Parse formatted numbers (strip commas etc.)
function parseFormattedNumber(val) {
  if (val === undefined || val === null) return 0;
  if (typeof val === 'string') val = val.replace(/,/g, '').replace(/[^0-9.\-]/g, '');
  var num = parseFloat(val);
  return isNaN(num) ? 0 : num;
}

//MAKE SUBTOTAL (correct Inclusive/Exclusive; use rowcount for qty)
function make_subtotal(item_id,rowcount){
  set_tax_value(rowcount);

  var tax_type = $("#tr_tax_type_"+rowcount).val() || 'Exclusive';
  var tax_amount = parseFormattedNumber($("#td_data_"+rowcount+"_11").val());
  var sales_price = parseFormattedNumber($("#sales_price_"+rowcount).val());
  var item_qty = parseFormattedNumber($("#item_qty_"+rowcount).val());
  var discount_amt = parseFormattedNumber($("#item_discount_"+rowcount).val());

  var tot_sales_price = item_qty * sales_price;
  var subtotal = tot_sales_price;
  if(tax_type!=='Inclusive') subtotal += tax_amount;
  subtotal -= discount_amt;
  if(subtotal<0) subtotal=0;

  $("#td_data_"+rowcount+"_4").val(to_Fixed(subtotal));
  final_total();
}


function calulate_discount(discount_input,discount_type,total){
  if(discount_type=='in_percentage'){
    return parseFloat((total*discount_input)/100);
  }
  else{//in_fixed
    return parseFloat(discount_input);
  }
}
//LEFT SIDE: FINAL TOTAL (correct parsing; no integer rounding)
function final_total(){
  var total=0;
  var item_qty=0;
  var rowcount=parseInt($("#hidden_rowcount").val(),10)||0;
  var discount_input=$("#discount_input").val();
  var discount_type=$("#discount_type").val();

  for(var i=0;i<rowcount;i++){
    if(document.getElementById('tr_item_id_'+i)){
      total += parseFormattedNumber($("#td_data_"+i+"_4").val());
      item_qty += parseFormattedNumber($("#item_qty_"+i).val());
    }
  }

  var discount_amt=0;
  if(total>0) discount_amt=calulate_discount(parseFormattedNumber(discount_input),discount_type,total);
  var subtotal = total - discount_amt;
  var coupon_amt = discount_coupon_tot(subtotal);
  subtotal -= coupon_amt;
  if(subtotal<0) subtotal=0;

  set_total(item_qty,total,discount_amt,subtotal);
  calculatePosTaxSummary();
  if(typeof adjust_payments === 'function') adjust_payments();
  if(typeof sync_customer_display_cart === 'function') sync_customer_display_cart();
}
function set_total(tot_qty=0, tot_amt=0, tot_disc=0, tot_grand=0){
  tot_qty=parseFormattedNumber(tot_qty); tot_amt=parseFormattedNumber(tot_amt);
  tot_disc=parseFormattedNumber(tot_disc); tot_grand=parseFormattedNumber(tot_grand);
  $(".tot_qty").html(format_qty(tot_qty));
  $(".tot_amt").html(to_Fixed(tot_amt));
  $(".tot_disc").html(to_Fixed(tot_disc));
  $(".tot_grand").html(to_Fixed(tot_grand));
  $("#pos-cart-tab-qty").text(typeof format_qty === 'function' ? format_qty(tot_qty) : (tot_qty || '0'));
}

function calculatePosTaxSummary(){
  var rowcount=parseInt($("#hidden_rowcount").val(),10)||0;
  var taxSummary={};
  for(var i=0;i<rowcount;i++){
    if(!document.getElementById('tr_item_id_'+i)) continue;
    var taxRate=parseFormattedNumber($("#tr_tax_value_"+i).val());
    var taxType=$("#tr_tax_type_"+i).val()||'Exclusive';
    var taxName=$("#tr_tax_name_"+i).val()||(taxRate+'%');
    var totalCost=parseFormattedNumber($("#td_data_"+i+"_4").val());
    var taxAmt=parseFormattedNumber($("#td_data_"+i+"_11").val());
    var taxableAmt=totalCost-taxAmt;
    var key=taxRate.toString();
    if(taxRate===0) key=(taxName&&taxName.toLowerCase().indexOf('exempt')>=0)?'exempt':'0';
    if(!taxSummary[key]) taxSummary[key]={rate:taxRate,name:taxName||(taxRate+'%'),taxable:0,tax:0,total:0};
    taxSummary[key].taxable+=taxableAmt;
    taxSummary[key].tax+=taxAmt;
    taxSummary[key].total+=totalCost;
  }
  var html='', totalTaxable=0, totalTax=0, totalAmount=0, hasItems=false;
  var sortedKeys=Object.keys(taxSummary).sort(function(a,b){ return parseFloat(b)-parseFloat(a); });
  var currencySymbol=(typeof window.baseCurrencySymbol!=='undefined')?window.baseCurrencySymbol:'';
  for(var j=0;j<sortedKeys.length;j++){
    var key=sortedKeys[j], item=taxSummary[key];
    hasItems=true;
    var rateLabel=(key==='exempt')?'Exempt':(item.rate===0?'0%':(item.name.indexOf('%')===-1?item.name+' ('+item.rate+'%)':item.name));
    html+='<tr><td style="text-align:left;padding:5px 8px;">'+rateLabel+'</td>';
    html+='<td style="text-align:right;padding:5px 8px;">'+currencySymbol+to_Fixed(item.taxable)+'</td>';
    html+='<td style="text-align:right;padding:5px 8px;">'+currencySymbol+to_Fixed(item.tax)+'</td>';
    html+='<td style="text-align:right;padding:5px 8px;font-weight:bold;">'+currencySymbol+to_Fixed(item.total)+'</td></tr>';
    totalTaxable+=item.taxable; totalTax+=item.tax; totalAmount+=item.total;
  }
  if(!hasItems) html='<tr><td colspan="4" class="text-center text-muted">No items</td></tr>';
  else {
    html+='<tr style="border-top:2px solid #333;background:#f5f5f5;"><td style="padding:8px;font-weight:bold;">Total</td>';
    html+='<td style="text-align:right;padding:8px;font-weight:bold;">'+currencySymbol+to_Fixed(totalTaxable)+'</td>';
    html+='<td style="text-align:right;padding:8px;font-weight:bold;">'+currencySymbol+to_Fixed(totalTax)+'</td>';
    html+='<td style="text-align:right;padding:8px;font-weight:bold;">'+currencySymbol+to_Fixed(totalAmount)+'</td></tr>';
    $("#pos_tax_summary_footer").show();
    $("#pos_tax_taxable_total").html(currencySymbol+to_Fixed(totalTaxable));
    $("#pos_tax_tax_total").html(currencySymbol+to_Fixed(totalTax));
    $("#pos_tax_grand_total").html(currencySymbol+to_Fixed(totalAmount));
  }
  $("#pos_tax_summary_body").html(html);
}

//LEFT SIDE: FINAL TOTAL - payment modal totals (correct parsing; display with to_Fixed, no integer round_off)
function adjust_payments(){
  var total=0, item_qty=0;
  var rowcount=parseInt($("#hidden_rowcount").val(),10)||0;
  var discount_input=$("#discount_input").val();
  var discount_type=$("#discount_type").val();

  for(var i=0;i<rowcount;i++){
    if(document.getElementById('tr_item_id_'+i)){
      total += parseFormattedNumber($("#td_data_"+i+"_4").val());
      item_qty += parseFormattedNumber($("#item_qty_"+i).val());
    }
  }

  var payments_row=parseInt(get_id_value("payment_row_count"),10)||0;
  var paid_amount=0;
  for(var i=1;i<=payments_row;i++){
    if(document.getElementById("amount_"+i))
      paid_amount += parseFormattedNumber(get_id_value("amount_"+i));
  }

  var discount_amt=calulate_discount(parseFormattedNumber(discount_input),discount_type,total);
  var subtotal=total-discount_amt;
  var coupon_amt=discount_coupon_tot(subtotal);
  subtotal -= coupon_amt;
  if(subtotal<0) subtotal=0;

  var balance=subtotal-paid_amount;
  var change_return=0;
  if(balance<0){ change_return=Math.abs(balance); balance=0; }

  $(".sales_div_tot_qty").html(format_qty(item_qty));
  $(".sales_div_tot_amt").html(to_Fixed(total));
  $(".sales_div_tot_discount").html(to_Fixed(discount_amt));
  $(".coupon_discount_div_amt").html(to_Fixed(coupon_amt));
  $("#coupon_discount_amt").val(coupon_amt);
  $(".sales_div_tot_payble").html(to_Fixed(subtotal));
  $(".sales_div_tot_paid").html(to_Fixed(paid_amount));
  $(".sales_div_tot_balance").html(to_Fixed(balance));
  $(".sales_div_change_return").html(to_Fixed(change_return));
  if (typeof sync_customer_display_cart === 'function') sync_customer_display_cart();
}

// Sync cart to server for Customer Display (second screen)
function sync_customer_display_cart(){
  var base_url = $("#base_url").val();
  var items = [];
  $('#pos-form-tbody tr[id^="row_"]').each(function() {
    var id = this.id;
    var i = id.replace('row_', '');
    if (i === '' || isNaN(parseInt(i, 10))) return;
    i = parseInt(i, 10);
    var nameEl = $("#td_data_"+i+"_0");
    var name = (nameEl.length ? nameEl.text().trim() : '').replace(/\s+/g, ' ');
    var qty = parseFormattedNumber($("#item_qty_"+i).val()) || 0;
    var price = parseFormattedNumber($("#sales_price_"+i).val()) || 0;
    var subtotal = parseFormattedNumber($("#td_data_"+i+"_4").val()) || 0;
    items.push({ name: name, qty: qty, price: typeof to_Fixed === 'function' ? to_Fixed(price) : price, subtotal: typeof to_Fixed === 'function' ? to_Fixed(subtotal) : subtotal });
  });
  var footer = $('.pos-totals-footer');
  var tot_qty = parseFormattedNumber(footer.find('.tot_qty').text()) || 0;
  var tot_amt = parseFormattedNumber(footer.find('.tot_amt').text()) || 0;
  var tot_disc = parseFormattedNumber(footer.find('.tot_disc').text()) || 0;
  var tot_grand = parseFormattedNumber(footer.find('.tot_grand').text()) || 0;
  var customer_name = $("#customer_id option:selected").text() || '';
  var paid_amount = 0;
  var payment_methods = [];
  var payments_row = parseInt($("#payment_row_count").val(), 10) || 0;
  for (var p = 1; p <= payments_row; p++) {
    if (document.getElementById("amount_"+p)) {
      var amt = parseFormattedNumber($("#amount_"+p).val()) || 0;
      if (amt > 0) {
        paid_amount += amt;
        var ptype = ($("#payment_type_"+p).length ? $("#payment_type_"+p).val() : '') || 'Payment';
        payment_methods.push({ type: ptype, amount: amt });
      }
    }
  }
  var balance_due = tot_grand - paid_amount;
  var change_return = 0;
  if (balance_due < 0) { change_return = Math.abs(balance_due); balance_due = 0; }
  var cart = { items: items, tot_qty: tot_qty, tot_amt: tot_amt, tot_disc: tot_disc, tot_grand: tot_grand, customer_name: customer_name, paid_amount: paid_amount, payment_methods: payment_methods, balance_due: balance_due, change_return: change_return };
  $.post(base_url + 'pos/sync_display_cart', { cart: JSON.stringify(cart), <?php echo $this->security->get_csrf_token_name();?>: '<?php echo $this->security->get_csrf_hash();?>' });
}

function load_hold_invoice_list(){
  var base_url = $("#base_url").val();
  $.getJSON(base_url + 'pos/hold_invoice_list').done(function(data){
    if (data && data.result !== undefined) {
      $('#hold_invoice_list').html(data.result);
      if (data.tot_count !== undefined) $('.hold_invoice_list_count').text(data.tot_count);
    }
  });
}
$(document).ready(function(){
  get_coupon_details();
  load_hold_invoice_list();
  if (typeof sync_customer_display_cart === 'function') sync_customer_display_cart();
  $('#multiple-payments-modal').on('show.bs.modal', function() {
    if (typeof sync_customer_display_cart === 'function') sync_customer_display_cart();
  });
  $('#multiple-payments-modal').on('hidden.bs.modal', function() {
    if (typeof sync_customer_display_cart === 'function') sync_customer_display_cart();
  });
});
// Returns { addNewRow: true to add new row, preferredUnit: 'main'|'alternate' for dual-unit items }
function check_same_item(item_id, has_dual_units){
  has_dual_units = (has_dual_units==='1' || has_dual_units===1);
  var rowcount = parseInt($("#hidden_rowcount").val(), 10) || 0;
  var lastUnit = null;
  for (var i = 0; i < rowcount; i++) {
    if (!document.getElementById('tr_item_id_'+i)) continue;
    if ($("#tr_item_id_"+i).val() == item_id) {
      var ut = ($('#unit_type_'+i).length ? $('#unit_type_'+i).val() : 'main');
      lastUnit = ut;
      if (!has_dual_units) {
        increment_qty(item_id, i);
        failed.currentTime = 0;
        failed.play();
        return { addNewRow: false, preferredUnit: 'main' };
      }
    }
  }
  if (has_dual_units && lastUnit !== null)
    return { addNewRow: true, preferredUnit: lastUnit === 'main' ? 'alternate' : 'main' };
  return { addNewRow: true, preferredUnit: 'main' };
}

$(document).ready(function(){
  
  $("#store_id").trigger('change');

  var customer_id = "<?= addslashes((string)(isset($customer_id)&&$customer_id!=='' ? $customer_id : '')); ?>";

  autoLoadFirstCustomer(customer_id);

  //FIRST TIME: LOAD
  //get_details();

  var first_div= parseFloat($(".content-wrapper").height());
  var second_div= parseFloat($("section").height());
  var items_table= parseFloat($(".items_table").height());
  $(".items_table").parent().css("height",(first_div-second_div)+items_table+230);/**/
  $(".search_div").height(((parseFloat(second_div)-parseFloat(items_table))>500) ? 500 : (second_div-items_table) );/**/
  $(".sec_div").height(((parseFloat(second_div)-parseFloat(items_table))>500) ? (parseFloat(second_div)-parseFloat(items_table)) : (second_div-items_table) );/**/

  



  //FIRST TIME: SET TOTAL ZERO
  set_total();

  //RIGHT DIV: FILTER INPUT BOX
 /* $("#search_it").on("keyup",function(){
    search_it();
  });*/

  //CATEGORY WISE ITEM FETCH FROM SERVER
  var show_only_searched=true;
  $("#category_id,#brand_id").on("change",function () {
      get_details(null,show_only_searched);
  });

  //DISCOUNT UPDATE
  $(".discount_update").on("click",function () {
      final_total();
      $('#discount-modal').modal('toggle');    
  });

  $("#item_name").on("keyup",function () {
      get_details(null,show_only_searched);
  });

  //RIGHT SIDE: CLEAR SEARCH BOX
 /* $(".show_all").on("click",function(){
    $("#search_it").val('').trigger("keyup");
    $("#category_id").val('').trigger("change");
  });*/

  //Reset Category & brand
  $(".reset_categories").on("click",function(){
      $("#category_id").val('').trigger("change");
  });
  $(".reset_brands").on("click",function(){
      $("#brand_id").val('').trigger("change");
  });
  $(".reset_item_name").on("click",function(){
      $("#item_name").val('');
      $("#brand_id").val('').trigger("change");
  });
  
  //UPDATE PROCESS START
 <?php if(isset($sales_id) && !empty($sales_id)){ ?>
    $("#store_id").attr('readonly',true);
    $(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
    $.get("<?php echo $base_url ?>pos/fetch_sales/<?php echo $sales_id ?>",{},function(result){
      console.log(result);

      result=result.split("<<<###>>>");
      $('#pos-form-tbody').append(result[0]);
      $('#discount_input').val(result[1]);
      $('#discount_type').val(result[2]);

      $('#invoice_terms').text(result[7]);
      /*if(store_module){
        $('#store_id').val(result[4]).select2();
      }
      else{*/
        $('#store_id').val(result[4]);
      /*}*/
      console.log("warehouse = "+result[5]);
      if(warehouse_module){
        $('#warehouse_id').val(result[5]).select2();
      }
      else{
        $('#warehouse_id').val(result[5]);
      }

      //$('#customer_id').val(result[3]).select2();
      //$("#customer_id").trigger("change");
      
      
      $("#hidden_rowcount").val(parseInt($(".items_table tr").length)-1);
      final_total();
      get_details();
      $(".overlay").remove();
      
      if(result[5]==1){
        $( "#binvoice" ).prop( "checked", true );
        $('#binvoice').parent('div').addClass('checked');
      }
    });
      //DISABLE THE HOLD BUTTON
      $("#hold_invoice,#show_cash_modal,#pay_all").attr('disabled',true).removeAttr('id');

 <?php } else{?>
  get_details();
 <?php } ?>
  //UPDATE PROCESS END


});//ready() end



//DATEPICKER INITIALIZATION
$('#order_date,#delivery_date,#cheque_date').datepicker({
      autoclose: true,
      format: 'dd-mm-yyyy',
      todayHighlight: true
    });
    $('#customer_dob,#birthday_person_dob').datepicker({
      calendarWeeks: true,
      todayHighlight: true,
      autoclose: true,
      format: 'dd-mm-yyyy',
      startView: 2
    });
    
    //Datemask dd-mm-yyyy
    //$("#customer_dob,#birthday_person_dob").inputmask("dd-mm-yyyy", {"placeholder": "dd-mm-yyyy"});

    //Timepicker
    /*$('.timepicker').timepicker({
      showInputs: false,
    });*/

    //Sale Items Modal Operations Start
    function show_sales_item_modal(row_id){
      $('#sales_item').modal('toggle');
      //$("#popup_tax_id").select2();

      //Find the item details
      var item_name = $("#td_data_"+row_id+"_0").html();
      var tax_type = $("#tr_tax_type_"+row_id).val();
      var tax_id = $("#tr_tax_id_"+row_id).val();
      var description = $("#description_"+row_id).val();

      /*Discount*/
      var item_discount_input = $("#item_discount_input_"+row_id).val();
      var item_discount_type = $("#item_discount_type_"+row_id).val();

      //Set to Popup
      $("#item_discount_input").val(item_discount_input);
      $("#item_discount_type").val(item_discount_type).select2();

      $("#popup_item_name").html(item_name);
      $("#popup_tax_type").val(tax_type).select2();
      $("#popup_tax_id").val(tax_id).select2();
      $("#popup_row_id").val(row_id);
      $("#popup_description").val(description);
    }

    function set_info(){
      var row_id = $("#popup_row_id").val();
      var tax_type = $("#popup_tax_type").val();
      var tax_id = $("#popup_tax_id").val();
      var description = $("#popup_description").val();
      var tax_name = ($('option:selected', "#popup_tax_id").attr('data-tax-value'));
      var tax = parseFloat($('option:selected', "#popup_tax_id").attr('data-tax'));

      /*Discounr*/
      var item_discount_input = $("#item_discount_input").val();
      var item_discount_type = $("#item_discount_type").val();

      //Set it into row 
      $("#item_discount_input_"+row_id).val(item_discount_input);
      $("#item_discount_type_"+row_id).val(item_discount_type);

      $("#tr_tax_type_"+row_id).val(tax_type);
      $("#tr_tax_id_"+row_id).val(tax_id);
      $("#description_"+row_id).val(description);
      $("#tr_tax_value_"+row_id).val(tax);//%
      //$("#td_data_"+row_id+"_12").html(tax_type+" "+tax_name);
      
      var item_id=$("#tr_item_id_"+row_id).val();
      make_subtotal(item_id,row_id);
      //calculate_tax(row_id);
      $('#sales_item').modal('toggle');
    }
    function set_tax_value(row_id){
      var tax_type = $("#tr_tax_type_"+row_id).val() || 'Exclusive';
      var tax_rate = parseFormattedNumber($("#tr_tax_value_"+row_id).val());
      var qty = parseFormattedNumber($("#item_qty_"+row_id).val());
      var unit_price = parseFormattedNumber($("#sales_price_"+row_id).val());
      var total_price = unit_price * qty;

      var item_discount_type = $("#item_discount_type_"+row_id).val() || 'Fixed';
      var item_discount_input = parseFormattedNumber($("#item_discount_input_"+row_id).val());
      var discount_amt = (item_discount_type=='Percentage') ? (total_price * item_discount_input)/100 : (item_discount_input * qty);
      var base_after_discount = total_price - discount_amt;
      if(base_after_discount<0) base_after_discount=0;

      var tax_amount = 0;
      if(tax_rate>0 && base_after_discount>0){
        if(tax_type=='Inclusive')
          tax_amount = (base_after_discount * tax_rate) / (100 + tax_rate);
        else
          tax_amount = (base_after_discount * tax_rate) / 100;
      }
      $("#item_discount_"+row_id).val(to_Fixed(discount_amt));
      $("#td_data_"+row_id+"_11").val(to_Fixed(tax_amount));
    }
    //Sale Items Modal Operations End

</script>
<script>
  $(function () {
    $('input').iCheck({
      checkboxClass: 'icheckbox_square-blue',
      radioClass: 'iradio_square-blue',
      increaseArea: '20%' // optional
    });
  });
</script>
<script type="text/javascript">
  shortcut.add("Alt+m",function(e) {
        e.preventDefault();
        $(".show_payments_modal").trigger('click');
    },{
        'type':'keydown',
        'propagate':true,
        'target':document
      });

  shortcut.add("Alt+h",function(e) {
        e.preventDefault();
        $("#hold_invoice").trigger('click');
    },{
        'type':'keydown',
        'propagate':true,
        'target':document
      });
  shortcut.add("Alt+c",function(e) {
        e.preventDefault();
        $(".Alt_c").trigger('click');
    },{
        'type':'keydown',
        'propagate':true,
        'target':document
      });
  shortcut.add("Alt+a",function(e) {
        e.preventDefault();
        $(".Alt_a").trigger('click');
    },{
        'type':'keydown',
        'propagate':true,
        'target':document
      });
</script>

<script>

//Reset Tooltip
function reset_tooltip() {
  $('[data-toggle="tooltip"]').tooltip("destroy");
  $('[data-toggle="tooltip"]').tooltip(); // re-enabling 
}
$('.search_div').on('scroll', function() {
    if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {

      if($(".scroll_or_not").val()=="true"){

        //Scroll Restriction Enabled
        $(".scroll_or_not").val("false");
        //Ajax Loader
        $('.ajax-load').show();

        setTimeout(function() {
          //Ajax Request
          load_next_details();  
          //Scroll Restriction Disabled
          $(".scroll_or_not").val("true");
          //Ajax Loader End
          $('.ajax-load').hide();    
        }, 1000);
        
        
      }
      
    }
});

function load_next_details(){
  var last_id = $(".item_box:last").attr("data-item-id");
  get_details(last_id);
}



function get_details(last_id='',show_only_searched=false){

  warehouse_id = $("#warehouse_id").val();

  $.ajax({
      url: '<?php echo $base_url; ?>pos/get_details',
      type: "post",
      data:{
        last_id       : (!show_only_searched) ? last_id : '',
        customer_id   : $("#customer_id").val(),
        id            : $("#category_id").val(),
        store_id      : $("#store_id").val(),
        warehouse_id  : $("#warehouse_id").val(),
        item_name     : $("#item_name").val(),
        //search_it  : $("#search_it").val(),
        brand_id  : $("#brand_id").val(),

      },
      beforeSend: function(){
          $('.ajax-load').show();
      }
  }).done(function(data){
      $('.ajax-load').hide();
      
      if(data=='') {
        $(".error_div").show();
      }
      else{
        $(".error_div").hide();
      }


      if(show_only_searched){
        $(".search_div").html('');
      }
      $(".search_div").append(data);
      reset_tooltip();
  }).fail(function(jqXHR, ajaxOptions, thrownError){
      if (jqXHR.status === 0 || !navigator.onLine) { $('.ajax-load').hide(); return; }
      if(typeof toastr!=='undefined') toastr.error('Server not responding...');
      else alert('server not responding...');
  });
}

// ----- Batch selection (POS) -----
function show_pos_batch_selection_modal(item_id, row_id) {
  window.current_pos_batch_row_id = row_id;
  $('#modal_item_name_sales').text($('#td_data_'+row_id+'_0').text().trim());
  $('#sales_batch_selection_modal').modal('show');
  load_pos_available_batches(item_id, row_id);
}
function load_pos_available_batches(item_id, row_id) {
  $('#batch_loading').show();
  $('#batch_table_container').hide();
  $('#no_batches_message').hide();
  var warehouse_id = $("#warehouse_id").val();
  $.ajax({
    url: base_url + 'sales/get_available_batches',
    type: 'POST',
    data: { item_id: item_id, warehouse_id: warehouse_id },
    dataType: 'json',
    success: function(response) {
      $('#batch_loading').hide();
      if(response.success && response.batches && response.batches.length>0) {
        window.current_pos_batch_batches = response.batches;
        display_pos_batches(response.batches, row_id, response.item_info || {});
        $('#batch_table_container').show();
        $('#no_batches_message').hide();
        // Auto-select the batch that expires first (first in list = expiry_date ASC); keep modal open
        var first = response.batches[0];
        setTimeout(function() {
          select_pos_batch(first.batch_number, first.manufacturing_date || '', first.expiry_date || '', row_id, parseFloat(first.available_qty) || 0, false);
        }, 100);
      } else {
        window.current_pos_batch_batches = [];
        $('#batch_table_container').hide();
        $('#no_batches_message').show();
      }
    },
    error: function() {
      $('#batch_loading').hide();
      $('#batch_table_container').hide();
      $('#no_batches_message').show();
    }
  });
}
function display_pos_batches(batches, row_id, item_info) {
  item_info = item_info || {};
  var conv = parseFloat(item_info.conversion_factor) || 0;
  var mainName = item_info.main_unit_name || '';
  var altName = item_info.alternate_unit_name || '';
  var hasDualUnits = conv > 0 && mainName && altName;
  var tbody = $('#batch_selection_tbody');
  tbody.empty();
  var totalInBatches = 0;
  batches.forEach(function(batch) {
    var exp_date = batch.expiry_date && batch.expiry_date!=='0000-00-00' ? batch.expiry_date.split('-').reverse().join('-') : 'N/A';
    var avail = parseFloat(batch.available_qty||0);
    totalInBatches += avail;
    var availDisplay = avail.toFixed(2);
    if (hasDualUnits) availDisplay += ' ' + mainName + ' (' + (avail*conv).toFixed(2) + ' ' + altName + ')';
    var days = 'N/A';
    if(batch.expiry_date && batch.expiry_date!=='0000-00-00'){
      var exp = new Date(batch.expiry_date);
      var today = new Date(); today.setHours(0,0,0,0);
      days = Math.ceil((exp - today)/(24*60*60*1000));
      days = (days < 0) ? ('Expired') : days.toFixed(0);
    }
    var availEsc = String(avail).replace(/'/g, "\\'");
    var row = '<tr>';
    row += '<td style="text-align:left;"><strong>'+batch.batch_number+'</strong></td>';
    row += '<td style="text-align:left;">'+exp_date+'</td>';
    row += '<td style="text-align:center;">'+days+'</td>';
    row += '<td style="text-align:right;">'+availDisplay+'</td>';
    row += '<td style="text-align:center;"><button type="button" class="btn btn-success btn-xs" onclick="select_pos_batch(\''+batch.batch_number.replace(/'/g,"\\'")+'\', \''+(batch.manufacturing_date||'')+'\', \''+(batch.expiry_date||'')+'\', '+row_id+', '+availEsc+')"><i class="fa fa-check"></i></button></td>';
    row += '</tr>';
    tbody.append(row);
  });
  if(typeof updateBatchStockSummary==='function') updateBatchStockSummary(totalInBatches, totalInBatches, 0);
}
function select_pos_batch(batch_number, manufacturing_date, expiry_date, row_id, available_qty_main, closeModal) {
  available_qty_main = parseFloat(available_qty_main) || 0;
  if (typeof closeModal === 'undefined') closeModal = true;
  $('#batch_number_'+row_id).val(batch_number);
  $('#manufacturing_date_'+row_id).val(manufacturing_date||'');
  $('#expiry_date_'+row_id).val(expiry_date||'');
  $('#row_'+row_id).attr('data-batch-available-main', available_qty_main);
  update_pos_batch_display(row_id, batch_number, manufacturing_date, expiry_date);
  if (closeModal) $('#sales_batch_selection_modal').modal('hide');
  if (typeof toastr !== 'undefined' && closeModal) toastr.success('Batch selected');
}
function update_pos_batch_display(row_id, batch_number, manufacturing_date, expiry_date) {
  var txt = '<br><small class="text-info">Batch: '+batch_number+'</small>';
  var cell = $('#td_data_'+row_id+'_0');
  var html = cell.html().replace(/<br>\s*<small[^>]*>Batch:[^<]*<\/small>/gi,'');
  cell.html(html+txt);
}

function autoSelectFIFOPOS() {
  if (!window.current_pos_batch_batches || window.current_pos_batch_batches.length === 0) {
    if (typeof toastr !== 'undefined') toastr.warning('No batches loaded');
    return;
  }
  var row_id = window.current_pos_batch_row_id;
  if (row_id === undefined || row_id === null) {
    if (typeof toastr !== 'undefined') toastr.warning('Row not set');
    return;
  }
  var first = window.current_pos_batch_batches[0];
  select_pos_batch(first.batch_number, first.manufacturing_date || '', first.expiry_date || '', row_id, parseFloat(first.available_qty) || 0, false);
  if (typeof toastr !== 'undefined') toastr.success('Batch that expires first selected. Close the modal when done.');
}
function validateBatchSelection() {
  var rowcount=parseInt($("#hidden_rowcount").val(),10)||0;
  for(var i=0;i<rowcount;i++){
    if(!document.getElementById('tr_item_id_'+i)) continue;
    var batchEnabled=$('#batch_tracking_enabled_'+i).val();
    var batchNum=$('#batch_number_'+i).val();
    if((batchEnabled=='1'||batchEnabled===1) && (!batchNum || String(batchNum).trim()==='')){
      var name=$('#td_data_'+i+'_0').text().trim();
      if(typeof toastr!=='undefined') toastr.warning('Select batch for: '+name);
      return false;
    }
  }
  return true;
}


</script> 
</body>
</html>
