{% extends "page_base.html" %}
{% load static %}
{% load plugin_extras %}
{% load inventree_extras %}
{% load status_codes %}
{% load i18n %}
{% load l10n %}

{% block page_title %}
{% inventree_title %} | {% trans "Stock Item" %} - {{ item }}
{% endblock %}

{% block breadcrumbs %}
<a href='#' id='breadcrumb-tree-toggle' class="breadcrumb-item"><span class="fas fa-bars"></span></a>
{% include 'stock/loc_link.html' with location=item.location %}
{% endblock %}

{% block breadcrumb_tree %}
<div id="breadcrumb-tree"></div>
{% endblock breadcrumb_tree %}

{% block heading %}
    {% trans "Stock Item" %}: {{ item.part.full_name}}
{% endblock heading %}

{% block actions %}

{% if user.is_staff and roles.stock.change %}
{% url 'admin:stock_stockitem_change' item.pk as url %}
{% include "admin_button.html" with url=url %}
{% endif %}
{% mixin_available "locate" as locate_available %}
{% if plugins_enabled and locate_available %}
<button id='locate-item-button' title='{% trans "Locate stock item" %}' class='btn btn-outline-secondary' typy='button'>
    <span class='fas fa-search-location'></span>
</button>
{% 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.stock.change %}
        {% if item.uid %}
        <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 %}
        <li><a class='dropdown-item' href='#' id='barcode-scan-into-location'><span class='fas fa-sitemap'></span> {% trans "Scan to Location" %}</a></li>
        {% endif %}
    </ul>
</div>
{% endif %}
<!-- Document / label menu -->
{% if test_report_enabled or labels_enabled %}
<div class='btn-group' role='group'>
    <button id='document-options' title='{% trans "Printing 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 labels_enabled %}
        <li><a class='dropdown-item' href='#' id='print-label'><span class='fas fa-tag'></span> {% trans "Print Label" %}</a></li>
        {% endif %}
        {% if test_report_enabled %}
        <li><a class='dropdown-item' href='#' id='stock-test-report'><span class='fas fa-file-pdf'></span> {% trans "Test Report" %}</a></li>
        {% endif %}
    </ul>
</div>
{% endif %}

