{% extends "page_base.html" %}

{% load i18n %}
{% load static %}
{% load inventree_extras %}
{% load status_codes %}

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

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

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

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

{% block actions %}
{% if user.is_staff and roles.sales_order.change %}
{% url 'admin:order_salesorder_change' order.pk as url %}
{% include "admin_button.html" with url=url %}
{% endif %}
<!-- Printing actions -->
<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 sales 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>
        <!--
        <li><a class='dropdown-item' href='#' id='print-packing-list'><span class='fas fa-clipboard-list'></span>{% trans "Print packing list" %}</a></li>
        -->
    </ul>
</div>
{% if roles.sales_order.change %}
<!-- Order actions -->
<div class='btn-group' role='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.status == SalesOrderStatus.PENDING %}
        <li><a class='dropdown-item' href='#' id='complete-order-shipments'><span class='fas fa-truck'></span> {% trans "Complete Shipments" %}</a></li>
        <li><a class='dropdown-item' href='#' id='cancel-order'><span class='fas fa-times-circle icon-red'></span> {% trans "Cancel order" %}</a></li>
        {% endif %}
    </ul>

</div>
{% if order.status == SalesOrderStatus.PENDING %}
<button type='button' class='btn btn-success' id='complete-order' title='{% trans "Complete Sales Order" %}'{% if not order.is_completed %} disabled{% endif %}>
    <span class='fas fa-check-circle'></span> {% trans "Complete Order" %}
</button>
{% endif %}
{% endif %}
{% endblock actions %}

{% 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>
    <tr>
        <td><span class='fas fa-info'></span></td>
        <td>{% trans "Order Status" %}</td>
        <td>
            {% sales_order_status_label order.status %}
            {% if order.is_overdue %}
            <span class='badge rounded-pill bg-danger'>{% trans "Overdue" %}</span>
            {% endif %}
        </td>
    </tr>
</table>

<div class='info-messages'>
    {% if order.status == SalesOrderStatus.PENDING and not order.is_fully_allocated %}
    <div class='alert alert-block alert-danger'>
        {% trans "This Sales Order has not been fully allocated" %}
    </div>
    {% endif %}
</div>

{% endblock %}

{% block details_right %}
<table class='table table-striped table-condensed'>
    <col width='25'>
    {% if order.customer %}
    <tr>
        <td><span class='fas fa-building'></span></td>
        <td>{% trans "Customer" %}</td>
        <td><a href="{% url 'company-detail' order.customer.id %}">{{ order.customer.name }}</a>{% include "clip.html"%}</td>
    </tr>
    {% endif %}
    {% if order.customer_reference %}
    <tr>
        <td><span class='fas fa-hashtag'></span></td>
        <td>{% trans "Customer Reference" %}</td>
        <td>{{ order.customer_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_completed %}
            <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>
    <tr>
        <td><span class='fas fa-truck'></span></td>
        <td>{% trans "Completed Shipments" %}</td>
        <td>
            {{ order.completed_shipment_count }} / {{ order.shipment_count }}
            {% if order.pending_shipment_count > 0 %}
            <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>External Link</td>
        <td><a href="{{ order.link }}">{{ order.link }}</a>{% include "clip.html"%}</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.target_date %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Target Date" %}</td>
        <td>{% render_date order.target_date %}</td>
    </tr>
    {% endif %}
    {% if order.shipment_date %}
    <tr>
        <td><span class='fas fa-truck'></span></td>
        <td>{% trans "Completed" %}</td>
        <td>
            {% render_date order.shipment_date %}
            {% if order.shipped_by %}
            <span class='badge badge-right rounded-pill bg-dark'>{{ order.shipped_by }}</span>
            {% endif %}
        </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 %}

    <tr>
        <td><span class='fas fa-dollar-sign'></span></td>
        <td>{% trans "Total cost" %}</td>
        <td id="soTotalPrice">
            {% with order.get_total_price as tp %}
            {% if tp == None %}
            <span class='badge bg-warning'>{% trans "Total cost could not be calculated" %}</span>
            {% else %}
            {{ tp }}
            {% endif %}
            {% endwith %}
        </td>
    </tr>
</table>
{% endblock %}

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

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

    constructForm('{% url "api-so-detail" order.pk %}', {
        fields: {
            reference: {
                icon: 'fa-hashtag',
            },
            {% if order.lines.count == 0 and order.status == SalesOrderStatus.PENDING %}
            customer: {
            },
            {% endif %}
            customer_reference: {},
            description: {},
            target_date: {
                icon: 'fa-calendar-alt',
            },
            link: {
                icon: 'fa-link',
            },
            responsible: {
                icon: 'fa-user',
            },
        },
        title: '{% trans "Edit Sales Order" %}',
        reload: true,
    });
});

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

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

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

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

$("#complete-order").click(function() {
    constructForm('{% url "api-so-complete" order.id %}', {
        method: 'POST',
        title: '{% trans "Complete Sales Order" %}',
        confirm: true,
        reload: true,
    });
});

{% if report_enabled %}
$('#print-order-report').click(function() {
    printSalesOrderReports([{{ order.pk }}]);
});
{% endif %}

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

{% endblock %}
