2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-15 19:45:46 +00:00

Refactor report printing javascript code

- Replace all existing functions with 'printReports'
This commit is contained in:
Oliver Walters
2023-03-15 23:35:02 +11:00
parent 918e1f3bef
commit 76589a2fd8
12 changed files with 107 additions and 270 deletions

View File

@ -247,7 +247,11 @@ src="{% static 'img/blank_image.png' %}"
{% if report_enabled %} {% if report_enabled %}
$('#print-build-report').click(function() { $('#print-build-report').click(function() {
printBuildReports([{{ build.pk }}]); printReports({
items: [{{ build.pk }}],
key: 'build',
url: '{% url "api-build-report-list" %}',
});
}); });
{% endif %} {% endif %}

View File

@ -71,7 +71,11 @@ $('#multi-build-print').click(function() {
build_ids.push(row.pk); build_ids.push(row.pk);
}); });
printBuildReports(build_ids); printReports({
items: build_ids,
key: 'build',
url: '{% url "api-build-report-list" %}'
});
}); });
{% endif %} {% endif %}

View File

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

View File

@ -63,8 +63,12 @@ $("#order-print").click(function() {
orders.push(row.pk); orders.push(row.pk);
}); });
printPurchaseOrderReports(orders); printReports({
}) items: orders,
key: 'order',
url: '{% url "api-po-report-list" %}',
});
});
{% endif %} {% endif %}
$("#po-create").click(function() { $("#po-create").click(function() {

View File

@ -54,7 +54,21 @@
loadReturnOrderTable('#return-order-table', {}); loadReturnOrderTable('#return-order-table', {});
{% if report_enabled %} {% if report_enabled %}
<!-- TODO: report button callbacks --> $("#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 %} {% endif %}
$('#return-order-create').click(function() { $('#return-order-create').click(function() {

View File

@ -250,7 +250,11 @@ $("#complete-order").click(function() {
{% if report_enabled %} {% if report_enabled %}
$('#print-order-report').click(function() { $('#print-order-report').click(function() {
printSalesOrderReports([{{ order.pk }}]); printReports({
items: [{{ order.pk }}],
key: 'order',
url: '{% url "api-so-report-list" %}',
});
}); });
{% endif %} {% endif %}

View File

@ -64,8 +64,12 @@ $("#order-print").click(function() {
orders.push(row.pk); orders.push(row.pk);
}); });
printSalesOrderReports(orders); printReports({
}) items: orders,
key: 'order',
url: '{% url "api-so-report-list" %}',
});
});
{% endif %} {% endif %}
$("#so-create").click(function() { $("#so-create").click(function() {

View File

@ -677,7 +677,11 @@
{% if report_enabled %} {% if report_enabled %}
$("#print-bom-report").click(function() { $("#print-bom-report").click(function() {
printBomReports([{{ part.pk }}]); printReports({
items: [{{ part.pk }}],
key: 'part',
url: '{% url "api-bom-report-list" %}'
});
}); });
{% endif %} {% endif %}
}); });

View File

@ -261,7 +261,11 @@
{% if item.has_test_reports %} {% if item.has_test_reports %}
$("#test-report").click(function() { $("#test-report").click(function() {
printTestReports([{{ item.pk }}]); printReports({
items: [{{ item.pk }}],
key: 'item',
url: '{% url "api-stockitem-testreport-list" %}',
});
}); });
{% endif %} {% endif %}

View File

@ -493,7 +493,11 @@ $('#stock-uninstall').click(function() {
}); });
$("#stock-test-report").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() { $("#print-label").click(function() {

View File

@ -14,11 +14,7 @@
*/ */
/* exported /* exported
printBomReports, printReports,
printBuildReports,
printPurchaseOrderReports,
printSalesOrderReports,
printTestReports,
*/ */
function selectReport(reports, items, options={}) { function selectReport(reports, items, options={}) {
@ -108,270 +104,57 @@ function selectReport(reports, items, options={}) {
} }
function printTestReports(items) { /*
/** * Print report(s) for the selected items:
* Print test reports for the provided stock item(s) *
*/ * - 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( showAlertDialog(
'{% trans "Select Stock Items" %}', '{% trans "Select Items" %}',
'{% trans "Stock item(s) must be selected before printing reports" %}' '{% trans "No items selected for printing" }',
); );
return; return;
} }
// Request available reports from the server let params = {
inventreeGet( enabled: true,
'{% 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)" %}',
);
return; params[options.key] = options.items;
}
// Select report template to print // Request a list of available reports
selectReport( inventreeGet(options.url, params, {
response, success: function(response) {
items, if (response.length == 0) {
{ showAlertDialog(
success: function(pk) { '{% trans "No Reports Found" %}',
var href = `/api/report/test/${pk}/print/?`; '{% trans "No report templates found which match the selected items" %}',
items.forEach(function(item) {
href += `item=${item}&`;
});
window.open(href);
}
}
); );
return;
} }
}
);
}
// Select report template for printing
selectReport(response, options.items, {
success: function(pk) {
let href = `${options.url}${pk}/print/?`;
function printBuildReports(builds) { options.items.forEach(function(item) {
/** href += `${options.key}=${item}&`;
* Print Build report for the provided build(s) });
*/
if (builds.length == 0) { window.open(href);
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;
} }
});
// 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);
}
}
);
}
}
);
} }

View File

@ -25,7 +25,7 @@
modalSubmit, modalSubmit,
openModal, openModal,
printStockItemLabels, printStockItemLabels,
printTestReports, printReports,
renderLink, renderLink,
scanItemsIntoLocation, scanItemsIntoLocation,
showAlertDialog, showAlertDialog,
@ -2216,7 +2216,11 @@ function loadStockTable(table, options) {
items.push(item.pk); items.push(item.pk);
}); });
printTestReports(items); printReports({
items: items,
key: 'item',
url: '{% url "api-stockitem-testreport-list" %}',
});
}); });
if (global_settings.BARCODE_ENABLE) { if (global_settings.BARCODE_ENABLE) {