<?php $this->view("includes/Inc_pagehead");?>
<!-- Page Styles Goes Here-->
<style>

@media print {

  .no-print{
    display: none !important;
  }
  .m-subheader,.m-portlet__head{
    display: none !important;
    height: 0px !important;
    padding: 0px !important;
  }
  .m-header{
    display: none !important;
    height: 0px !important;
  }
  .m-header--fixed .m-body {
    padding-top: 0px !important;
  }
  body * {
    visibility: hidden;
  }
  .m-portlet__body * {
    visibility: visible;
    overflow: visible !important;
  }
  .m-body .m-content {
    padding: 0px !important;
  }
  .m-content--skin-light2 .m-body {
    background-color: white !important;
  }
  body.m-content--skin-light2 {
    background-color: white !important;
  }
  .m-wrapper {
    margin-bottom: 0px !important;
  }
  .m-portlet__body {
    padding: 0px !important;
  }
}
</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">
        Supplier - Material Report
        </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().'supplier/material_report' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Supplier - Material Report
              </span>
            </a>
          </li>


        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <div class="m-portlet  m-portlet--rounded m-portlet--bordered no-print">
      <div class="m-portlet__body" style="padding: 1rem 2.2rem;">
        <form autocomplete="off" id="form_account_ledger" method="post" action="<?php echo base_url()?>supplier/material_report" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right " >
          <div class="m-form m-form--label-align-right m--margin-bottom-10 ">
            <div class="row ">
              <div class="col-xl-12 order-2 order-xl-1">
                <div class="form-group m-form__group row">
                  <div class="col-lg-5">
                    <div class="form-group m-form__group"  >
                      <label for="user_name">
                        Select Supplier
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select required id="account" name="account" style="width: 100%" class="form-control m-input"  aria-describedby="basic-addon1" data-placeholder="Select Supplier">
                          <option selected disabled>Select Supplier</option>
                                                    <?php foreach ($suppliers as $key => $value) { ?>
                                                      <option value="<?php echo $value->supplier_id ?>"><?php echo $value->supplier_name ?></option>
                                                    <?php } ?>
                        </select>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-5" >
                    <div class="row ">
                      <div class="col-lg-6">
                        <div class="form-group m-form__group" >
                          <label> Start Date:</label>
                          <div class="input-group m-input-group--air m-input-group--pill date">
                            <div class="input-group-prepend">
                              <span class="input-group-text">
                                <i class="la la-calendar"></i>
                              </span>
                            </div>
                            <input value="<?php if($selected_start_date!=''){echo date('d-m-Y',strtotime($selected_start_date));}?>"  data-date-format="dd-mm-yyyy" name="start_date" autocomplete="off" id="start_date"  type="text" class="form-control form_client_payment_add_fields m-input " placeholder="Starting Date" >
                          </div>
                        </div>
                      </div>
                      <div class="col-lg-6">
                        <div class="form-group m-form__group" >
                          <label> End Date:</label>
                          <div class="input-group m-input-group--air m-input-group--pill date">
                            <div class="input-group-prepend">
                              <span class="input-group-text">
                                <i class="la la-calendar"></i>
                              </span>
                            </div>
                            <input value="<?php if($selected_end_date!=''){echo date('d-m-Y',strtotime($selected_end_date));}?>" data-date-format="dd-mm-yyyy" name="end_date" autocomplete="off" id="end_date"  type="text" class="form-control form_client_payment_add_fields m-input " placeholder="Ending Date" >
                          </div>
                        </div>
                      </div>
                      <div class="col-lg-12" >
                        <span class="m-form__help">* Leave Start Date and End Date Empty to Get All Transactions</span>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-2">
                    <br/>
                    <button type="submit" style="margin-top: 7px" id="btn_go" class="btn btn-focus m-btn m-btn--custom m-btn--icon m-btn--air m-btn--pill">
                      <span>
                        <i class="la la-search"></i>
                        <span>
                          Go
                        </span>
                      </span>
                    </button>
                  </div>
                </div>

              </div>
            </div>
          </div>
        </form>
      </div>
    </div>
    <?php if($selected_account!=''){  ?>
    <div class="m-portlet m-portlet--mobile m-portlet--info m-portlet--head-solid-bg m-portlet--bordered m-portlet--rounded">
      <div class="m-portlet__head">
        <div class="m-portlet__head-caption">
          <div class="m-portlet__head-title">
            <h3 class="m-portlet__head-text">
              Supplier - Material Report
            </h3>
          </div>
        </div>
        <div class="m-portlet__head-tools">
          <a id="btn_print" href="javascript:;" onclick="window.print()" class="btn btn-warning m-btn m-btn--custom m-btn--icon m-btn--air m-btn--pill">
            <span>
              <i class="la la-print"></i>
              <span>
                Print Report
              </span>
            </span>
          </a>
        </div>
      </div>
      <div class="m-portlet__body">
        <!--begin: Datatable -->
        <table width="100%" class="table m-table nowrap table-header-fixed table-striped table-bordered table-hover table-header-fixed" id="ledger_view_table">
          <?php
            $caption="Account Ledger of [".$selected_account_data->acc_head_name."]";
            if($selected_start_date!='' || $selected_end_date!=''){
              if($selected_start_date=='' && $selected_end_date!=''){
                $caption.=" To ".$selected_end_date;
              }else if($selected_start_date!='' && $selected_end_date==''){
                $caption.=" From ".$selected_start_date;
              }else{
                $caption.=" From ".$selected_start_date." To ".$selected_end_date;
              }
            }
            ?>
            <caption class="table_caption" style="caption-side: top !important;text-align: center"><strong><?php echo $caption ?></strong></caption>
          <thead>
            <tr class="">
              <th>Date</th>
              <th>Invoice No</th>
              <th>Vehicle</th>
              <th>Material</th>
              <th>UOM</th>
              <th>QTY</th>
              <th>Rate</th>
              <th>Amount Dr</th>
              <th>Amount Cr</th>
              <th>Balance</th>
              <th>Site Location</th>
            </tr>
          </thead>
          <tbody >
            <?php
            $j=$i=0;$sl_no=1;
            $op_balance = $total_debit = $total_credit = $total_balance = 0;
              $op_balance = $selected_account_data->op_balance;
              if($selected_start_date==''){

            if($selected_account_data->acc_supplier_id!=0){
              $total_balance -= $op_balance;
              $total_debit+=$op_balance;
            }else{
              $total_balance += $op_balance;
              $total_credit+=$op_balance;
            }

              ?>
              <tr>
                <td></td>
                <td><?php echo $selected_account_data->acc_head_name ?></td>
                <td>Account Opening Balance [on A/C Creation - <?php echo date('d-m-Y',strtotime($selected_account_data->created_date)) ?>]</td>
                <?php if($selected_account_data->acc_supplier_id!=0 || $selected_account_prefix=='labour'){ ?>
                <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($op_balance,$this->config->item('Currency_currency_shortname')) ?></td>
                <td></td>
              <?php }else{ ?>
                <td></td>
                <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($op_balance,$this->config->item('Currency_currency_shortname')) ?></td>
              <?php } ?>
                <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($total_balance,$this->config->item('Currency_currency_shortname')) ?></td>
              </tr>
              <?php

            if($selected_account_data->acc_employee_id!=0){
              $total_balance -= $selected_account_data->salary_op_balance;
              $total_debit+=$selected_account_data->salary_op_balance;
              ?>
              <tr>
                <td></td>
                <td><?php echo $selected_account_data->acc_head_name ?></td>
                <td>Salary Opening Balance [on A/C Creation - <?php echo date('d-m-Y',strtotime($selected_account_data->created_date)) ?>]</td>
                <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($selected_account_data->salary_op_balance,$this->config->item('Currency_currency_shortname')) ?></td>
                <td></td>
                <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($total_balance,$this->config->item('Currency_currency_shortname')) ?></td>
              </tr>
              <?php
            }
          }
         ?>
            <?php if($selected_start_date!=''){
              $current_op_balance = $selected_account_data->current_op_balance;
              $total_balance+=$current_op_balance;
              $total_credit+=$current_op_balance;
              ?>
              <tr>
                <td></td>
                <td><?php echo $selected_account_data->acc_head_name ?></td>
                <td>Opening Balance on <?php echo date('d-m-Y',strtotime($selected_start_date)) ?></td>
              <td></td>
              <td><?php echo $current_op_balance ?></td>
              <td><?php echo $total_balance ?></td>
            </tr>

            <?php } ?>
            <?php
            foreach ($transactions as $key => $value) {
              if($value['debit']!=''){
                $total_balance-=$value['debit'];
                $total_debit+=$value['debit'];
                $value['debit'] = $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($value['debit'],$this->config->item('Currency_currency_shortname'));
                ?>
                <tr>
                  <td><?php echo date('d-m-Y',strtotime($value['date'])) ?></td>
                  <td><?php echo $value['account'] ?></td>
                  <td><?php echo $value['description'] ?></td>
                  <td><?php echo $value['debit'] ?></td>
                  <td></td>
                  <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($total_balance,$this->config->item('Currency_currency_shortname')) ?></td>
                </tr>
              <?php }elseif($value['credit']!=''){
                $total_balance+=$value['credit'];
                $total_credit+=$value['credit'];
                $value['credit'] = $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($value['credit'],$this->config->item('Currency_currency_shortname'));
                ?>
                <tr>
                  <td><?php echo date('d-m-Y',strtotime($value['date'])) ?></td>
                  <td><?php echo $value['account'] ?></td>
                  <td><?php echo $value['description'] ?></td>
                  <td></td>
                  <td><?php echo $value['credit'] ?></td>
                  <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($total_balance,$this->config->item('Currency_currency_shortname')) ?></td>
                </tr>
                <?php
              }
            }
            ?>
            <tr>
              <td></td>
              <td></td>
              <td><strong>Total</strong></td>
              <td><strong><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($total_debit,$this->config->item('Currency_currency_shortname')) ?></strong></td>
              <td><strong><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($total_credit,$this->config->item('Currency_currency_shortname')) ?></strong></td>
              <td><strong><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($total_balance,$this->config->item('Currency_currency_shortname')) ?></strong></td>
            </tr>
          </tbody>

        </table>
        <!--end: Datatable -->

      </div>
    </div>
  <?php } ?>


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

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