<!-- Stock adjustment menu -->
{% if user_owns_item %}
    {% if roles.stock.change and not item.is_building %}
    <div class='btn-group'>
        <button id='stock-actions' title='{% trans "Stock adjustment actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'><span class='fas fa-boxes'></span> <span class='caret'></span></button>
        <ul class='dropdown-menu' role='menu'>
            {% if not item.serialized %}
            {% if item.in_stock %}
            <li><a class='dropdown-item' href='#' id='stock-count' title='{% trans "Count stock" %}'><span class='fas fa-check-circle icon-green'></span> {% trans "Count stock" %}</a></li>
            {% endif %}
            {% if not item.customer %}
            <li><a class='dropdown-item' href='#' id='stock-add' title='{% trans "Add stock" %}'><span class='fas fa-plus-circle icon-green'></span> {% trans "Add stock" %}</a></li>
            {% endif %}
            {% if item.in_stock %}
            <li><a class='dropdown-item' href='#' id='stock-remove' title='{% trans "Remove stock" %}'><span class='fas fa-minus-circle icon-red'></span> {% trans "Remove stock" %}</a></li>
            {% endif %}
            {% if item.in_stock and item.part.trackable %}
            <li><a class='dropdown-item' href='#' id='stock-serialize' title='{% trans "Serialize stock" %}'><span class='fas fa-hashtag'></span> {% trans "Serialize stock" %}</a> </li>
            {% endif %}
            {% endif %}
            {% if item.in_stock and item.can_adjust_location %}
            <li><a class='dropdown-item' href='#' id='stock-move' title='{% trans "Transfer stock" %}'><span class='fas fa-exchange-alt icon-blue'></span> {% trans "Transfer stock" %}</a></li>
            {% endif %}
            {% if item.in_stock and item.can_adjust_location and item.part.salable and not item.customer %}
            <li><a class='dropdown-item' href='#' id='stock-assign-to-customer' title='{% trans "Assign to customer" %}'><span class='fas fa-user-tie'></span> {% trans "Assign to customer" %}</a></li>
            {% endif %}
            {% if item.customer %}
            <li><a class='dropdown-item' href='#' id='stock-return-from-customer' title='{% trans "Return to stock" %}'><span class='fas fa-undo'></span> {% trans "Return to stock" %}</a></li>
            {% endif %}
            {% if item.belongs_to %}
            <li><a class='dropdown-item' href='#' id='stock-uninstall' title='{% trans "Uninstall stock item" %}'><span class='fas fa-unlink'></span> {% trans "Uninstall" %}</a></li>
            {% else %}
            {% if item.part.get_used_in %}
            <!--
                <li><a class='dropdown-item' href='#' id='stock-install-in' title='{% trans "Install stock item" %}'><span class='fas fa-link'></span> {% trans "Install" %}</a></li>
            -->
            {% endif %}
            {% endif %}
        </ul>
    </div>
    {% endif %}
    <!-- Edit stock item -->
    {% if roles.stock.change and not item.is_building %}
    <div class='btn-group'>
        <button id='stock-edit-actions' title='{% trans "Stock 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'>
            {% if item.part.can_convert %}
            <li><a class='dropdown-item' href='#' id='stock-convert' title='{% trans "Convert to variant" %}'><span class='fas fa-screwdriver'></span> {% trans "Convert to variant" %}</a></li>
            {% endif %}
            {% if roles.stock.add %}
            <li><a class='dropdown-item' href='#' id='stock-duplicate' title='{% trans "Duplicate stock item" %}'><span class='fas fa-copy'></span> {% trans "Duplicate stock item" %}</a></li>
            {% endif %}
            <li><a class='dropdown-item' href='#' id='stock-edit' title='{% trans "Edit stock item" %}'><span class='fas fa-edit icon-blue'></span> {% trans "Edit stock item" %}</a></li>
            {% if user.is_staff or roles.stock.delete %}
            {% if item.can_delete %}
            <li><a class='dropdown-item' href='#' id='stock-delete' title='{% trans "Delete stock item" %}'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete stock item" %}</a></li>
            {% endif %}
            {% endif %}
        </ul>
    </div>
    {% endif %}
{% endif %}
{% endblock actions %}

