<script>
// Bill & Account Transaction Scripts
$("#debit_acc_add_bill_payment").select2({
  placeholder: "Select TR Account",
  dropdownParent: $("#form_bill_payment_add")
})
$("#payment_mode_add_bill_payment").select2({
  placeholder: "Select TR Mode",
  dropdownParent: $("#form_bill_payment_add")
})

$("#debit_acc_edit_bill_payment").select2({
  placeholder: "Select TR Account",
  dropdownParent: $("#form_bill_payment_edit")
})
$("#payment_mode_edit_bill_payment").select2({
  placeholder: "Select TR Mode",
  dropdownParent: $("#form_bill_payment_edit")
})
$("#salary_advance_work_edit").select2({
  placeholder: "Select Expense Under",
  dropdownParent: $("#form_bill_payment_edit")
})

$("#paid_date_add_bill_payment,#reference_date_add_bill_payment,#paid_date_edit_bill_payment,#reference_date_edit_bill_payment,#received_date_add_receive_payment").datepicker( {
  todayHighlight:!0, orientation:"bottom left", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  },
  endDate: '+0d',
  <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
    startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
    <?php } ?>
  })

  $('body').on('change', '#payment_mode_add_bill_payment', function() {
    var payment_mode=$('#payment_mode_add_bill_payment :selected').val();
    if(payment_mode=='POST DATED CHEQUE'){
      $('#paid_date_add_bill_payment').datepicker('destroy');
      $("#paid_date_add_bill_payment").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->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
          startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
          <?php } ?>
        })
      }else{
        $('#paid_date_add_bill_payment').datepicker('destroy');
        $("#paid_date_add_bill_payment").datepicker( {
          todayHighlight:!0, orientation:"bottom left", templates: {
            leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
          },
          endDate: '+0d',
          <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
            startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
            <?php } ?>
          })
        }
      })
      $('body').on('change', '#payment_mode_add_bill_payment2', function() {
        var payment_mode=$('#payment_mode_add_bill_payment2 :selected').val();
        if(payment_mode=='POST DATED CHEQUE'){
          $('#paid_date_add_bill_payment').datepicker('destroy');
          $("#paid_date_add_bill_payment").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->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
              startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
              <?php } ?>
            })
          }else{
            $('#paid_date_add_bill_payment').datepicker('destroy');
            $("#paid_date_add_bill_payment").datepicker( {
              todayHighlight:!0, orientation:"bottom left", templates: {
                leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
              },
              endDate: '+0d',
              <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
                startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
                <?php } ?>
              })
            }
          })
      $('body').on('change', '#payment_mode_edit_bill_payment', function() {
        var payment_mode=$('#payment_mode_edit_bill_payment :selected').val();
        if(payment_mode=='POST DATED CHEQUE'){
          $('#paid_date_edit_bill_payment').datepicker('destroy');
          $("#paid_date_edit_bill_payment").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->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
              startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
              <?php } ?>
            })
          }else{
            $('#paid_date_edit_bill_payment').datepicker('destroy');
            $("#paid_date_edit_bill_payment").datepicker( {
              todayHighlight:!0, orientation:"bottom left", templates: {
                leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
              },
              endDate: '+0d',
              <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
                startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
                <?php } ?>
              })
            }
          })

          $("#credit_acc_add_receive_payment").select2({
            placeholder: "Select Credit Account",
            dropdownParent: $("#form_receive_payment_add")
          })

          $('body').on('click', '#btn_add_bill_payment', function() {
            var no = $(this).closest('tr').children('td');
            var bill_type = $(this).attr('data-bill-type');
            var bill_id = $(this).attr('data-bill-id');
            if(bill_type=='user_payment_return'){
              $('#label_paid_amount_add_bill_payment').text("Received Amount");
              $('#label_paid_date_add_bill_payment').text("Received Date");
              $('#label_debit_acc_add_bill_payment').text("Received To Account");
            }else{
              $('#label_paid_amount_add_bill_payment').text("Paid Amount");
              $('#label_paid_date_add_bill_payment').text("Paid Date");
              $('#label_debit_acc_add_bill_payment').text("Paid From Account");
            }

            if(bill_id=='' || bill_id==null){
              // For Table Entries
              var bill_id_column_no = $(this).attr('data-bill-id-column');
              bill_id = no.eq(bill_id_column_no).text();
            }
            $(".add_bill_payment_fields").hide();
            $('#labour_add_bill_payment').attr('required',false);
            if(bill_type=='labour'){
              $("#labour_add_bill_payment").select2({
                placeholder: "Select Labour",
                dropdownParent: $("#form_bill_payment_add")
              })
              $('#labour_add_bill_payment').attr('required',true);
              $(".add_bill_payment_labour_fields").show();
              $(".add_bill_payment_labour_hide_fields").hide();
              $('#tr_type_add_bill_payment').val(bill_type+"_transaction");
              $('#reference_type_add_bill_payment').val(bill_type);
            }else if(bill_type=='user_payment_receipt' || bill_type=='user_payment_return'){
              $("#user_add_bill_payment").select2({
                placeholder: "Select User Account",
                dropdownParent: $("#form_bill_payment_add")
              })
              $('#user_add_bill_payment').attr('required',true);
              $(".add_bill_payment_user_fields").show();
              $(".add_bill_payment_labour_hide_fields").hide();
              $('#tr_type_add_bill_payment').val(bill_type+"_transaction");
              $('#reference_type_add_bill_payment').val('user_account_head');
            }else if(bill_type=='loan'){
              $(".add_bill_payment_loan_fields").show();
              $(".add_bill_payment_labour_hide_fields").hide();
              $('#tr_type_add_bill_payment').val(bill_type+"_transaction");
              $('#reference_type_add_bill_payment').val(bill_type);
              // Get From Table
              var no = $(this).closest('tr').children('td');
              $('#reference_id_add_bill_payment').val(no.eq(1).text());
              $('#loan_title_add_bill_payment').val(no.eq(2).text());
              $('#loan_provider_add_bill_payment').val(no.eq(5).text());
            }else if(bill_type=='tool_transfer' || bill_type=='tool_rent'){
              $(".add_bill_payment_tool_payment_fields").show();
              $(".add_bill_payment_labour_hide_fields").hide();
              $('#tr_type_add_bill_payment').val("tool_payment_transaction");
              $('#reference_type_add_bill_payment').val(bill_type);
              // Get From Table
              var no = $(this).closest('tr').children('td');
              $('#tool_name_add_bill_payment').val(no.eq(4).text());
              $('#work_name_add_bill_payment').val(no.eq(16).text());
              $('#tool_payable_amount_add_bill_payment').val(no.eq(12).text());
              $('#paid_amount_add_bill_payment').attr('max',no.eq(12).text());
              $('#reference_id_add_bill_payment').val(no.eq(14).text());
            }else{
              $.post("<?php echo base_url() ?>bills/get_details",{bill_id:bill_id,bill_type:bill_type},function(data) {
                var obj = $.parseJSON(data);
                if(obj.count==1){
                  $('#tr_type_add_bill_payment').val(bill_type+"_transaction");
                  $('#reference_type_add_bill_payment').val(bill_type);
                  $('#reference_id_add_bill_payment').val(bill_id);
                  $('#bill_id_add_bill_payment_1').val(obj.bill_id);
                  $('#bill_date_add_bill_payment').val(obj.bill_date);
                  $('#bill_no_add_bill_payment').val(obj.bill_no);
                  $('#bill_total_amount_add_bill_payment').val(obj.bill_total_amount);
                  $('#bill_total_paid_add_bill_payment').val(obj.bill_total_paid);
                  if(bill_type!='purchase_order'){
                    $('#bill_balance_amount_add_bill_payment').val(obj.bill_balance_amount);
                    $("#paid_amount_add_bill_payment").attr('max',obj.bill_balance_amount);
                    $("#balance_amount_add_bill_payment").val(obj.bill_balance_amount);
                  }

                  if(bill_type=='expense_bill'){
                    $('#expense_head_add_bill_payment').val(obj.credit_account);
                    $('#consignee_add_bill_payment').val(obj.consignee);
                    $('#subwork_name_add_bill_payment').val(obj.subwork_name);
                    $(".add_bill_payment_expense_bill_fields").show();
                  }else if(bill_type=='subcontract_bill'){
                    $('#expense_head_add_bill_payment').val(obj.credit_account);
                    $('#consignee_add_bill_payment').val(obj.consignee);
                    $('#subwork_name_add_bill_payment').val(obj.subwork_name);
                    $(".add_bill_payment_subcontract_bill_fields").show();
                  }else if(bill_type=='purchase_bill'){
                    $('#supplier_add_bill_payment').val(obj.supplier);
                    $('#location_add_bill_payment').val(obj.location);
                    $(".add_bill_payment_purchase_bill_fields").show();
                  }else if(bill_type=='purchase_order'){
                    $('#supplier_add_bill_payment').val(obj.supplier);
                    $('#location_add_bill_payment_2').val(obj.location);
                    $("#bill_total_est_amount_add_bill_payment").val(obj.bill_total_amount);
                    $(".add_bill_payment_purchase_order_fields").show();
                    $(".add_bill_payment_purchase_order_hide_fields").hide();
                  }else if(bill_type=='salary_slip'){
                    $(".add_bill_payment_labour_hide_fields").hide();
                    $(".add_bill_payment_user_fields").hide();
                    $(".add_bill_payment_salary_slip_fields").show();
                    $('#salary_slip_id_add_bill_payment').val(obj.bill_id);
                    $('#salary_month_add_bill_payment').val(obj.bill_no);
                    $('#employee_add_bill_payment').val(obj.employee);
                    $('#employee_designation_add_bill_payment').val(obj.designation);
                  }else if(bill_type=='vehicle_work_slip'){
                    var work_slip_id = obj.work_slip_id.slice(1);
                    $(".add_bill_payment_vehicle_hide_fields").hide();
                    $(".add_bill_payment_vehicle_slip_fields").show();
                    $('#vehicle_number').val(obj.vehicle_no);
                    $('#bill_date_add_bill_payment').val(obj.worked_date.toString('dd-MM-yy'));
                    $('#bill_total_paid_add_bill_payment').val(obj.total_paid);
                    $('#consignee_name_add_payment').val(obj.consignee_name);
                    $('#bill_balance_amount_add_bill_payment').val(obj.balance);
                    $('#bill_total_amount_add_bill_payment').val(obj.total_payable_amount);
                    $('#vehicle_number_edit').val(obj.vehicle_no);
                    $('#balance_amount_edit_bill_payment').val(obj.balance);
                  }
                }else{
                  toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
                  $("#bill_payment_add_modal").modal('toggle');
                  $("#bill_payment_add_modal").modal('toggle');
                  $("#form_bill_payment_add_body").hide();
                }
              })
            }

          })

          $('body').on('click', '#btn_receive_payment_add', function() {
            var no = $(this).closest('tr').children('td');
            var bill_type = $(this).attr('data-bill-type');
            var bill_id = $(this).attr('data-bill-id');
            if(bill_id=='' || bill_id==null){
              // For Table Entries
              var bill_id_column_no = $(this).attr('data-bill-id-column');
              bill_id = no.eq(bill_id_column_no).text();
            }
            if(bill_type=='sales_invoice'){
              $.post("<?php echo base_url() ?>sales_invoice/get_invoice_details",{invoice_id:bill_id},function(data) {
                var obj = $.parseJSON(data);
                if(obj.count==1){
                  $('#tr_type_add_receive_payment').val(bill_type+"_transaction");
                  $('#reference_type_add_receive_payment').val(bill_type);
                  $('#reference_id_add_receive_payment').val(bill_id);
                  $('#bill_id_add_receive_payment_1').val(obj.bill_id);
                  $('#invoice_date_add_receive_payment').val(obj.bill_date);
                  $('#invoice_id_add_receive_payment_1').val(obj.bill_no);
                  $('#invoice_total_amount_add_receive_payment').val(obj.bill_total_amount);
                  $('#invoice_total_received_add_receive_payment').val(obj.bill_total_paid);
                  $('#invoice_balance_amount_add_receive_payment').val(obj.bill_balance_amount);

                  $('#customer_add_receive_payment').val(obj.customer);
                  $(".add_receive_payment_purchase_bill_fields").show();

                }else{
                  toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
                  $("#receive_payment_add_modal").modal('toggle');
                  $("#receive_payment_add_modal").modal('toggle');
                  $("#form_receive_payment_add_body").hide();
                }
              })
            }
          })
          $(".add_bill_payment_labour_group_leader_fields").hide();
          $('body').on('change', '#labour_add_bill_payment', function() {
            var labour_id=$('#labour_add_bill_payment :selected').val();
            $('#reference_id_add_bill_payment').val(labour_id);
            $.post("<?php echo base_url() ?>labours/select_details",{labour_id:labour_id},function(data) {
              var obj = $.parseJSON(data);
              var i=0;
              if(obj.count==1) {
                $("#work_type_add_bill_payment").val(obj.work_type);
                if(obj.labour_type==1 && obj.group_position==1){
                  $("#previous_balance_personal_add_bill_payment").val(obj.current_balance);
                  $("#previous_balance_group_add_bill_payment").val(obj.current_balance_group);
                  $("#total_members_add_bill_payment").val(obj.total_members);
                  $(".add_bill_payment_labour_group_leader_fields").show();
                  $(".add_bill_payment_labour_group_leader_hide_fields").hide();
                }else{
                  $("#previous_balance_add_bill_payment").val(obj.current_balance);
                  $(".add_bill_payment_labour_group_leader_fields").hide();
                  $(".add_bill_payment_labour_group_leader_hide_fields").show();
                }
              }
            })
          })
          $('body').on('change', '#user_add_bill_payment', function() {
            var acc_head_id=$('#user_add_bill_payment :selected').val();
            $('#reference_id_add_bill_payment').val(acc_head_id);
            // get User Details
            $.post("<?php echo base_url() ?>user/select_summary/count",{acc_head_id:acc_head_id},function(data) {
              if(data!=''){
                var obj = $.parseJSON(data);
                $('#user_total_paid_add_bill_payment').val(obj.total_user_payments_received);
                $('#user_total_spent_add_bill_payment').val(obj.total_expense_amount);
                $('#user_total_returned_add_bill_payment').val(obj.total_user_payments_returned);
                $('#user_total_balance_add_bill_payment').val(obj.total_balance);
                $(".add_bill_payment_user_detail_fields").show();
              }else{
                $(".add_bill_payment_user_detail_fields").hide();
              }
            })
          })

          $('body').on('change keyup', '#paid_amount_add_bill_payment', function() {
            var value = $(this).val();
            // if(value=="" || value==null){
            //   $(this).val(0)
            // }
            var bill_type = $('#reference_type_add_bill_payment').val();
            if(bill_type!='purchase_order'){
              var total_amount = parseFloat($("#bill_balance_amount_add_bill_payment").val());
              var paid_amount = ($("#paid_amount_add_bill_payment").val()=='') ? 0 : parseFloat($("#paid_amount_add_bill_payment").val());
              $("#balance_amount_add_bill_payment").val((total_amount-paid_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
            }
          })

          $('body').on('click', '#btn_edit_bill_payment', function() {
            var no = $(this).closest('tr').children('td');
            var bill_type = $(this).attr('data-bill-type');
            var bill_id = $(this).attr('data-bill-id');
            var tr_id_column_no = $(this).attr('data-id-column');
            var tr_display_type = $(this).attr('data-tr-display-type');
            if(bill_type=='user_payment_return'){
              $('#label_paid_amount_old_edit_bill_payment').text("Received Amount");
              $('#label_paid_date_edit_bill_payment').text("Received Date");
              $('#label_debit_acc_edit_bill_payment').text("Received To Account");
            }else{
              $('#label_paid_amount_old_edit_bill_payment').text("Paid Amount");
              $('#label_paid_date_edit_bill_payment').text("Paid Date");
              $('#label_debit_acc_edit_bill_payment').text("Paid From Account");
            }
            $(".edit_bill_payment_fields").hide();
            var tr_id = no.eq(tr_id_column_no).text();
            $.post("<?php echo base_url() ?>bills/get_details",{bill_id:bill_id,bill_type:bill_type},function(data) {
              var obj = $.parseJSON(data);
              if(obj.count==1){

                $('#tr_id_edit_bill_payment').val(tr_id);
                $('#paid_date_edit_bill_payment').datepicker("setDate",no.eq(2).text());
                if(bill_type=='salary_slip' || bill_type=='employee'){
                  var paid_amount = parseFloat(no.eq(11).text());
                  $('#debit_acc_edit_bill_payment').val(no.eq(20).text()).trigger('change');
                  $('#payment_mode_edit_bill_payment').val(no.eq(13).text()).trigger('change');
                  $('#reference_no_edit_bill_payment').val(no.eq(14).text());
                  var reference_date = no.eq(15).text();
                  if(reference_date!=''){
                    $('#reference_date_edit_bill_payment').datepicker("setDate",no.eq(15).text());
                  }
                  $('#tr_remarks_edit_bill_payment').val(no.eq(16).text());
                }else{
                  var paid_amount = parseFloat(no.eq(10).text());
                  if(bill_type=='user_payment_return'){
                    $('#debit_acc_edit_bill_payment').val(no.eq(20).text()).trigger('change');
                  }else{
                    $('#debit_acc_edit_bill_payment').val(no.eq(19).text()).trigger('change');
                  }
                  $('#payment_mode_edit_bill_payment').val(no.eq(12).text()).trigger('change');
                  $('#reference_no_edit_bill_payment').val(no.eq(13).text());
                  var reference_date = no.eq(14).text();
                  if(reference_date!=''){
                    $('#reference_date_edit_bill_payment').datepicker("setDate",no.eq(14).text());
                  }
                  $('#tr_remarks_edit_bill_payment').val(no.eq(15).text());
                }

                $('#paid_amount_edit_bill_payment').val(paid_amount);
                $('#paid_amount_old_edit_bill_payment').val(paid_amount);
                $('#tr_type_edit_bill_payment').val(bill_type+"_transaction");
                $('#reference_type_edit_bill_payment').val(bill_type);
                $('#reference_id_edit_bill_payment').val(bill_id);
                $('#bill_id_edit_bill_payment_1').val(obj.bill_id);
                $('#bill_date_edit_bill_payment').val(obj.bill_date);
                $('#bill_no_edit_bill_payment').val(obj.bill_no);
                $('#bill_total_amount_edit_bill_payment').val(obj.bill_total_amount);
                $('#bill_total_paid_edit_bill_payment').val(obj.bill_total_paid);
                if(bill_type!='purchase_order' && bill_type!='labour' && bill_type!='subcontract' && obj.credit_acc!=19 && obj.credit_acc!=20){
                  $('#bill_balance_amount_edit_bill_payment').val(obj.bill_balance_amount);
                  var current_bill_balance = parseFloat(obj.bill_balance_amount);
                  $("#paid_amount_edit_bill_payment").attr('max',current_bill_balance+paid_amount);
                  $("#balance_amount_edit_bill_payment").val(obj.bill_balance_amount);
                }else{
                  $("#paid_amount_edit_bill_payment").removeAttr('max');
                }

                $(".subcontract_info_fields_edit_payment").hide();
                $(".bill_info_fields_edit_payment").show();
                $(".edit_bill_payment_labour_hide_fields").show();
                $('#salary_advance_work_edit').removeAttr('required',true);

                if(bill_type=='expense_bill'){
                  $('#expense_head_edit_bill_payment').val(obj.credit_account);
                  $('#consignee_edit_bill_payment').val(obj.consignee);
                  $('#subwork_name_edit_bill_payment').val(obj.subwork_name);
                  $(".edit_bill_payment_expense_bill_fields").show();
                }else if(bill_type=='subcontract_bill'){
                  $('#expense_head_edit_bill_payment').val(obj.credit_account);
                  $('#consignee_edit_bill_payment').val(obj.consignee);
                  $('#subwork_name_edit_bill_payment').val(obj.subwork_name);
                  $(".edit_bill_payment_subcontract_bill_fields").show();
                }else if(bill_type=='purchase_bill'){
                  $('#supplier_edit_bill_payment').val(obj.supplier);
                  $('#location_edit_bill_payment').val(obj.location);
                  $(".edit_bill_payment_purchase_bill_fields").show();
                }else if(bill_type=='purchase_order'){
                  $('#supplier_edit_bill_payment').val(obj.supplier);
                  $('#location_edit_bill_payment_2').val(obj.location);
                  $("#bill_total_est_amount_edit_bill_payment").val(obj.bill_total_amount);
                  $(".edit_bill_payment_purchase_order_fields").show();
                  $(".edit_bill_payment_purchase_order_hide_fields").hide();
                }else if(bill_type=='salary_slip' || bill_type=='employee'){
                  $(".edit_bill_payment_labour_hide_fields").hide();
                  $(".edit_bill_payment_user_fields").hide();
                  $(".edit_bill_payment_salary_slip_fields").show();
                  $('#salary_slip_id_edit_bill_payment').val(obj.bill_id);
                  $('#salary_month_edit_bill_payment').val(obj.bill_no);
                  $('#employee_edit_bill_payment').val(obj.employee);
                  $('#employee_designation_edit_bill_payment').val(obj.designation);
                  $(".edit_bill_payment_salary_advance_fields").hide();
                  $(".edit_bill_payment_salary_slip_id_fields").show();
                  if(bill_type=='employee'){
                    $('#salary_month_edit_bill_payment').val(no.eq(9).text());
                    $(".edit_bill_payment_salary_slip_fields").hide();
                    $(".edit_bill_payment_employee_fields").show();
                    $(".edit_bill_payment_salary_slip_id_fields").hide();
                    var tr_type = no.eq(23).text();
                    if(tr_type=='salary_advance_transaction'){
                      $('#salary_advance_work_edit').val(no.eq(22).text()).trigger('change');
                      $(".edit_bill_payment_salary_advance_fields").show();
                      $('#salary_advance_work_edit').attr('required',true);
                    }
                  }
                }else if(bill_type=='vehicle_work_slip'){
                  $(".add_bill_payment_vehicle_hide_fields").hide();
                  // $(".add_bill_payment_user_fields").hide();
                  $(".edit_bill_payment_total_field").show();
                  $(".edit_bill_payment_labour_hide_fields").show();
                  $(".edit_bill_payment_purchase_order_hide_fields").show();
                  $(".add_bill_payment_vehicle_slip_fields").show();
                  $('#bill_date_edit_bill_payment').val(obj.worked_date);
                  $('#bill_total_amount_edit_bill_payment').val(obj.total_payable_amount);
                  $('#bill_total_paid_edit_bill_payment').val(obj.total_paid);
                  $('#bill_balance_amount_edit_bill_payment').val(obj.balance);
                  $('#consignee_edit_bill_payment').val(obj.consignee_name);
                  $('#vehicle_no_edit_bill_payment').val(obj.vehicle_no);
                  $('#vehicle_name_edit_bill_payment').val(obj.vehicle_name);

                  $('#bill_work_slip_id_edit').val(bill_id);
                }else if(bill_type=='vehicle_opbalance' || bill_type=='vehicle_renewal'){
                  $(".edit_bill_payment_vehicle_hide_fields").hide();
                  $(".edit_bill_payment_purchase_order_hide_fields").hide();
                  $(".edit_bill_payment_total_field").hide();
                  $(".edit_bill_payment_labour_hide_fields").hide();
                  $(".edit_bill_payment_vehicle_fields").show();
                  $('#consignee_edit_bill_payment').val(obj.consignee_name);
                  $('#vehicle_no_edit_bill_payment').val(obj.vehicle_no);
                  $('#vehicle_name_edit_bill_payment').val(obj.vehicle_name);
                }else if(bill_type=='labour' || bill_type=='labour_insurance_fd'){
                  $('#labour_edit_bill_payment').val(obj.labour);
                  $('#work_type_edit_bill_payment').val(obj.work_type);
                  $(".edit_bill_payment_labour_fields").show();
                  $(".edit_bill_payment_labour_hide_fields").hide();
                  if(bill_type=='labour_insurance_fd' && no.eq(9).text()!=''){
                    var labour_payment_type = no.eq(9).text().toLowerCase();
                    $('input[type=radio][name=labour_payment_type_edit][value='+labour_payment_type+']').prop('checked',true);
                    $(".edit_bill_payment_labour_fields_work_type").hide();
                  }
                }else if(bill_type=='user_payment_receipt' || bill_type=='user_payment_return'){
                  $('#user_edit_bill_payment').val(obj.user);
                  $(".edit_bill_payment_user_fields").show();
                  $(".edit_bill_payment_labour_hide_fields").hide();
                }else if(bill_type=='loan'){
                  $('#loan_title_edit_bill_payment').val(obj.loan_title);
                  $('#loan_provider_edit_bill_payment').val(obj.provider);
                  $(".edit_bill_payment_loan_fields").show();
                  $(".edit_bill_payment_labour_hide_fields").hide();
                }else if(bill_type=='tool_transfer' || bill_type=='tool_rent'){
                  $('#tool_name_edit_bill_payment').val(obj.name);
                  $('#no_tools_edit_bill_payment').val(obj.cat);
                  $(".edit_bill_payment_tool_payment_fields").hide();
                  $(".edit_bill_payment_labour_hide_fields").hide();
                }else if(bill_type=='subcontract'){
                  $("#payment_move_option").prop("checked", false);
                  $(".payment_move_div").hide();
                  $("#move_bill_id_edit_bill_payment").removeAttr('required',true);
                  $("#move_bill_amount_edit_bill_payment").removeAttr('required',true);

                  $(".subcontract_info_fields_edit_payment").show();
                  $(".bill_info_fields_edit_payment").hide();
                  $('#contract_id_payment_edit').val(obj.bill_id);
                  $('#subcontract_contractor_edit_payment').val(obj.consignee);
                  $('#subcontract_project_name_payment_edit').val(obj.project_name);
                  $('#subwork_name_payment_edit').val(obj.subwork_name);
                  $('#subwork_type_payment_edit').html(obj.subwork_types);
                  $('#subcontract_est_budget_edit_payment').val(obj.est_budget);
                  $('#subcontract_total_bill_amount_edit_payment').val(obj.bill_amount);
                  $('#subcontract_total_bill_paid_edit_payment').val(obj.bill_paid_amount);
                  $('#subcontract_advance_paid_edit_payment').val(obj.advance_paid_amount);
                  $('#subcontract_bill_balance_edit_payment').val(obj.bill_balance_amount);
                  $('#subcontract_budget_balance_edit_payment').val(obj.budget_balance);
                  $(".edit_bill_payment_labour_hide_fields").hide();

                  $("#move_bill_id_edit_bill_payment").select2({
                    placeholder: "Select Bill",
                    dropdownParent: $("#form_bill_payment_edit")
                  })

                  $('body').on('change', '#payment_move_option', function() {
                    if(this.checked) {
                      $(".payment_move_div").show();
                      $("#move_bill_id_edit_bill_payment").attr('required',true);
                      // $("#move_bill_amount_edit_bill_payment").attr('required',true);
                    }else{
                      $(".payment_move_div").hide();
                      $("#move_bill_id_edit_bill_payment").removeAttr('required',true);
                      // $("#move_bill_amount_edit_bill_payment").removeAttr('required',true);
                    }
                  })
                  // $('body').on('change', '#move_bill_id_edit_bill_payment', function() {
                  //   var transaction_amount = parseFloat($('#paid_amount_edit_bill_payment').val());
                  //   var bill_balance_amount = parseFloat($('#move_bill_id_edit_bill_payment :selected').data('balance-amount'));
                  //   if(transaction_amount>=bill_balance_amount){
                  //     $("#move_bill_amount_edit_bill_payment").val(bill_balance_amount);
                  //   }else{
                  //     $("#move_bill_amount_edit_bill_payment").val(transaction_amount);
                  //   }
                  // })
                  select_payable_subcontract_bills(obj.contract_id);
                }

                if(obj.credit_acc==19 || obj.credit_acc==20){
                  $(".bill_info_fields_edit_payment_row2").hide();
                  $(".bill_balance_amount_field").hide();
                }
              }else{
                toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
                $("#bill_payment_edit_modal").modal('toggle');
                $("#bill_payment_edit_modal").modal('toggle');
                $("#form_bill_payment_edit_body").hide();
              }
            })
          })
          $('body').on('change keyup', '#paid_amount_edit_bill_payment', function() {
            var value = $(this).val();
            // if(value=="" || value==null){
            //   $(this).val(0)
            // }
            var bill_type = $('#reference_type_edit_bill_payment').val();
            if(bill_type!='purchase_order'){
              var payable_amount = parseFloat($("#bill_total_amount_edit_bill_payment").val());
              var balance_amount = parseFloat($("#bill_balance_amount_edit_bill_payment").val());
              var total_paid = parseFloat($("#bill_total_paid_edit_bill_payment").val());
              var paid_amount_current = ($("#paid_amount_edit_bill_payment").val()=='') ? 0 : parseFloat($("#paid_amount_edit_bill_payment").val());
              var paid_amount_old = parseFloat($("#paid_amount_old_edit_bill_payment").val());
              $("#balance_amount_edit_bill_payment").val((balance_amount+(paid_amount_old-paid_amount_current)).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
            }
          })

          $('body').on('click', '#btn_delete_bill', function() {
            var no = $(this).closest('tr').children('td');
            var bill_type = $(this).attr('data-bill-type');
            var bill_id_column_no = $(this).attr('data-bill-id-column');
            $('#bill_id_delete').val(no.eq(bill_id_column_no).text());
            $('#bill_type_delete').val(bill_type);
          })

          function select_payable_subcontract_bills(contract_id){
            $.post("<?php echo base_url() ?>subcontracts/select_subcontract_bills_2",{contract_id:contract_id},function(data) {
              var obj = $.parseJSON(data);
              var i=0;
              $("#move_bill_id_edit_bill_payment").html('');
              $("#move_bill_id_edit_bill_payment").append("<option selected disabled>Select Bill</option>");
              if(obj.count>0) {
                while (i < obj.count) {
                  $("#move_bill_id_edit_bill_payment").append("<option data-balance-amount=" + obj.balance_amount[i] + " value=" + obj.bill_id[i] + ">#" + obj.bill_id[i] + " (Bill No: " + obj.bill_no[i] + ", Date: " + obj.bill_date[i] + ") [Balance: " + obj.balance_amount[i] + "]</option>");
                  i++;
                }
              }
            })
          }
        </script>
