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

<style>
.item_table td{
  padding: .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%;
}
.m-invoice-2 .m-invoice__wrapper .m-invoice__body.m-invoice__body--centered {
  width: 90%;
}
</style>


<style>

@media print {

  .table-bordered td{
    padding: .75rem !important;
  }
  .table-bordered th{
    white-space: nowrap;
    padding: .75rem !important;
  }
  .table-bordered .table thead th {
    border-top: 2px solid #f4f5f8 !important;
  }
  .m-invoice-2 .m-invoice__wrapper .m-invoice__head .m-invoice__container.m-invoice__container--centered {
    width: 97%;
  }
  .m-invoice-2 .m-invoice__wrapper .m-invoice__body.m-invoice__body--centered {
    width: 97%;
  }
  .m-invoice-2 .m-invoice__wrapper .m-invoice__body.m-invoice__body--centered {
    margin-left: 10px !important;
  }

  #print_view {
    margin-top: -20%;
  }

  body * {
    visibility: hidden;
  }
  .m-portlet__body * {
    visibility: visible;
    overflow: visible !important;
  }
  .letter_head_img{
    display: block !important;
  }
}
.m-invoice-2 .m-invoice__wrapper .m-invoice__head .m-invoice__container.m-invoice__container--centered {
  width: 90%;
}
</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">
          Delivery Challan Details
        </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().'purchase_order' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Delivery Challan Details
              </span>
            </a>
          </li>


        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <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" >
          <?php
          if($challan_data->order_id != 0){?>
            <a href="<?php echo base_url().'purchase_order/profile/'.$challan_data->order_id?>" style="margin-left: 5px;margin-right: 5px" class="btn m-btn--gradient-from-primary m-btn--gradient-to-success  m-btn  m-btn--icon m-btn--air m-btn--pill">
              <span>
                <i class="fa fa-eye"></i>
                <span>
                  View Purchase Order of This Order
                </span>
              </span>
            </a>
          <?php } ?>

          <?php if($this->session->userdata('user_role') != 'auditor' || $this->session->userdata('user_data_permission')['adding']==1) { ?>
            <?php if($challan_data->entry_approval_status==1){ ?>
              <a href="<?php echo base_url().'purchases/add?challan_id='.$challan_data->challan_id?>" style="margin-left: 5px;margin-right: 5px" class="btn m-btn--gradient-from-warning m-btn--gradient-to-success  m-btn  m-btn--icon m-btn--air m-btn--pill">
                <span>
                  <i class="fa fa-plus-circle"></i>
                  <span>
                    Create Purchase Bill
                  </span>
                </span>
              </a>
            <?php } ?>

          <?php } ?>
          <?php if($this->session->userdata('user_id')==$challan_data->challan_created_by){?>
            <!-- <a href="<?php echo base_url().'purchases/delivery_challan/edit/'.$challan_data->challan_id?>" style="margin-left: 5px;margin-right: 5px" class="btn m-btn--gradient-from-focus m-btn--gradient-to-danger  m-btn  m-btn--icon m-btn--air m-btn--pill">
              <span>
                <i class="fa fa-edit"></i>
                <span>
                  Edit Challan
                </span>
              </span>
            </a> -->
          <?php } ?>
          <?php if($challan_data->entry_approval_status==1){ ?>
            <a href="javascript:;" onclick="window.print()" id="btn_print_bill" style="margin-left: 5px;margin-right: 5px" class="btn m-btn--gradient-from-danger m-btn--gradient-to-info  m-btn  m-btn--icon m-btn--air m-btn--pill">
              <span>
                <i class="fa fa-print"></i>
                <span>
                  Print Challan
                </span>
              </span>
            </a>
          <?php } ?>
        </div>
      </div>
      <div class="m-portlet__body m-portlet__body--no-padding" id="print_view">
        <?php if($this->config->item('company_letter_head')!=''){ ?>
          <img class="letter_head_img" style="max-width: 1000px;display:none" src="<?php echo base_url().'uploads/company/'.$this->config->item('company_letter_head')?>" />
        <?php } ?>
        <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="m-invoice__logo" style="padding-top: 3rem;">
                  <a href="#">

                  </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;">
                      <span class="m-invoice__subtitle">Supplier.</span>
                      <span><strong><?php echo $challan_data->supplier_name?></strong></span>
                      <span><strong><?php echo $challan_data->supplier_address?></strong></span>

                    </span>
                  </div>
                  <div class="col-md-4">
                    <div class="m-invoice__logo" style="padding-top: 0px;">
                      <a href="#">
                        <h2>DELIVERY CHALLAN</h2>
                        <h2>#<?php echo $challan_data->challan_id?></h2>
                        <?php if($challan_data->entry_approval_status!=1){ ?>
                          <span class='m-badge m-badge--danger badge-lg m-badge--wide'><i class='fa fa-times-circle'></i> Order Not Approved</span>
                        <?php } ?>
                      </a>
                    </div>

                  </div>
                </div>
                <div class="m-invoice__items" style="padding: 2rem 0 3rem 0;">
                  <div class="m-invoice__item">
                    <span class="m-invoice__subtitle">ORDERED TO </span>
                    <span class="m-invoice__text"><strong><?php echo $challan_data->location_name?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: center">
                    <span class="m-invoice__subtitle">ORDER DATE</span>
                    <span class="m-invoice__text"><strong><?php echo date('d-m-Y',strtotime($challan_data->order_date))?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: center">
                    <span class="m-invoice__subtitle">REFERENCE NO</span>
                    <span class="m-invoice__text"><strong><?php echo $challan_data->reference_no ?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: center">
                    <span class="m-invoice__subtitle">VEHICLE NO</span>
                    <span class="m-invoice__text"><strong><?php echo $challan_data->vehicle_no ?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: right">
                    <span class="m-invoice__subtitle">CREATED BY</span>
                    <span class="m-invoice__text"><strong><?php echo $challan_data->user_alias?></strong></span>
                  </div>
                </div>
                <div class="m-invoice__items" style="padding: 2rem 0 3rem 0;">
                  <div class="m-invoice__item">
                    <span class="m-invoice__subtitle">CHALLAN NO </span>
                    <span class="m-invoice__text"><strong><?php echo $challan_data->challan_no?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: center">
                    <span class="m-invoice__subtitle">CHALLAN DATE</span>
                    <span class="m-invoice__text"><strong><?php echo date('d-m-Y',strtotime($challan_data->challan_date))?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: center">
                    <span class="m-invoice__subtitle">DELIVERY DATE</span>
                    <span class="m-invoice__text"><strong><?php echo date('d-m-Y',strtotime($challan_data->delivery_date))?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: center">
                    <span class="m-invoice__subtitle">GATE ENTRY</span>
                    <span class="m-invoice__text"><strong><?php echo $challan_data->gate_entry ?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: right">
                    <span class="m-invoice__subtitle">QC STAMP</span>
                    <span class="m-invoice__text"><strong><?php echo $challan_data->qc_stamp?></strong></span>
                  </div>
                </div>

                <div class="m-invoice__items" style="padding: 2rem 0 3rem 0;">

                  <div class="m-invoice__item" style="text-align: left">
                    <span class="m-invoice__subtitle">BILLING ADDRESS </span>
                    <span class="m-invoice__text"><strong><?php echo nl2br($challan_data->billing_address) ?></strong></span>
                  </div>

                  <div class="m-invoice__item" style="text-align: right">
                    <span class="m-invoice__subtitle">DELIVERY ADDRESS</span>
                    <span class="m-invoice__text"><strong><?php echo nl2br($challan_data->delivery_address) ?></strong></span>
                  </div>

                </div>

              </div>
            </div>
            <div class="m-invoice__body m-invoice__body--centered" style="min-height: 200px">
              <div class="table-responsive">
                <table class="table m-table table-bordered item_table">
                  <thead>
                    <tr>

                      <th >SNo</th>
                      <th  style="text-align: left">Material Name</th>
                      <th  >Unit</th>
                      <th  >Unit Price</th>
                      <th style="text-align: center"  >Delivery Qty</th>
                      <th  >Est. Amount</th>
                    </tr>

                  </thead>
                  <tbody>
                    <?php
                    $n = 1;
                    foreach($challan_items as $row):
                      ?>
                      <tr>
                        <td><?php echo $n++?></td>
                        <td style="text-align: left"><?php echo $row->material_name?><br/><?php echo $row->item_code?></td>
                        <td><?php echo $row->unit_name?></td>
                        <td style="white-space:nowrap"><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($row->unit_price,$this->config->item('Currency_currency_shortname'));?></td>
                        <td><?php echo $row->delivery_qty?></td>
                        <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($row->delivery_qty*$row->unit_price,$this->config->item('Currency_currency_shortname'));?></td>
                      </tr>
                      <tr>
                        <td style="border-bottom: 1px solid #c5c5c5"></td>
                        <td colspan="5" style="text-align: left;font-weight: 300;border-bottom: 1px solid #c5c5c5">Description: <?php echo $row->description?></td>
                      </tr>
                      <?php
                    endforeach;
                    ?>
                  </tbody>
                </table>
              </div>
              <div class="m-invoice__table  m-invoice__table--centered table-responsive" style="margin-bottom: 20px">
                <table class="table">
                  <thead>
                    <tr>
                      <th>
                        <?php echo "Delivery Challan Created From Purchase Order #".$challan_data->order_id; ?>
                      </th>
                      <th width="40%">
                        Purchase Bill Status :
                        <?php if(sizeof($purchase_bills)>0){
                          $no_of_purchase_bills = sizeof($purchase_bills); ?>
                        <span class='m-badge m-badge--success m-badge--wide'><i class='fa fa-check-circle'></i> <?= $no_of_purchase_bills?> Purchase Bills Created</span>
                        <?php
                        foreach ($purchase_bills as $key => $value) { ?>
                          <div style="margin-top:10px !important">
                          <a target="_blank" href="<?php echo base_url().'purchases/profile/'.$value->purchase_id  ?>"><span class='m-badge m-badge--focus m-badge--wide'><i class='fa fa-check-circle'></i>  View Purchase Bill #<?= $value->purchase_id ?></span></a><br/>
                        </div>
                        <?php } ?>
                      <?php }else{?>
                        <span class='m-badge m-badge--danger m-badge--wide'><i class='fa fa-times-circle'></i> Purchase Bill Not Created</span>
                      <?php } ?>
                    </th>
                    </tr>
                </thead>
              </table>
              </div>
            </div>

        </div>
      </div>
    </div>
  </div>
  <?php $this->view("modules/include_modals/modal_bills");?>
</div>
</div>
</div>

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

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

<!-- end:: Page -->

<?php $this->view("includes/Inc_sidebar");?>
<?php $this->view("includes/Inc_scripts");?>
<!--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");

$("#delivery_challan_menu").addClass('m-menu__item--active');
$("#page_title").text("Delivery Challan - #<?php echo $challan_data->challan_id.' ['.$challan_data->supplier_name.'] ['.$challan_data->work_name.']'?>");
</script>

<script>

$(".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: {
      "paid_amount": {
        required: !0,
        min: 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);
        $("#"+modal_id).modal('toggle');
        toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
        location.reload();
      } 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>
