{% load i18n %} {% load inventree_extras %} {% load status_codes %} /* Stock API functions * Requires api.js to be loaded first */ {% settings_value 'BARCODE_ENABLE' as barcodes %} function stockStatusCodes() { return [ {% for code in StockStatus.list %} { key: {{ code.key }}, text: "{{ code.value }}", }, {% endfor %} ]; } 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, align='float-right') { if (result) { return `{% trans "PASS" %}`; } else { return `{% trans "FAIL" %}`; } } function noResultBadge(align='float-right') { return `{% trans "NO RESULT" %}`; } 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 loadStockTestResultsTable(table, options) { /* * Load StockItemTestResult table */ 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; } var parent_node = "parent node"; table.inventreeTable({ url: "{% url 'api-part-test-template-list' %}", method: 'get', name: 'testresult', treeEnable: true, rootParentId: parent_node, parentIdField: 'parent', idField: 'pk', uniqueId: 'key', treeShowField: 'test_name', formatNoMatches: function() { return '{% trans "No test results found" %}'; }, queryParams: { part: options.part, }, onPostBody: function() { table.treegrid({ treeColumn: 0, }); table.treegrid("collapseAll"); }, 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" %}', sortable: true, formatter: function(value, row) { return formatDate(row); }, }, { field: 'buttons', formatter: function(value, row) { return makeButtons(row, false); } } ], onLoadSuccess: function(tableData) { // Set "parent" for each existing row tableData.forEach(function(item, idx) { tableData[idx].parent = parent_node; }); // Once the test template data are loaded, query for test results inventreeGet( '{% url "api-stock-test-result-list" %}', { stock_item: options.stock_item, user_detail: true, attachment_detail: true, ordering: "-date", }, { success: function(data) { // Iterate through the returned test data data.forEach(function(item, index) { var match = false; var override = false; // Extract the simplified test key var key = item.key; // Attempt to associate this result with an existing test for (var idx = 0; idx < tableData.length; idx++) { var row = tableData[idx]; if (key == row.key) { item.test_name = row.test_name; item.required = row.required; if (row.result == null) { item.parent = parent_node; tableData[idx] = item; override = true; } else { item.parent = row.pk; } match = true; break; } } // No match could be found if (!match) { item.test_name = item.test; item.parent = parent_node; } if (!override) { tableData.push(item); } }); // Push 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 -