<?php
if( !hasPermission($_SESSION['user_detail']['role_id'], 'lhc_add') ) {
   echo '<script>window.location.href="index.php?page=no_permission.php"</script>';die();
}
$agent = mysqli_query($conn, "SELECT * FROM `vendor_master` WHERE `type` = 'A' AND `status` = '0'");
$agent_data = array();
while ($agent_row = mysqli_fetch_assoc($agent)) {
   $agent_data[] = $agent_row;
}
?>
<link rel="stylesheet" href="assets/plugins/toaster/jquery.toast.css">
<link rel="stylesheet" href="assets/plugins/toaster/jquery.toast.min.css">
<script src="assets/plugins/toaster/jquery.toast.js"></script>
<section class="content-header">
  <div class="row">
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 bottom20">
        <h3 class="capital font15 padding5 bold">ADD NEW LHC</h3>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 text-right bottom20">
        <h3>
          <?php if( hasPermission($_SESSION['user_detail']['role_id'], 'lhc_list') ) { ?>
          <a href="index.php?page=lhc/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW LHC DETAILS</a>
          <?php } ?>
        </h3>
      </div>
  </div>
</section>
<hr class="bottom35"> 
<section class="hitz-top-header">
   <div class="row bottom40">
      <div class="col-xs-12">
         <div class="box box-success">
            <div class="box-body">
               <form action="lhc/insert.php" method="post" enctype="multipart/form-data" class="common-frm-validation" id="signupForm">
                  <div class="row">
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">LHC NO</label><span style="color: red">*</span>
                           <?php 
                              if($codeType == 1){  ?>
                           <input type="text" size="35" id="lhc_no" name="lhc_no" class="form-control lr_number" autocomplete="off" placeholder="LHC NO" value="<?php echo lhcCode($conn,$financial_years);?>" onChange="CheckNumberSeries(this.value);CheckLhc(this.value);" autofocus="" />
                           <?php }else{ ?>
                           <input type="text" size="35" id="lhc_no" name="lhc_no" class="form-control lr_number" autocomplete="off" placeholder="LHC NO" onChange="CheckLhc(this.value);" value="<?php echo lhcCode($conn);?>" readonly="" autofocus=""/>
                           <?php } ?>
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">DATE</label><span style="color: red">*</span>
                           <div class="input-group date">
                              <div class="input-group-addon">
                                 <i class="fa fa-calendar"></i>
                              </div>
                              <input type="date" size="35" id="lhc_date" name="lhc_date" class="form-control date-picker" placeholder="DATE" autocomplete="off" value="<?php echo date('d-m-Y')?>" onfocusout="CheckDate(this.value);"/>
                           </div>
                        </div>
                     </div>
                     
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">truck no</label>
                           <input type="text" name="truck_no" id="truck_no" class="form-control" readonly="" onchange="CheckTruck(this);">
                           <input type="hidden" name="old_truck_no" id="old_truck_no" class="form-control">
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">FROM</label>
                           <input type="text" size="35" id="from_place" name="from_place" class="form-control" autocomplete="off" placeholder="FROM PLACE" readonly=""/>
                           <input type="hidden" size="35" id="old_from_place" name="old_from_place" class="form-control" autocomplete="off" placeholder="FROM PLACE" readonly=""/>
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">TO</label>
                           <input type="text" size="35" id="to_place" name="to_place" class="form-control" autocomplete="off" placeholder="TO PLACE" readonly=""/>
                           <input type="hidden" size="35" id="old_to_place" name="old_to_place" class="form-control" autocomplete="off" placeholder="TO PLACE" readonly=""/>
                        </div>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-md-6">
                        <div id="product-wapper" class="box-body table-responsive">
                           <table class="table table-bordered">
                              <thead>
                                 <tr>
                                    <th width="2%" class="text-center">#</th>
                                    <th width="17%" class="text-center">LR NO</th>
                                    <th width="15%" class="text-center">DATE</th>
                                    <th class="text-center">CONTENT</th>
                                    <th width="10%" class="text-center">PKGS</th>
                                    <th width="10%" class="text-center">WEIGHT</th>
                                 </tr>
                              </thead>
                              <tbody>
                                 <?php $i = 0;?>
                                 <tr>
                                    <td>
                                       <a href="javascript:;" class="btn green btn-block add-more" style="display: block;"><i class="fa fa-plus"></i></a>
                                       <a href="javascript:;" class="btn red btn-block remove-me" style="display: none;"><i class="fa fa-minus"></i></a>
                                    </td>
                                    <td>
                                       <input type="text" name="product[<?php echo $i; ?>][lr_no]" class="add-more-table form-control product-name top lr_no lr_cal" placeholder="Search By LR NO" id="truck" data-count="<?php echo $i;?>" onchange="CheckLr(this);CheckDuplicateRow(this);check_payment_to();">
                                    </td>
                                    <td><input type="date" name="product[<?php echo $i; ?>][lr_date]" class="form-control top add-more-table lr_cal lr_date" readonly></td>
                                    <td><input type="text" name="product[<?php echo $i; ?>][content]" class="form-control lhc-content top add-more-table lr_cal" readonly></td>
                                    <td><input type="text" name="product[<?php echo $i; ?>][pkgs]" class="form-control lhc-pkgs top add-more-table lr_cal" readonly></td>
                                    <td><input type="text" name="product[<?php echo $i; ?>][weight]" class="form-control lhc-weight top add-more-table lr_cal" readonly></td>
                                 </tr>
                                 <?php $i++; ?>
                              </tbody>
                           </table>
                        </div>
                        <div class="box-body">
                           <table class="table table-bordered">
                              <thead>
                                 <tr>
                                    <th colspan="3" class="text-center">TOTAL</th>
                                    <th width="10%" class="text-center"> <input id="total_pkg" name="total_pkg" class="form-control add-more-table top" placeholder="0.00" readonly = ""></th>
                                    <th width="17%" class="text-center"><input id="total_weight" name="total_weight" class="form-control add-more-table top" placeholder="0.00" readonly = ""></th>
                                 </tr>
                              </thead>
                           </table>
                        </div>
                     </div>
                     <div class="col-md-6">
                        <div class="col-xs-6 col-sm-6">
                           <div class="form-group">
                              <label class="capital">AGENT</label>
                              <select class="form-control hitz_agent" id="agent" name="agent">
                                 <option value></option>
                                 <?php foreach ($agent_data as $key => $agent_datas) { ?>
                                 <option value="<?php echo $agent_datas['name']?>"><?php echo $agent_datas['name']?></option>
                                 <?php } ?>
                              </select>
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">lorry type</label>
                              <input type="text" size="35" id="lorry_type" name="lorry_type" class="form-control" autocomplete="off" placeholder="LORRY TYPE"/>
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">chasis no</label>
                              <input type="text" size="35" id="chasis_no" name="chasis_no" class="form-control" autocomplete="off" placeholder="CHASIS NO"/>
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">engine no</label>
                              <input type="text" size="35" id="engine_no" name="engine_no" class="form-control" autocomplete="off" placeholder="ENGINE NO" />
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">permit no</label>
                              <input type="text" size="35" id="permit_no" name="permit_no" class="form-control" autocomplete="off" placeholder="PERMIT NO"/>
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">from</label>
                              <div class="input-group date">
                                 <div class="input-group-addon">
                                    <i class="fa fa-calendar"></i>
                                 </div>
                                 <input type="text" size="35" id="p_from" name="p_from" class="form-control date-picker" autocomplete="off" placeholder="FROM"/>
                              </div>
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">up to</label>
                              <div class="input-group date">
                                 <div class="input-group-addon">
                                    <i class="fa fa-calendar"></i>
                                 </div>
                                 <input type="text" size="35" id="up_to" name="up_to" class="form-control date-picker" autocomplete="off" placeholder="UP TO"/>
                              </div>
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">INSURANCE CO.</label>
                              <input type="text" size="35" id="insurance_no" name="insurance_no" class="form-control" autocomplete="off" placeholder="INSURANCE CO."/>
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">BRANCH</label>
                              <input type="text" size="35" id="branch" name="branch" class="form-control" autocomplete="off" placeholder="BRANCH"/>
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">policy no</label>
                              <input type="text" size="35" id="policy_no" name="policy_no" class="form-control" autocomplete="off" placeholder="POLICY NO" />
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">up to</label>
                              <div class="input-group date">
                                 <div class="input-group-addon">
                                    <i class="fa fa-calendar"></i>
                                 </div>
                                 <input type="text" size="35" id="p_up_to" name="p_up_to" class="form-control date-picker" autocomplete="off" placeholder="UP TO"/>
                              </div>
                           </div>
                        </div>
                        <div class="col-md-3">
                           <div class="form-group">
                              <label class="capital">IMAGE</label>
                              <input type="file" id="image" name="image" autocomplete="off" />
                           </div>
                        </div>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-md-3">
                        <legend class="capital hitz-legend">DRIVER INFORMATION</legend>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">driver</label>
                              <input type="text" size="35" id="d_name" name="d_name" class="form-control" autocomplete="off" />
                           </div>
                        </div>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">address</label>
                              <textarea class="form-control hit-textarea" name="d_address" id="d_address" style="height: 50px !important;"></textarea>
                           </div>
                        </div>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">lic no</label>
                              <input type="text" size="35" id="lic_no" name="lic_no" class="form-control" autocomplete="off"/>
                           </div>
                        </div>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">date</label>
                              <div class="input-group date">
                                 <div class="input-group-addon">
                                    <i class="fa fa-calendar"></i>
                                 </div>
                                 <input type="text" size="35" id="date" name="date" class="form-control date-picker" placeholder="DATE" autocomplete="off"/>
                              </div>
                           </div>
                        </div>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">Issued from</label>
                              <input type="text" size="35" id="issued_from" name="issued_from" class="form-control" autocomplete="off"/>
                           </div>
                        </div>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">mobile</label>
                              <input type="text" size="35" id="mobile" name="mobile" class="form-control" autocomplete="off"/>
                           </div>
                        </div>
                     </div>
                     <div class="col-md-3">
                        <legend class="capital hitz-legend">OWNER information</legend>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">OWNER</label>
                              <input type="text" size="35" id="owner_name" class="form-control" autocomplete="off" />
                              <input type="hidden" size="35" id="owner_id" name="owner_name" class="form-control" autocomplete="off" />
                           </div>
                        </div>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">address</label>
                              <textarea class="form-control hit-textarea" name="owner_address" id="owner_address" style="height: 50px !important;"></textarea>
                           </div>
                        </div>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">PAN no</label>
                              <input type="text" size="35" id="pan_no" name="pan_no" class="form-control" autocomplete="off"/>
                           </div>
                        </div>
                        <div class="col-md-12">
                           <div class="form-group">
                              <label class="capital">MOBILE</label>
                              <input type="text" size="35" id="o_mobile" name="o_mobile" class="form-control" autocomplete="off"/>
                           </div>
                        </div>
                        <!--div class="row">
                           <div class="col-md-8">
                              <div class="form-group">
                                 <label class="capital">RATE ON</label> <span class="error">*</span>
                                 <select class="form-control select2" name="rate_on" id="rate_on">
                                    <option></option>
                                    <option value="WEIGHT">WEIGHT</option>
                                    <option value="FIXED">FIXED</option>
                                 </select>
                              </div>
                           </div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label class="capital">RATE</label>
                                 <input type="text" size="35" id="rate" name="rate" class="form-control number" autocomplete="off"/>
                              </div>
                           </div>
                        </div-->
                     </div>
                     <div class="col-sm-6">
                        <legend class="hitz-legend">PAYMENT DETAIL</legend>
                        <div class="col-md-12">
                           <div class="col-md-4">
                              <div class="col-md-12">
                                 <div class="form-group">
                                    <label class="capital">lorry hire</label><span style="color:red">*</span>
                                    <input type="text" size="35" id="lorry_hire" name="lorry_hire" class="form-control lorry-hire required" autocomplete="off" placeholder="LORRY HIRE"/>
                                 </div>
                              </div>
                              <div class="col-md-12" id="product-wappers">
                                 <table class="table table-bordered">
                                    <tbody>
                                       <?php
                                          $tax = "SELECT * FROM `tax_mst` WHERE FIND_IN_SET('LHC', module) AND `sign` = '+'";
                                          $tax_res = mysqli_query($conn,$tax);
                                          
                                          $tax_rows = array();
                                          while($tax_row = mysqli_fetch_assoc($tax_res)) {
                                            $tax_rows[] = $tax_row;
                                          }
                                          ?>
                                       <?php for ($i=1; $i <=4 ; $i++) { ?>
                                       <tr>
                                          <td width="65%;">
                                             <select class="form-control height29 chosen-select hitz-add" name="add[<?php echo $i ?>][tax_mst_id]" style="width: 100%;">
                                                <option value="">---SELECT---</option>
                                                <?php foreach ($tax_rows as $tax_row) { ?>
                                                <option value="<?php echo $tax_row['id'];?>"><?php echo $tax_row['descr'];?></option>
                                                <?php } ?>
                                             </select>
                                          </td>
                                          <td><input type="text" name="add[<?php echo $i ?>][amount]" class="form-control hitz_add addition payment-table number" data-count="<?php echo $i ?>" readonly=""></td>
                                       </tr>
                                       <?php } ?>
                                    </tbody>
                                 </table>
                              </div>
                           </div>
                           <div class="col-md-4">
                              <div class="col-md-12">
                                 <div class="form-group">
                                    <label class="capital">ADVANCE</label>
                                    <input type="text" size="35" id="adv_amnt" name="adv_amnt" class="form-control lorry-hire payment_amount" autocomplete="off" placeholder="ADVANCE"/>
                                 </div>
                              </div>
                              <div class="col-md-12" id="sub-wapper">
                                 <table class="table table-bordered">
                                    <tbody>
                                       <?php for ($i=1; $i <=4 ; $i++) { ?>
                                       <tr>
                                          <td width="65%;">
                                             <?php 
                                                $d_tax =  "SELECT * FROM `tax_mst` WHERE `sign` = '-' AND FIND_IN_SET('LHC', module)";
                                                $d_tax_res = mysqli_query($conn,$d_tax);
                                                $d_tax_rows = array();
                                                while($d_tax_row = mysqli_fetch_assoc($d_tax_res)) {
                                                  $d_tax_rows[] = $d_tax_row;
                                                }
                                                ?>
                                             <select class="form-control chosen-select hitz-del height29" name="sub[<?php echo $i ?>][tax_mst_id]" style="width: 100%;">
                                                <option value="">---SELECT---</option>
                                                <?php foreach ($d_tax_rows as $d_tax_row) { ?>
                                                <option value="<?php echo $d_tax_row['id'];?>"><?php echo $d_tax_row['descr'];?></option>
                                                <?php } ?>
                                             </select>
                                          </td>
                                          <td><input type="text" name="sub[<?php echo $i ?>][amount]" class="form-control hitz_subs height29 payment-table number deduction" data-count="<?php echo $i ?>" readonly=""></td>
                                       </tr>
                                       <?php } ?>
                                    </tbody>
                                 </table>
                              </div>
                           </div>
                           <div class="col-md-4">
                              <div class="col-md-12">
                                 <div class="form-group">
                                    <label class="capital">Total ADDITION</label>
                                    <input type="text" size="35" id="total" name="total" class="form-control" autocomplete="off" placeholder="TOTAL" readonly="" />
                                    <input type="hidden" size="35" id="other_add" name="other_add" class="form-control" autocomplete="off" readonly="" />
                                 </div>
                              </div>
                              <div class="col-md-12">
                                 <div class="form-group">
                                    <label class="capital">TOTAL DEDUCTION</label>
                                    <input type="text" size="35" id="total_del" name="total_del" class="form-control" autocomplete="off" placeholder="TOTAL" readonly="" />
                                    <input type="hidden" size="35" id="other_less" name="other_less" class="form-control hitz_sub" autocomplete="off" readonly="" />
                                 </div>
                              </div>
                              <div class="col-md-12">
                                 <div class="form-group">
                                    <label class="capital">BALANCE</label>
                                    <input type="text" size="35" id="balance" name="balance" class="form-control calc-amt" autocomplete="off" placeholder="BALANCE"/>
                                 </div>
                              </div>
                              <div class="col-md-12">
                                 <div class="form-group">
                                    <label class="capital">NET AMOUNT</label>
                                    <input type="text" size="35" id="net_amnt" name="net_amnt" class="form-control" autocomplete="off" placeholder="NET AMOUNT" readonly=""/>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <!--legend>PAYMENT MODE</legend-->
                        <div class="col-md-12">
                           <div class="row">
                              <div class="col-md-2">
                                 <div class="col-md-12">
                                    <!--div class="form-group">
                                       <label class="radio-inline fontradiobuttion">
                                       <input type="checkbox" name="payment_type[]" value="C" class="hit-cash"> <span class="label-text">CASH</span>
                                       </label>
                                    </div-->
                                 </div>
                              </div>
                              <div class="col-md-5">
                                 <div class="col-md-12">
                                    <!--div class="form-group">
                                       <label class="col-md-4 control-label">AMOUNT</label>  
                                       <div class="col-md-8">
                                          <input type="text" name="cash"  id="cash" class="form-control calc-amt payment_amount cash_amnt" autocomplete="off" onchange="CheckCashAct(this);"/>
                                       </div>
                                    </div-->
                                 </div>
                              </div>
                              <div class="col-md-5">
                                 <div class="row">
                                    <!--div class="form-group">
                                       <label class="col-md-2 control-label">NAME</label>  
                                       <div class="col-md-9">
                                          <select name="cash_name" id="cash_name" class="form-control at-cash" style="width: 100%">
                                             <option></option>
                                             <?php 
                                                $truck     = "SELECT * FROM `act_master` WHERE `account_group` ='CASH'";
                                                $truck_res = mysqli_query($conn,$truck);
                                                while ($truck_row=mysqli_fetch_array($truck_res)) { ?>f
                                             <option value="<?php echo $truck_row['id']?>"><?php echo $truck_row['ac_name']?></option>
                                             <?php } ?>
                                          </select>
                                       </div>
                                    </div-->
                                 </div>
                              </div>
                           </div>
                           <div class="row">
                              <div class="col-md-2">
                                 <div class="col-md-12">
                                    <!--div class="form-group">
                                       <label class="radio-inline fontradiobuttion">
                                       <input type="checkbox" name="payment_type[]" value="B" class="hit-bank"> <span class="label-text">BANK</span>
                                       </label>
                                    </div-->
                                 </div>
                              </div>
                              <div class="col-md-5">
                                 <div class="col-md-12">
                                    <!--div class="form-group">
                                       <label class="col-md-4 control-label">AMOUNT</label>  
                                       <div class="col-md-8">
                                          <input type="text" name="pay_mode_amnt" autocomplete= "off"  id="pay_mode_amnt" class="form-control calc-amt payment_amount bank_amnt" autocomplete="off"/>
                                       </div>
                                    </div-->
                                 </div>
                              </div>
                              <div class="col-md-5">
                                 <div class="row">
                                    <!--div class="form-group">
                                       <label class="col-md-2 control-label">MODE</label>
                                       <div class="col-md-9">
                                          <select name="payment_mode" id="payment_mode" class="form-control hit-payment_mode" style="height: 30px !important;">
                                             <option value=""></option>
                                             <option value="CHEQUE">CHEQUE</option>
                                             <option value="NEFT">NEFT</option>
                                             <option value="RTGS">RTGS</option>
                                             <option value="IMPS">IMPS</option>
                                             <option value="GPAY">GPAY</option>
                                          </select>
                                       </div>
                                    </div-->
                                 </div>
                              </div>
                           </div>
                           <div class="row">
                              <div class="col-lg-12" id="imps_number_div" style="display:none">
                                 <div class="form-group">
                                    <label class="capital">IMPS Transaction Number</label><span style="color: red"><sup>*</sup></span>
                                    <input type="text" name="imps_number"  id="imps_number" class="form-control" autocomplete="off" />
                                 </div>
                              </div>
                              <div class="col-lg-12" id="rtgs_number_div" style="display:none">
                                 <div class="form-group">
                                    <label class="capital">RTGS Transaction Number</label><span style="color: red"><sup>*</sup></span>
                                    <input type="text" name="rtgs_number"  id="rtgs_number" class="form-control" autocomplete="off"/>
                                 </div>
                              </div>
                              <div class="col-lg-12" id="neft_number_div" style="display:none">
                                 <div class="form-group">
                                    <label class="capital">NEFT Transaction Number</label><span style="color: red"><sup>*</sup></span>
                                    <input type="text" name="neft_number"  id="neft_number" class="form-control" autocomplete="off"/>
                                 </div>
                              </div>
                              <div class="col-lg-12" id="cheque_number_div" style="display:none">
                                 <div class="form-group">
                                    <label class="capital">Enter Cheque Number</label><span style="color: red"><sup>*</sup></span>
                                    <input type="text" name="cheque_number"  id="cheque_number" class="form-control" autocomplete="off"/>
                                 </div>
                              </div>
                              <div class="col-lg-12" id="cheque_date_div" style="display:none">
                                 <div class="form-group">
                                    <label class="capital">Enter Cheque Date</label>
                                    <input type="text" name="cheque_date" autocomplete= "off"  id="cheque_date" class="form-control date-picker" autocomplete="off"/>
                                 </div>
                              </div>
                              <div class="col-lg-12" id="bank_name_div" style="display:none">
                                 <div class="form-group">
                                    <label class="capital">Bank Name</label>
                                    <select name="bank_name" id="bank_name" class="form-control at-bank" style="width: 100%">
                                       <option value></option>
                                       <?php 
                                          $truck     = "SELECT * FROM `act_master` WHERE `account_group` ='BANK'";
                                          $truck_res = mysqli_query($conn,$truck);
                                          while ($truck_row=mysqli_fetch_array($truck_res)) { ?>f
                                       <option value="<?php echo $truck_row['id']?>"><?php echo $truck_row['ac_name']?></option>
                                       <?php } ?>
                                    </select>
                                 </div>
                              </div>
                              <input type="hidden" name="id" value="">
                           </div>
                           <div class="row">
                              <div class="col-md-2">
                                 <div class="col-md-12">
                                    <!--div class="form-group">
                                       <label class="radio-inline fontradiobuttion">
                                       <input type="checkbox" name="payment_type[]" value="D" class="hit-disel"> <span class="label-text">DISEL</span>
                                       </label>
                                    </div-->
                                 </div>
                              </div>
                              <div class="col-md-5">
                                 <div class="col-md-12">
                                    <!--div class="form-group">
                                       <label class="col-md-4 control-label">AMOUNT</label>  
                                       <div class="col-md-8">
                                          <input type="text" name="disel_amnt"  id="disel_amnt" class="form-control calc-amt payment_amount" autocomplete="off" onchange="CheckDiselAct(this);CheckBalance(this);" />
                                       </div>
                                    </div-->
                                 </div>
                              </div>
                              <div class="col-md-5">
                                 <div class="row">
                                    <div class="form-group">
                                       <label class="col-md-2 control-label">PAY. TO</label>
                                       <div class="col-md-9">
                                          <select name="payment_to" id="payment_to" class="form-control required" style="height: 28px !important;">
                                             <option value=''></option>
                                             <option value="AGENT">AGENT</option>
                                             <option value="OWNER">OWNER</option>
                                          </select>
                                       </div>
                                    </div-->
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="col-md-12">
                           <div class="col-md-3">
                              <!--div class="form-group">
                                 <label class="capital">PAYMENT DATE</label><span style="color: red">*</span>
                                 <div class="input-group date">
                                    <div class="input-group-addon">
                                       <i class="fa fa-calendar"></i>
                                    </div>
                                    <input type="text" size="35" id="payment_date" name="payment_date" class="form-control date-picker required" autocomplete="off" placeholder="PAYMENT DATE" value="<?php echo date('d-m-Y')?>" />
                                 </div>
                              </div-->
                           </div>
                        </div>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-sm-6">
                        <legend class="capital hitz-legend">EXPENSE</legend>
                        <div id="expense-wapper" class="box-body table-responsive">
                        <table class="table table-bordered">
                        <thead>
                        <tr>
                          <th width="2%" class="text-center">#</th>
                          <th class="text-left">ACCOUNT NAME</th>
                          <th width="25%" class="text-center">AMOUNT</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php $i = 0;?>
                        <tr>
                           <td>
                              <a href="javascript:;" class="btn green btn-block add-more-expense" style="display: block;"><i class="fa fa-plus"></i></a>
                              <a href="javascript:;" class="btn red btn-block remove-me-expense" style="display: none;"><i class="fa fa-minus"></i></a>
                           </td>
                           <td>
                              <input type="text" name="expense[<?php echo $i; ?>][account_name]" class="add-more-table form-control account_name top account_name lr_cal" data-count="<?php echo $i;?>">

                              <input type="hidden" name="expense[<?php echo $i; ?>][account_id]" class="add-more-table form-control account_id top account_id lr_cal" data-count="<?php echo $i;?>">

                              <input type="hidden" name="expense[<?php echo $i; ?>][account_code]" class="add-more-table form-control account_code top lr_cal">
                           </td>
                           
                           <td><input type="text" name="expense[<?php echo $i; ?>][amount]" class="form-control top add-more-table lr_cal amount" autocomplete="off"></td>
                        </tr>
                        <?php $i++; ?>
                        </tbody>
                      </table>
                    </div>
                     </div>
                  </div>
                  <input type="hidden" value="<?php echo $_SESSION['branch_id'];?>" name="branch_id" id="branch_id">
                  <input type="hidden" value="LHC" name="module" id="module">
                  <input type="hidden" value="" name="id" id="id">
                  <hr>
                  <div class="row" align="center">
                     <button id="btn" class="btn btn-primary ajax-submit capital" type="submit"> Save & List</button>
                     <a href="index.php?page=lhc/list.php" class="btn btn-danger capital"> Cancel</a>
                  </div>
               </form>
            </div>
         </div>
      </div>
   </div>
