<?php $this->view("includes/Inc_pagehead");?>
<!-- Page Styles Goes Here-->
<style>
.item_table td {
  padding: .75rem !important;
}

.item_table_3 td {
  padding: 0rem !important;
  padding-top: .75rem !important;
  padding-bottom: 0.75rem !important;
}

.item_table th {
  white-space: nowrap;
  padding: .75rem !important;
}

.table thead th {
  border-top: 2px solid #f4f5f8 !important;
}

.payment-table tbody td {
  border-top: 2px solid #f4f5f8 !important;
}

.m-invoice-2 .m-invoice__wrapper .m-invoice__head .m-invoice__container.m-invoice__container--centered {
  width: 90% !important;
}

.m-invoice-2 .m-invoice__wrapper .m-invoice__body.m-invoice__body--centered {
  width: 90% !important;
}

<?php if ($this->config->item('Client_Payment_client_payment_invoice_template') == 3) { ?>
  .item_table .form-control {
    padding: 0.85rem 0.3rem !important;
  }
  <?php } ?>
</style>
<?php $this->view("includes/Inc_navigation");?>
<div class="m-grid__item m-grid__item--fluid m-wrapper">
  <!-- BEGIN: Subheader -->
  <div class="m-subheader ">
    <div class="d-flex align-items-center">
      <div class="mr-auto">
        <h3 class="m-subheader__title m-subheader__title--separator">
          Invoice
        </h3>
        <ul class="m-subheader__breadcrumbs m-nav m-nav--inline">
          <li class="m-nav__item m-nav__item--home">
            <a href="#" class="m-nav__link m-nav__link--icon">
              <i class="m-nav__link-icon la la-home"></i>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'dashboard'?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Home
              </span>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'invoices' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Invoice
              </span>
            </a>
          </li>
        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <form autocomplete="off" id="form_invoice_add" data-form-type="invoice_create" method="post" action="<?php echo base_url()?>invoices/update" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right " >
      <div class="m-portlet m-portlet--full-height">
        <div class="m-portlet__head">
          <div class="m-portlet__head-caption">
            <div class="m-portlet__head-title">
              <h3 class="m-portlet__head-text">

              </h3>
            </div>
          </div>
          <div class="m-portlet__head-tools" >

          </div>
        </div>
        <div class="m-portlet__body m-portlet__body--no-padding">
          <div class="m-invoice-2" style="padding-bottom: 20px">
            <div  class="m-invoice__wrapper">
              <div class="m-invoice__head" >
                <div class="m-invoice__container m-invoice__container--centered">
                  <div class="" style="padding-top: 3rem;text-align:center">
                    <a href="#">
                      <?php
                      if($invoice_type=='proforma'){
                        echo '<h4>PROFORMA INVOICE</h4>';
                      }else{
                        if($this->config->item('Client_Payment_client_payment_invoice_template')==1 || $this->config->item('Client_Payment_client_payment_invoice_template')==3 || $this->config->item('Client_Payment_client_payment_invoice_template')==4 || $this->config->item('Client_Payment_client_payment_invoice_template')==5 || $this->config->item('Client_Payment_client_payment_invoice_template')==6 || $this->config->item('Client_Payment_client_payment_invoice_template') == 7){ ?> <h4><?php echo $this->config->item('Client_Payment_client_payment_invoice_title') ?></h4> <?php
                        }
                      } ?>
                    </a>
                  </div>
                  <div class="row">
                    <div class="col-md-8" style="border-right: 1px solid #ebedf2;">
                      <span class="m-invoice__desc" style="text-align: left;padding: 1rem 0 1rem 0 !important;">
                        <div class="row">
                          <div class="col-md-12">
                            <span class="m-invoice__subtitle"><strong>INVOICE FROM</strong></span>
                            <div class="form-group m-form__group" >
                              <?php
                              $invoice_from = $invoice->invoice_from;
                              $invoice_from = str_replace('<br />','&#13;&#10;',$invoice_from);
                              $invoice_from = str_replace('<br/>','&#13;&#10;',$invoice_from);
                              $invoice_from = str_replace('<br>','&#13;&#10;',$invoice_from);
                              ?>
                              <textarea style="" rows="8" id="bill_from" name="bill_from" class="form-control m-input m-input--pill m-input--air"><?php  echo $invoice_from; ?></textarea>
                              <span class="m-form__help"></span>
                            </div>
                          </div>
                        </div>
                      </span>
                    </div>
                    <div class="col-md-4">
                      <div class="m-invoice__logo" style="padding-top: 1rem;">
                        <a href="#">
                          <!-- <h2>INVOICE #<?php echo $invoice->invoice_id?></h2> -->
                          <img src="<?= base_url()?>uploads/company/logo.png" height="80%"  style="margin-bottom: 8%;">
                        </a>
                      </div>
                    </div>
                  </div>
                  <div class="m-invoice__items" style="padding: 2rem 0 3rem 0;">
                    <div class="m-invoice__item">
                      <div class="row">
                        <div class="col-lg-4">
                          <span class="m-invoice__subtitle" >INVOICE TYPE</span>
                          <div class="form-group m-form__group" >
                            <input  type="text" value="<?php if($invoice->tax_invoice==1){ echo "TAX INVOICE";}else{ echo "NON TAX INVOICE";} ?>" readonly disabled class="form-control" >
                            <span class="m-form__help"></span>
                          </div>
                        </div>
                        <?php if($this->config->item('Quotation_rate_type')=='supply_installation'){?>
                          <div class="col-lg-4">
                            <span class="m-invoice__subtitle"><strong>Rate Type</strong></span>
                            <div class="form-group m-form__group">
                              <div class="m-radio-inline">
                                <label class="m-radio m-radio--solid m-radio--state-info">
                                  <input required type="radio" class="form_fields_radio"  name="invoice_rate_type" value="supply" > Supply
                                  <span></span>
                                </label>
                                <label class="m-radio m-radio--solid m-radio--state-focus">
                                  <input required type="radio" class="form_fields_radio"  name="invoice_rate_type" value="installation" > Installation
                                  <span></span>
                                </label>
                              </div>
                              <span class="m-form__help"></span>
                            </div>
                          </div>
                        <?php } ?>
                        <?php if($invoice_type!='proforma'){ ?>
                          <?php if ($this->config->item('Client_Payment_invoice_business_type') == 1) { ?>
                            <div class="col-lg-4 tax_invoice_fields">
                              <span class="m-invoice__subtitle" >BUSINESS TYPE</span>
                              <div class="form-group m-form__group" >
                                <input  type="text" value="<?php echo $invoice->business_type ?>" readonly disabled class="form-control" >
                                <span class="m-form__help"></span>
                              </div>
                            </div>
                          <?php } ?>
                        <?php } ?>
                      </div>
                      <div class="row">
                        <?php if($invoice_type=='other_income'){ ?>
                          <div class="col-lg-4">
                            <span class="m-invoice__subtitle">CLIENT</span>
                            <div class="form-group m-form__group">
                              <div class="input-group m-input-group  m-input-group--air">
                                <div class="input-group-prepend">
                                  <a href="#client_add_modal"   data-toggle="modal" data-target="#client_add_modal" class="btn btn-focus">
                                    <i class="fa fa-plus-circle"></i>
                                  </a>
                                </div>
                                <select style="width:88%" class="form-control m-select2 m-select2-general" required id="client" name="client">

                                </select>
                              </div>
                              <span class="m-form__help"></span>
                            </div>
                          </div>
                        <?php }else{ ?>
                          <div class="col-lg-4">
                            <span class="m-invoice__subtitle">PROJECT</span>
                            <div class="form-group m-form__group">
                              <div class="m-input-icon m-input-icon--right">
                                <div class="m-select2">
                                  <select required style="width: 100%" class="form-control m-select2 table_filter" id="work_invoice" name="work" data-placeholder="Select Project">
                                    <option value="" disabled selected>Select Project</option>
                                    <?php
                                    foreach ($work_contracts as $row) :
                                      ?>
                                      <option data-work-status="<?php echo $row->work_status ?>" value="<?php echo $row->work_id ?>"><?php echo $row->work_name ?></option>
                                      <?php
                                    endforeach;
                                    ?>
                                  </select>
                                </div>
                              </div>
                              <span class="m-form__help"></span>
                            </div>
                          </div>
                        <?php } ?>
                        <div class="col-lg-2">
                          <span class="m-invoice__subtitle" >INVOICE DATE</span>
                          <div class="form-group m-form__group" >
                            <input value="<?= date('d-m-Y',strtotime($invoice->invoice_date))?>"  data-date-format="dd-mm-yyyy" id="bill_date" required type="text" name="bill_date" class="form-control " placeholder="Bill Date" >
                            <span class="m-form__help"></span>
                          </div>
                        </div>
                        <div class="col-lg-2">
                          <span class="m-invoice__subtitle" >REFERENCE NO</span>
                          <div class="form-group m-form__group" >
                            <input  type="text" value="<?= $invoice->reference_no ?>"  id="bill_no" name="bill_no" class="form-control" placeholder="REFERENCE NO" >
                            <span class="m-form__help"></span>
                          </div>
                        </div>
                        <div class="col-lg-2 tax_invoice_fields">
                          <span class="m-invoice__subtitle"><strong>Tax Type</strong></span>
                          <div class="form-group m-form__group" >
                            <div class="m-radio-inline" >
                              <label class="m-radio m-radio--solid m-radio--state-info">
                                <input required type="radio" class="form_fields_radio form_client_tax_amount_add_fields" checked name="tax_inclusive" value="1"> Included
                                <span></span>
                              </label>
                              <label class="m-radio m-radio--solid m-radio--state-focus">
                                <input required type="radio" class="form_fields_radio form_client_tax_amount_add_fields"  name="tax_inclusive"  value="0"> Excluded
                                <span></span>
                              </label>
                            </div>
                            <span class="m-form__help"></span>
                          </div>
                        </div>
                        <?php if ($this->config->item('Client_Payment_client_payment_invoice_template') != 3 && $this->config->item('Client_Payment_client_payment_invoice_template') != 5 && $this->config->item('Client_Payment_client_payment_invoice_template') != 6 && $this->config->item('Client_Payment_client_payment_invoice_template') != 7) { ?>
                          <div class="col-lg-2 tax_invoice_fields">
                            <span class="m-invoice__subtitle"><strong><?php echo $this->config->item('Tax_tax_name')?> %</strong></span>
                            <div class="form-group m-form__group">
                              <div class="m-select2">
                                <select required id="client_payment_tax_percent_add" name="client_payment_tax_percent" style="width: 100%" class="form-control m-input form_expense_bill_add_select form_client_tax_amount_add_fields" aria-describedby="basic-addon1" data-placeholder="Select Tax Percentage">
                                  <?php
                                  $tax_percent = $this->config->item('Tax_tax_percent');
                                  foreach($tax_percent as $row):
                                    $selected_tax='';
                                    if($row->tax_percentage==0){
                                      $selected_tax='selected';
                                    }
                                    ?>
                                    <option <?php echo $selected_tax?> data-tax-percent="<?php echo $row->tax_percentage?>" value="<?php echo $row->tax_id?>"><?php echo $row->tax_name?></option>
                                    <?php
                                  endforeach;
                                  ?>
                                </select>
                              </div>
                              <span class="m-form__help"></span>
                            </div>
                          </div>
                        <?php } ?>
                        <?php if ($this->config->item('Client_Payment_client_payment_invoice_template') == 3) { ?>
                          <div class="col-lg-4">
                            <div class="form-group m-form__group" >
                              <label>Vehicle:</label>
                              <div class="input-group m-input-group ">
                                <div class="input-group-prepend">
                                  <a href="#vehicle_add_modal"   data-toggle="modal" data-target="#vehicle_add_modal" class="btn btn-focus">
                                    <i class="fa fa-plus-circle"></i>
                                  </a>
                                </div>
                                <select style="width: 82%" class="form-control m-select2 m-select2-general" id="vehicle_add" name="vehicle_id" data-placeholder="Select the vehicle" >

                                </select>
                              </div>
                              <span class="m-form__help"></span>
                            </div>
                          </div>
                        <?php } ?>
                        <?php if($invoice_type=='other_income'){ ?>
                          <div class="col-lg-4">
                            <div class="form-group m-form__group" >
                              <label>Work Status:</label>
                              <div class="input-group m-input-group ">
                                <select style="width: 100%" class="form-control m-select2 m-select2-general" id="work_status" name="work_status" data-placeholder="Select Status" >
                                  <option selected disabled>Select Status</option>
                                  <option value="2">Pending</option>
                                  <option value="1">Completed</option>
                                </select>
                              </div>
                              <span class="m-form__help"></span>
                            </div>
                          </div>
                        <?php } ?>
                      </div>
                      <div class="row">
                        <div class="col-lg-2">
                          <span class="m-invoice__subtitle">Supplier's Reference No</span>
                          <div class="form-group m-form__group">
                            <input type="text" name="suppliers_ref_no" class="form-control" value="<?php echo $invoice->supplier_reference_no ?>" placeholder="Supplier's Reference No">
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                        <div class="col-lg-2">
                          <span class="m-invoice__subtitle">Other Reference</span>
                          <div class="form-group m-form__group">
                            <input type="text" name="other_reference" class="form-control" value="<?php echo $invoice->other_reference ?>" placeholder="Other Reference">
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                        <div class="col-lg-2">
                          <span class="m-invoice__subtitle">Buyers Order No</span>
                          <div class="form-group m-form__group">
                            <input type="text" name="buyers_order_no" value="<?php echo $invoice->buyers_orderno ?>" id="buyers_order_no" class="form-control" placeholder="Buyers Order No">
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                        <div class="col-lg-2">
                          <span class="m-invoice__subtitle">Buyers Order Date</span>
                          <div class="form-group m-form__group">
                            <input type="text" name="buyers_order_date" value="<?php if($invoice->buyers_order_date!='0000-00-00'){ echo date('d-m-Y',strtotime($invoice->buyers_order_date));}?>" data-date-format="dd-mm-yyyy" id="buyers_order_date" class="form-control" placeholder="Buyers Order Date">
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                        <div class="col-lg-2">
                          <span class="m-invoice__subtitle">Quotation No</span>
                          <div class="form-group m-form__group">
                            <input type="text" name="quotation_no" value="<?php echo $invoice->quotation_no ?>" id="quotation_no" class="form-control" placeholder="Quotation No">
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                        <div class="col-lg-2">
                          <span class="m-invoice__subtitle">Quotation Date</span>
                          <div class="form-group m-form__group">
                            <input type="text" name="quotation_date" data-date-format="dd-mm-yyyy" id="quotation_date" value="<?php if($invoice->quotation_date!='0000-00-00'){ echo date('d-m-Y',strtotime($invoice->quotation_date));}?>" class="form-control" placeholder="Quotation Date">
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                        <input type="hidden" name="quotation_id" id="quotation_id" value="<?php echo $invoice->quotation_id ?>">
                        <input type="hidden" name="work_order_id" id="work_order_id" value="<?php echo $invoice->work_order_id ?>">
                      </div>
                    </div>
                  </div>
                  <div class="m-invoice__items" style="padding: 1rem 0 3rem 0;">
                    <div class="m-invoice__item">
                      <span class="m-invoice__subtitle">INVOICE TO/CUSTOMER</span>
                      <span class="m-invoice__text" id="client_name" style="margin-top: 2%;"><?php if (isset($work_data)) {
                        echo $work_data->client_name;
                      } ?></span>
                      <span class="m-invoice__text" id="client_contact"><?php if (isset($work_data)) {
                        echo $work_data->client_contact;
                      } ?></span>
                      <span class="m-invoice__text" id="client_email"><?php if (isset($work_data)) {
                        echo $work_data->client_email;
                      } ?></span>
                      <span class="m-invoice__text" id="client_address"><?php if (isset($work_data)) {
                        echo $work_data->client_address;
                      } ?></span>
                      <span class="m-invoice__text" id="client_taxno"><?php if (isset($work_data)) {
                        echo $this->config->item('Tax_tax_name') . " No: " . $work_data->tax_no;
                      } ?></span>
                      <span class="m-invoice__text" id="client_pobox" style="display:none"><?php if (isset($work_data)) {
                        echo "PO Box: " . $work_data->po_box_no;
                      } ?></span>
                    </div>
                    <div class="m-invoice__item">
                      <span class="m-invoice__subtitle">CONTACT PERSON</span>
                      <span class="m-invoice__text" id="contact_person" style="margin-top: 2%;"><?php if (isset($work_data)) {
                        echo $work_data->contact_person;
                      } ?></span>
                    </div>
                  </div>
                </div>
              </div>
              <div class="m-invoice__body m-invoice__body--centered" style="min-height: 200px">
                <div class="table-responsive">
                  <?php if ($this->config->item('Client_Payment_client_payment_invoice_template') == 1 || $this->config->item('Client_Payment_client_payment_invoice_template')==4) { ?>
                    <table class="table m-table table-bordered item_table">
                      <thead>
                        <tr>
                          <th width="7%">Sl No</th>
                          <th width="31%" style="text-align: left">Description</th>
                          <th width="10%" style="text-align: left"><?php echo $this->config->item('Client_Payment_invoice_item_code_title')?></th>
                          <th width="10%" style="text-align: left">Unit</th>
                          <th width="10%" style="text-align: left">Quantity</th>
                          <th width="10%" style="text-align: left">Price</th>
                          <th width="18%" style="text-align: left">Total Amount</th>
                          <th width="5%">&nbsp;</th>
                          <!-- <th>Item Type</th> -->
                        </tr>
                      </thead>
                      <tbody id="item_body">

                      </tbody>
                    </table>
                  <?php } elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 2) { ?>
                    <table class="table m-table table-bordered item_table">
                      <thead>
                        <tr>
                          <th width="7%">Sl No</th>
                          <th width="20%" style="text-align: left">Date</th>
                          <th width="40%" style="text-align: left">Complaint</th>
                          <th width="18%" style="text-align: left">Amount</th>
                          <th width="7%">&nbsp;</th>
                          <!-- <th>Item Type</th> -->
                        </tr>
                      </thead>
                      <tbody id="item_body">

                      </tbody>
                    </table>
                  <?php } elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 3) { ?>
                    <table class="table m-table table-bordered item_table item_table_3">
                      <thead>
                        <tr>
                          <th>SNo</th>
                          <th style="text-align: left"><?php echo $this->config->item('Client_Payment_invoice_item_code_title')?></th>
                          <th style="text-align: left;width:15%">Description</th>
                          <th style="text-align: left">HSN</th>
                          <th style="text-align: left">MRP</th>
                          <th style="display:none">Unit</th>
                          <th style="text-align: left">Trade Rate</th>
                          <th style="text-align: left">Quantity</th>
                          <th style="text-align: left">Taxable Amt</th>
                          <th style="text-align: left" class="tax_invoice_fields">Tax %</th>
                          <th style="text-align: left" class="tax_invoice_fields">Tax Amt</th>
                          <th style="text-align: left">Disc %</th>
                          <th style="text-align: left">Disc Amt</th>
                          <th style="text-align: left">Total Amount</th>
                          <th width="4%">&nbsp;</th>
                          <!-- <th>Item Type</th> -->
                        </tr>
                      </thead>
                      <tbody id="item_body">

                      </tbody>
                    </table>
                  <?php }elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 5 || $this->config->item('Client_Payment_client_payment_invoice_template') == 6 || $this->config->item('Client_Payment_client_payment_invoice_template') == 7) { ?>
                    <table class="table m-table table-bordered item_table item_table_3">
                      <thead>
                        <tr>
                          <th>SNo</th>
                          <th style="text-align: left;width:25%">Item Name/Description</th>
                          <th width="7%" style="text-align: left"><?php echo $this->config->item('Client_Payment_invoice_item_code_title')?></th>
                          <th style="text-align: left">Quantity</th>
                          <th style="text-align: left;width:8%">Unit</th>
                          <th style="text-align: left">Unit Rate</th>
                          <th class="item_discount_columns" style="text-align: left">Disc %</th>
                          <th style="text-align: left;display:none">Disc Amt</th>
                          <th style="text-align: left">Taxable Amt</th>
                          <th style="text-align: left" class="tax_invoice_fields">Tax %</th>
                          <th style="text-align: left" class="tax_invoice_fields">Tax Amt</th>
                          <th style="text-align: left">Total Amount</th>
                          <th width="4%">&nbsp;</th>
                          <!-- <th>Item Type</th> -->
                        </tr>
                      </thead>
                      <tbody id="item_body">

                      </tbody>
                    </table>
                  <?php } ?>
                  <table class="table m-table table-bordered item_table">
                    <thead>

                      <th>
                        <button type="button" style=" width: 100%; " id="btn_add_row" class="btn btn-brand"><i class="fa fa-plus-circle"></i> Add New Item</button>
                      </th>
                    </thead>
                  </table>
                  <table class="table m-table table-bordered item_table">
                    <thead>
                      <tr>
                        <?php
                        $tds_view='';
                        if($this->config->item('Client_Payment_client_payment_tds_enable')==0){
                          $tds_view='display:none';
                        }
                        ?>
                        <?php if ($this->config->item('Client_Payment_client_payment_invoice_retention_before_tax') == 1) { ?>
                          <th style=" width: 8%;text-align: left">
                            Retention %
                          </th>
                          <th style=" width: 10%;text-align: left">
                            Retention Amt
                          </th>
                        <?php } ?>
                        <th style=" width: 12%;text-align: left" class="">
                          Taxable Amt
                        </th>
                        <th style=" width: 10%;text-align: left" class="tax_invoice_fields">
                          Tax Amt
                        </th>
                        <th style=" width: 8%;text-align: left;<?php echo $tds_view?>" class="tax_invoice_fields">
                          TDS %
                        </th>
                        <th style=" width: 10%;text-align: left;<?php echo $tds_view?>" class="tax_invoice_fields">
                          TDS Amt
                        </th>
                        <?php if ($this->config->item('Client_Payment_client_payment_invoice_retention_before_tax') != 1) { ?>
                          <th style=" width: 8%;text-align: left">
                            Retention %
                          </th>
                          <th style=" width: 10%;text-align: left">
                            Retention Amt
                          </th>
                        <?php } ?>
                        <th style=" width: 10%;text-align: left">
                          Advance/<br/>Recovery Amt
                        </th>
                        <th style=" width: 10%;text-align: left">
                          Discount/<br/>Different Amt
                        </th>
                        <th style=" width: 8%;text-align: left">
                          Round Amt
                        </th>
                        <th style=" width: 15%;text-align: left">
                          Total Amt
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr>
                        <?php if ($this->config->item('Client_Payment_client_payment_invoice_retention_before_tax') == 1) { ?>
                          <td>
                            <input required type="text" required name="bill_retention_percent" id="retention_percent" value="<?php echo $invoice->invoice_retention_percent ?>" class="form-control item_rate_fields" value="0" placeholder="">
                          </td>
                          <td>
                            <input required type="text" required readonly name="bill_retention_amount" value="<?php echo $invoice->invoice_retention_amount ?>" id="retention_amount" class="form-control" value="0" placeholder="">
                          </td>
                        <?php } ?>
                        <td>
                          <input required type="text" required readonly name="bill_taxable_amount" value="<?php echo $invoice->invoice_taxable_amount ?>" id="payable_amount" class="form-control"  placeholder="">
                        </td>
                        <td class="tax_invoice_fields">
                          <input required type="text" required readonly name="bill_tax_amount" value="<?php echo $invoice->invoice_tax_amount ?>" id="tax_amount" class="form-control" placeholder="">
                        </td>
                        <td style="<?php echo $tds_view?>" class="tax_invoice_fields">
                          <input required type="text" required name="bill_tds_percent" value="<?php echo $invoice->invoice_tds_percent ?>" id="tds_percent" class="form-control item_rate_fields" value="0" placeholder="">
                        </td>
                        <td style="<?php echo $tds_view?>" class="tax_invoice_fields">
                          <input required type="text" required readonly name="bill_tds_amount" value="<?php echo $invoice->invoice_tds_amount ?>" id="tds_amount" class="form-control" value="0" placeholder="">
                        </td>
                        <?php if ($this->config->item('Client_Payment_client_payment_invoice_retention_before_tax') != 1) { ?>
                          <td>
                            <input required type="text" required name="bill_retention_percent" id="retention_percent" value="<?php echo $invoice->invoice_retention_percent ?>" class="form-control item_rate_fields" value="0" placeholder="">
                          </td>
                          <td>
                            <input required type="text" required readonly name="bill_retention_amount" value="<?php echo $invoice->invoice_retention_amount ?>" id="retention_amount" class="form-control" value="0" placeholder="">
                          </td>
                        <?php } ?>
                        <td>
                          <input required type="text" required name="bill_advance_recovery_amount" value="<?php echo $invoice->advance_recovery_amt ?>" id="advance_recovery_amount" class="form-control item_rate_fields" placeholder="">
                        </td>
                        <td>
                          <input required type="text" required name="bill_discount_amount" value="<?php echo $invoice->invoice_discount_amount ?>" <?php if ($this->config->item('Client_Payment_client_payment_invoice_template') == 3 || $this->config->item('Client_Payment_client_payment_invoice_template') == 5 || $this->config->item('Client_Payment_client_payment_invoice_template') == 6 || $this->config->item('Client_Payment_client_payment_invoice_template') == 7) { echo "readonly"; }?> id="discount_amount" class="form-control item_rate_fields" placeholder="">
                        </td>
                        <td>
                          <input required type="text" required name="bill_round_amount" value="<?php echo $invoice->invoice_round_amount ?>" id="round_amount" class="form-control item_rate_fields"  placeholder="">
                        </td>
                        <td>
                          <input required type="text" required readonly name="bill_total_amount" id="total_amount" class="form-control" value="<?php echo $invoice->invoice_total_amount ?>" placeholder="">
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </div>
                <?php if($invoice_type=='proforma'){?>
                  <div class="row">
                    <div class="col-lg-4">
                      <div class="form-group m-form__group">
                        <label style="font-weight:900">Advance Claiming % (On Invoice Amt)</label>
                        <input type="text" required   name="advance_claim_percent" id="advance_claim_percent" value="<?php echo $invoice->claiming_advance_percentage ?>" class="form-control" >
                      </div>
                    </div>
                    <div class="col-lg-4">
                      <div class="form-group m-form__group">
                        <label style="font-weight:900">Advance Claiming Amount</label>
                        <input type="text" readonly name="advance_claim_amount" id="advance_claim_amount" class="form-control" value="<?php echo $invoice->claiming_advance_amount ?>">
                      </div>
                    </div>
                  </div>
                  <br/>
                <?php } ?>
                <div class="row">
                  <div class="col-lg-2">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Delivery Note Date</label>
                      <input type="text" name="delivery_note_date" value="<?php if($invoice->delivery_note_date!='0000-00-00'){ echo date('d-m-Y',strtotime($invoice->delivery_note_date));}?>" data-date-format="dd-mm-yyyy" id="delivery_note_date" class="form-control" placeholder="Delivery Note Date">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                  <div class="col-lg-2">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Delivery Date</label>
                      <input type="text" data-date-format="dd-mm-yyyy" value="<?php if($invoice->delivery_date!='0000-00-00'){ echo date('d-m-Y',strtotime($invoice->delivery_date));}?>" name="delivery_date" class="form-control" id="delivery_date" placeholder="Delivery Date">
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-2">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Despatch Document No.</label>
                      <input type="text"  name="despatch_doc_no" class="form-control" value="<?php echo $invoice->despatch_doc_no ?>" placeholder="Despatch Document No.">
                    </div>
                  </div>
                  <div class="col-lg-2">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Despatched Through</label>
                      <input type="text"  name="despatched_through" class="form-control" value="<?php echo $invoice->despatched_through ?>" placeholder="Despatched Through">
                    </div>
                  </div>
                  <div class="col-lg-2">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Shipping Name</label>
                      <input type="text"  name="shipping_name" id="shipping_name" value="<?php echo $invoice->shipping_name ?>" class="form-control" placeholder="Shipping Name">
                    </div>
                  </div>
                  <div class="col-lg-2">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Destination</label>
                      <input type="text"  name="destination" class="form-control" value="<?php echo $invoice->destination ?>" placeholder="Destination">
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-lg-3">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Mode/Terms of Payment</label>
                      <textarea rows="2"  name="payment_terms" class="form-control" placeholder="Mode/Terms of Payment"><?php echo $invoice->payment_terms ?></textarea>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Delivery Notes/Reference</label>
                      <textarea rows="2"  name="delivery_note" class="form-control" placeholder="Delivery Notes"><?php echo $invoice->delivery_note ?></textarea>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Terms of Delivery</label>
                      <textarea rows="2"  name="delivery_terms" class="form-control" placeholder="Terms of Delivery"><?php echo $invoice->delivery_terms ?></textarea>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Shipping Address</label>
                      <textarea rows="2"  name="shipping_address" id="shipping_address" class="form-control" placeholder="Shipping Address"><?php echo $invoice->shipping_address ?></textarea>
                    </div>
                  </div>

                </div>

                <div class="row">
                  <div class="col-lg-9">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Invoice Notes</label>
                      <textarea class="summernote" name="invoice_notes" id="invoice_notes" ></textarea>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group">
                      <label style="font-weight:900">Works Carried At</label>
                      <input type="text"  name="works_carried_at" value="<?php echo $invoice->works_carried_at ?>" class="form-control" placeholder="Works Carried At">
                    </div>
                    <div class="form-group m-form__group">
                      <label>
                        Bank Account (Display)
                      </label>
                      <div class="m-select2 m-select2--air">
                        <select style="width: 100%" class="form-control m-select2" id="bank_account_add" name="bank_account"  >
                          <option selected disabled> Select Bank Account</option>
                          <?php foreach ($bank_accounts as $key => $value) {?>
                            <option value="<?php echo $value->account_id?>"><?php echo $value->company_name?> [ <?php echo $value->bank_name."-".$value->bank_branch?> ]</option>
                          <?php } ?>
                        </select>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                    <div class="form-group m-form__group m-select2--pill" style="padding-top:0px">
                      <label for="user_name">
                        Prepared By
                      </label>
                      <div class="m-select2 m-select2--air ">
                        <select style="width: 100%" class="form-control m-select2 m-select2-general" name="prepared_by" id="prepared_by">
                          <option selected disabled> Select Prepared By</option>
                          <option value="0">N/A</option>
                          <?php foreach ($users as $key => $value) { ?>
                            <option value="<?php echo $value->user_id ?>"><?php echo $value->user_alias ?></option>
                          <?php } ?>
                        </select>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <input type="hidden" name="current_row_count" id="current_row_count" value="0">
              <input type="hidden" name="invoice_id" id="invoice_id" value="<?php echo $invoice->invoice_id?>">
              <input type="hidden" name="invoice_type"  value="<?php echo $invoice->invoice_type?>">
              <input type="hidden"  id="deleted_items" name="deleted_items">
              <input type="hidden" id="items_count"  >
              <input type="hidden" name="reference_id" id="reference_id_add_client_payment" value="<?php if($page_name=='work_profile' || $page_name=='projects_profile'){ echo $work_data->work_id; }?>">
            </div>
          </div>
        </div>
        <div class="m-portlet__foot m-portlet__foot--fit">
          <div class="m-form__actions m-form__actions">
            <div class="row">
              <div class="col-lg-3 ml-lg-auto" style="border-right: 1px solid #ebedf2;">
                <button type="submit" class="btn btn-primary form_submit_btn_3">
                  Save & Submit
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </form>
    <?php //$this->view("modules/include_modals/modal_bills");?>
    <?php $this->view("modules/include_modals/modal_client_add");?>
  </div>