{% block thumbnail %}
<img class='part-thumb' {% if item.part.image %}src="{{ item.part.image.preview.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-shapes'></span></td>
        <td>{% trans "Base Part" %}</td>
        <td>
            {% if roles.part.view %}
            <a href="{% url 'part-detail' item.part.id %}">
            {% endif %}
            {{ item.part.full_name }}
            {% if roles.part.view %}
            </a>
            {% endif %}
        </td>
    </tr>

    {% if item.uid %}
    <tr>
        <td><span class='fas fa-barcode'></span></td>
        <td>{% trans "Barcode Identifier" %}</td>
        <td>{{ item.uid }}</td>
    </tr>
    {% endif %}
    {% if item.batch %}
    <tr>
        <td><span class='fas fa-layer-group'></span></td>
        <td>{% trans "Batch" %}</td>
        <td>{{ item.batch }}</td>
    </tr>
    {% endif %}
    {% if item.build %}
    <tr>
        <td><span class='fas fa-tools'></span></td>
        <td>{% trans "Build" %}</td>
        <td><a href="{% url 'build-detail' item.build.id %}">{{ item.build }}</a></td>
    </tr>
    {% endif %}
    {% if item.purchase_order %}
    <tr>
        <td><span class='fas fa-shopping-cart'></span></td>
        <td>{% trans "Purchase Order" %}</td>
        <td><a href="{% url 'po-detail' item.purchase_order.id %}">{{ item.purchase_order }}</a></td>
    </tr>
    {% endif %}
    {% if item.purchase_price != None %}
    <tr>
        <td><span class='fas fa-dollar-sign'></span></td>
        <td>{% trans "Purchase Price" %}</td>
        <td>{{ item.purchase_price }}</td>
    </tr>
    {% endif %}
    {% if item.parent %}
    <tr>
        <td><span class='fas fa-sitemap'></span></td>
        <td>{% trans "Parent Item" %}</td>
        <td><a href="{% url 'stock-item-detail' item.parent.id %}">{% trans "Stock Item" %} #{{ item.parent.id }}</a></td>
    </tr>
    {% endif %}
    {% if item.link %}
    <tr>
        <td><span class='fas fa-link'></span>
        <td>{% trans "External Link" %}</td>
        <td><a href="{{ item.link }}">{{ item.link }}</a></td>
    </tr>
    {% endif %}
    {% if item.supplier_part.manufacturer_part %}
    <tr>
        <td><span class='fas fa-industry'></span></td>
        <td>{% trans "Manufacturer" %}</td>
        {% if item.supplier_part.manufacturer_part.manufacturer %}
            <td><a href="{% url 'company-detail' item.supplier_part.manufacturer_part.manufacturer.id %}">{{ item.supplier_part.manufacturer_part.manufacturer.name }}</a></td>
        {% else %}
            <td><em>{% trans "No manufacturer set" %}</em></td>
        {% endif %}

    </tr>
    <tr>
        <td><span class='fas fa-hashtag'></span></td>
        <td>{% trans "Manufacturer Part" %}</td>
        <td><a href="{% url 'manufacturer-part-detail' item.supplier_part.manufacturer_part.id %}">{{ item.supplier_part.manufacturer_part.MPN }}</a></td>
    </tr>
    {% endif %}
    {% if item.supplier_part %}
    <tr>
        <td><span class='fas fa-building'></span></td>
        <td>{% trans "Supplier" %}</td>
        <td>
            {% if item.supplier_part.supplier %}
            <a href="{% url 'company-detail' item.supplier_part.supplier.id %}">{{ item.supplier_part.supplier.name }}</a>
            {% endif %}
        </td>
    </tr>
    <tr>
        <td><span class='fas fa-shapes'></span></td>
        <td>{% trans "Supplier Part" %}</td>
        <td><a href="{% url 'supplier-part-detail' item.supplier_part.id %}">{{ item.supplier_part.SKU }}</a></td>
    </tr>
    {% endif %}
    {% if item.packaging %}
    <tr>
        <td><span class='fas fa-cube'></span></td>
        <td>{% trans "Packaging" %}</td>
        <td>{{ item.packaging }}</td>
    </tr>
    {% endif %}
    {% if ownership_enabled and item_owner %}
    <tr>
        <td><span class='fas fa-users'></span></td>
        <td>{% trans "Owner" %}</td>
        <td>
            {{ item_owner }}
            {% if not user_owns_item %}
            <span class='badge rounded-pill bg-warning badge-right' title='{% trans "You are not in the list of owners of this item. This stock item cannot be edited." %}'>
                {% trans "Read only" %}
            </span>
            {% endif %}
        </td>
    </tr>
    {% endif %}

</table>