</section>
<div id="product-template" style="display:none;">
  <table>
    <tr>
      <td>
        <a href="javascript:;" class="btn green btn-block add-more" style="display: block;"><i class="fa fa-plus"></i></a>
        <a href="javascript:;" class="btn red btn-block remove-me" style="display: none;"><i class="fa fa-minus"></i></a>
      </td>

      <td><input type="text" name="product[%i%][lr_no]" class="add-more-table form-control product-name top lr_no lr_cal" placeholder="Search By LR NO" onchange="CheckLr(this);CheckDuplicateRow(this);"></td>

      <td><input type="date" name="product[%i%][lr_date]" class="add-more-table form-control lr_date top lr_cal" readonly></td>

      <td><input type="text" name="product[%i%][content]" class="add-more-table form-control lhc-content top lr_cal" readonly></td>

      <td><input type="text" name="product[%i%][pkgs]" class="add-more-table form-control lhc-pkgs top lr_cal" readonly></td>

      <td><input type="text" name="product[%i%][weight]" class="add-more-table form-control lhc-weight top lr_cal" readonly></td>
    </tr>
  </table>
</div>

<div id="expense-template" style="display:none;">
   <table>
      <tr>
         <td>
            <a href="javascript:;" class="btn green btn-block add-more-expense" style="display: block;"><i class="fa fa-plus"></i></a>
            <a href="javascript:;" class="btn red btn-block remove-me-expense" style="display: none;"><i class="fa fa-minus"></i></a>
         </td>

         <td>
            <input type="text" name="expense[%i%][account_name]" class="add-more-table form-control account_name top account_name lr_cal" data-count="%i%">

            <input type="hidden" name="expense[%i%][account_id]" class="add-more-table form-control account_id top account_id lr_cal" data-count="%i%">

            <input type="hidden" name="expense[%i%][account_code]" class="add-more-table form-control account_code top lr_cal">
         </td>
         
         <td><input type="text" name="expense[%i%][amount]" class="form-control top add-more-table lr_cal amount" autocomplete="off"></td>
      </tr>
   </table>