</div>
</div>


<!-- end:: Body -->
<!-- begin::Footer -->

<?php $this->view("includes/Inc_footer");?>
<!-- end::Footer -->

<!-- end:: Page -->

<?php $this->view("includes/Inc_sidebar");?>
<?php $this->view("includes/Inc_scripts");?>
<?php $this->view("modules/include_scripts/vehicles_scripts");?>
<!--begin::Page Snippets -->
<?php //$this->view("modules/include_scripts/bill_scripts");?>
<script>
//    Toggle Sidebar
$("#m_aside_left_minimize_toggle").addClass("m-brand__toggler--active");
$("#body").addClass("m-brand--minimize m-aside-left--minimize");

$("#state_id_add").select2({
  placeholder: "Select State Code",
  dropdownParent: $("#form_client_add")
})
$("#prepared_by").select2({
  placeholder: "Select Prepared By"
})
$('#prepared_by').val('<?= $invoice->prepared_by?>').trigger('change');
$("#bill_date,#paid_date,#reference_date_add,#delivery_date,#delivery_note_date,#quotation_date,#buyers_order_date").datepicker( {
  todayHighlight:!0, orientation:"bottom left", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  }
})
$("#bank_account_add").select2({
  placeholder: "Select Bak Account"
})
$("#bank_account_add").val("<?php echo $invoice->bank_account ?>").trigger('change');


