<?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">
          <?php echo $invoice_title ?>
        </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/view/'.$invoice_type ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                <?php echo $invoice_title ?>
              </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/create" 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($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){ ?> <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
                              $company_address = $this->config->item('company_name') .$this->config->item('company_address');
                              $company_address = preg_replace('#(<br */?>\s*)+#i', '<br />',$company_address);
                              $company_address = str_replace('<br />', '&#13;&#10;', $company_address);
                              $company_address = str_replace('<br/>', '&#13;&#10;', $company_address);
                              $company_address = str_replace('<br>', '&#13;&#10;', $company_address);
                              ?>
                              <textarea style="" rows="8" id="bill_from" name="bill_from" class="form-control m-input m-input--pill m-input--air"><?php echo $company_address; ?></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="#">
                          <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"><strong>Invoice 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-success">
                                <input required type="radio" class="form_fields_radio"  name="invoice_tax_type" value="1" > Tax Invoice
                                <span></span>
                              </label>
                              <label class="m-radio m-radio--solid m-radio--state-danger">
                                <input required type="radio" class="form_fields_radio"  name="invoice_tax_type" value="0" > Non Tax Invoice
                                <span></span>
                              </label>
                            </div>
                            <span class="m-form__help"></span>
                          </div>

                        </div>
                        <div class="col-lg-4 tax_invoice_fields">
                          <span class="m-invoice__subtitle"><strong>Business 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-brand">
                                <input required type="radio" class="form_fields_radio"  name="business_type" value="B2B"> B2B
                                <span></span>
                              </label>
                              <label class="m-radio m-radio--solid m-radio--state-warning">
                                <input required type="radio" class="form_fields_radio"  name="business_type" value="B2C"> B2C
                                <span></span>
                              </label>
                            </div>
                            <span class="m-form__help"></span>
                          </div>

                        </div>
                      </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 style="width: 100%" required 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 data-date-format="dd-mm-yyyy" id="bill_date" required type="text" name="bill_date" class="form-control " placeholder="Invoice Date">
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                        <div class="col-lg-2">
                          <span class="m-invoice__subtitle">REFERENCE NO</span>
                          <div class="form-group m-form__group">
                            <input type="text" id="bill_no" name="bill_no" class="form-control" placeholder="Reference No">
                          </div>
                          <span class="m-form__help"></span>
                        </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"  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" checked name="tax_inclusive" value="0"> Excluded
                                <span></span>
                              </label>
                            </div>
                          </div>
                          <span class="m-form__help"></span>
                        </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) { ?>
                          <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-input-icon m-input-icon--right">
                                <div class="m-select2">
                                  <select required style="width: 100%" class="form-control m-select2 table_filter" id="client_payment_tax_percent_add" name="client_payment_tax_percent" 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>
                          </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>
                  </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">Unit Price</th>
                          <th width="15%" style="text-align: left">Total Amount</th>
                          <th width="5%">&nbsp;</th>
                          <!-- <th>Item Type</th> -->
                        </tr>
                      </thead>
                      <tbody id="item_body">
                        <tr class="row_0">
                          <td>1</td>
                          <td style="text-align: left">
                            <div class="form-group m-form__group">
                              <div class="m-input-icon m-input-icon--right">
                                <input type="text" required name="item_name[0]" id="item_name_0" class="form-control" data-sl-no="1" placeholder="Description">
                              </div>
                            </div>
                          </td>
                          <td style="text-align: left">
                            <div class="form-group m-form__group">
                              <div class="m-input-icon ">
                                <input type="text" name="item_code[0]" class="form-control item_rate_fields" id="item_code_1" data-sl-no="1" placeholder="<?php echo $this->config->item('Client_Payment_invoice_item_code_title')?>">
                              </div>
                            </div>
                          </td>
                          <td style="text-align: left">
                            <div class="form-group m-form__group">
                              <select style="width: 100%" data-width="100%" class="m-bootstrap-select m_selectpicker m-bootstrap-select--air" required id="item_unit_0" name="item_unit[0]">

                              </select>
                            </div>
                          </td>
                          <td style="text-align: left">
                            <div class="form-group m-form__group">
                              <div class="m-input-icon ">
                                <input type="text" required name="item_qty[0]" class="form-control item_rate_fields" id="item_qty_1" data-sl-no="1" placeholder="Quantity">
                              </div>
                            </div>
                          </td>
                          <td style="text-align: left">
                            <div class="form-group m-form__group">
                              <div class="m-input-icon ">
                                <input type="text" required name="item_price[0]" id="item_price_1" class="form-control item_rate_fields" data-sl-no="1" placeholder="Unit Price">
                              </div>
                            </div>
                          </td>
                          <td style="text-align: left">
                            <div class="form-group m-form__group">
                              <div class="m-input-icon m-input-icon--right">
                                <input type="text" required name="item_total[0]" readonly id="item_total_1" class="form-control item_amount_field" data-sl-no="1" placeholder="Total Amount">
                              </div>
                            </div>
                          </td>
                          <td>
                            <a href="javascript:" data-row-sl-no='0' class="ibtnDel btn btn-danger btn-sm btn-icon-only m-btn--pill m-btn--air"><i class="fa fa-trash"></i>
                            </td>
                          </tr>
                        </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">
                          <tr class="row_0">
                            <td>1</td>
                            <td style="text-align: left">
                              <div class="form-group m-form__group">
                                <div class="m-input-icon m-input-icon--right">
                                  <input type="text" data-date-format="dd-mm-yyyy" required name="item_date[0]" id="item_date_0" class="form-control item_date_fields" data-sl-no="1" placeholder="Date">
                                </div>
                              </div>
                            </td>
                            <td style="text-align: left">
                              <div class="form-group m-form__group">
                                <div class="m-input-icon ">
                                  <input type="text" required name="item_description[0]" class="form-control" id="item_description_1" data-sl-no="1" placeholder="Complaint">
                                </div>
                              </div>
                            </td>
                            <td style="text-align: left">
                              <div class="form-group m-form__group">
                                <div class="m-input-icon m-input-icon--right">
                                  <input type="text" required name="item_total[0]" id="item_total_1" class="form-control item_amount_field item_rate_fields" data-sl-no="1" placeholder="Amount">
                                </div>
                              </div>
                            </td>
                            <td>
                              <a href="javascript:" data-row-sl-no='0' class="ibtnDel btn btn-danger btn-sm btn-icon-only m-btn--pill m-btn--air"><i class="fa fa-trash"></i>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      <?php } elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 3) { ?>
                        <div class="table-responsive">
                        <table class="table m-table table-bordered item_table item_table_3" style="">
                          <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">
                            <tr class="row_0">
                              <td style="padding-left: 12px !important;">1</td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text"  name="item_code[0]" id="item_code_0" class="form-control" data-sl-no="1" placeholder="<?php echo $this->config->item('Client_Payment_invoice_item_code_title')?>">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text" required name="item_name[0]" id="item_name_0" class="form-control" data-sl-no="1" placeholder="Description">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text"  name="item_hsn[0]" id="item_hsn_0" class="form-control" data-sl-no="1" placeholder="HSN">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text"  name="item_mrp[0]" id="item_mrp_0" class="form-control" data-sl-no="1" placeholder="MRP">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left;display:none">
                                <div class="form-group m-form__group">
                                  <select style="width: 100%" data-width="100%" class="m-bootstrap-select m_selectpicker m-bootstrap-select--air" required id="item_unit_0" name="item_unit[0]">

                                  </select>
                                </div>
                              </td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text" required name="item_price[0]" id="item_price_1" class="form-control item_rate_fields" data-sl-no="1" placeholder="Unit Rate">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text" required name="item_qty[0]" class="form-control item_rate_fields" id="item_qty_1" data-sl-no="1" placeholder="Qty">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text" required name="item_taxable_amt[0]" readonly id="item_taxable_amt_1" class="form-control item_taxable_amount_field" data-sl-no="1" placeholder="Taxable Amt">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left" class="tax_invoice_fields">
                                <div class="form-group m-form__group">
                                  <div class="m-select2">
                                    <select required name="item_tax_percent[0]" readonly id="item_tax_percent_1" style="width: 100%" class="form-control m-input item_rate_fields" data-sl-no="1" 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>
                                </div>
                              </td>
                              <td style="text-align: left" class="tax_invoice_fields">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text" required name="item_tax_amt[0]" readonly id="item_tax_amt_1" class="form-control item_tax_amount_field" data-sl-no="1" placeholder="Tax Amt">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text" required name="item_disc_percent[0]" id="item_disc_percent_1" class="form-control item_rate_fields" data-sl-no="1" placeholder="Disc %">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text" required name="item_disc_amt[0]" readonly id="item_disc_amt_1" class="form-control item_discount_amount_field" data-sl-no="1" placeholder="Disc Amt">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align: left">
                                <div class="form-group m-form__group">
                                  <div class="m-input-icon ">
                                    <input type="text" required name="item_total[0]" readonly id="item_total_1" class="form-control item_amount_field" data-sl-no="1" placeholder="Total Amount">
                                  </div>
                                </div>
                              </td>
                              <td style="text-align:center !important">
                                <a href="javascript:" data-row-sl-no='0' class="ibtnDel btn btn-danger btn-sm btn-icon-only m-btn--pill m-btn--air"><i class="fa fa-trash"></i>
                                </td>
                              </tr>
                            </tbody>
                          </table>
                        </div>
                      <?php }elseif ($this->config->item('Client_Payment_client_payment_invoice_template') == 5 || $this->config->item('Client_Payment_client_payment_invoice_template') == 6) { ?>
                          <table class="table m-table table-bordered item_table item_table_3">
                            <thead>
                              <tr>
                                <th>SNo</th>
                                <?php if($this->config->item('Client_Payment_client_payment_invoice_template') == 6){?>
                                <th style="text-align: left">Item</th>
                              <?php } ?>
                                <th style="text-align: left;width:25%">Description</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 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">
                              <tr class="row_0">
                                <td style="padding-left: 12px !important;">1</td>
                                <?php if($this->config->item('Client_Payment_client_payment_invoice_template') == 6){?>
                                  <td style="text-align: left">
                                    <div class="form-group m-form__group">
                                      <div class="m-input-icon ">
                                        <input type="text" required name="item_code[0]" class="form-control" id="item_code_1" data-sl-no="1" placeholder="Item">
                                      </div>
                                    </div>
                                  </td>
                              <?php } ?>
                                <td style="text-align: left">
                                  <div class="form-group m-form__group">
                                    <div class="m-input-icon ">
                                      <textarea required name="item_name[0]" id="item_name_0" class="form-control" data-sl-no="1" placeholder="Description" ></textarea>
                                    </div>
                                  </div>
                                </td>
                                <td style="text-align: left">
                                  <div class="form-group m-form__group">
                                    <div class="m-input-icon ">
                                      <input type="text" required name="item_qty[0]" class="form-control item_rate_fields" id="item_qty_1" data-sl-no="1" placeholder="Qty">
                                    </div>
                                  </div>
                                </td>
                                <td style="text-align: left;">
                                  <div class="form-group m-form__group">
                                    <select required style="width: 100%" data-width="100%" class="m-bootstrap-select m_selectpicker m-bootstrap-select--air" required id="item_unit_0" name="item_unit[0]">

                                    </select>
                                  </div>
                                </td>
                                <td style="text-align: left">
                                  <div class="form-group m-form__group">
                                    <div class="m-input-icon ">
                                      <input type="text" required name="item_price[0]" id="item_price_1" class="form-control item_rate_fields" data-sl-no="1" placeholder="Unit Rate">
                                    </div>
                                  </div>
                                </td>

                                <td style="text-align: left">
                                  <div class="form-group m-form__group">
                                    <div class="m-input-icon ">
                                      <input type="text" required name="item_taxable_amt[0]" readonly id="item_taxable_amt_1" class="form-control item_taxable_amount_field" data-sl-no="1" placeholder="Taxable Amt">
                                    </div>
                                  </div>
                                </td>
                                <td style="text-align: left" class="tax_invoice_fields">
                                  <div class="form-group m-form__group">
                                    <div class="m-select2">
                                      <select required name="item_tax_percent[0]" readonly id="item_tax_percent_1" style="width: 100%" class="form-control m-input item_rate_fields" data-sl-no="1" 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>
                                  </div>
                                </td>
                                <td style="text-align: left" class="tax_invoice_fields">
                                  <div class="form-group m-form__group">
                                    <div class="m-input-icon ">
                                      <input type="text" required name="item_tax_amt[0]" readonly id="item_tax_amt_1" class="form-control item_tax_amount_field" data-sl-no="1" placeholder="Tax Amt">
                                    </div>
                                  </div>
                                </td>
                                <td style="text-align: left">
                                  <div class="form-group m-form__group">
                                    <div class="m-input-icon ">
                                      <input type="text" required name="item_total[0]" readonly id="item_total_1" class="form-control item_amount_field" data-sl-no="1" placeholder="Total Amount">
                                    </div>
                                  </div>
                                </td>
                                <td style="text-align:center !important">
                                  <a href="javascript:" data-row-sl-no='0' class="ibtnDel btn btn-danger btn-sm btn-icon-only m-btn--pill m-btn--air"><i class="fa fa-trash"></i>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          <?php } ?>
                          <input type="hidden" name="current_row_count" id="current_row_count" value="1">
                          <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_invoice_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">
                                  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" class="form-control item_rate_fields" value="0" placeholder="">
                                  </td>
                                  <td>
                                    <input required type="text" required readonly name="bill_retention_amount" id="retention_amount" class="form-control" value="0" placeholder="">
                                  </td>
                                <?php } ?>
                                <td>
                                  <input required type="text" required readonly name="bill_taxable_amount" id="payable_amount" class="form-control" value="0" placeholder="">
                                </td>
                                <td class="tax_invoice_fields">
                                  <input required type="text" required readonly name="bill_tax_amount" id="tax_amount" class="form-control" value="0" placeholder="">
                                </td>
                                <td style="<?php echo $tds_view?>" class="tax_invoice_fields">
                                  <input required type="text" required name="bill_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" 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" class="form-control item_rate_fields" value="0" placeholder="">
                                  </td>
                                  <td>
                                    <input required type="text" required readonly name="bill_retention_amount" id="retention_amount" class="form-control" value="0" placeholder="">
                                  </td>
                                <?php } ?>
                                <td>
                                  <input required type="text" required name="bill_advance_recovery_amount" id="advance_recovery_amount" class="form-control item_rate_fields" value="0" placeholder="">
                                </td>
                                <td>
                                  <input required type="text" required name="bill_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) { echo "readonly"; }?> id="discount_amount" class="form-control item_rate_fields" value="0" placeholder="">
                                </td>
                                <td>
                                  <input required type="text" required name="bill_round_amount" id="round_amount" class="form-control item_rate_fields" value="0" placeholder="">
                                </td>
                                <td>
                                  <input required type="text" required readonly name="bill_total_amount" id="total_amount" class="form-control" value="0" placeholder="">
                                </td>
                              </tr>
                            </tbody>
                          </table>
                        </div>
                        <span class="m-invoice__desc" style="text-align: left;padding: 1rem 0 1rem 0 !important;">
                          <!-- <span class="m-invoice__subtitle" ><strong>Account Details</strong></span><br> -->

                        </span>
                        <div class="row">
                          <div class="col-lg-3">
                            <div class="form-group m-form__group">
                              <label style="font-weight:900">Delivery Notes/Ref</label>
                              <input type="text"  name="delivery_note" class="form-control" placeholder="Delivery Notes">
                              <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">Works Carried At</label>
                              <input type="text"  name="works_carried_at" class="form-control" placeholder="Works Carried At">
                            </div>
                          </div>
                          <div class="col-lg-3">
                            <div class="form-group m-form__group">
                              <label style="font-weight:900">Payment Terms</label>
                              <input type="text"  name="payment_terms" class="form-control" placeholder="Payment Terms">
                            </div>
                          </div>
                          <div class="col-lg-3">
                            <div class="form-group m-form__group">
                              <label style="font-weight:900">Other Terms</label>
                              <input type="text"  name="other_terms" class="form-control" placeholder="Other Terms">
                            </div>
                          </div>
                          <div class="col-lg-3">
                            <div class="form-group m-form__group">
                              <label style="font-weight:900">Delivery Date</label>
                              <input type="text" data-date-format="dd-mm-yyyy" name="delivery_date" class="form-control" id="delivery_date" placeholder="Delivery Date">
                              <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">Shipping Name</label>
                              <input type="text"  name="shipping_name" id="shipping_name" class="form-control" placeholder="Shipping Name">
                            </div>
                          </div>
                          <div class="col-lg-3">
                            <div class="form-group m-form__group">
                              <label style="font-weight:900">Shipping Address</label>
                              <textarea  name="shipping_address" id="shipping_address" class="form-control" placeholder="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/Remarks</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>
                                Bank Account (Display)
                              </label>
                              <div class="m-select2 m-select2--air">
                                <select 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>
                        </div>
                        <div style="padding-top: 2%;border: 2px solid #e4e4e4;padding: 2%;margin-bottom: 2%;margin-top: 2%;display: none" id="payment_div">
                          <div class="row">
                            <div class="col-lg-4">
                              <div class="form-group m-form__group">
                                <label for="user_name">
                                  Received Amount
                                </label>
                                <div class="m-input-icon m-input-icon--right">
                                  <input required type="text" name="received_amount" id="client_payment_received_amount_add" class="form-control form_client_tax_amount_add_fields form_client_payment_add_fields m-input m-input--pill m-input--air" placeholder="Received Amount" aria-describedby="basic-addon1">
                                  <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                                </div>
                                <span class="m-form__help"></span>
                              </div>
                            </div>
                            <div class="col-lg-4 payment_fields" style="display: none">
                              <div class="form-group m-form__group">
                                <label for="user_name">
                                  Received Date
                                </label>
                                <div class="m-input-icon m-input-icon--right">
                                  <input data-date-format="dd-mm-yyyy" id="client_payment_received_date_add" required type="text" name="received_date" class="form-control form_client_payment_add_fields m-input m-input--pill m-input--air payment_fields_input" placeholder="Received Date">
                                  <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-calendar"></i></span></span>
                                </div>
                                <span class="m-form__help"></span>
                              </div>
                            </div>
                            <div class="col-lg-4 payment_fields" style="display: none">
                              <div class="form-group m-form__group">
                                <label for="user_name">
                                  Received To Acc
                                </label>
                                <div class="m-select2 m-select2--air m-select2--pill">
                                  <select required id="client_payment_acc_add" name="credit_acc" style="width: 100%" class="form-control m-input payment_fields_input" aria-describedby="basic-addon1">
                                    <?php
                                    foreach ($this->config->item('account_heads') as $row) :
                                      $selected = "";
                                      if ($row->acc_head_id == 1) {
                                        $selected = "selected";
                                      }
                                      ?>
                                      <option <?php echo $selected ?> value="<?php echo $row->acc_head_id ?>"><?php echo $row->acc_head_name ?></option>
                                      <?php
                                    endforeach;
                                    ?>
                                  </select>
                                </div>
                                <span class="m-form__help"></span>
                              </div>
                            </div>
                          </div>
                          <div class="row">
                            <div class="col-lg-6 payment_fields" style="display: none">
                              <div class="form-group m-form__group" style="">
                                <label for="user_name">
                                  Remarks
                                </label>
                                <div class="m-input-icon m-input-icon--right">
                                  <input type="text" name="tr_remarks" class="form-control form_client_payment_add_fields m-input m-input--pill m-input--air " placeholder="Remarks" aria-describedby="basic-addon1">
                                  <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                                </div>
                                <span class="m-form__help"></span>
                              </div>
                            </div>
                            <div class="col-lg-6 payment_fields" style="display: none">
                              <div class="form-group m-form__group" style="">
                                <label for="user_name">
                                  Receipt No
                                </label>
                                <div class="m-input-icon m-input-icon--right">
                                  <input type="text" name="reference_no" class="form-control form_client_payment_add_fields m-input m-input--pill m-input--air" placeholder="Receipt No" aria-describedby="basic-addon1">
                                  <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-tag"></i></span></span>
                                </div>
                                <span class="m-form__help"></span>
                              </div>
                            </div>
                          </div>
                        </div>
                        <input type="hidden" name="reference_id" id="reference_id_add_client_payment" value="">
                        <input type="hidden" name="invoice_type"  value="<?= $invoice_type ?>">
                      </div>
                    </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"); ?>
          </div>
        </div>
      </div>
      <?php $this->view("modules/include_modals/modal_vehicle_add");?>
      <?php $this->view("modules/include_modals/modal_client_add");?>

      <!-- 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");
      $("#bank_account_add").select2({
        placeholder: "Select Bak Account"
      })
      <?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');

        $("#state_id_add").select2({
          placeholder: "Select State Code",
          dropdownParent: $("#form_client_add")
        })
        $("#client").select2({
          placeholder: "Select Client",
          dropdownParent: $("#form_invoice_add")
        })
        $("#work_status").select2({
          placeholder: "Select Status",
          dropdownParent: $("#form_invoice_add")
        })
        $('body').on('change', '#client', function() {
          fetch_client_details();
        })
        function fetch_client_details() {
          $('#client_pobox').hide();
          var client_id = $('#client').val()
          $.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']);
              $('#client_pobox').html(obj.data['po_box_no']);
              if(obj.data['po_box_no']!=''){
                $('#client_pobox').show();
              }
              $('#contact_person').html(obj.data['contact_person']);
              $('#reference_id_add_client_payment').val(obj.data['client_id']);
            } else {
              $('#client_name').html('');
              $('#client_contact').html('');
              $('#client_address').html('');
              $('#client_taxno').html('');
              $('#client_pobox').html('');
              $('#contact_person').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++;
              }
            }
          })
        }
        <?php }else{ ?>
          $("#client_payments_menu").addClass('m-menu__item--active m-menu__item--open');
          $("#client_payments_menu_1").addClass('m-menu__item--active');

          $("#work_invoice").select2({
            placeholder: "Select  Work",
            templateResult: format_project_dropdown
          })
          <?php if (isset($invoice_work_id)) { ?>
            $("#work_invoice").val(<?= $invoice_work_id ?>).trigger('change');
            fetch_work_details();
            <?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['tax_no']);
                  $('#reference_id_add_client_payment').val(obj.data['work_id']);
                  $('#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('');
                  $('#client_pobox').html('');
                  $('#contact_person').html('');
                  $('#shipping_name').val('');
                  $('#shipping_address').val('');
                  $('#reference_id_add_client_payment').val('');
                }

              })
            }
            <?php } ?>

            $("#invoice_notes").summernote({height:150});
            $('#invoice_notes').summernote('code', '<?php echo $this->config->item('Client_Payment_client_payment_invoice_notes_content') ?>');

            $("#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");
            $("#bill_date,#paid_date,#reference_date_add,#delivery_date").datepicker({
              todayHighlight: !0,
              orientation: "bottom left",
              templates: {
                leftArrow: '<i class="la la-angle-left"></i>',
                rightArrow: '<i class="la la-angle-right"></i>'
              }
            })

            function select_units(select_id) {
              $('#' + 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++;
                  }
                  $('#' + select_id).val(3);
                  $('#' + select_id).selectpicker("refresh");
                  // $('#'+select_id).val(3).trigger('change');
                }
              })
            }
            select_units('item_unit_0');
            $('#item_unit_0').selectpicker('refresh');

            <?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="Unit 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-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).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-sm btn-icon-only m-btn--pill m-btn--air" ><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 + ']"  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 + ']"  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 + ']"  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="Unit 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 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).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);
                      var invoice_tax_type = $('input[type=radio][name=invoice_tax_type]:checked').val();
                      if(invoice_tax_type==1){
                        $(".tax_invoice_fields").show();
                        $('input[type=radio][name=business_type]').attr('required',true);
                      }else{
                        $(".tax_invoice_fields").hide();
                        $("#client_payment_tax_percent_add").val(0).trigger('change');
                        $('input[type=radio][name=business_type]').removeAttr('required',true);
                        $('input[type=radio][name=business_type]').prop('checked',false);
                      }
                    });
                    <?php }
                    if ($this->config->item('Client_Payment_client_payment_invoice_template') == 5 || $this->config->item('Client_Payment_client_payment_invoice_template') == 6) { ?>
                      $("#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>';
                        <?php if($this->config->item('Client_Payment_client_payment_invoice_template') == 6){?>
                            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="Item" ></div></div></td>';
                          <?php } ?>
                        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 + '" class="form-control" data-sl-no="' + (counter + 1) + '" placeholder="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_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 + '" 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"><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).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);