<?php $this->view("includes/Inc_sidebar");?>
<?php $this->view("includes/Inc_scripts");?>

<!--begin::Page Snippets -->
<script>
  $("#accounts_menu").addClass('m-menu__item--open m-menu__item--active');
  $("#accounts_menu_4").addClass('m-menu__item--active');

  $("#start_date,#end_date").datepicker( {
    todayHighlight:!0, orientation:"bottom left", templates: {
      leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
    }
  })
  $("#account").select2({
    // dropdownParent: $("#form_purchase_add"),
    placeholder: "Select Supplier"
  })
  $("#form_account_ledger").validate( {
    rules: {
      account: {
        required: !0
      }
    },
    invalidHandler:function(e, r) {
      var i=$("#form_work_msg");
      i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
    }
    , submitHandler:function(e) {
      this.submit();
    }
  }
  );

  var ledger_table=function() {
    $('#ledger_view_table').dataTable().fnDestroy();
      var table = $('#ledger_view_table').DataTable({
            "scrollX" : true,
            buttons: [
            { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Supplier Material Report' },
            { extend: 'csv', className: 'btn purple btn-outline ' },
            { extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
            ],
            dom:"<'row'<'col-sm-6 text-left'B><'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
            "lengthMenu": [
            [25, 50, 100, -1],
            [25, 50, 100, "All"] // change per page values here
            ],
            // set the initial value
            "pageLength": 25,
            "columnDefs": [
            {
              "targets": [ 1 ],
              className: "hidden"
            }
            ],
            'ordering': false
          });
          $(".hide_column").hide();

      }

      jQuery(document).ready(function() {
        $(".hidden").hide();
          <?php if($selected_account!=''){  ?>
            ledger_table();
            <?php } ?>
      });

          </script>