<div class='info-messages'>

    {% if item.is_building %}
    <div class='alert alert-block alert-info'>
        {% trans "This stock item is in production and cannot be edited." %}<br>
        {% trans "Edit the stock item from the build view." %}<br>

        {% if item.build %}
        <a href="{% url 'build-detail' item.build.id %}">
            <strong>{{ item.build }}</strong>
        </a>
        {% endif %}

    </div>
    {% endif %}

    {% if item.hasRequiredTests and not item.passedAllRequiredTests %}
    <div class='alert alert-block alert-danger'>
        {% trans "This stock item has not passed all required tests" %}
    </div>
    {% endif %}

    {% for allocation in item.sales_order_allocations.all %}
    <div class='alert alert-block alert-info'>
        {% object_link 'so-detail' allocation.line.order.id allocation.line.order as link %}
        {% decimal allocation.quantity as qty %}
        {% trans "This stock item is allocated to Sales Order" %} {{ link }} {% if qty < item.quantity %}({% trans "Quantity" %}: {{ qty }}){% endif %}
    </div>
    {% endfor %}

    {% for allocation in item.allocations.all %}
    <div class='alert alert-block alert-info'>
        {% object_link 'build-detail' allocation.build.id allocation.build as link %}
        {% decimal allocation.quantity as qty %}
        {% trans "This stock item is allocated to Build Order" %} {{ link }} {% if qty < item.quantity %}({% trans "Quantity" %}: {{ qty }}){% endif %}
    </div>
    {% endfor %}

    {% if item.serialized %}
    <div class='alert alert-block alert-warning'>
        {% trans "This stock item is serialized - it has a unique serial number and the quantity cannot be adjusted." %}
    </div>
    {% endif %}
</div>
{% endblock details %}