$('input[type=radio][name=invoice_tax_type]').trigger("change");
                        });
                        <?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);
                          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) { ?>
                              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 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;
                              }
                              $("#item_taxable_amt_" + sl_no).val(taxable_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2') ?>));
                              var discount_amount = (taxable_amount + tax_amount) * discount_percent / 100;
                              var total_amount = taxable_amount + tax_amount - discount_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 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) { ?>
                                $(".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 (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();
                                  }
                                }

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

                                $("#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>'
                                  }
                                })
                                </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,
                                          <?php if($invoice_type=='other_income'){ ?>
                                            confirmButtonText: "Go to View All Invoices",
                                            <?php }else{ ?>
                                              confirmButtonText: "Go to Work Profile",
                                              <?php } ?>
                                              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 Details' + '</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",
                                      })

                                      $('#client_payment_acc_add').select2({
                                        placeholder: "Select Account"
                                      })

                                      $(".tax_invoice_fields").hide();
                                      $('input[type=radio][name=invoice_tax_type]').on("change", function(e) {
                                        var invoice_tax_type = $('input[type=radio][name=invoice_tax_type]:checked').val();
                                        if(invoice_tax_type==1){
                                          $(".tax_invoice_fields").show();
                                          $('input[type=radio][name=business_type]').attr('required',true);
                                        }else{
                                          $(".tax_invoice_fields").hide();
                                          $("#client_payment_tax_percent_add").val(0).trigger('change');
                                          $('input[type=radio][name=business_type]').removeAttr('required',true);
                                          $('input[type=radio][name=business_type]').prop('checked',false);
                                        }
                                      })
                                      $('input[type=radio][name=invoice_tax_type][value=0]').prop('checked',true).trigger('change');

                                      $('input[type=radio][name=tax_inclusive]').on("change", function(e) {
                                        <?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) { ?>
                                          $(".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");
                                              }
                                            })
                                          )
                                        });
                                        </script>
