{% extends "page_base.html" %}

{% load i18n %}
{% load static %}
{% load inventree_extras %}
{% load generic %}

{% block page_title %}
{% inventree_title %} | {% trans "Purchase Order" %}
{% endblock page_title %}

{% block breadcrumbs %}
<li class='breadcrumb-item'><a href='{% url "purchase-order-index" %}'>{% trans "Purchase Orders" %}</a></li>
<li class="breadcrumb-item active" aria-current="page"><a href='{% url "po-detail" order.id %}'>{{ order }}</a></li>
{% endblock breadcrumbs %}

{% block heading %}
{% trans "Purchase Order" %}: {{ order.reference }}
{% endblock heading %}

{% block actions %}
{% if user.is_staff and roles.purchase_order.change %}
{% url 'admin:order_purchaseorder_change' order.pk as url %}
{% include "admin_button.html" with url=url %}
{% endif %}
{% if barcodes %}
<!-- Barcode actions menu -->
<div class='btn-group' role='group'>
    <button id='barcode-options' title='{% trans "Barcode actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'>
        <span class='fas fa-qrcode'></span> <span class='caret'></span>
    </button>
    <ul class='dropdown-menu' role='menu'>
        <li><a class='dropdown-item' href='#' id='show-qr-code'><span class='fas fa-qrcode'></span> {% trans "Show QR Code" %}</a></li>
        {% if roles.purchase_order.change %}
        {% if order.barcode_hash %}
        <li><a class='dropdown-item' href='#' id='barcode-unlink'><span class='fas fa-unlink'></span> {% trans "Unlink Barcode" %}</a></li>
        {% else %}
        <li><a class='dropdown-item' href='#' id='barcode-link'><span class='fas fa-link'></span> {% trans "Link Barcode" %}</a></li>
        {% endif %}
        {% endif %}
    </ul>
</div>
{% endif %}
<!-- Printing options -->
<div class='btn-group' role='group'>
    <button id='print-options' title='{% trans "Print actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'>
        <span class='fas fa-print'></span> <span class='caret'></span>
    </button>
    <ul class='dropdown-menu' role='menu'>
        {% if report_enabled %}
        <li><a class='dropdown-item' href='#' id='print-order-report'><span class='fas fa-file-pdf'></span> {% trans "Print purchase order report" %}</a></li>
        {% endif %}
        <li><a class='dropdown-item' href='#' id='export-order'><span class='fas fa-file-download'></span> {% trans "Export order to file" %}</a></li>
    </ul>
</div>
{% if roles.purchase_order.change %}
<!-- order actions -->
<div class='btn-group'>
    <button id='order-options' title='{% trans "Order actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'>
        <span class='fas fa-tools'></span> <span class='caret'></span>
    </button>
    <ul class='dropdown-menu' role='menu'>
        <li><a class='dropdown-item' href='#' id='edit-order'>
            <span class='fas fa-edit icon-green'></span> {% trans "Edit order" %}
        </a></li>
        {% if order.can_cancel %}
        <li><a class='dropdown-item' href='#' id='cancel-order'>
            <span class='fas fa-times-circle icon-red'></span> {% trans "Cancel order" %}
        </a></li>
        {% endif %}
        {% if roles.purchase_order.add %}
        <li><a class='dropdown-item' href='#' id='duplicate-order'>
            <span class='fas fa-clone'></span> {% trans "Duplicate order" %}
        </a></li>
        {% endif %}
    </ul>
</div>
{% if order.is_pending %}
<button type='button' class='btn btn-primary' id='place-order' title='{% trans "Issue Order" %}'>
    <span class='fas fa-paper-plane'></span> {% trans "Issue Order" %}
</button>
{% elif order.is_open %}
<button type='button' class='btn btn-success' id='complete-order' title='{% trans "Mark order as complete" %}'>
    <span class='fas fa-check-circle'></span> {% trans "Complete Order" %}
</button>
{% endif %}
{% endif %}
{% endblock actions %}

{% block thumbnail %}
<img alt="{% trans "Supplier part thumbnail" %}"  class='part-thumb'
{% if order.supplier and order.supplier.image %}
src="{{ order.supplier.image.url }}"
{% else %}
src="{% static 'img/blank_image.png' %}"
{% endif %}
/>
{% endblock thumbnail %}

{% block details %}

<table class='table table-striped table-condensed'>
    <col width='25'>
    <tr>
        <td><span class='fas fa-hashtag'></span></td>
        <td>{% trans "Order Reference" %}</td>
        <td>{{ order.reference }}{% include "clip.html" %}</td>
    </tr>
    <tr>
        <td><span class='fas fa-info-circle'></span></td>
        <td>{% trans "Order Description" %}</td>
        <td>{{ order.description }}{% include "clip.html" %}</td>
    </tr>
    {% include "project_code_data.html" with instance=order %}
    {% include "barcode_data.html" with instance=order %}
    <tr>
        <td><span class='fas fa-info'></span></td>
        <td>{% trans "Order Status" %}</td>
        <td>
            {% status_label 'purchase_order' order.status %}
            {% if order.is_overdue %}
            <span class='badge rounded-pill bg-danger'>{% trans "Overdue" %}</span>
            {% endif %}
        </td>
    </tr>

</table>

{% endblock details %}

