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 %}
$('#print-build-report').click(function() {
printBuildReports([{{ build.pk }}]);
printReports({
items: [{{ build.pk }}],
key: 'build',
url: '{% url "api-build-report-list" %}',
});
});
{% endif %}

View File

@ -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 %}

View File

@ -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 %}

View File

@ -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() {

View File

@ -54,7 +54,21 @@
loadReturnOrderTable('#return-order-table', {});
{% 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 %}
$('#return-order-create').click(function() {

View File

@ -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 %}

View File

@ -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() {

View File

@ -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 %}
});

View File

@ -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 %}

View File

@ -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() {

View File

@ -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" %}',
{
let params = {
enabled: true,
items: items,
},
{
};
params[options.key] = options.items;
// 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 selected stock item(s)" %}',
'{% trans "No report templates found which match the selected items" %}',
);
return;
}
// Select report template to print
selectReport(
response,
items,
{
// Select report template for printing
selectReport(response, options.items, {
success: function(pk) {
var href = `/api/report/test/${pk}/print/?`;
let href = `${options.url}${pk}/print/?`;
items.forEach(function(item) {
href += `item=${item}&`;
options.items.forEach(function(item) {
href += `${options.key}=${item}&`;
});
window.open(href);
}
}
);
}
}
);
}
function printBuildReports(builds) {
/**
* Print Build report for the provided build(s)
*/
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;
}
// 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,
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) {