From 71c1faf9ffeb4b0b183566782794ec995d19f012 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 9 Dec 2019 21:55:00 +1100 Subject: [PATCH] Use the client-side PO table in more places --- .../static/script/inventree/order.js | 57 +++++++++++++++++++ .../company/detail_purchase_orders.html | 12 ++-- InvenTree/order/api.py | 10 ++++ .../templates/order/purchase_orders.html | 49 +--------------- InvenTree/part/templates/part/orders.html | 10 ++-- 5 files changed, 79 insertions(+), 59 deletions(-) diff --git a/InvenTree/InvenTree/static/script/inventree/order.js b/InvenTree/InvenTree/static/script/inventree/order.js index 04bbee513f..d811cc5ea1 100644 --- a/InvenTree/InvenTree/static/script/inventree/order.js +++ b/InvenTree/InvenTree/static/script/inventree/order.js @@ -100,6 +100,63 @@ function removePurchaseOrderLineItem(e) { }); } + +function loadPurchaseOrderTable(table, options) { + /* Create a purchase-order table */ + + table.inventreeTable({ + url: options.url, + formatNoMatches: function() { return "No purchase orders found"; }, + columns: [ + { + field: 'pk', + title: 'ID', + visible: false, + }, + { + sortable: true, + field: 'supplier', + title: 'Supplier', + formatter: function(value, row, index, field) { + return imageHoverIcon(row.supplier__image) + renderLink(row.supplier__name, '/company/' + value + '/purchase-orders/'); + } + }, + { + sortable: true, + field: 'reference', + title: 'Reference', + formatter: function(value, row, index, field) { + return renderLink(value, "/order/purchase-order/" + row.pk + "/"); + } + }, + { + sortable: true, + field: 'creation_date', + title: 'Date', + }, + { + sortable: true, + field: 'description', + title: 'Description', + }, + { + sortable: true, + field: 'status', + title: 'Status', + formatter: function(value, row, index, field) { + return orderStatusLabel(row.status, row.status_text); + } + }, + { + sortable: true, + field: 'lines', + title: 'Items' + }, + ], + }); +} + + function orderStatusLabel(code, label) { /* Render a purchase-order status label. */ diff --git a/InvenTree/company/templates/company/detail_purchase_orders.html b/InvenTree/company/templates/company/detail_purchase_orders.html index 8c299cbd4e..b2eabc129d 100644 --- a/InvenTree/company/templates/company/detail_purchase_orders.html +++ b/InvenTree/company/templates/company/detail_purchase_orders.html @@ -13,17 +13,19 @@ -{% include "order/po_table.html" with orders=company.outstanding_purchase_orders.all toolbar='#button-bar' %} - -{% if company.closed_purchase_orders.count > 0 %} -{% include "order/po_table_collapse.html" with title="Closed Orders" orders=company.closed_purchase_orders.all %} -{% endif %} + +
{% endblock %} {% block js_ready %} {{ block.super }} + loadPurchaseOrderTable($("#purchase-order-table"), { + url: "{% url 'api-po-list' %}?supplier={{ company.id }}", + }); + + function newOrder() { launchModalForm("{% url 'purchase-order-create' %}", { diff --git a/InvenTree/order/api.py b/InvenTree/order/api.py index 56273ae018..69580c3bb2 100644 --- a/InvenTree/order/api.py +++ b/InvenTree/order/api.py @@ -17,6 +17,8 @@ from InvenTree.status_codes import OrderStatus import os +from part.models import Part + from .models import PurchaseOrder, PurchaseOrderLineItem from .serializers import POSerializer, POLineItemSerializer @@ -52,6 +54,14 @@ class POList(generics.ListCreateAPIView): except ValueError: pass + # Attempt to filter by part + if 'part' in request.GET: + try: + part = Part.objects.get(pk=request.GET['part']) + queryset = queryset.filter(id__in=[p.id for p in part.purchase_orders()]) + except (Part.DoesNotExist, ValueError): + pass + data = queryset.values( 'pk', 'supplier', diff --git a/InvenTree/order/templates/order/purchase_orders.html b/InvenTree/order/templates/order/purchase_orders.html index 1fc15d623c..17f3115c75 100644 --- a/InvenTree/order/templates/order/purchase_orders.html +++ b/InvenTree/order/templates/order/purchase_orders.html @@ -37,55 +37,8 @@ $("#po-create").click(function() { $("#po-table").inventreeTable({ }); -$("#purchase-order-table").inventreeTable({ +loadPurchaseOrderTable($("#purchase-order-table"), { url: "{% url 'api-po-list' %}", - formatNoMatches: function() { return "{% trans "No purchase orders found" %}"; }, - columns: [ - { - field: 'pk', - title: 'ID', - visible: false, - }, - { - sortable: true, - field: 'supplier', - title: 'Supplier', - formatter: function(value, row, index, field) { - return imageHoverIcon(row.supplier__image) + renderLink(row.supplier__name, '/company/' + value + '/'); - } - }, - { - sortable: true, - field: 'reference', - title: 'Reference', - formatter: function(value, row, index, field) { - return renderLink(value, "/order/purchase-order/" + row.pk + "/"); - } - }, - { - sortable: true, - field: 'creation_date', - title: 'Date', - }, - { - sortable: true, - field: 'description', - title: 'Description', - }, - { - sortable: true, - field: 'status_text', - title: 'Status', - formatter: function(value, row, index, field) { - return orderStatusLabel(row.status, row.status_text); - } - }, - { - sortable: true, - field: 'lines', - title: 'Items' - }, - ], }); {% endblock %} \ No newline at end of file diff --git a/InvenTree/part/templates/part/orders.html b/InvenTree/part/templates/part/orders.html index b3f84735f3..fbb0413e34 100644 --- a/InvenTree/part/templates/part/orders.html +++ b/InvenTree/part/templates/part/orders.html @@ -14,19 +14,17 @@ -{% include "order/po_table.html" with orders=part.open_purchase_orders toolbar='#button-bar' %} + +
-{% if part.closed_purchase_orders|length > 0 %} -

Closed Orders

-{% include "order/po_table.html" with orders=part.closed_purchase_orders %} -{% endif %} {% endblock %} {% block js_ready %} {{ block.super }} -$("#po-table").inventreeTable({ +loadPurchaseOrderTable($("#purchase-order-table"), { + url: "{% url 'api-po-list' %}?part={{ part.id }}", }); $("#part-order2").click(function() {