</div>
<script type="text/javascript">
  jQuery(document).ready(function() {
    $('#agent').select2();
    $('#rate_on').select2();
    $('input').iCheck({
      checkboxClass: 'icheckbox_flat',
      radioClass: 'iradio_flat'
    });
    /*AUTO CHECK*/
    $('.payment_amount').on('change', function() {
      var $cash = parseFloat(jQuery('#cash').val());
      if ($cash == '' || $cash == undefined || isNaN($cash)) {
          $cash = 0;
      }
      
      jQuery('.hit-cash').prop("checked", false).iCheck('update');
      jQuery('.at-cash').removeClass('required');
      if($cash > 0) {
        jQuery('.hit-cash').prop("checked", true).iCheck('update');
        jQuery('.at-cash').addClass('required');
      }
      
      var $bank = parseFloat(jQuery('#pay_mode_amnt').val());
      if ($bank == '' || $bank == undefined || isNaN($bank)) {
          $bank = 0;
      }
      jQuery('.hit-bank').prop("checked", false).iCheck('update');
      jQuery('.at-bank').removeClass('required');
      jQuery('.hit-payment_mode').removeClass('required');
      if($bank > 0) {
        jQuery('.hit-bank').prop("checked", true).iCheck('update');
        jQuery('.at-bank').addClass('required');
        jQuery('.hit-payment_mode').addClass('required');
      }

      var $amnt = parseFloat(jQuery('#adv_amnt').val());
      if ($amnt == '' || $amnt == undefined || isNaN($amnt)) {
          $amnt = 0;
      }

      if($bank < $cash){
        jQuery('.cash_amnt').addClass('max-validation').attr('max',$amnt); 
      }else{
        jQuery('.cash_amnt').removeClass('max-validation');
      }

      if($cash < $bank){
        jQuery('.bank_amnt').addClass('max-validation').attr('max',$amnt); 
      }else{
        jQuery('.bank_amnt').removeClass('max-validation');
      }


      var $disel = parseFloat(jQuery('#disel_amnt').val());
      if ($disel == '' || $disel == undefined || isNaN($disel)) {
          $disel = 0;
      }
      jQuery('.hit-disel').prop("checked", false).iCheck('update');
      if($disel > 0) {
        jQuery('.hit-disel').prop("checked", true).iCheck('update');
      }
    });

    $('#location_id').select2();
    $('#cash_name').select2();
    //$('#payment_to').select2();
    $(".chosen-select").chosen({disable_search_threshold: 1});

    // CHANGE TRUCK NO
    //TRUCK NO
    $( "#truck_no" ).autocomplete({
      source: function( request, response ) {
        $.ajax({
          url: "lr/get_truck_data.php",
          type: 'post',
          dataType: "json",
          data: {
            search: request.term
          },
          success: function( data ) {
            response( data );
          }
        });
      },
      select: function (event, ui) {
        if(ui.item.value != 0) {
          $('#truck_no').val(ui.item.label);
          get_Truck_details();
          return false;
        }
      }
    });

    $( "#from_place" ).autocomplete({
      source: function( request, response ) {
        $.ajax({
            url: "lhc/get_from_place.php",
            type: 'post',
            dataType: "json",
            data: {
                search: request.term
            },
            success: function( data ) {
                response( data );
            }
        });
      },
      select: function (event, ui) {
        $('#from_place').val(ui.item.label);
        return false;
      }
    });

    $( "#to_place" ).autocomplete({
      source: function( request, response ) {
        $.ajax({
            url: "lhc/get_to_place.php",
            type: 'post',
            dataType: "json",
            data: {
                search: request.term
            },
            success: function( data ) {
                response( data );
            }
        });
      },
      select: function (event, ui) {
        $('#to_place').val(ui.item.label);
        return false;
      }
    });

    var $length = 0;
    productAutoComplete();
    AccountAutoComplete();
    jQuery(document).on('click', '.add-more', function(e) {
      $length = jQuery('#product-wapper').find('table tbody tr').length;
      var $clone = jQuery('#product-template table tr').clone();
      $clone.find('input').each(function() {
        this.name = this.name.replace('[%i%]', '[' + $length + ']');
      });

      $clone.find('.product-name').attr('data-count', $length);
      jQuery('#product-wapper table tbody').append($clone);
      productAutoComplete();
      AccountAutoComplete();
      jQuery('#product-wapper table tbody').find('.remove-me').show();
      jQuery('#product-wapper table tbody').find('.add-more').hide();
      jQuery('#product-wapper table tbody').find('.remove-me:last').hide();
      jQuery('#product-wapper table tbody').find('.add-more:last').show();
    });

    jQuery(document).on('click', '.add-more-expense', function(e) {
      $length = jQuery('#expense-wapper').find('table tbody tr').length;
      var $clone = jQuery('#expense-template table tr').clone();
      $clone.find('input').each(function() {
        this.name = this.name.replace('[%i%]', '[' + $length + ']');
      });

      $clone.find('.account_name').attr('data-count', $length);
      jQuery('#expense-wapper table tbody').append($clone);
      productAutoComplete();
      AccountAutoComplete();
      jQuery('#expense-wapper table tbody').find('.remove-me-expense').show();
      jQuery('#expense-wapper table tbody').find('.add-more-expense').hide();
      jQuery('#expense-wapper table tbody').find('.remove-me-expense:last').hide();
      jQuery('#expense-wapper table tbody').find('.add-more-expense:last').show();
    });

    jQuery(document).on('click', '.remove-me', function(e) {
      e.preventDefault();
      jQuery(this).closest('tr').remove();
      calculateFinalAmount();
    });

    jQuery(document).on('click', '.remove-me-expense', function(e) {
      e.preventDefault();
      jQuery(this).closest('tr').remove();
    });

    $(document).on('keyup', ".lorry-hire", function() {
      calculateFinalAmount();
    });

    $(document).on('keyup', ".calc-amt", function() {
      calculateFinalAmount();
    });

    $(document).on('keyup', '.hitz_add', function() {
      calculateFinalAmount();
    });

    $(document).on('keyup', '.hitz_subs', function() {
      calculateFinalAmount();
    });
    $(document).on('change', '.hitz-del', function() {
      var $tr = jQuery(this).closest('tr').find('.deduction').attr('data-count');
      jQuery('input[name="sub[' + $tr + '][tax_per]"]').removeAttr("readonly", "readonly");
      jQuery('input[name="sub[' + $tr + '][amount]"]').removeAttr("readonly", "readonly");
    });

    $(document).on('change', '.hitz-add', function() {
      var $trs = jQuery(this).closest('tr').find('.addition').attr('data-count');
      jQuery('input[name="add[' + $trs + '][amount]"]').removeAttr("readonly", "readonly");
    });

    $('#payment_mode').on('change', function() {
      if (this.value == 'CASH') {
        $("#cash_div").show();
        $("#cheque_date_div").hide();
        $("#cheque_deposite_div").hide();
        $("#bank_name_div").hide();
        $("#bank_branch_div").hide();
        $("#cheque_status_div").hide();
        $("#neft_number_div").hide();
        $("#rtgs_number_div").hide();
        $("#imps_number_div").hide();
        $("#amount_div").show();
      }else if (this.value == 'CHEQUE') {
        $("#cheque_number_div").show();
        $("#cheque_date_div").show();
        $("#cheque_deposite_div").show();
        $("#bank_name_div").show();
        $("#bank_branch_div").show();
        $("#cheque_status_div").show();
        $("#neft_number_div").hide();
        $("#rtgs_number_div").hide();
        $("#imps_number_div").hide();
        $("#cash_div").hide();
        $("#amount_div").show();      
      } else if (this.value == 'NEFT'){
        
        $("#neft_number_div").show();
        $("#rtgs_number_div").hide();
        $("#imps_number_div").hide();
        $("#cheque_date_div").hide();
        $("#cheque_deposite_div").hide();
        $("#bank_name_div").show();
        $("#bank_branch_div").hide();
        $("#cheque_status_div").hide();
        $("#cheque_number_div").hide();
        $("#rtgs_number").val('');
        $("#cheque_number").val('');
        $("#cheque_date").val('');
        $("#bank_name").val('').trigger('change');
        $("#bank_branch").val('');
        $("#cheque_status").val('');
        $("#cash_div").hide();
        $("#amount_div").show();
      
      } else if (this.value == 'RTGS'){
        
        $("#rtgs_number_div").show();
        $("#neft_number_div").hide();
        $("#imps_number_div").hide();
        $("#cheque_date_div").hide();
        $("#cheque_deposite_div").hide();
        $("#bank_name_div").show();
        $("#bank_branch_div").hide();
        $("#cheque_status_div").hide();
        $("#cheque_number_div").hide();
        $("#neft_number").val('');
        $("#cheque_number").val('');
        $("#cheque_date").val('');
        $("#bank_name").val('').trigger('change');
        $("#bank_branch").val('');
        $("#cheque_status").val('');
        $("#cash_div").hide();
        $("#amount_div").show();
      } else if (this.value == 'IMPS'){
        $("#imps_number_div").show();
        $("#rtgs_number_div").hide();
        $("#neft_number_div").hide();
        $("#cheque_date_div").hide();
        $("#cheque_deposite_div").hide();
        $("#bank_name_div").show();
        $("#bank_branch_div").hide();
        $("#cheque_status_div").hide();
        $("#cheque_number_div").hide();
        $("#rtgs_number").val('');
        $("#neft_number").val('');
        $("#cheque_number").val('');
        $("#cheque_date").val('');
        $("#bank_name").val('').trigger('change');
        $("#bank_branch").val('');
        $("#cheque_status").val('');
        $("#cash_div").hide();
        $("#amount_div").show();
      } else if (this.value == 'DD') {
        $("#cheque_number_div").show();
        $("#cheque_date_div").show();
        $("#cheque_deposite_div").show();
        $("#bank_name_div").show();
        $("#bank_branch_div").show();
        $("#cheque_status_div").show();
        $("#neft_number_div").hide();
        $("#rtgs_number_div").hide();
        $("#imps_number_div").hide();
        $("#cash_div").hide();
        $("#amount_div").show();
              
      }else if (this.value == 'GPAY') {
        $("#imps_number_div").hide();
        $("#rtgs_number_div").hide();
        $("#neft_number_div").hide();
        $("#cheque_date_div").hide();
        $("#cheque_deposite_div").hide();
        $("#bank_name_div").show();
        $("#bank_branch_div").hide();
        $("#cheque_status_div").hide();
        $("#cheque_number_div").hide();
        $("#rtgs_number").val('');
        $("#neft_number").val('');
        $("#cheque_number").val('');
        $("#cheque_date").val('');
        $("#bank_name").val('').trigger('change');
        $("#bank_branch").val('');
        $("#cheque_status").val('');
        $("#cash_div").hide();
        $("#amount_div").show();
      } else {
        $("#payment_collection_date").val('');
        $("#imps_number_div").hide();
        $("#imps_number").val('');
        $("#rtgs_number_div").hide();
        $("#rtgs_number").val('');
        $("#neft_number_div").hide();
        $("#neft_number").val('');
        $("#cheque_number_div").hide();
        $("#cheque_number").val('');
        $("#cheque_date_div").hide();
        $("#cheque_deposite_div").hide();
        $("#cheque_date").val('');
        $("#bank_name_div").hide();
        $("#bank_name").val('').trigger('change');
        $("#bank_branch_div").hide();
        $("#bank_branch").val('');
        $("#cheque_status_div").show();
        $("#cheque_status").val('');
        $("#cash_div").hide();
        $("#amount_div").show();
      }
    });

    $('#payment_mode').select2();
    $('#bank_name').select2();

    jQuery('#rate_on').on('change', function() {
      calculate(this);
    });

    jQuery('#rate').on('change', function() {
      calculate();
    });

  });
  function calculate(ele){
    var rate_on = jQuery("#rate_on").val();
    var rate    = jQuery("#rate").val();

    if(rate_on == 'WEIGHT'){
      var $total_weight = parseFloat(jQuery('#total_weight').val());
      if ($total_weight == '' || $total_weight == undefined || isNaN($total_weight)) {
        $total_weight = 0;
      }

      $lhcharges  = $total_weight * rate;
      jQuery('#lorry_hire').val($lhcharges.toFixed(2));
      jQuery("#lorry_hire").prop('readonly', 'readonly');
    }else{
      jQuery("#rate").prop('readonly', 'readonly');
      jQuery('#lorry_hire').val('').trigger('focus');
    }
  }
  
  function CheckTruck() {
    var truck_no      = $("#truck_no").val();
    console.log(truck_no);
    $.ajax({
      'type': 'POST',
      'url': 'lr/check-truck-no.php',
      'data': {
        'truck_no': truck_no
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status != 1) {
          var $truck      = $("#truck_no").val();
          
          var url = "index.php?page=truck_mst/add.php" + '&type=' + 'truck' + '&no=' + jQuery('#truck_no').val();

          window.open(url);
        }
      }
    });
  }

  function get_Truck_details() {
    var truck_no      = $("#truck_no").val();
    $.ajax({
        'type': 'POST',
        'url': 'lhc/get_truck.php',
        'data': 'truck_no=' + truck_no,
        dataType: 'JSON',
        success: function (result) {
           if (result != '') {
              $('#lorry_type').val(result.lorry_type);
              $('#chasis_no').val(result.chasis_no);
              $('#engine_no').val(result.eng_no);
              $('#permit_no').val(result.per_no);
              $('#p_from').val(result.from);
              $('#up_to').val(result.up_to);
              $('#insurance_no').val(result.insurance);
              $('#branch').val(result.branch);
              $('#policy_no').val(result.policy);
              $('#p_up_to').val(result.up_too);
              $('#d_name').val(result.d_name);
              $('#d_address').val(result.d_address);
              $('#lic_no').val(result.lic_no);
              $('#mobile').val(result.d_contact);
              $('#owner_name').val(result.o_name);
              $('#owner_id').val(result.id);
              $('#owner_address').val(result.o_address);
              $('#pan_no').val(result.o_pan);
              $('#o_mobile').val(result.o_contact);
           }
           check_payment_to();
        }

    });
  }

  function productAutoComplete() {
    $( ".product-name" ).autocomplete({
      position: { my: "left bottom", at: "left top", collision: "flip" },
      source: function( request, response ) {
        $.ajax({
           url: "lhc/get_lr_no.php",
           type: 'post',
           dataType: "json",
           data: {
              search: request.term
           },
           success: function( data ) {
              response( data );
           }
        });
      },
      select: function (event, ui) {
        var $parent = jQuery(this).parents('tr');
        var $i = jQuery(this).attr('data-count');
        $checkpoint = false;
        $checkpoint = CheckDuplicateRow(ui.item.lr_no, ui.item.id);
        if( $checkpoint ) {
          $parent.find('.product-name').val(ui.item.lr_no);
          $parent.find('.lhc-pkgs').val(ui.item.pcs);
          $parent.find('.lhc-content').val(ui.item.nature_of_goods);
          $parent.find('.lhc-weight').val(ui.item.weight);
          $parent.find('.lhc-from').val(ui.item.from_place);
          $parent.find('.lhc-to').val(ui.item.to_place);
          $parent.find('.truck-no').val(ui.item.truck_no);
          $parent.find('.hitz_agent').val(ui.item.agent);
          $parent.find('.lr_date').val(ui.item.l_date);
          jQuery('input[name="truck_no"]').removeAttr("readonly", "readonly");
          jQuery('input[name="from_place"]').removeAttr("readonly", "readonly");
          jQuery('input[name="to_place"]').removeAttr("readonly", "readonly");
        }else{
          var $this = jQuery(this);
          setTimeout(function(){
            $this.focus();
            $this.select();
          }, 100);
        }
        calculateFinalAmount();
        get_truck();
        check_payment_to();
        return false;
      }
    }).each(function() {
       $(this).autocomplete( 'instance' )._renderItem = function( ul, item ) {
          return $( "<li>" )
             .append( "<a><span>" + item.lr_no + '</span></a>' )
             .appendTo( ul );
        };
    });
  }
  
  function get_truck(){
    var lr_no      = $("#truck").val();
    $.ajax({
      'type': 'POST',
      'url': 'lhc/get_lr_truck.php',
      'data': 'lr_no=' + lr_no,
      dataType: 'JSON',
      success: function (result) {
        if (result != '') {
          $('#truck_no').val(result.truck_no);
          $('#old_truck_no').val(result.truck_no);
          $('#from_place').val(result.from_place);
          $('#old_from_place').val(result.from_place);
          $('#to_place').val(result.to_place);
          $('#old_to_place').val(result.to_place);
          $('#agent').val(result.agent).change();
          $('#lhc_date').val(result.l_date);
        }
        get_Truck_details();
        check_payment_to();
      }
    });
  }
  
  function check_payment_to(){
    var $agent      = $("#agent").val();
    var $owner_name = $("#owner_name").val();

    if($agent != '' && $owner_name != ''){
      jQuery('#payment_to').children('option[value="AGENT"]').show();
      jQuery('#payment_to').children('option[value="OWNER"]').show();
    }else if($agent != ''){
      jQuery('#payment_to').children('option[value="AGENT"]').show();
      jQuery('#payment_to').children('option[value="OWNER"]').hide();
    }else if($owner_name != ''){
      jQuery('#payment_to').children('option[value="AGENT"]').hide();
      jQuery('#payment_to').children('option[value="OWNER"]').show();
    }else{
      jQuery('#payment_to').children('option[value="AGENT"]').hide();
      jQuery('#payment_to').children('option[value="OWNER"]').hide();
    }
  }

  function CheckBalance(){
    var $balances = parseFloat(jQuery('#balance').val());
    if ($balances == '' || $balances == undefined || isNaN($balances)) {
      $balances = 0;
    }
    if($balances < 0 ){
      swal("Opps !!", "NAGATIVE BALANCE NOT ACCEPTED", "info"); 
      setTimeout("location.reload(true);",2000); 
    }
  }

  function calculateFinalAmount() {
    var $product_wapper  = jQuery('#product-wapper').find('table tbody tr');
    var $product_wappers = jQuery('#product-wappers').find('table tbody tr');
    var $sub_wapper      = jQuery('#sub-wapper').find('table tbody tr');
    
    // add
    var $net_amts = 0;
    $product_wappers.each(function(i, v) {
      $add_amnt  = parseFloat(jQuery(v).find('.hitz_add').val());
        if ($add_amnt == '' || $add_amnt == undefined || isNaN($add_amnt)) {
          $add_amnt = 0;
        }
      $net_amts += $add_amnt;
    });

    // SUB
    var $sub_amnts = 0;
    $sub_wapper.each(function(i, v) {
      $sub_amnt  = parseFloat(jQuery(v).find('.hitz_subs').val());
        if ($sub_amnt == '' || $sub_amnt == undefined || isNaN($sub_amnt)) {
          $sub_amnt = 0;
        }
      $sub_amnts += $sub_amnt;
    });
    
    var $net_amt = 0;
    $product_wapper.each(function(i, v) {
      $tmp_amt  = parseFloat(jQuery(v).find('.lhc-pkgs').val());
        if ($tmp_amt == '' || $tmp_amt == undefined || isNaN($tmp_amt)) {
          $tmp_amt = 0;
        }
      $net_amt += $tmp_amt;
    });

    var $wht_amt = 0;
    $product_wapper.each(function(i, v) {
      $tmp_amt  = parseFloat(jQuery(v).find('.lhc-weight').val());
        if ($tmp_amt == '' || $tmp_amt == undefined || isNaN($tmp_amt)) {
          $tmp_amt = 0;
        }
      $wht_amt += $tmp_amt;
    });

    var $lorry_hire = parseFloat(jQuery('#lorry_hire').val());

    if ($lorry_hire == '' || $lorry_hire == undefined || isNaN($lorry_hire)) {
       $lorry_hire = 0;
    }
    
    $total_amnt = $lorry_hire + $net_amts;
    $other_add  = $net_amts;

    // SUB
    var $adv_amnt = parseFloat(jQuery('#adv_amnt').val());

    if ($adv_amnt == '' || $adv_amnt == undefined || isNaN($adv_amnt)) {
        $adv_amnt = 0;
    }

    var $d_amnt = parseFloat(jQuery('#disel_amnt').val());

    if ($d_amnt == '' || $d_amnt == undefined || isNaN($d_amnt)) {
        $d_amnt = 0;
    }

    var $net_amnt = parseFloat(jQuery('#net_amnt').val());

    if ($net_amnt == '' || $net_amnt == undefined || isNaN($net_amnt)) {
        $net_amnt = 0;
    }   
    
    $total_de_amnt  = $sub_amnts + $d_amnt;
    /*$total_de_amnts = $adv_amnt + $sub_amnts;*/
    $other_less    = $sub_amnts;

    jQuery('#total').val($total_amnt.toFixed(2));
    jQuery('#other_add').val($other_add.toFixed(2));
    jQuery('#total_del').val($total_de_amnt.toFixed(2));
    jQuery('#other_less').val($other_less.toFixed(2));
    /*jQuery('#cash').val($total_de_amnts.toFixed(2));*/

    $balance = $total_amnt - $total_de_amnt;

    var $ca = parseFloat(jQuery('#cash').val());
    if ($ca == '' || $ca == undefined || isNaN($ca)) {
      $ca = 0;
    }

    var $bal = parseFloat(jQuery('#balance').val());
    if ($bal == '' || $bal == undefined || isNaN($bal)) {
      $bal = 0;
    }

    $rem = $adv_amnt - $ca;
    
    jQuery('#pay_mode_amnt').val($rem);
    jQuery('#balance').val($balance.toFixed(2));
    jQuery('#net_amnt').val($total_amnt.toFixed(2));
    jQuery('#total_weight').val($wht_amt.toFixed(2));
    jQuery('#total_pkg').val($net_amt.toFixed(2));
    /*document.getElementById('adv_amnt').setAttribute('max', $net_amnt);
    document.getElementById('cash').setAttribute('max', $adv_amnt);*/
  }
  
  function CheckLhc() {
    var lhc_no      = $("#lhc_no").val();
    var branch_id   = $("#branch_id").val();
    var module      = $("#module").val();
    $.ajax({
     'type': 'POST',
     'url': 'lhc/check-lhc-no.php',
     'data': {
        'lhc_no': lhc_no,
        'branch_id': branch_id,
        'module': module
     },
     dataType: 'JSON',
     success: function (result) {
        if(result.status == 0) {
           jQuery('.ajax-submit').attr('disabled', false);
        }else{
          jQuery('.ajax-submit').attr('disabled', true);
          swal("Opps !!", "ALREADY CODE EXIST...!!!", "info")  
          setTimeout("location.reload(true);",1500);
          //$('#lhc_no').val('');
        }
     }
    });
  }

  function CheckNumberSeries() {
    var lr_no      = $("#lhc_no").val();
    var branch_id  = $("#branch_id").val();
    var module     = $("#module").val();
    $.ajax({
      'type': 'POST',
      'url': 'lhc/check_number_series.php',
      'data': {
        'lr_no': lr_no,
        'branch_id': branch_id,
        'module': module
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 1) {
          jQuery('.ajax-submit').attr('disabled', false);
        }else{
          jQuery('.ajax-submit').attr('disabled', true);
          swal("Opps !!", "Not A Valid LHC Number", "info");
          setTimeout("location.reload(true);",1500);
        }
      }
    });
  }

  function CheckLr(ele){
    var lr_no = $(ele).val();
    var $tr = jQuery(ele).closest('tr');
    $.ajax({
      'type': 'POST',
      'url': 'lhc/check_lr.php',
      'data': {
        'lr_no' : lr_no,
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 0) {
          $tr.find('.lr_no').val(result.lr_no);
          $.toast({
            heading: 'Error',
            text: 'INVALID LR...',
            showHideTransition: 'fade',
            position: 'top-right',
            icon: 'error'
          })  
          $tr.find('.lr_no').focus(); 
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  }

  function CheckDuplicateRow(lr_no){
    var $container = jQuery('#product-wapper');
    var $check = true;
    jQuery.each($container.find('tbody tr'), function(i, v){
      var $tmp_data_count     = jQuery(v).find('.lr_cal').attr('data-count');
      var $tmp_bill_no        = jQuery('input[name="product['+ $tmp_data_count +'][lr_no]"]').val();
      if( $tmp_bill_no == lr_no) {
        $check = false;
        return false;
      }
    });
    if( $check ) {
            return true;
        } else {
          $.toast({
            heading: 'Error',
            text: 'ALREADY EXISTS ABOVE...',
            showHideTransition: 'fade',
            position: 'top-right',
            icon: 'error'
          })  
          return false;
        }
  }

  function CheckCashAct(ele){
    $.ajax({
      'type': 'POST',
      'url': 'lhc/check_cash_act.php',
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 0) {
          swal("Opps !!", "ACCOUNT NOT DETECT...!!!", "info")
          setTimeout("location.reload(true);",2000); 
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  }

  function CheckDiselAct(ele){
    $.ajax({
      'type': 'POST',
      'url': 'lhc/check_disel_act.php',
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 0) {
          swal("Opps !!", "ACCOUNT NOT DETECT...!!!", "info")
          setTimeout("location.reload(true);",2000); 
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  }

  function AccountAutoComplete() {
    $( ".account_name" ).autocomplete({
      position: { my: "left bottom", at: "left top", collision: "flip" },
      source: function( request, response ) {
        $.ajax({
           url: "lhc/get_account.php",
           type: 'post',
           dataType: "json",
           data: {
              search: request.term
           },
           success: function( data ) {
              response( data );
           }
        });
      },
      select: function (event, ui) {
        var $parent = jQuery(this).parents('tr');
        var $i = jQuery(this).attr('data-count');
        $checkpoint = false;
        $checkpoint = CheckDuplicateRow(ui.item.lr_no, ui.item.id);
        if( $checkpoint ) {
          $parent.find('.account_name').val(ui.item.ac_name);
          $parent.find('.account_code').val(ui.item.act_code);
          $parent.find('.account_id').val(ui.item.id);
        }else{
          var $this = jQuery(this);
          setTimeout(function(){
            $this.focus();
            $this.select();
          }, 100);
        }
        return false;
      }
    }).each(function() {
       $(this).autocomplete( 'instance' )._renderItem = function( ul, item ) {
          return $( "<li>" )
             .append( "<a><span>" + item.ac_name + '</span></a>' )
             .appendTo( ul );
        };
    });
}
function CheckDate() {
   var lhc_date      = $("#lhc_date").val();
   $.ajax({
      'type': 'POST',
      'url': 'lhc/check-date.php',
      'data': {
         'lhc_date': lhc_date
      },
      dataType: 'JSON',
      success: function (result) {
         if(result.status == 0) {
            swal("Opps !!", "Date should be between Financial Year...!!!", "info");
            setTimeout("location.reload(true);",2000);
            $('#lhc_date').val('').trigger('focus');
         }else{
            jQuery('.ajax-submit').attr('enable', true);
         }
      }
   });
}

</script>