{% block details_right %}
<table class='table table-condensed table-striped'>
    <col width='25'>
    <tr>
        <td><span class='fas fa-building'></span></td>
        <td>{% trans "Supplier" %}</td>
        <td>
            {% if order.supplier %}
            <a href="{% url 'company-detail' order.supplier.id %}">{{ order.supplier.name }}</a>{% include "clip.html" %}
            {% else %}
            <em>{% trans "No suppplier information available" %}</em>
            {% endif %}
        </td>
    </tr>
    {% if order.supplier_reference %}
    <tr>
        <td><span class='fas fa-hashtag'></span></td>
        <td>{% trans "Supplier Reference" %}</td>
        <td>{{ order.supplier_reference }}{% include "clip.html" %}</td>
    </tr>
    {% endif %}
    <tr>
        <td><span class='fas fa-tasks'></span></td>
        <td>{% trans "Completed Line Items" %}</td>
        <td>
            {{ order.completed_line_count }} / {{ order.line_count }}
            {% if order.is_complete %}
            <span class='badge bg-success badge-right rounded-pill'>{% trans "Complete" %}</span>
            {% else %}
            <span class='badge bg-danger badge-right rounded-pill'>{% trans "Incomplete" %}</span>
            {% endif %}
        </td>
    </tr>
    {% if order.link %}
    <tr>
        <td><span class='fas fa-link'></span></td>
        <td>{% trans "External Link" %}</td>
        <td>{% include 'clip_link.html' with link=order.link %}</td>
    </tr>
    {% endif %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Created" %}</td>
        <td>{% render_date order.creation_date %}<span class='badge badge-right rounded-pill bg-dark'>{{ order.created_by }}</span></td>
    </tr>
    {% if order.issue_date %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Issued" %}</td>
        <td>{% render_date order.issue_date %}</td>
    </tr>
    {% endif %}
    {% if order.target_date %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Target Date" %}</td>
        <td>
            {% render_date order.target_date %}
            {% if order.is_overdue %}<span class='fas fa-calendar-times icon-red float-right'></span>{% endif %}
        </td>
    </tr>
    {% endif %}
    {% if order.status == PurchaseOrderStatus.COMPLETE %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Received" %}</td>
        <td>{% render_date order.complete_date %}<span class='badge badge-right rounded-pill bg-dark'>{{ order.received_by }}</span></td>
    </tr>
    {% endif %}
    {% if order.contact %}
    <tr>
        <td><span class='fas fa-user-tie'></span></td>
        <td>{% trans "Contact" %}</td>
        <td>{{ order.contact.name }}</td>
    </tr>
    {% endif %}
    {% if order.address %}
    <tr>
        <td><span class='fas fa-map'></span></td>
        <td>{% trans "Address" %}</td>
        <td><b>{{ order.address.title }}</b>: {{ order.address }}</td>
    </tr>
    {% endif %}
    {% if order.responsible %}
    <tr>
        <td><span class='fas fa-users'></span></td>
        <td>{% trans "Responsible" %}</td>
        <td>{{ order.responsible }}</td>
    </tr>
    {% endif %}
    {% include "currency_data.html" with instance=order %}
    <tr>
        <td><span class='fas fa-dollar-sign'></span></td>
        <td>{% trans "Total cost" %}</td>
        <td id="poTotalPrice">
            {% with order.total_price as tp %}
            {% if tp == None %}
            <span class='badge bg-warning'>{% trans "Total cost could not be calculated" %}</span>
            {% else %}
            {% render_currency tp currency=order.currency %}
            {% endif %}
            {% endwith %}
        </td>
    </tr>
</table>
{% endblock details_right %}

{% block js_ready %}
{{ block.super }}

{% if order.status == PurchaseOrderStatus.PENDING %}
$("#place-order").click(function() {

    issuePurchaseOrder(
        {{ order.pk }},
        {
            reload: true,
        }
    );

});
{% endif %}

{% if report_enabled %}
$('#print-order-report').click(function() {
    printReports({
        items: [{{ order.pk }}],
        key: 'order',
        url: '{% url "api-po-report-list" %}',
    });
});
{% endif %}

{% if roles.purchase_order.change %}

$("#edit-order").click(function() {

    editPurchaseOrder({{ order.pk }}, {
        {% if order.lines.count > 0 or order.status != PurchaseOrderStatus.PENDING %}
        hide_supplier: true,
        {% endif %}
        reload: true,
    });
});

$("#complete-order").click(function() {

    completePurchaseOrder(
        {{ order.pk }},
        {
            onSuccess: function() {
                window.location.reload();
            }
        }
    );
});

$("#cancel-order").click(function() {

    cancelPurchaseOrder(
        {{ order.pk }},
        {
            onSuccess: function() {
                window.location.reload();
            }
        },
    );
});

{% endif %}

{% if roles.purchase_order.add %}
$('#duplicate-order').click(function() {
    duplicatePurchaseOrder(
        {{ order.pk }},
    );
});
{% endif %}

$("#export-order").click(function() {
    exportOrder('{% url "po-export" order.id %}');
});

{% if barcodes %}
<!-- Barcode functionality callbacks -->
$('#show-qr-code').click(function() {
    showQRDialog(
        '{% trans "Purchase Order QR Code" %}',
        '{"purchaseorder": {{ order.pk }} }'
    );
});

{% if roles.purchase_order.change %}
$("#barcode-link").click(function() {
    linkBarcodeDialog(
        {
            purchaseorder: {{ order.pk }},
        },
        {
            title: '{% trans "Link Barcode to Purchase Order" %}',
        }
    );
});

$("#barcode-unlink").click(function() {
    unlinkBarcode({
        purchaseorder: {{ order.pk }},
    });
});
{% endif %}
{% endif %}

{% endblock js_ready %}