{% block details_right %}
<table class="table table-striped table-condensed">
    <col width='25'>

    <tr>
        {% if item.serialized %}
        <td>
            <h5><span class='fas fa-hashtag'></span></h5>
        </td>
        <td>
            <h5>{% trans "Serial Number" %}</h5>
        </td>
        <td><h5>
            {{ item.serial }}
            <div class='btn-group float-right' role='group'>
                {% if previous %}
                <a class="btn btn-small btn-outline-secondary" aria-label="{% trans 'previous page' %}" href="{% url request.resolver_match.url_name previous.id %}" title='{% trans "Navigate to previous serial number" %}'>
                    <span class='fas fa-angle-left'></span>
                    <small>{{ previous.serial }}</small>
                </a>
                {% endif %}
                <a class='btn btn-small btn-outline-secondary text-sm' href='#' id='serial-number-search' title='{% trans "Search for serial number" %}'>
                    <span class='fas fa-search'></span>
                </a>
                {% if next %}
                <a class="btn btn-small btn-outline-secondary text-sm" aria-label="{% trans 'next page' %}" href="{% url request.resolver_match.url_name next.id %}" title='{% trans "Navigate to next serial number" %}'>
                    <small>{{ next.serial }}</small>
                    <span class='fas fa-angle-right'></span>
                </a>
                {% endif %}
            </div>
            </h5>
        </td>
        {% else %}
        <td>
            <h5><div class='fas fa-boxes'></div></h5>
        </td>
        <td>
            <h5>{% trans "Available Quantity" %}</h5>
        </td>
        <td>
            <h5>{% if item.quantity != available %}{% decimal available %} / {% endif %}{% decimal item.quantity %} {% if item.part.units %}{{ item.part.units }}{% endif %}</h5>
        </td>
        {% endif %}
    </tr>
    {% if item.belongs_to %}
    <tr>
        <td><span class='fas fa-box'></span></td>
        <td>
            {% trans "Installed In" %}
        </td>
        <td>
            <a href="{% url 'stock-item-detail' item.belongs_to.id %}">{{ item.belongs_to }}</a>
        </td>
    </tr>
    {% elif item.sales_order %}
    <tr>
        <td><span class='fas fa-user-tie'></span></td>
        <td>{% trans "Sales Order" %}</td>
        <td><a href="{% url 'so-detail' item.sales_order.id %}">{{ item.sales_order.reference }}</a> - <a href="{% url 'company-detail' item.sales_order.customer.id %}">{{ item.sales_order.customer.name }}</a></td>
    </tr>
    {% else %}
    {% if allocated_to_sales_orders %}
    <tr>
        <td><span class='fas fa-truck'></span></td>
        <td>{% trans "Allocated to Sales Orders" %}</td>
        <td>{% decimal allocated_to_sales_orders %}</td>
    </tr>
    {% endif %}
    {% if allocated_to_build_orders %}
    <tr>
        <td><span class='fas fa-tools'></span></td>
        <td>{% trans "Allocated to Build Orders" %}</td>
        <td>{% decimal allocated_to_build_orders %}</td>
    </tr>
    {% endif %}
    <tr>
        <td><span class='fas fa-map-marker-alt'></span></td>
        <td>{% trans "Location" %}</td>
        {% if item.location %}
        <td><a href="{% url 'stock-location-detail' item.location.id %}">{{ item.location.name }}</a></td>
        {% elif not item.customer %}
        <td><em>{% trans "No location set" %}</em></td>
        {% endif %}
    </tr>
    {% endif %}
    {% if item.customer %}
    <tr>
        <td><span class='fas fa-user-tie'></span></td>
        <td>{% trans "Customer" %}</td>
        <td><a href="{% url 'company-detail' item.customer.id %}?display=assigned-stock">{{ item.customer.name }}</a></td>
    </tr>
    {% endif %}

    {% if item.hasRequiredTests %}
    <tr>
        <td><span class='fas fa-vial'></span></td>
        <td>{% trans "Tests" %}</td>
        <td>
            {{ item.requiredTestStatus.passed }} / {{ item.requiredTestStatus.total }}
            {% if item.passedAllRequiredTests %}
            <span class='fas fa-check-circle float-right icon-green'></span>
            {% else %}
            <span class='fas fa-times-circle float-right icon-red'></span>
            {% endif %}
        </td>
    </tr>
    {% endif %}

    <tr>
        <td><span class='fas fa-info'></span></td>
        <td>{% trans "Status" %}</td>
        <td>{% stock_status_label item.status %}</td>
    </tr>
    {% if item.expiry_date %}
    <tr>
        <td><span class='fas fa-calendar-alt{% if item.is_expired %} icon-red{% endif %}'></span></td>
        <td>{% trans "Expiry Date" %}</td>
        <td>
            {% render_date item.expiry_date %}
            {% if item.is_expired %}
            <span title='{% blocktrans %}This StockItem expired on {{ item.expiry_date }}{% endblocktrans %}' class='badge rounded-pill bg-danger badge-right'>{% trans "Expired" %}</span>
            {% elif item.is_stale %}
            <span title='{% blocktrans %}This StockItem expires on {{ item.expiry_date }}{% endblocktrans %}' class='badge rounded-pill bg-warning badge-right'>{% trans "Stale" %}</span>
            {% endif %}
        </td>
    </tr>
    {% endif %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Last Updated" %}</td>
        <td>{{ item.updated }}</td>
    </tr>
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Last Stocktake" %}</td>
        {% if item.stocktake_date %}
        <td>{% render_date item.stocktake_date %} <span class='badge badge-right rounded-pill bg-dark'>{{ item.stocktake_user }}</span></td>
        {% else %}
        <td><em>{% trans "No stocktake performed" %}</em></td>
        {% endif %}
    </tr>


</table>
{% endblock details_right %}


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

$("#stock-serialize").click(function() {

    serializeStockItem({{ item.pk }}, {
        part: {{ item.part.pk }},
        reload: true,
        data: {
            quantity: {{ item.quantity|unlocalize }},
            {% if item.location %}
            destination: {{ item.location.pk }},
            {% elif item.part.default_location %}
            destination: {{ item.part.default_location.pk }},
            {% endif %}
        }
    });
});

$('#stock-install-in').click(function() {

    // TODO - Launch dialog to install this item *into* another stock item
});

$('#stock-uninstall').click(function() {

    uninstallStockItem(
        {{ item.pk }},
        {
            reload: true,
        }
    );
});

$("#stock-test-report").click(function() {
    printTestReports([{{ item.pk }}]);
});

$("#print-label").click(function() {
    printStockItemLabels([{{ item.pk }}]);
});

{% if roles.stock.change %}
$("#stock-duplicate").click(function() {
    // Duplicate a stock item
    duplicateStockItem({{ item.pk }}, {});
});

$('#stock-edit').click(function() {
    editStockItem({{ item.pk }}, {
        reload: true,
    });
});

$('#stock-edit-status').click(function () {

    constructForm('{% url "api-stock-detail" item.pk %}', {
        fields: {
            status: {},
        },
        reload: true,
        title: '{% trans "Edit Stock Status" %}',
    });
});

{% endif %}

$("#show-qr-code").click(function() {
    launchModalForm("{% url 'stock-item-qr' item.id %}",
    {
        no_post: true,
    });
});

$("#barcode-link").click(function() {
    linkBarcodeDialog({{ item.id }});
});

$("#barcode-unlink").click(function() {
    unlinkBarcode({{ item.id }});
});

$("#barcode-scan-into-location").click(function() {

    inventreeGet('{% url "api-stock-detail" item.pk %}', {}, {
        success: function(item) {
            scanItemsIntoLocation([item]);
        }
    });
});

{% if plugins_enabled %}
$('#locate-item-button').click(function() {
    locateItemOrLocation({
        item: {{ item.pk }},
    });
});
{% endif %}

function itemAdjust(action) {

    inventreeGet(
        '{% url "api-stock-detail" item.pk %}',
        {
            part_detail: true,
            location_detail: true,
        },
        {
            success: function(item) {
                adjustStock(action, [item], {
                    success: function() {
                        location.reload();
                    }
                });
            }
        }
    );
}

$('#stock-add').click(function() {
    itemAdjust('add');
});

$("#stock-delete").click(function () {
    deleteStockItem({{ item.pk }}, {
        redirect: '{% url "part-detail" item.part.pk %}',
    });
});

{% if item.part.can_convert %}
$("#stock-convert").click(function() {

    var html = `
    <div class='alert alert-block alert-info'>
        {% trans "Select one of the part variants listed below." %}
    </div>
    <div class='alert alert-block alert-warning'>
        <strong>{% trans "Warning" %}</strong>
        {% trans "This action cannot be easily undone" %}
    </div>
    `;

    constructForm(
        '{% url "api-stock-item-convert" item.pk %}',
        {
            method: 'POST',
            title: '{% trans "Convert Stock Item" %}',
            preFormContent: html,
            reload: true,
            fields: {
                part: {
                    filters: {
                        convert_from: {{ item.part.pk }}
                    }
                },
            }
        }
    );
});
{% endif %}

{% if item.in_stock %}
$("#stock-assign-to-customer").click(function() {

    inventreeGet('{% url "api-stock-detail" item.pk %}', {}, {
        success: function(response) {
            assignStockToCustomer(
                [response],
                {
                    success: function() {
                        location.reload();
                    },
                }
            );
        }
    });
});

$("#stock-move").click(function() {
    itemAdjust("move");
});

$("#stock-count").click(function() {
    itemAdjust('count');
});

$('#stock-remove').click(function() {
    itemAdjust('take');
});

{% else %}

$("#stock-return-from-customer").click(function() {

    constructForm('{% url "api-stock-item-return" item.pk %}', {
        fields: {
            location: {
                {% if item.part.default_location %}
                value: {{ item.part.default_location.pk }},
                {% endif %}
            },
            notes: {},
        },
        method: 'POST',
        title: '{% trans "Return to Stock" %}',
        reload: true,
    });

});

{% endif %}

$('#serial-number-search').click(function() {
    findStockItemBySerialNumber({{ item.part.pk }});
});

enableBreadcrumbTree({
    label: 'stockitem',
    url: '{% url "api-location-tree" %}',
    {% if item.location %}
    selected: {{ item.location.pk }},
    {% endif %}
    processNode: function(node) {
        node.text = node.name;
        node.href = `/stock/location/${node.pk}/`;

        return node;
    }
});

{% endblock %}
