From 76589a2fd87c41c6910a5829d9d2c4572b41caa9 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 15 Mar 2023 23:35:02 +1100 Subject: [PATCH] Refactor report printing javascript code - Replace all existing functions with 'printReports' --- .../build/templates/build/build_base.html | 6 +- InvenTree/build/templates/build/index.html | 6 +- .../order/templates/order/order_base.html | 6 +- .../templates/order/purchase_orders.html | 8 +- .../order/templates/order/return_orders.html | 16 +- .../templates/order/sales_order_base.html | 6 +- .../order/templates/order/sales_orders.html | 8 +- InvenTree/part/templates/part/detail.html | 6 +- InvenTree/stock/templates/stock/item.html | 6 +- .../stock/templates/stock/item_base.html | 6 +- InvenTree/templates/js/translated/report.js | 295 +++--------------- InvenTree/templates/js/translated/stock.js | 8 +- 12 files changed, 107 insertions(+), 270 deletions(-) diff --git a/InvenTree/build/templates/build/build_base.html b/InvenTree/build/templates/build/build_base.html index 01aae6e573..a372a8cb2f 100644 --- a/InvenTree/build/templates/build/build_base.html +++ b/InvenTree/build/templates/build/build_base.html @@ -247,7 +247,11 @@ src="{% static 'img/blank_image.png' %}" {% if report_enabled %} $('#print-build-report').click(function() { - printBuildReports([{{ build.pk }}]); + printReports({ + items: [{{ build.pk }}], + key: 'build', + url: '{% url "api-build-report-list" %}', + }); }); {% endif %} diff --git a/InvenTree/build/templates/build/index.html b/InvenTree/build/templates/build/index.html index 4d1476f959..d9c746f2d9 100644 --- a/InvenTree/build/templates/build/index.html +++ b/InvenTree/build/templates/build/index.html @@ -71,7 +71,11 @@ $('#multi-build-print').click(function() { build_ids.push(row.pk); }); - printBuildReports(build_ids); + printReports({ + items: build_ids, + key: 'build', + url: '{% url "api-build-report-list" %}' + }); }); {% endif %} diff --git a/InvenTree/order/templates/order/order_base.html b/InvenTree/order/templates/order/order_base.html index 244a5caf43..85914c3915 100644 --- a/InvenTree/order/templates/order/order_base.html +++ b/InvenTree/order/templates/order/order_base.html @@ -229,7 +229,11 @@ $("#place-order").click(function() { {% if report_enabled %} $('#print-order-report').click(function() { - printPurchaseOrderReports([{{ order.pk }}]); + printReports({ + items: [{{ order.pk }}], + key: 'order', + url: '{% url "api-po-report-list" %}', + }); }); {% endif %} diff --git a/InvenTree/order/templates/order/purchase_orders.html b/InvenTree/order/templates/order/purchase_orders.html index 7f91f01600..a9670da226 100644 --- a/InvenTree/order/templates/order/purchase_orders.html +++ b/InvenTree/order/templates/order/purchase_orders.html @@ -63,8 +63,12 @@ $("#order-print").click(function() { orders.push(row.pk); }); - printPurchaseOrderReports(orders); -}) + printReports({ + items: orders, + key: 'order', + url: '{% url "api-po-report-list" %}', + }); +}); {% endif %} $("#po-create").click(function() { diff --git a/InvenTree/order/templates/order/return_orders.html b/InvenTree/order/templates/order/return_orders.html index e305724e39..60f451d2f0 100644 --- a/InvenTree/order/templates/order/return_orders.html +++ b/InvenTree/order/templates/order/return_orders.html @@ -54,7 +54,21 @@ loadReturnOrderTable('#return-order-table', {}); {% if report_enabled %} - +$("#order-print").click(function() { + var rows = getTableData('#return-order-table'); + + var orders = []; + + rows.forEach(function(row) { + orders.push(row.pk); + }); + + printReports({ + url: '{% url "api-return-order-report-list" %}', + key: 'order', + items: orders, + }); +}); {% endif %} $('#return-order-create').click(function() { diff --git a/InvenTree/order/templates/order/sales_order_base.html b/InvenTree/order/templates/order/sales_order_base.html index dfc208304e..3542ae31fa 100644 --- a/InvenTree/order/templates/order/sales_order_base.html +++ b/InvenTree/order/templates/order/sales_order_base.html @@ -250,7 +250,11 @@ $("#complete-order").click(function() { {% if report_enabled %} $('#print-order-report').click(function() { - printSalesOrderReports([{{ order.pk }}]); + printReports({ + items: [{{ order.pk }}], + key: 'order', + url: '{% url "api-so-report-list" %}', + }); }); {% endif %} diff --git a/InvenTree/order/templates/order/sales_orders.html b/InvenTree/order/templates/order/sales_orders.html index 9b85df95b7..7726c9a1c7 100644 --- a/InvenTree/order/templates/order/sales_orders.html +++ b/InvenTree/order/templates/order/sales_orders.html @@ -64,8 +64,12 @@ $("#order-print").click(function() { orders.push(row.pk); }); - printSalesOrderReports(orders); -}) + printReports({ + items: orders, + key: 'order', + url: '{% url "api-so-report-list" %}', + }); +}); {% endif %} $("#so-create").click(function() { diff --git a/InvenTree/part/templates/part/detail.html b/InvenTree/part/templates/part/detail.html index 372eecd076..83b80a2b2f 100644 --- a/InvenTree/part/templates/part/detail.html +++ b/InvenTree/part/templates/part/detail.html @@ -677,7 +677,11 @@ {% if report_enabled %} $("#print-bom-report").click(function() { - printBomReports([{{ part.pk }}]); + printReports({ + items: [{{ part.pk }}], + key: 'part', + url: '{% url "api-bom-report-list" %}' + }); }); {% endif %} }); diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index 15e905f96e..633da1417f 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -261,7 +261,11 @@ {% if item.has_test_reports %} $("#test-report").click(function() { - printTestReports([{{ item.pk }}]); + printReports({ + items: [{{ item.pk }}], + key: 'item', + url: '{% url "api-stockitem-testreport-list" %}', + }); }); {% endif %} diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 049c0973c7..aa9f6b49db 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -493,7 +493,11 @@ $('#stock-uninstall').click(function() { }); $("#stock-test-report").click(function() { - printTestReports([{{ item.pk }}]); + printReports({ + items: [{{ item.pk }}], + key: 'item', + url: '{% url "api-stockitem-testreport-list" %}', + }); }); $("#print-label").click(function() { diff --git a/InvenTree/templates/js/translated/report.js b/InvenTree/templates/js/translated/report.js index 7187674635..68ee04d158 100644 --- a/InvenTree/templates/js/translated/report.js +++ b/InvenTree/templates/js/translated/report.js @@ -14,11 +14,7 @@ */ /* exported - printBomReports, - printBuildReports, - printPurchaseOrderReports, - printSalesOrderReports, - printTestReports, + printReports, */ function selectReport(reports, items, options={}) { @@ -108,270 +104,57 @@ function selectReport(reports, items, options={}) { } -function printTestReports(items) { - /** - * Print test reports for the provided stock item(s) - */ +/* + * Print report(s) for the selected items: + * + * - Retrieve a list of matching report templates from the server + * - Present the available templates to the user (if more than one available) + * - Request printed document + * + * Required options: + * - url: The list URL for the particular template + * - items: The list of objects to print + * - key: The key to use in the query parameters + */ +function printReports(options) { - if (items.length == 0) { + if (!options.items || options.items.length == 0) { showAlertDialog( - '{% trans "Select Stock Items" %}', - '{% trans "Stock item(s) must be selected before printing reports" %}' + '{% trans "Select Items" %}', + '{% trans "No items selected for printing" }', ); - return; } - // Request available reports from the server - inventreeGet( - '{% url "api-stockitem-testreport-list" %}', - { - enabled: true, - items: items, - }, - { - success: function(response) { - if (response.length == 0) { - showAlertDialog( - '{% trans "No Reports Found" %}', - '{% trans "No report templates found which match selected stock item(s)" %}', - ); + let params = { + enabled: true, + }; - return; - } + params[options.key] = options.items; - // Select report template to print - selectReport( - response, - items, - { - success: function(pk) { - var href = `/api/report/test/${pk}/print/?`; - - items.forEach(function(item) { - href += `item=${item}&`; - }); - - window.open(href); - } - } + // Request a list of available reports + inventreeGet(options.url, params, { + success: function(response) { + if (response.length == 0) { + showAlertDialog( + '{% trans "No Reports Found" %}', + '{% trans "No report templates found which match the selected items" %}', ); + return; } - } - ); -} + // Select report template for printing + selectReport(response, options.items, { + success: function(pk) { + let href = `${options.url}${pk}/print/?`; -function printBuildReports(builds) { - /** - * Print Build report for the provided build(s) - */ + options.items.forEach(function(item) { + href += `${options.key}=${item}&`; + }); - if (builds.length == 0) { - showAlertDialog( - '{% trans "Select Builds" %}', - '{% trans "Build(s) must be selected before printing reports" %}', - ); - - return; - } - - inventreeGet( - '{% url "api-build-report-list" %}', - { - enabled: true, - builds: builds, - }, - { - success: function(response) { - if (response.length == 0) { - showAlertDialog( - '{% trans "No Reports Found" %}', - '{% trans "No report templates found which match selected build(s)" %}' - ); - - return; + window.open(href); } - - // Select which report to print - selectReport( - response, - builds, - { - success: function(pk) { - var href = `/api/report/build/${pk}/print/?`; - - builds.forEach(function(build) { - href += `build=${build}&`; - }); - - window.open(href); - } - } - ); - } + }); } - ); -} - - -function printBomReports(parts) { - /** - * Print BOM reports for the provided part(s) - */ - - if (parts.length == 0) { - showAlertDialog( - '{% trans "Select Parts" %}', - '{% trans "Part(s) must be selected before printing reports" %}' - ); - - return; - } - - // Request available reports from the server - inventreeGet( - '{% url "api-bom-report-list" %}', - { - enabled: true, - parts: parts, - }, - { - success: function(response) { - if (response.length == 0) { - showAlertDialog( - '{% trans "No Reports Found" %}', - '{% trans "No report templates found which match selected part(s)" %}', - ); - - return; - } - - // Select which report to print - selectReport( - response, - parts, - { - success: function(pk) { - var href = `/api/report/bom/${pk}/print/?`; - - parts.forEach(function(part) { - href += `part=${part}&`; - }); - - window.open(href); - } - } - ); - } - } - ); -} - - -function printPurchaseOrderReports(orders) { - /** - * Print PurchaseOrder reports for the provided purchase order(s) - */ - - if (orders.length == 0) { - showAlertDialog( - '{% trans "Select Purchase Orders" %}', - '{% trans "Purchase Order(s) must be selected before printing report" %}', - ); - - return; - } - - // Request avaiable report templates - inventreeGet( - '{% url "api-po-report-list" %}', - { - enabled: true, - orders: orders, - }, - { - success: function(response) { - if (response.length == 0) { - showAlertDialog( - '{% trans "No Reports Found" %}', - '{% trans "No report templates found which match selected orders" %}', - ); - - return; - } - - // Select report template - selectReport( - response, - orders, - { - success: function(pk) { - var href = `/api/report/po/${pk}/print/?`; - - orders.forEach(function(order) { - href += `order=${order}&`; - }); - - window.open(href); - } - } - ); - } - } - ); -} - - -function printSalesOrderReports(orders) { - /** - * Print SalesOrder reports for the provided purchase order(s) - */ - - if (orders.length == 0) { - showAlertDialog( - '{% trans "Select Sales Orders" %}', - '{% trans "Sales Order(s) must be selected before printing report" %}', - ); - - return; - } - - // Request avaiable report templates - inventreeGet( - '{% url "api-so-report-list" %}', - { - enabled: true, - orders: orders, - }, - { - success: function(response) { - if (response.length == 0) { - showAlertDialog( - '{% trans "No Reports Found" %}', - '{% trans "No report templates found which match selected orders" %}', - ); - - return; - } - - // Select report template - selectReport( - response, - orders, - { - success: function(pk) { - var href = `/api/report/so/${pk}/print/?`; - - orders.forEach(function(order) { - href += `order=${order}&`; - }); - - window.open(href); - } - } - ); - } - } - ); + }); } diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js index f25efdee43..5b99266c4e 100644 --- a/InvenTree/templates/js/translated/stock.js +++ b/InvenTree/templates/js/translated/stock.js @@ -25,7 +25,7 @@ modalSubmit, openModal, printStockItemLabels, - printTestReports, + printReports, renderLink, scanItemsIntoLocation, showAlertDialog, @@ -2216,7 +2216,11 @@ function loadStockTable(table, options) { items.push(item.pk); }); - printTestReports(items); + printReports({ + items: items, + key: 'item', + url: '{% url "api-stockitem-testreport-list" %}', + }); }); if (global_settings.BARCODE_ENABLE) {