{% load i18n %} {% load status_codes %} /* Stock API functions * Requires api.js to be loaded first */ /* Functions for interacting with stock management forms */ function removeStockRow(e) { // Remove a selected row from a stock modal form e = e || window.event; var src = e.target || e.srcElement; var row = $(src).attr('row'); $('#' + row).remove(); } function passFailBadge(result) { if (result) { return `{% trans "PASS" %}`; } else { return `{% trans "FAIL" %}`; } } function noResultBadge() { return `{% trans "NO RESULT" %}`; } function loadStockTestResultsTable(table, options) { /* * Load StockItemTestResult table */ function formatDate(row) { // Function for formatting date field var html = row.date; if (row.user_detail) { html += `${row.user_detail.username}`; } if (row.attachment) { html += ``; } return html; } function makeButtons(row, grouped) { var html = `
`; html += makeIconButton('fa-plus icon-green', 'button-test-add', row.test_name, '{% trans "Add test result" %}'); if (!grouped && row.result != null) { var pk = row.pk; html += makeIconButton('fa-edit icon-blue', 'button-test-edit', pk, '{% trans "Edit test result" %}'); html += makeIconButton('fa-trash-alt icon-red', 'button-test-delete', pk, '{% trans "Delete test result" %}'); } html += "
"; return html; } // First, load all the test templates table.inventreeTable({ url: "{% url 'api-part-test-template-list' %}", method: 'get', name: 'testresult', formatNoMatches: function() { return "{% trans 'No test results found' %}"; }, queryParams: { part: options.part, }, columns: [ { field: 'pk', title: 'ID', visible: false, switchable: false, }, { field: 'test_name', title: "{% trans "Test Name" %}", sortable: true, formatter: function(value, row) { var html = value; if (row.required) { html = `${value}`; } if (row.result == null) { html += noResultBadge(); } else { html += passFailBadge(row.result); } return html; } }, { field: 'value', title: '{% trans "Value" %}', }, { field: 'notes', title: '{% trans "Notes" %}', }, { field: 'date', title: '{% trans "Test Date" %}', formatter: function(value, row) { return formatDate(row); } }, { field: 'buttons', formatter: function(value, row) { return makeButtons(row, false); } }, ], groupBy: true, groupByField: 'test_name', groupByFormatter: function(field, id, data) { // Extract the "latest" row (data are returned in date order from the server) var latest = data[data.length-1]; switch (field) { case 'test_name': return latest.test_name + ` (${data.length})` + passFailBadge(latest.result); case 'value': return latest.value; case 'notes': return latest.notes; case 'date': return formatDate(latest); case 'buttons': // Buttons are done differently for grouped rows return makeButtons(latest, true); default: return "---"; } }, onLoadSuccess: function(tableData) { // Once the test template data are loaded, query for results inventreeGet( "{% url 'api-stock-test-result-list' %}", { stock_item: options.stock_item, user_detail: true, attachment_detail: true, }, { success: function(data) { // Iterate through the returned test result data, and group by test data.forEach(function(item) { var match = false; var override = false; var key = item.key; // Try to associate this result with a test row tableData.forEach(function(row, index) { // The result matches the test template row if (key == row.key) { // Force the names to be the same! item.test_name = row.test_name; item.required = row.required; if (row.result == null) { // The original row has not recorded a result - override! tableData[index] = item; override = true; } match = true; } }); // No match could be found (this is a new test!) if (!match) { item.test_name = item.test; } if (!override) { tableData.push(item); } }); // Finally, push the data back into the table! table.bootstrapTable("load", tableData); } }, ); } }); } function loadStockTable(table, options) { /* Load data into a stock table with adjustable options. * Fetches data (via AJAX) and loads into a bootstrap table. * Also links in default button callbacks. * * Options: * url - URL for the stock query * params - query params for augmenting stock data request * groupByField - Column for grouping stock items * buttons - Which buttons to link to stock selection callbacks * filterList -