<?php if($invoice_type=='other_income'){?>
  $("#other_income_menu").addClass('m-menu__item--active m-menu__item--open');
  $("#other_income_menu_1").addClass('m-menu__item--active');

  $("#client").select2({
    placeholder: "Select Client",
    dropdownParent: $("#form_invoice_add")
  })
  $("#work_status").select2({
    placeholder: "Select Status",
    dropdownParent: $("#form_invoice_add")
  })
  $('#work_status').val('<?= $invoice->project_status?>').trigger('change');
  $('body').on('change', '#client', function() {
    fetch_client_details();
  })
  function fetch_client_details() {
    var client_id = $('#client').val();
    if(client_id!='' && client_id!=null && client_id!='Select Client'){
      $.post("<?php echo base_url() ?>clients/select_details", {
        client_id: client_id
      }, function(data) {
        var obj = $.parseJSON(data);
        if (obj.count == 1) {
          $('#client_name').html(obj.data['client_name']);
          $('#client_contact').html(obj.data['client_contact']);
          $('#client_address').html(obj.data['client_address']);
          $('#client_taxno').html("<?php echo $this->config->item('Tax_tax_name') ?> No: " + obj.data['tax_no']);
          $('#reference_id_add_client_payment').val(obj.data['client_id']);
        } else {
          $('#client_name').html('');
          $('#client_contact').html('');
          $('#client_address').html('');
          $('#client_taxno').html('');
          $('#reference_id_add_client_payment').val('');
        }

      })
    }
  }
  select_client();
  function select_client(){
    $('#client').html('');
    $("#client").append("<option disabled selected >Select Client</option>");
    $.post("<?php echo base_url() ?>clients/select",{},function(data) {
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count>0) {
        while (i < obj.id.length) {
          $("#client").append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
          i++;
        }
        $('#client').val('<?= $invoice->client_id?>').trigger('change');
      }
    })
  }
  <?php }else{ ?>
    <?php if($invoice_type=='proforma'){?>
      $("#proforma_invoices_menu").addClass('m-menu__item--active m-menu__item--open');
      <?php }else{?>
        $("#client_payments_menu").addClass('m-menu__item--active m-menu__item--open');
        $("#client_payments_menu_1").addClass('m-menu__item--active');
        <?php } ?>
        $('body').on('change', '#work_invoice', function() {
          fetch_work_details();
        })
        function fetch_work_details() {
          $('#client_pobox').hide();
          var work_id = $('#work_invoice').val()
          $.post("<?php echo base_url() ?>work/select_details", {
            work_id: work_id
          }, function(data) {
            var obj = $.parseJSON(data);
            if (obj.count == 1) {
              $('#client_name').html(obj.data['client_name']);
              $('#client_contact').html(obj.data['client_contact']);
              $('#client_address').html(obj.data['client_address']);
              $('#client_taxno').html("<?php echo $this->config->item('Tax_tax_name') ?> No: " + obj.data['client_taxno']);
              $('#reference_id_add_client_payment').val(obj.data['client_name']);
              $('#client_pobox').html('PO Box : '+ obj.data['po_box_no']);
              if(obj.data['po_box_no']!=''){
                $('#client_pobox').show();
              }
              $('#contact_person').html(obj.data['contact_person']);
              // $('#shipping_name').val(obj.data['work_name']);
              // $('#shipping_address').val(obj.data['work_location']);
            } else {
              $('#client_name').html('');
              $('#client_contact').html('');
              $('#client_address').html('');
              $('#client_taxno').html('');
              $('#reference_id_add_client_payment').val('');
              $('#client_pobox').html('');
              $('#contact_person').html('');
              $('#shipping_name').val('');
              $('#shipping_address').val('');
            }
            <?php if ($this->config->item('Client_Invoice_work_order_connected') == 1){?>
              fetch_work_order_items(1);
              <?php } ?>
          })
        }
        $("#work_invoice").select2({
          placeholder: "Select  Work",
          templateResult: format_project_dropdown
        })
        $('#work_invoice').val('<?= $invoice->project_id?>').trigger('change');
        <?php } ?>
        <?php if($this->config->item('Quotation_rate_type')=='supply_installation'){?>
          <?php } ?>
          $("#vehicle_add").select2({
            placeholder: "Select vehicle",
            // dropdownParent: $("#form_vehicle_work_slip_add")
          })
          function select_vehicles(select_id,selected_value='',category='',vehicle_type=''){
            $('#'+select_id).html('');
            $('#'+select_id).append("<option disabled selected >Select Vehicle</option>");

            $.post("<?php echo base_url() ?>vehicles/select_vehicles_list",{category:category,vehicle_type:vehicle_type},function(data) {
              var obj = $.parseJSON(data);
              var i=0;
              if(obj.count>0) {
                while (i < obj.data.length) {
                  $('#'+select_id).append("<option  value=" + obj.data[i]['id'] + ">" + obj.data[i]['vehicle_name'] + " : "+obj.data[i]['vehicle_no']+" [" + obj.data[i]['consignee_name'] + "]</option>");
                  i++;
                }
                if(selected_value!=''){
                  $("#"+select_id).val(selected_value).trigger("change");
                }
              }
            })
          }
          select_vehicles("vehicle_add","<?php echo $invoice->vehicle_id ?>");

          <?php if ($this->config->item('Client_Payment_client_payment_invoice_template') == 1 || $this->config->item('Client_Payment_client_payment_invoice_template')==4) { ?>
            $("#btn_add_row").on("click", function() {
              var counter = parseInt($("#current_row_count").val());
              var newRow = '<tr class="row_' + (counter) + '"><td>' + (counter + 1) + '</td>';
              newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon m-input-icon--right"><input  type="text" name="item_name[' + counter + ']" required id="item_name_' + counter + '" class="form-control" data-sl-no="' + (counter + 1) + '" placeholder="Description" ></div></div></td>';
              newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_code[' + counter + ']" class="form-control" id="item_code_' + (counter + 1) + '" data-sl-no="' + (counter + 1) + '" placeholder="<?php echo $this->config->item('Client_Payment_invoice_item_code_title')?>" ></div></div></td>';
              newRow += '<td><div class="form-group m-form__group"><select required  style="width: 100%" class="form-control m-bootstrap-select m_selectpicker m-bootstrap-select--air" id="item_unit_' + counter + '" data-width="100%"  name="item_unit[' + (counter) + ']"></select></div></td>';
              newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_qty[' + counter + ']" required class="form-control item_rate_fields" id="item_qty_' + (counter + 1) + '" data-sl-no="' + (counter + 1) + '" placeholder="Quantity" ></div></div></td>';
              newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_price[' + counter + ']" required id="item_price_' + (counter + 1) + '" class="form-control item_rate_fields" data-sl-no="' + (counter + 1) + '" placeholder="Price" ></div></div></td>';
              newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_total[' + counter + ']" required readonly id="item_total_' + (counter + 1) + '" class="form-control item_amount_field" data-sl-no="' + (counter + 1) + '" placeholder="Total Amount" ></div></div></td>';
              newRow += '<td><a href="javascript:" data-row-sl-no=' + (counter) + ' class="ibtnDel btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
              newRow += '</tr>';
              $("#item_body").append(newRow);
              $('#item_unit_' + counter).selectpicker('refresh');
              select_units('item_unit_' + counter);

              // $("#item_unit_"+counter).select2({
              //   placeholder: "Select Unit"
              // })
              counter++;
              $("#current_row_count").val(counter);

            });
            <?php } elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 2) { ?>
              $(".item_date_fields").datepicker({
                todayHighlight: !0,
                orientation: "bottom left",
                templates: {
                  leftArrow: '<i class="la la-angle-left"></i>',
                  rightArrow: '<i class="la la-angle-right"></i>'
                }
              })
              $("#btn_add_row").on("click", function() {
                var counter = parseInt($("#current_row_count").val());
                var newRow = '<tr class="row_' + (counter) + '"><td>' + (counter + 1) + '</td>';
                newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon m-input-icon--right"><input  type="text" name="item_date[' + counter + ']" required id="item_date_' + counter + '" data-date-format="dd-mm-yyyy" class="form-control item_date_fields" data-sl-no="' + (counter + 1) + '" placeholder="Date" ></div></div></td>';
                newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_description[' + counter + ']" required class="form-control item_rate_fields" id="item_description_' + (counter + 1) + '" data-sl-no="' + (counter + 1) + '" placeholder="Complaint" ></div></div></td>';
                newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_total[' + counter + ']" required  id="item_total_' + (counter + 1) + '" class="form-control item_amount_field item_rate_fields" data-sl-no="' + (counter + 1) + '" placeholder="Amount" ></div></div></td>';
                newRow += '<td><a href="javascript:" data-row-sl-no=' + (counter) + ' class="ibtnDel btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
                newRow += '</tr>';
                $("#item_body").append(newRow);
                counter++;
                $("#current_row_count").val(counter);

                $(".item_date_fields").datepicker({
                  todayHighlight: !0,
                  orientation: "bottom left",
                  templates: {
                    leftArrow: '<i class="la la-angle-left"></i>',
                    rightArrow: '<i class="la la-angle-right"></i>'
                  }
                })
              });
              <?php }
              if ($this->config->item('Client_Payment_client_payment_invoice_template') == 3) { ?>
                $("#item_tax_percent_1").select2({
                  placeholder: "Select  Tax",
                  dropdownAutoWidth: true
                })
                $("#btn_add_row").on("click", function() {
                  var counter = parseInt($("#current_row_count").val());
                  var newRow = '<tr class="row_' + (counter) + '"><td style="padding-left: 12px !important;">' + (counter + 1) + '</td>';
                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon m-input-icon--right"><input  type="text" name="item_code[' + counter + ']" required id="item_code_' + counter + '" class="form-control" data-sl-no="' + (counter + 1) + '" placeholder="<?php echo $this->config->item('Client_Payment_invoice_item_code_title')?>" ></div></div></td>';
                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon m-input-icon--right"><input  type="text" name="item_name[' + counter + ']" required id="item_name_' + counter + '" class="form-control" data-sl-no="' + (counter + 1) + '" placeholder="Description" ></div></div></td>';
                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon m-input-icon--right"><input  type="text" name="item_hsn[' + counter + ']" required id="item_hsn_' + counter + '" class="form-control" data-sl-no="' + (counter + 1) + '" placeholder="HSN" ></div></div></td>';
                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon m-input-icon--right"><input  type="text" name="item_mrp[' + counter + ']" required id="item_mrp_' + counter + '" class="form-control" data-sl-no="' + (counter + 1) + '" placeholder="MRP" ></div></div></td>';
                  newRow += '<td style="display:none"><div class="form-group m-form__group"><select required  style="width: 100%" class="form-control m-bootstrap-select m_selectpicker m-bootstrap-select--air" id="item_unit_' + counter + '" data-width="100%"  name="item_unit[' + (counter) + ']"></select></div></td>';
                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_price[' + counter + ']" required id="item_price_' + (counter + 1) + '" class="form-control item_rate_fields" data-sl-no="' + (counter + 1) + '" placeholder="Rate" ></div></div></td>';
                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_qty[' + counter + ']" required class="form-control item_rate_fields" id="item_qty_' + (counter + 1) + '" data-sl-no="' + (counter + 1) + '" placeholder="Qty" ></div></div></td>';
                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_taxable_amt[' + counter + ']" required readonly id="item_taxable_amt_' + (counter + 1) + '" class="form-control item_taxable_amount_field" data-sl-no="' + (counter + 1) + '" placeholder="Taxable Amt" ></div></div></td>';
                  newRow += '<td class="tax_invoice_fields"><div class="form-group m-form__group"> <div class="m-select2">' +
                  '<select required name="item_tax_percent[' + counter + ']" readonly id="item_tax_percent_' + (counter + 1) + '" style="width: 100%" class="form-control m-input item_rate_fields"  data-placeholder="Select Tax Percentage" data-sl-no="' + (counter + 1) + '">' +
                  <?php $tax_percent = $this->config->item('Tax_tax_percent');
                  foreach ($tax_percent as $row) :
                    $selected_tax = "";
                    if ($row->tax_percentage == 0) {
                      $selected_tax = "selected";
                    } ?> '<option <?php echo $selected_tax ?> data-tax-percent="<?php echo $row->tax_percentage ?>" value="<?php echo $row->tax_id ?>"><?php echo $row->tax_name ?></option>' +
                    <?php endforeach; ?> '</select></div></div></td>';
                    newRow += '<td style="text-align: left" class="tax_invoice_fields"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_tax_amt[' + counter + ']" required readonly id="item_tax_amt_' + (counter + 1) + '" class="form-control item_tax_amount_field" data-sl-no="' + (counter + 1) + '" placeholder="Tax Amt" ></div></div></td>';
                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_disc_percent[' + counter + ']" required id="item_disc_percent_' + (counter + 1) + '" class="form-control item_rate_fields" data-sl-no="' + (counter + 1) + '" placeholder="Disc %" ></div></div></td>';
                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_disc_amt[' + counter + ']" required readonly id="item_disc_amt_' + (counter + 1) + '" class="form-control item_discount_amount_field" data-sl-no="' + (counter + 1) + '" placeholder="Disc Amt" ></div></div></td>';
                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_total[' + counter + ']" required readonly id="item_total_' + (counter + 1) + '" class="form-control item_amount_field" data-sl-no="' + (counter + 1) + '" placeholder="Total Amount" ></div></div></td>';
                    newRow += '<td style="text-align:center"><a href="javascript:" data-row-sl-no=' + (counter) + ' class="ibtnDel btn-sm btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
                    newRow += '</tr>';
                    $("#item_body").append(newRow);
                    $('#item_unit_' + counter).selectpicker('refresh');
                    $('#item_tax_percent_' + (counter + 1)).select2({
                      placeholder: "Select  Tax",
                      dropdownAutoWidth: true
                    })
                    select_units('item_unit_' + counter);

                    // $("#item_unit_"+counter).select2({
                    //   placeholder: "Select Unit"
                    // })
                    counter++;
                    $("#current_row_count").val(counter);

                  });
                  <?php } if ($this->config->item('Client_Payment_client_payment_invoice_template') == 5  || $this->config->item('Client_Payment_client_payment_invoice_template') == 6 || $this->config->item('Client_Payment_client_payment_invoice_template') == 7) { ?>
                    $("#item_tax_percent_1").select2({
                      placeholder: "Select  Tax",
                      dropdownAutoWidth: true
                    })
                    $("#btn_add_row").on("click", function() {
                      var work_id = $('#work_invoice').val();
                      <?php if($invoice_type!='other_income'){ ?>
                        if(work_id!='' && work_id!=null){
                          <?php } ?>
                          var counter = parseInt($("#current_row_count").val());
                          var newRow = '';
                          <?php if ($this->config->item('Client_Invoice_work_order_connected') == 1){?>
                            newRow += '<tr class="row_' + (counter) + '"><td></td>';
                            newRow += '<td colspan="7"><div class="form-group m-form__group">';
                            newRow += '<select style="width: 100%" data-width="100%" class="m-bootstrap-select m_selectpicker m-bootstrap-select--air work_order_task_select" data-sl-no="' + (counter + 1) + '" required id="item_work_order_task_' + (counter + 1) + '" name="item_work_order_task[' + counter + ']"></select></div></td>';
                            newRow += '<td class="tax_invoice_fields"></td>';
                            newRow += '<td class="tax_invoice_fields"></td>';
                            newRow += '<td></td><td></td></tr>';
                            <?php } ?>
                            newRow += '<tr class="row_' + (counter) + '"><td style="padding-left: 12px !important;">' + (counter + 1) + '</td>';
                            newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon m-input-icon--right"><textarea name="item_name[' + counter + ']" required id="item_name_' + (counter + 1) + '" class="form-control" data-sl-no="' + (counter + 1) + '" placeholder="Item Name/Description" ></textarea></div></div></td>';
                            newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_code[' + counter + ']" class="form-control" id="item_code_' + (counter + 1) + '" data-sl-no="' + (counter + 1) + '" placeholder="<?php echo $this->config->item('Client_Payment_invoice_item_code_title')?>" ></div></div></td>';
                            newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_qty[' + counter + ']" required class="form-control item_rate_fields" id="item_qty_' + (counter + 1) + '" data-sl-no="' + (counter + 1) + '" placeholder="Qty" ></div></div></td>';
                            newRow += '<td><div class="form-group m-form__group"><select required  style="width: 100%" class="form-control m-bootstrap-select m_selectpicker m-bootstrap-select--air" id="item_unit_' + (counter + 1) + '" data-width="100%"  name="item_unit[' + (counter) + ']"></select></div></td>';
                            newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_price[' + counter + ']" required id="item_price_' + (counter + 1) + '" class="form-control item_rate_fields" data-sl-no="' + (counter + 1) + '" placeholder="Unit Rate" ></div></div></td>';
                            newRow += '<td style="text-align: left" class="item_discount_columns"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_disc_percent[' + counter + ']" required id="item_disc_percent_' + (counter + 1) + '" value="0"  class="form-control item_rate_fields item_discount_percent_field" data-sl-no="' + (counter + 1) + '" placeholder="Disc %" ></div></div></td>';
                            newRow += '<td style="text-align: left;display:none"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_disc_amt[' + counter + ']" value="0" required readonly id="item_disc_amt_' + (counter + 1) + '" class="form-control item_discount_amount_field" data-sl-no="' + (counter + 1) + '" placeholder="Disc Amt" ></div></div></td>';
                            newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_taxable_amt[' + counter + ']" required readonly id="item_taxable_amt_' + (counter + 1) + '" class="form-control item_taxable_amount_field" data-sl-no="' + (counter + 1) + '" placeholder="Taxable Amt" ></div></div></td>';
                            newRow += '<td class="tax_invoice_fields"><div class="form-group m-form__group"> <div class="m-select2">' +
                            '<select required name="item_tax_percent[' + counter + ']" readonly id="item_tax_percent_' + (counter + 1) + '" style="width: 100%" class="form-control m-input item_rate_fields"  data-placeholder="Select Tax Percentage" data-sl-no="' + (counter + 1) + '">' +
                            <?php $tax_percent = $this->config->item('Tax_tax_percent');
                            foreach ($tax_percent as $row) :
                              $selected_tax = "";
                              if ($row->tax_percentage == 0) {
                                $selected_tax = "selected";
                              } ?> '<option <?php echo $selected_tax ?> data-tax-percent="<?php echo $row->tax_percentage ?>" value="<?php echo $row->tax_id ?>"><?php echo $row->tax_name ?></option>' +
                              <?php endforeach; ?> '</select></div></div></td>';
                              newRow += '<td style="text-align: left" class="tax_invoice_fields"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_tax_amt[' + counter + ']" required readonly id="item_tax_amt_' + (counter + 1) + '" class="form-control item_tax_amount_field" data-sl-no="' + (counter + 1) + '" placeholder="Tax Amt" ></div></div></td>';
                              newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_total[' + counter + ']" required readonly id="item_total_' + (counter + 1) + '" class="form-control item_amount_field" data-sl-no="' + (counter + 1) + '" placeholder="Total Amount" ></div></div></td>';
                              newRow += '<td style="text-align:center"><a href="javascript:" data-row-sl-no=' + (counter) + ' class="ibtnDel  btn btn-danger btn-sm btn-icon-only m-btn--pill m-btn--air" ><i class="fa fa-trash"></i></td>';
                              newRow += '</tr>';
                              $("#item_body").append(newRow);
                              $('#item_unit_' + (counter + 1)).selectpicker('refresh');
                              $('#item_tax_percent_' + (counter + 1)).select2({
                                placeholder: "Select  Tax",
                                dropdownAutoWidth: true
                              })
                              select_units('item_unit_' + (counter + 1));
                              <?php if ($this->config->item('Client_Invoice_work_order_connected') == 1){?>
                                fetch_work_order_items(counter+1);
                                <?php } ?>

                                // $("#item_unit_"+(counter + 1)).select2({
                                //   placeholder: "Select Unit"
                                // })
                                counter++;
                                $("#current_row_count").val(counter);

                                $('input[type=radio][name=invoice_tax_type]').trigger("change");
                                <?php if($invoice_type!='other_income'){ ?>
                                }else{
                                  toastr['warning']('Please Select Project', 'Select Project');
                                }
                                <?php } ?>
                                var invoice_tax_type = "<?php echo $invoice->tax_invoice ?>";
                                if(invoice_tax_type==1){
                                  $(".tax_invoice_fields").show();
                                }else{
                                  $(".tax_invoice_fields").hide();
                                }
                              });
                              <?php } ?>
                              $("#item_body").on("click", ".ibtnDel", function (event) {
                                var row_sl_no = $(this).data('row-sl-no');
                                var counter = parseInt($("#current_row_count").val());
                                $('.row_'+row_sl_no).remove();
                                counter -= 1;
                                $("#current_row_count").val(counter);

                                var items_count =  parseInt($("#items_count").val());
                                items_count -= 1;
                                $("#items_count").val(items_count)

                                var deleted_sl_no = $(this).attr('data-sl-no');
                                var deleted_item_id = $(this).attr('data-item-id');

                                // Check Item Id Exist, If Exist, Store it in Deleted  Item Array
                                if(deleted_item_id!='' && deleted_item_id != null){
                                  var value = $("#deleted_items").val();
                                  value += deleted_item_id+",";
                                  $("#deleted_items").val(value);
                                }

                                //rearrange higher sl no
                                var new_sl_no = 0;
                                $(".item_slno").each(function () {
                                  new_sl_no++;
                                  $(this).val(new_sl_no);
                                })
                                calculate_total_amount();
                              });


                              $('body').on('change keyup', '.item_rate_fields', function() {
                                var sl_no = $(this).attr('data-sl-no');
                                <?php if ($this->config->item('Client_Payment_client_payment_invoice_template') == 1 || $this->config->item('Client_Payment_client_payment_invoice_template')==4) { ?>
                                  var qty = parseFloat($("#item_qty_" + sl_no).val());
                                  var price = parseFloat($("#item_price_" + sl_no).val());
                                  if (isNaN(qty)) {
                                    var qty = 0;
                                  }
                                  if (isNaN(price)) {
                                    var price = 0;
                                  }
                                  var total_amount = (qty * price).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>);
                                  $("#item_total_" + sl_no).val(total_amount);
                                  <?php } elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 3 ||$this->config->item('Client_Payment_client_payment_invoice_template') == 5 || $this->config->item('Client_Payment_client_payment_invoice_template') == 6 || $this->config->item('Client_Payment_client_payment_invoice_template') == 7) { ?>
                                    var tax_inclusive = $('input[type=radio][name=tax_inclusive]:checked').val();
                                    var qty = parseFloat($("#item_qty_" + sl_no).val());
                                    var price = parseFloat($("#item_price_" + sl_no).val());
                                    if (isNaN(qty)) {
                                      var qty = 0;
                                    }
                                    if (isNaN(price)) {
                                      var price = 0;
                                    }

                                    var tax_percent = parseFloat($('#item_tax_percent_' + sl_no + ' :selected').data('tax-percent'));
                                    var discount_percent = parseFloat($("#item_disc_percent_" + sl_no).val());
                                    if (isNaN(tax_percent)) {
                                      var tax_percent = 0;
                                    }
                                    if (isNaN(discount_percent)) {
                                      var discount_percent = 0;
                                    }
                                    if (tax_inclusive == 1) {
                                      var discount_amount = 0;
                                      var taxable_amount = (qty * price) * 100 / (tax_percent + 100);
                                      var tax_amount = (qty * price) - taxable_amount;
                                    }else{
                                      var sub_total = (qty * price);
                                      var discount_amount = sub_total * discount_percent / 100;
                                      var taxable_amount = sub_total - discount_amount;
                                      var tax_amount = taxable_amount * tax_percent / 100;
                                    }
                                    $("#item_taxable_amt_" + sl_no).val(taxable_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                    var total_amount = taxable_amount + tax_amount;
                                    $("#item_disc_amt_" + sl_no).val(discount_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                    $("#item_tax_amt_" + sl_no).val(tax_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                    $("#item_total_" + sl_no).val(total_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                    <?php } ?>
                                    calculate_total_amount();
                                  })

                                  $('body').on('change keyup', '#client_payment_received_amount_add', function() {
                                    var amount = $(this).val();
                                    if(amount>0){
                                      $('.payment_fields').show();
                                      $('.payment_fields_input').attr('required','required');
                                    }
                                    else{
                                      $('.payment_fields').hide();
                                      $('.payment_fields_input').removeAttr('required');
                                    }
                                  });

                                  function select_units(select_id,selected_value=''){
                                    $('#'+select_id).html('');
                                    $('#'+select_id).append("<option disabled selected >Select Unit</option>");
                                    $.post("<?php echo base_url() ?>units/select",{},function(data) {
                                      var obj = $.parseJSON(data);
                                      var i=0;
                                      if(obj.count>0) {
                                        while (i < obj.id.length) {
                                          $('#'+select_id).append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
                                          i++;
                                        }
                                        if(selected_value!=''){
                                          $('#'+select_id).val(selected_value);
                                        }else{
                                          $('#'+select_id).val(3);
                                        }
                                        $('#'+select_id).selectpicker("refresh");
                                        // $('#'+select_id).val(3).trigger('change');
                                      }
                                    })
                                  }
                                  $('body').on('change keyup', '#advance_claim_percent', function() {
                                    var advance_claim_percent = parseFloat($("#advance_claim_percent").val());
                                    var total_amount = parseFloat($("#total_amount").val());
                                    if (isNaN(advance_claim_percent)) {
                                      var advance_claim_percent = 0;
                                    }
                                    if (isNaN(taxable_amount)) {
                                      var taxable_amount = 0;
                                    }
                                    var advance_claim_amount = total_amount*advance_claim_percent/100;
                                    $("#advance_claim_amount").val(advance_claim_amount.toFixed(2));
                                  })
                                  function calculate_total_amount() {
                                    var total_amount = discount_amount = tax_amount = taxable_amount = 0;
                                    $(".item_amount_field").each(function() {
                                      if ($(this).val() != '' && $(this).val() != null) {
                                        total_amount += parseFloat($(this).val());
                                      }
                                    });
                                    $("#payable_amount").val(total_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                    var round_amount = parseFloat($('#round_amount').val());
                                    if (isNaN(round_amount)) {
                                      var round_amount = 0;
                                    }
                                    if (isNaN(total_amount)) {
                                      var total_amount = 0;
                                    }
                                    <?php if ($this->config->item('Client_Payment_client_payment_invoice_template') == 3 || $this->config->item('Client_Payment_client_payment_invoice_template') == 5  || $this->config->item('Client_Payment_client_payment_invoice_template') == 6 || $this->config->item('Client_Payment_client_payment_invoice_template') == 7) { ?>
                                      $(".item_taxable_amount_field").each(function() {
                                        if ($(this).val() != '' && $(this).val() != null) {
                                          taxable_amount += parseFloat($(this).val());
                                        }
                                      });
                                      $(".item_tax_amount_field").each(function() {
                                        if ($(this).val() != '' && $(this).val() != null) {
                                          tax_amount += parseFloat($(this).val());
                                        }
                                      });
                                      $(".item_discount_amount_field").each(function() {
                                        if ($(this).val() != '' && $(this).val() != null) {
                                          discount_amount += parseFloat($(this).val());
                                        }
                                      });
                                      $("#payable_amount").val(taxable_amount);
                                      $('#tax_amount').val(tax_amount);
                                      $('#discount_amount').val(discount_amount);
                                      var advance_recovery_amount = parseFloat($('#advance_recovery_amount').val());
                                      var tds_percent = parseFloat($('#tds_percent').val());
                                      var retention_percent = parseFloat($('#retention_percent').val());
                                      var retention_type = "<?php echo $this->config->item('Client_Payment_client_payment_invoice_retention_type')?>";
                                      var retention_before_tax = "<?php echo $this->config->item('Client_Payment_client_payment_invoice_retention_before_tax')?>";
                                      if (isNaN(tds_percent)) {
                                        var tds_percent = 0;
                                      }
                                      if (isNaN(retention_percent)) {
                                        var retention_percent = 0;
                                      }
                                      if(retention_before_tax==1){
                                        var retention_amount  = taxable_amount * retention_percent / 100;
                                        if(retention_type=='deduction'){
                                          taxable_amount -= retention_amount;
                                        }else{
                                          taxable_amount += retention_amount;
                                        }
                                      }
                                      var tds_amount  = taxable_amount * tds_percent / 100;
                                      if(retention_before_tax!=1){
                                        var retention_amount  = taxable_amount * retention_percent / 100;
                                        if(retention_type=='deduction'){
                                          var grand_total_amount = total_amount - tds_amount - retention_amount - advance_recovery_amount- round_amount;
                                        }else if(retention_type=='addition'){
                                          var grand_total_amount = total_amount - tds_amount + retention_amount - advance_recovery_amount- round_amount;
                                        }else{
                                          var grand_total_amount = total_amount - tds_amount  - advance_recovery_amount- round_amount;
                                        }
                                      }else{
                                        var grand_total_amount = total_amount - tds_amount - advance_recovery_amount- round_amount;
                                      }

                                      $("#tds_amount").val(tds_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                      $("#retention_amount").val(retention_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                      $("#total_amount").val(grand_total_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));

                                      <?php } else { ?>
                                        var tax_inclusive = $('input[type=radio][name=tax_inclusive]:checked').val();
                                        var tax_percent = parseFloat($('#client_payment_tax_percent_add :selected').data('tax-percent'));
                                        var discount_amount = parseFloat($('#discount_amount').val());
                                        var advance_recovery_amount = parseFloat($('#advance_recovery_amount').val());
                                        var tds_percent = parseFloat($('#tds_percent').val());
                                        var retention_percent = parseFloat($('#retention_percent').val());
                                        var retention_type = "<?php echo $this->config->item('Client_Payment_client_payment_invoice_retention_type')?>";
                                        var retention_before_tax = "<?php echo $this->config->item('Client_Payment_client_payment_invoice_retention_before_tax')?>";
                                        if (isNaN(tax_percent)) {
                                          var tax_percent = 0;
                                        }
                                        if (isNaN(discount_amount)) {
                                          var discount_amount = 0;
                                        }
                                        if (isNaN(tds_percent)) {
                                          var tds_percent = 0;
                                        }
                                        if (isNaN(retention_percent)) {
                                          var retention_percent = 0;
                                        }
                                        if (isNaN(tax_percent)) {
                                          var tax_percent = 0;
                                        }
                                        if (isNaN(discount_amount)) {
                                          var discount_amount = 0;
                                        }


                                        if (tax_inclusive == 1) {
                                          var taxable_amount = total_amount * 100 / (tax_percent + 100);
                                        }else {
                                          var taxable_amount = total_amount;
                                        }
                                        if(retention_before_tax==1){
                                          var retention_amount  = taxable_amount * retention_percent / 100;
                                          if(retention_type=='deduction'){
                                            taxable_amount -= retention_amount;
                                          }else{
                                            taxable_amount += retention_amount;
                                          }
                                        }
                                        var tax_amount = taxable_amount * tax_percent / 100;
                                        var tds_amount  = taxable_amount * tds_percent / 100;
                                        if(retention_before_tax!=1){
                                          var retention_amount  = taxable_amount * retention_percent / 100;
                                          if(retention_type=='deduction'){
                                            var grand_total_amount = taxable_amount + tax_amount - tds_amount - retention_amount - discount_amount - advance_recovery_amount - round_amount;
                                          }else if(retention_type=='addition'){
                                            var grand_total_amount = taxable_amount + tax_amount - tds_amount + retention_amount - discount_amount - advance_recovery_amount - round_amount;
                                          }else{
                                            var grand_total_amount = taxable_amount + tax_amount - tds_amount - discount_amount - advance_recovery_amount - round_amount;
                                          }
                                        }else{
                                          var grand_total_amount = taxable_amount + tax_amount - tds_amount - discount_amount - advance_recovery_amount - round_amount;
                                        }
                                        $("#payable_amount").val(taxable_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                        $("#tds_amount").val(tds_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                        $("#retention_amount").val(retention_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                        $("#tax_amount").val(tax_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                                        $("#total_amount").val(grand_total_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));

                                        <?php } ?>


                                        $('#client_payment_received_amount_add').attr('max', $("#total_amount").val());
                                        if (grand_total_amount > 0) {
                                          $('#payment_div').show();
                                        } else {
                                          $('#payment_div').hide();
                                        }
                                        $("#advance_claim_percent").trigger('change');
                                      }


                                      $('body').on('change', '#client_payment_tax_percent_add', function() {
                                        calculate_total_amount();
                                      })

                                      // $("#client_payment_acc_edit").select2({
                                      //   placeholder: "Select Credit Account"
                                      // })

                                      $("#client_payment_received_date_add").datepicker( {
                                        todayHighlight:!0, orientation:"bottom left", templates: {
                                          leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
                                        }
                                      })

                                      function fetch_work_order_items(slno) {
                                        var work_id = $('#work_invoice').val();
                                        $('#item_work_order_task_'+slno).html("");
                                        $('#item_work_order_task_'+slno).append("<option selected disabled>Select Work Order Item</option>");
                                        if(work_id!='' && work_id!=null){
                                          $("#quotation_no").attr('readonly',true);
                                          $("#quotation_date").attr('readonly',true);
                                          $.post("<?php echo base_url() ?>work_order/select_items", {
                                            work_id: work_id
                                          }, function(data) {
                                            var obj = $.parseJSON(data);
                                            if (obj.count >0) {
                                              var i=0;
                                              var work_order_data = obj.work_order_data;
                                              $("#work_order_id").val(work_order_data['order_id']);
                                              $("#quotation_id").val(work_order_data['quotation_id']);
                                              $("#quotation_no").val(work_order_data['quotation_no']);
                                              $("#suppliers_ref_no").val(work_order_data['order_no']);
                                              $("#reference_no").val(work_order_data['order_reference_no']);
                                              $("#quotation_date").datepicker("setDate",work_order_data['quotation_date']);
                                              while (i < obj.items_data.length) {
                                                var j=0;
                                                var items = obj.items_data[i]['items'];
                                                while (j < items.length) {
                                                  $('#item_work_order_task_'+slno).append("<option  value=" + items[j]['item_id'] + ">" + obj.items_data[i]['item_no'] +"."+ (j+1) +" - "+ obj.items_data[i]['type_name'] +  " : "+items[j]['description']+ "</option>");
                                                  j++;
                                                }
                                                i++;
                                              }
                                              $('#item_work_order_task_'+slno).selectpicker();
                                              $('#item_work_order_task_'+slno).selectpicker('refresh');
                                            }else{
                                              toastr['warning']('Work Order Details Not Found', 'Work Order Not Found');
                                            }
                                          })
                                        }
                                      }
                                      $('body').on('change', '.work_order_task_select', function() {
                                        var sl_no = $(this).data('sl-no');
                                        var item_id = $(this).val();
                                        var invoice_rate_type = '';
                                        var invoice_rate_type_valid=1;
                                        <?php if($this->config->item('Quotation_rate_type')=='supply_installation'){?>
                                          invoice_rate_type = $('input[type=radio][name=invoice_rate_type]:checked').val();
                                          if(invoice_rate_type=='' || invoice_rate_type==null || invoice_rate_type==undefined){
                                            invoice_rate_type_valid=0;
                                            toastr['warning']('Please Select Rate Type', 'Select Rate Type');
                                          }
                                          <?php } ?>
                                          if(item_id!='' && item_id!=null && invoice_rate_type_valid==1){
                                            $.post("<?php echo base_url() ?>work_order/select_item_details", {
                                              item_id: item_id
                                            }, function(data) {
                                              var obj = $.parseJSON(data);
                                              if (obj.count==1) {

                                                $("#item_name_"+sl_no).val(obj.data['description']);
                                                $("#item_unit_"+sl_no).val(obj.data['unit']).selectpicker('refresh');
                                                $("#item_qty_"+sl_no).val(obj.data['quantity']);
                                                var price = parseFloat(obj.data['unit_price']);
                                                <?php if($this->config->item('Quotation_rate_type')=='supply_installation'){?>
                                                  if(invoice_rate_type=='installation'){
                                                    price = parseFloat(obj.data['unit_price_2']);
                                                  }
                                                  <?php } ?>
                                                  $("#item_price_"+sl_no).val(price).trigger('change');
                                                  var qty = parseFloat(obj.data['quantity']);
                                                  var tax_percent = parseFloat($('#item_tax_percent_' + sl_no + ' :selected').data('tax-percent'));
                                                  var tax_inclusive = $('input[type=radio][name=tax_inclusive]:checked').val();
                                                  if (tax_inclusive == 1) {
                                                    var taxable_amount = (qty * price) * 100 / (tax_percent + 100);
                                                    var tax_amount = (qty * price) - taxable_amount;
                                                  }else{
                                                    var taxable_amount = (qty * price);
                                                    var tax_amount = taxable_amount * tax_percent / 100;
                                                  }
                                                  var total_amount = taxable_amount+tax_amount;
                                                  $("#item_tax_amt_"+sl_no).val(tax_amount);
                                                  $("#item_taxable_amt_"+sl_no).val(taxable_amount);
                                                  $("#item_total_"+sl_no).val(total_amount);
                                                }
                                              })
                                            }
                                          })
                                          $('input[type=radio][name=invoice_rate_type]').on("change", function(e) {
                                            var invoice_rate_type = $('input[type=radio][name=invoice_rate_type]:checked').val();
                                            $(".work_order_task_select").each(function() {
                                              $(this).trigger('change');
                                            });
                                          })

                                          var tax_type = <?= json_encode($invoice->invoice_tax_inclusive)?>;
                                          var tax_percent = <?= json_encode($invoice->invoice_tax_percent)?>;
                                          var rate_type = <?= json_encode($invoice->invoice_rate_type)?>;
                                          $('input[type=radio][name=tax_inclusive][value='+tax_type+']').prop('checked',true);
                                          $('#client_payment_tax_percent_add').val(tax_percent).trigger('change');
                                          <?php if($this->config->item('Quotation_rate_type')=='supply_installation'){?>
                                            $('input[type=radio][name=invoice_rate_type][value='+rate_type+']').prop('checked',true);
                                            <?php } ?>


                                            var invoice_items = <?php echo json_encode($invoice_items); ?>;
                                            var row_count = <?php echo sizeof($invoice_items) ?>;
                                            $("#current_row_count").val(row_count);
                                            var row = row_count;
                                            for (var i = 0; i <= row_count-1; i++) {
                                              var newRow='';
                                              <?php if ($this->config->item('Client_Payment_client_payment_invoice_template') == 1 || $this->config->item('Client_Payment_client_payment_invoice_template')==4) { ?>
                                                newRow += '<tr class="row_'+(i)+'"><td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group "><input style="padding: 10px" readonly type="text" class="form-control m-input item_slno" name="sl_no['+(i)+']" value="' + (i+1) + '"/></div></div></td>';
                                                newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input type="hidden" name="item_id['+i+']" value="'+invoice_items[i]['item_id']+'" id="item_name_'+i+'"><input  type="text" name="item_name['+i+']" value="'+invoice_items[i]['name']+'" id="item_name_'+i+'" class="form-control" data-sl-no="'+(i+1)+'" placeholder="Description" ></div></td>';
                                                newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_code['+i+']" value="'+invoice_items[i]['item_code']+'" class="form-control" id="item_code_'+(i+1)+'" data-sl-no="'+(i+1)+'" placeholder="<?php echo $this->config->item('Client_Payment_invoice_item_code_title')?>" ></div></td>';
                                                newRow += '<td><div class="form-group m-form__group"><select required  style="width: 100%" class="form-control m-bootstrap-select m_selectpicker m-bootstrap-select--air" id="item_unit_'+i+'" data-width="100%"  name="item_unit['+i+']"></select></div></td>';
                                                newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_qty['+i+']" value="'+invoice_items[i]['quantity']+'" class="form-control item_rate_fields" id="item_qty_'+(i+1)+'" data-sl-no="'+(i+1)+'" placeholder="Quantity" ></div></td>';
                                                newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_price['+i+']" value="'+invoice_items[i]['price']+'" id="item_price_'+(i+1)+'" class="form-control item_rate_fields" data-sl-no="'+(i+1)+'" placeholder="Price" ></div></td>';
                                                newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_total['+i+']" value="'+invoice_items[i]['total_amount']+'" readonly id="item_total_'+(i+1)+'" class="form-control item_amount_field" data-sl-no="'+(i+1)+'" placeholder="Total Amount" ></div></td>';
                                                newRow += '<td><a href="javascript:" data-row-sl-no='+(i)+' data-item-id='+invoice_items[i]['item_id']+' class="ibtnDel btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
                                                newRow += '</tr>';
                                                <?php }elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 2) { ?>
                                                  newRow += '<tr class="row_'+(i)+'"><td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group "><input style="padding: 10px" readonly type="text" class="form-control m-input item_slno" name="sl_no['+(i)+']" value="' + (i+1) + '"/></div></div></td>';
                                                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input type="hidden" name="item_id['+i+']" value="'+invoice_items[i]['item_id']+'" id="item_name_'+i+'"><input  type="text" name="item_name['+i+']" value="'+invoice_items[i]['name']+'" id="item_name_'+i+'" class="form-control" data-sl-no="'+(i+1)+'" placeholder="Description" ></div></td>';
                                                  newRow += '<td><div class="form-group m-form__group"><select required  style="width: 100%" class="form-control m-bootstrap-select m_selectpicker m-bootstrap-select--air" id="item_unit_'+i+'" data-width="100%"  name="item_unit['+i+']"></select></div></td>';
                                                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_qty['+i+']" value="'+invoice_items[i]['quantity']+'" class="form-control item_rate_fields" id="item_qty_'+(i+1)+'" data-sl-no="'+(i+1)+'" placeholder="Quantity" ></div></td>';
                                                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_price['+i+']" value="'+invoice_items[i]['price']+'" id="item_price_'+(i+1)+'" class="form-control item_rate_fields" data-sl-no="'+(i+1)+'" placeholder="Price" ></div></td>';
                                                  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_total['+i+']" value="'+invoice_items[i]['total_amount']+'" readonly id="item_total_'+(i+1)+'" class="form-control item_amount_field" data-sl-no="'+(i+1)+'" placeholder="Total Amount" ></div></td>';
                                                  newRow += '<td><a href="javascript:" data-row-sl-no='+(i)+' data-item-id='+invoice_items[i]['item_id']+' class="ibtnDel btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
                                                  newRow += '</tr>';
                                                  <?php }elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 3) { ?>
                                                    newRow += '<tr class="row_'+(i)+'"><td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group "><input style="padding: 10px" readonly type="text" class="form-control m-input item_slno" name="sl_no['+(i)+']" value="' + (i+1) + '"/></div></div></td>';
                                                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input type="hidden" name="item_id['+i+']" value="'+invoice_items[i]['item_id']+'" id="item_name_'+i+'"><input  type="text" name="item_code['+i+']" value="'+invoice_items[i]['item_code']+'" id="item_code_'+i+'" class="form-control" data-sl-no="'+(i+1)+'" placeholder="<?php echo $this->config->item('Client_Payment_invoice_item_code_title')?>" ></div></td>';
                                                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input type="hidden" name="item_id['+i+']" value="'+invoice_items[i]['item_id']+'" id="item_name_'+i+'"><input  type="text" name="item_name['+i+']" value="'+invoice_items[i]['name']+'" id="item_name_'+i+'" class="form-control" data-sl-no="'+(i+1)+'" placeholder="Description" ></div></td>';
                                                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input type="hidden" name="item_id['+i+']" value="'+invoice_items[i]['item_id']+'" id="item_name_'+i+'"><input  type="text" name="item_hsn['+i+']" value="'+invoice_items[i]['hsn_code']+'" id="item_hsn_'+i+'" class="form-control" data-sl-no="'+(i+1)+'" placeholder="HSN" ></div></td>';
                                                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input type="hidden" name="item_id['+i+']" value="'+invoice_items[i]['item_id']+'" id="item_name_'+i+'"><input  type="text" name="item_mrp['+i+']" value="'+invoice_items[i]['mrp_amount']+'" id="item_mrp_'+i+'" class="form-control" data-sl-no="'+(i+1)+'" placeholder="MRP" ></div></td>';
                                                    newRow += '<td style="display:none"><div class="form-group m-form__group"><select required  style="width: 100%" class="form-control m-bootstrap-select m_selectpicker m-bootstrap-select--air" id="item_unit_'+i+'" data-width="100%"  name="item_unit['+i+']"></select></div></td>';
                                                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_price['+i+']" value="'+invoice_items[i]['price']+'" id="item_price_'+(i+1)+'" class="form-control item_rate_fields" data-sl-no="'+(i+1)+'" placeholder="Trade Rate" ></div></td>';
                                                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_qty['+i+']" value="'+invoice_items[i]['quantity']+'" class="form-control item_rate_fields" id="item_qty_'+(i+1)+'" data-sl-no="'+(i+1)+'" placeholder="Quantity" ></div></td>';
                                                    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_taxable_amt['+i+']" value="'+invoice_items[i]['item_taxable_amount']+'" readonly id="item_taxable_amt_'+(i+1)+'" class="form-control item_taxable_amount_field" data-sl-no="'+(i+1)+'" placeholder="Taxable Amount" ></div></td>';
                                                    newRow += '<td class="tax_invoice_fields"><div class="form-group m-form__group"> <div class="m-select2">' +
                                                    '<select required name="item_tax_percent[' + i + ']" readonly id="item_tax_percent_' + (i + 1) + '" style="width: 100%" class="form-control m-input item_rate_fields"  data-placeholder="Select Tax Percentage" data-sl-no="' + (i + 1) + '">' +
                                                    <?php $tax_percent = $this->config->item('Tax_tax_percent');
                                                    foreach ($tax_percent as $row) :
                                                      $selected_tax = "";
                                                      if ($row->tax_percentage == 0) {
                                                        $selected_tax = "selected";
                                                      } ?> '<option <?php echo $selected_tax ?> data-tax-percent="<?php echo $row->tax_percentage ?>" value="<?php echo $row->tax_id ?>"><?php echo $row->tax_name ?></option>' +
                                                      <?php endforeach; ?> '</select></div></div></td>';
                                                      newRow += '<td class="tax_invoice_fields" style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_tax_amt['+i+']" value="'+invoice_items[i]['item_tax_amount']+'" readonly id="item_tax_amt_'+(i+1)+'" class="form-control item_tax_amount_field" data-sl-no="'+(i+1)+'" placeholder="Tax Amt" ></div></td>';
                                                      newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_disc_percent['+i+']" value="'+invoice_items[i]['item_discount_percent']+'" class="form-control item_rate_fields " id="item_disc_percent_'+(i+1)+'" data-sl-no="'+(i+1)+'" placeholder="Disc %" ></div></td>';
                                                      newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_disc_amt['+i+']" value="'+invoice_items[i]['item_discount_amount']+'" readonly id="item_disc_amt_'+(i+1)+'" class="form-control item_discount_amount_field" data-sl-no="'+(i+1)+'" placeholder="Disc Amt" ></div></td>';

                                                      newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_total['+i+']" value="'+invoice_items[i]['total_amount']+'" readonly id="item_total_'+(i+1)+'" class="form-control item_amount_field" data-sl-no="'+(i+1)+'" placeholder="Total Amount" ></div></td>';
                                                      newRow += '<td><a href="javascript:" data-row-sl-no='+(i)+' data-item-id='+invoice_items[i]['item_id']+' class="ibtnDel btn btn-sm btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
                                                      newRow += '</tr>';
                                                      <?php }elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 5  || $this->config->item('Client_Payment_client_payment_invoice_template') == 6 || $this->config->item('Client_Payment_client_payment_invoice_template') == 7) { ?>
                                                        <?php if ($this->config->item('Client_Invoice_work_order_connected') == 1){?>
                                                          newRow += '<tr class="row_' + (i) + '"><td></td>';
                                                          newRow += '<td colspan="7"><div class="form-group m-form__group">';
                                                          newRow += '<select style="width: 100%" data-width="100%" class="m-bootstrap-select m_selectpicker m-bootstrap-select--air work_order_task_select" data-sl-no="' + (i + 1) + '" required id="item_work_order_task_' + (i + 1) + '" name="item_work_order_task[' + i + ']"></select></div></td>';
                                                          newRow += '<td class="tax_invoice_fields"></td>';
                                                          newRow += '<td class="tax_invoice_fields"></td>';
                                                          newRow += '<td></td><td></td></tr>';
                                                          <?php } ?>
                                                          newRow += '<tr class="row_'+(i)+'"><td style="padding-left: 12px !important;">' + (i+1) + '</td>';
                                                          newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input type="hidden" name="item_id['+i+']" value="'+invoice_items[i]['item_id']+'" id="item_name_'+i+'"><textarea name="item_name['+i+']"  id="item_name_'+i+'" class="form-control" data-sl-no="'+(i+1)+'" placeholder="Item Name/Description" >'+invoice_items[i]['name']+'</textarea></div></td>';
                                                          newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_code[' + i + ']" value="'+invoice_items[i]['item_code']+'" class="form-control" id="item_code_' + (i+1) + '" data-sl-no="' + (i + 1) + '" placeholder="<?php echo $this->config->item('Client_Payment_invoice_item_code_title')?>" ></div></div></td>';
                                                          newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_qty['+i+']" value="'+invoice_items[i]['quantity']+'" class="form-control item_rate_fields" id="item_qty_'+(i+1)+'" data-sl-no="'+(i+1)+'" placeholder="Quantity" ></div></td>';
                                                          newRow += '<td><div class="form-group m-form__group"><select required  style="width: 100%" class="form-control m-bootstrap-select m_selectpicker m-bootstrap-select--air" id="item_unit_'+i+'" data-width="100%"  name="item_unit['+i+']"></select></div></td>';
                                                          newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_price['+i+']" value="'+invoice_items[i]['price']+'" id="item_price_'+(i+1)+'" class="form-control item_rate_fields" data-sl-no="'+(i+1)+'" placeholder="Unit Price" ></div></td>';
                                                          newRow += '<td style="text-align: left" class="item_discount_columns"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_disc_percent[' + i + ']" required id="item_disc_percent_' + (i + 1) + '" value="'+invoice_items[i]['item_discount_percent']+'"  class="form-control item_rate_fields item_discount_percent_field" data-sl-no="' + (i + 1) + '" placeholder="Disc %" ></div></div></td>';
                                                          newRow += '<td style="text-align: left;display:none"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_disc_amt[' + i + ']" value="'+invoice_items[i]['item_discount_amount']+'" required readonly id="item_disc_amt_' + (i + 1) + '" class="form-control item_discount_amount_field" data-sl-no="' + (i + 1) + '" placeholder="Disc Amt" ></div></div></td>';
                                                          newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_taxable_amt['+i+']" value="'+invoice_items[i]['item_taxable_amount']+'" readonly id="item_taxable_amt_'+(i+1)+'" class="form-control item_taxable_amount_field" data-sl-no="'+(i+1)+'" placeholder="Taxable Amount" ></div></td>';
                                                          newRow += '<td class="tax_invoice_fields"><div class="form-group m-form__group"> <div class="m-select2">' +
                                                          '<select required name="item_tax_percent[' + i + ']" readonly id="item_tax_percent_' + (i + 1) + '" style="width: 100%" class="form-control m-input item_rate_fields"  data-placeholder="Select Tax Percentage" data-sl-no="' + (i + 1) + '">' +
                                                          <?php $tax_percent = $this->config->item('Tax_tax_percent');
                                                          foreach ($tax_percent as $row) :
                                                            $selected_tax = "";
                                                            if ($row->tax_percentage == 0) {
                                                              $selected_tax = "selected";
                                                            } ?> '<option <?php echo $selected_tax ?> data-tax-percent="<?php echo $row->tax_percentage ?>" value="<?php echo $row->tax_id ?>"><?php echo $row->tax_name ?></option>' +
                                                            <?php endforeach; ?> '</select></div></div></td>';
                                                            newRow += '<td class="tax_invoice_fields" style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_tax_amt['+i+']" value="'+invoice_items[i]['item_tax_amount']+'" readonly id="item_tax_amt_'+(i+1)+'" class="form-control item_tax_amount_field" data-sl-no="'+(i+1)+'" placeholder="Tax Amt" ></div></td>';

                                                            newRow += '<td style="text-align: left"><div class="form-group m-form__group"><input  type="text" name="item_total['+i+']" value="'+invoice_items[i]['total_amount']+'" readonly id="item_total_'+(i+1)+'" class="form-control item_amount_field" data-sl-no="'+(i+1)+'" placeholder="Total Amount" ></div></td>';
                                                            newRow += '<td><a href="javascript:" data-row-sl-no='+(i)+' data-item-id='+invoice_items[i]['item_id']+' class="ibtnDel btn btn-sm btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
                                                            newRow += '</tr>';
                                                            <?php } ?>
                                                            $("#item_body").append(newRow);
                                                            $('#item_unit_'+i).selectpicker('refresh');
                                                            select_units('item_unit_'+i,invoice_items[i]['unit']);
                                                            $('#item_tax_percent_' + (i + 1)).select2({
                                                              placeholder: "Select  Tax",
                                                              dropdownAutoWidth: true
                                                            })
                                                            <?php if ($this->config->item('Client_Invoice_work_order_connected') == 1){?>
                                                              fetch_work_order_items(i+1);
                                                              <?php } ?>
                                                              $('#item_tax_percent_' + (i + 1)).val(invoice_items[i]['item_tax_percent']).trigger('change');
                                                              $("#current_row_count").val(i+1);
                                                            }
                                                            $("items_count").val(row_count);
                                                            calculate_total_amount();

                                                            $("#invoice_notes").summernote({height:150});
                                                            $('#invoice_notes').summernote('code', '<?php echo str_replace(array("\r", "\n"), "'+'", str_replace("'","",$invoice->invoice_notes))?>');

                                                            </script>
                                                            <script>
                                                            $('#payment_view_table').DataTable({
                                                              "scrollX" : true,
                                                              scrollX:!0, scrollCollapse:!0,
                                                              dom:"<'row'<'col-sm-6 text-left'><'col-sm-6 text-right'f>>\n\t\t\t<'row'<'col-sm-12'tr>>\n\t\t\t<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>", // horizobtal scrollable datatable
                                                              "paging":   false,
                                                              "ordering": false,
                                                              "info":     false,
                                                              searching: false
                                                            });
                                                            $(".form_submit_btn_3").click(function (e) {
                                                              e.preventDefault();
                                                              var a = $(this),
                                                              l = $(this).closest("form"),
                                                              action = l.attr('action');
                                                              var form_id = l.attr('id');
                                                              l.validate({
                                                                rules: {
                                                                  "new_paid_amount": {
                                                                    required: !0,
                                                                    number: !0
                                                                  }
                                                                }, invalidHandler: function (e, r) {
                                                                  var i = $("#" + form_id + "_msg");
                                                                  i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
                                                                }, submitHandler: function (e) {
                                                                }
                                                              }), l.valid() && (a.addClass("m-loader m-loader--right m-loader--light").attr("disabled", !0),
                                                              l.ajaxSubmit({
                                                                url: action,
                                                                success: function (e, t, r, s) {
                                                                  var modal_id = l.attr('data-modal-id');
                                                                  var form_type = l.attr('data-form-type');
                                                                  $("#" + form_id + "_msg").addClass("m--hide");
                                                                  // Block UI
                                                                  mApp.blockPage({
                                                                    overlayColor: "#000000",
                                                                    type: "loader",
                                                                    state: "success",
                                                                    message: "Please wait..."
                                                                  }), setTimeout(function () {
                                                                    mApp.unblockPage()
                                                                  }, 2e3);
                                                                  var obj = $.parseJSON(e);
                                                                  if (obj.status == 1) {
                                                                    a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                                                                    // toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                                                                    // $("#"+modal_id).modal('toggle');
                                                                    // $("."+form_id+"_fields").val("");
                                                                    // location.reload();
                                                                    swal({
                                                                      width: 500,
                                                                      title: obj.flashdata_title,
                                                                      text: obj.flashdata_msg,
                                                                      type: obj.flashdata_type,
                                                                      showCancelButton: !0,
                                                                      confirmButtonText: "Go to Invoice Profile",
                                                                      cancelButtonText: "Add New Invoice",
                                                                      confirmButtonColor: '#3085d6',
                                                                      cancelButtonColor: '#d33',
                                                                      reverseButtons: !0,
                                                                      closeOnClickOutside: false,
                                                                      allowOutsideClick: false,
                                                                      html: "<br>" +
                                                                      '<a href="<?php echo base_url()?>invoices/profile/'+obj.invoice_id+'"  class="SwalBtn1  btn btn-primary m-btn m-btn--custom customSwalBtn">' + 'View Invoice' + '</button>'
                                                                    }).then(function(e) {
                                                                      <?php if($invoice_type=='other_income'){ ?>
                                                                        e.value ? window.location.replace("<?php echo base_url().'invoices/view/'.$invoice_type ?>") : "cancel" === e.dismiss && window.location.replace("<?php echo base_url().'invoices/add/'.$invoice_type ?>/");
                                                                        <?php }else{ ?>
                                                                          e.value ? window.location.replace("<?php echo base_url() ?>work/profile/" + obj.work_id) : "cancel" === e.dismiss && window.location.replace("<?php echo base_url().'invoices/add/'.$invoice_type ?>/" + obj.work_id);
                                                                          <?php } ?>
                                                                        })
                                                                      } else {
                                                                        a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                                                                        toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                                                                      }

                                                                      if (obj == null) {
                                                                        setTimeout(function () {
                                                                          a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                                                                          toastr["warning"]("Your Attempt is Timed Out. Please Try Again.", "Timed Out");
                                                                        }, 2e4)
                                                                      }
                                                                    },error: function() {
                                                                      a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                                                                      toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
                                                                    }
                                                                  })
                                                                )
                                                              });

                                                              </script>
                                                              <script type="text/javascript">

                                                              $("#client_payment_tax_percent_add").select2({
                                                                placeholder: "Select  Tax"
                                                              })



                                                              $('input[type=radio][name=tax_inclusive]').on("change", function(e) {
                                                                var tax_inclusive = $('input[type=radio][name=tax_inclusive]:checked').val();
                                                                if (tax_inclusive == 1) {
                                                                  // Disable All Discount Fields
                                                                  $(".item_discount_percent_field").val(0);
                                                                  $(".item_discount_amount_field").val(0);
                                                                  $(".item_discount_percent_field").attr('readonly',true);
                                                                  $(".item_discount_columns").hide();
                                                                }else{
                                                                  $(".item_discount_columns").show();
                                                                  $(".item_discount_percent_field").removeAttr('readonly',true);
                                                                }
                                                                <?php if ($this->config->item('Client_Payment_client_payment_invoice_template') == 3 || $this->config->item('Client_Payment_client_payment_invoice_template') == 5  || $this->config->item('Client_Payment_client_payment_invoice_template') == 6 || $this->config->item('Client_Payment_client_payment_invoice_template') == 7) { ?>
                                                                  $(".item_rate_fields").each(function() {
                                                                    $(this).trigger('change');
                                                                  });
                                                                  <?php } ?>
                                                                  calculate_total_amount();

                                                                });

                                                                $(".form_submit_btn").click(function (e) {
                                                                  e.preventDefault();
                                                                  var a = $(this),
                                                                  l = $(this).closest("form"),
                                                                  action = l.attr('action');
                                                                  var form_id = l.attr('id');
                                                                  l.validate({
                                                                    rules: {
                                                                      worked_duration: {
                                                                        number: !0,
                                                                        min: 1
                                                                      },
                                                                      basic_charge: {
                                                                        number: !0,
                                                                        min:0
                                                                      }
                                                                    }, invalidHandler: function (e, r) {
                                                                      var i = $("#" + form_id + "_msg");
                                                                      i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
                                                                    }, submitHandler: function (e) {
                                                                    }
                                                                  }), l.valid() && (a.addClass("m-loader m-loader--right m-loader--light").attr("disabled", !0),
                                                                  l.ajaxSubmit({
                                                                    url: action,
                                                                    success: function (e, t, r, s) {
                                                                      var modal_id = l.attr('data-modal-id');
                                                                      var form_type = l.attr('data-form-type');
                                                                      $("#" + form_id + "_msg").addClass("m--hide");
                                                                      // Block UI
                                                                      mApp.blockPage({
                                                                        overlayColor: "#000000",
                                                                        type: "loader",
                                                                        state: "success",
                                                                        message: "Please wait..."
                                                                      }), setTimeout(function () {
                                                                        mApp.unblockPage()
                                                                      }, 2e3);
                                                                      var obj = $.parseJSON(e);
                                                                      if (obj.status == 1) {
                                                                        a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                                                                        $("#"+modal_id).modal('toggle');
                                                                        $("."+form_id+"_fields").val('');
                                                                        $("."+form_id+"_fields_1").val(0);
                                                                        $("."+form_id+"_fields_2").val(1);
                                                                        $("."+form_id+"_select").val('').trigger("change");
                                                                        toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                                                                        select_vehicles("vehicle_add");
                                                                        <?php if($invoice_type=='other_income'){?>
                                                                          select_client();
                                                                          <?php } ?>
                                                                        } else {
                                                                          a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                                                                          toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                                                                        }
                                                                        if (obj == null) {
                                                                          setTimeout(function () {
                                                                            a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                                                                            toastr["warning"]("Your Attempt is Timed Out. Please Try Again.", "Timed Out");
                                                                          }, 2e4)
                                                                        }
                                                                      },error: function() {
                                                                        a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                                                                        toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
                                                                      }
                                                                    })
                                                                  )
                                                                });
                                                                var invoice_tax_type = "<?php echo $invoice->tax_invoice ?>";
                                                                if(invoice_tax_type==1){
                                                                  $(".tax_invoice_fields").show();
                                                                }else{
                                                                  $(".tax_invoice_fields").hide();
                                                                  // $("#client_payment_tax_percent_add").val(0).trigger('change');
                                                                }
                                                                </script>
