2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-04 06:18:48 +00:00
2020-05-17 22:33:41 +10:00

704 lines
21 KiB
HTML

{% load i18n %}
/* 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 `<span class='label label-green float-right'>{% trans "PASS" %}</span>`;
} else {
return `<span class='label label-red float-right'>{% trans "FAIL" %}</span>`;
}
}
function noResultBadge() {
return `<span class='label label-blue float-right'>{% trans "NO RESULT" %}</span>`;
}
function testKey(test_name) {
// Convert test name to a unique key without any illegal chars
test_name = test_name.trim().toLowerCase();
test_name = test_name.replace(' ', '');
test_name = test_name.replace(/[^0-9a-z]/gi, '');
return test_name;
}
function loadStockTestResultsTable(table, options) {
/*
* Load StockItemTestResult table
*/
function formatDate(row) {
// Function for formatting date field
var html = row.date;
if (row.user_detail) {
html += `<span class='badge'>${row.user_detail.username}</span>`;
}
if (row.attachment_detail) {
html += `<a href='${row.attachment_detail.attachment}'><span class='fas fa-file-alt label-right'></span></a>`;
}
return html;
}
function makeButtons(row, grouped) {
var html = `<div class='btn-group float-right' role='group'>`;
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 += "</div>";
return html;
}
// First, load all the test templates
table.inventreeTable({
url: "{% url 'api-part-test-template-list' %}",
method: 'get',
formatNoMatches: function() {
return "{% trans 'No test results found' %}";
},
queryParams: {
part: options.part,
},
columns: [
{
field: 'pk',
title: 'ID',
visible: false,
},
{
field: 'test_name',
title: "{% trans "Test Name" %}",
sortable: true,
formatter: function(value, row) {
var html = value;
if (row.required) {
html = `<b>${value}</b>`;
}
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 + ` <i>(${data.length})</i>` + 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 = testKey(item.test);
// Try to associate this result with a test row
tableData.forEach(function(row, index) {
// The result matches the test template row
if (key == testKey(row.test_name)) {
// 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 - <ul> element where filters are displayed
* disableFilters: If true, disable custom filters
*/
// List of user-params which override the default filters
options.params['part_detail'] = true;
options.params['location_detail'] = true;
var params = options.params || {};
var filterListElement = options.filterList || "#filter-list-stock";
var filters = {};
if (!options.disableFilters) {
filters = loadTableFilters("stock");
}
var original = {};
for (var key in params) {
original[key] = params[key];
}
setupFilterList("stock", table, filterListElement);
// Override the default values, or add new ones
for (var key in params) {
filters[key] = params[key];
}
table.inventreeTable({
method: 'get',
formatNoMatches: function() {
return '{% trans "No stock items matching query" %}';
},
url: options.url,
queryParams: filters,
customSort: customGroupSorter,
groupBy: true,
original: original,
groupByField: options.groupByField || 'part',
groupByFormatter: function(field, id, data) {
var row = data[0];
if (field == 'part_name') {
var name = row.part_detail.full_name;
return imageHoverIcon(row.part_detail.thumbnail) + name + ' <i>(' + data.length + ' items)</i>';
}
else if (field == 'part_description') {
return row.part_detail.description;
}
else if (field == 'quantity') {
var stock = 0;
var items = 0;
data.forEach(function(item) {
stock += parseFloat(item.quantity);
items += 1;
});
stock = +stock.toFixed(5);
return stock + " (" + items + " items)";
} else if (field == 'status') {
var statii = [];
data.forEach(function(item) {
var status = String(item.status);
if (!status || status == '') {
status = '-';
}
if (!statii.includes(status)) {
statii.push(status);
}
});
// Multiple status codes
if (statii.length > 1) {
return "-";
} else if (statii.length == 1) {
return stockStatusDisplay(statii[0]);
} else {
return "-";
}
} else if (field == 'batch') {
var batches = [];
data.forEach(function(item) {
var batch = item.batch;
if (!batch || batch == '') {
batch = '-';
}
if (!batches.includes(batch)) {
batches.push(batch);
}
});
if (batches.length > 1) {
return "" + batches.length + " batches";
} else if (batches.length == 1) {
if (batches[0]) {
return batches[0];
} else {
return '-';
}
} else {
return '-';
}
} else if (field == 'location__path') {
/* Determine how many locations */
var locations = [];
data.forEach(function(item) {
var loc = item.location;
if (!locations.includes(loc)) {
locations.push(loc);
}
});
if (locations.length > 1) {
return "In " + locations.length + " locations";
} else {
// A single location!
return renderLink(row.location__path, '/stock/location/' + row.location + '/')
}
} else if (field == 'notes') {
var notes = [];
data.forEach(function(item) {
var note = item.notes;
if (!note || note == '') {
note = '-';
}
if (!notes.includes(note)) {
notes.push(note);
}
});
if (notes.length > 1) {
return '...';
} else if (notes.length == 1) {
return notes[0] || '-';
} else {
return '-';
}
}
else {
return '';
}
},
columns: [
{
checkbox: true,
title: '{% trans "Select" %}',
searchable: false,
},
{
field: 'pk',
title: 'ID',
visible: false,
},
{
field: 'part_name',
title: '{% trans "Part" %}',
sortable: true,
formatter: function(value, row, index, field) {
var url = '';
var thumb = row.part_detail.thumbnail;
var name = row.part_detail.full_name;
if (row.supplier_part) {
url = `/supplier-part/${row.supplier_part}/`;
} else {
url = `/part/${row.part}/`;
}
html = imageHoverIcon(thumb) + renderLink(name, url);
return html;
}
},
{
field: 'part_description',
title: '{% trans "Description" %}',
sortable: true,
formatter: function(value, row, index, field) {
return row.part_detail.description;
}
},
{
field: 'quantity',
title: '{% trans "Stock" %}',
sortable: true,
formatter: function(value, row, index, field) {
var val = parseFloat(value);
// If there is a single unit with a serial number, use the serial number
if (row.serial && row.quantity == 1) {
val = '# ' + row.serial;
} else {
val = +val.toFixed(5);
}
var html = renderLink(val, `/stock/item/${row.pk}/`);
if (row.allocated) {
html += `<span class='fas fa-bookmark label-right' title='{% trans "StockItem has been allocated" %}'></span>`;
}
// 70 = "LOST"
if (row.status == 70) {
html += `<span class='fas fa-question-circle label-right' title='{% trans "StockItem is lost" %}'></span>`;
}
return html;
}
},
{
field: 'status',
title: '{% trans "Status" %}',
sortable: 'true',
formatter: function(value, row, index, field) {
return stockStatusDisplay(value);
},
},
{
field: 'batch',
title: '{% trans "Batch" %}',
sortable: true,
},
{
field: 'location_detail.pathstring',
title: '{% trans "Location" %}',
sortable: true,
formatter: function(value, row, index, field) {
if (value) {
return renderLink(value, '/stock/location/' + row.location + '/');
}
else {
return '<i>{% trans "No stock location set" %}</i>';
}
}
},
{
field: 'notes',
title: '{% trans "Notes" %}',
}
],
});
if (options.buttons) {
linkButtonsToSelection(table, options.buttons);
}
function stockAdjustment(action) {
var items = $("#stock-table").bootstrapTable("getSelections");
var stock = [];
items.forEach(function(item) {
stock.push(item.pk);
});
// Buttons for launching secondary modals
var secondary = [];
if (action == 'move') {
secondary.push({
field: 'destination',
label: 'New Location',
title: 'Create new location',
url: "/stock/location/new/",
});
}
launchModalForm("/stock/adjust/",
{
data: {
action: action,
stock: stock,
},
success: function() {
$("#stock-table").bootstrapTable('refresh');
},
secondary: secondary,
}
);
}
// Automatically link button callbacks
$('#multi-item-stocktake').click(function() {
stockAdjustment('count');
});
$('#multi-item-remove').click(function() {
stockAdjustment('take');
});
$('#multi-item-add').click(function() {
stockAdjustment('add');
});
$("#multi-item-move").click(function() {
stockAdjustment('move');
});
$("#multi-item-order").click(function() {
var selections = $("#stock-table").bootstrapTable("getSelections");
var stock = [];
selections.forEach(function(item) {
stock.push(item.pk);
});
launchModalForm("/order/purchase-order/order-parts/", {
data: {
stock: stock,
},
});
});
$("#multi-item-delete").click(function() {
var selections = $("#stock-table").bootstrapTable("getSelections");
var stock = [];
selections.forEach(function(item) {
stock.push(item.pk);
});
stockAdjustment('delete');
});
}
function loadStockTrackingTable(table, options) {
var cols = [
{
field: 'pk',
visible: false,
},
{
field: 'date',
title: '{% trans "Date" %}',
sortable: true,
formatter: function(value, row, index, field) {
var m = moment(value);
if (m.isValid()) {
var html = m.format('dddd MMMM Do YYYY'); // + '<br>' + m.format('h:mm a');
return html;
}
return 'N/A';
}
},
];
// If enabled, provide a link to the referenced StockItem
if (options.partColumn) {
cols.push({
field: 'item',
title: '{% trans "Stock Item" %}',
sortable: true,
formatter: function(value, row, index, field) {
return renderLink(value.part_name, value.url);
}
});
}
// Stock transaction description
cols.push({
field: 'title',
title: '{% trans "Description" %}',
sortable: true,
formatter: function(value, row, index, field) {
var html = "<b>" + value + "</b>";
if (row.notes) {
html += "<br><i>" + row.notes + "</i>";
}
if (row.link) {
html += "<br><a href='" + row.link + "'>" + row.link + "</a>";
}
return html;
}
});
cols.push({
field: 'quantity',
title: '{% trans "Quantity" %}',
formatter: function(value, row, index, field) {
return parseFloat(value);
},
});
cols.push({
sortable: true,
field: 'user',
title: '{% trans "User" %}',
formatter: function(value, row, index, field) {
if (value)
{
// TODO - Format the user's first and last names
return row.user_detail.username;
}
else
{
return "{% trans "No user information" %}";
}
}
});
cols.push({
sortable: false,
formatter: function(value, row, index, field) {
// Manually created entries can be edited or deleted
if (!row.system) {
var bEdit = "<button title='Edit tracking entry' class='btn btn-entry-edit btn-default btn-glyph' type='button' url='/stock/track/" + row.pk + "/edit/'><span class='fas fa-edit'/></button>";
var bDel = "<button title='Delete tracking entry' class='btn btn-entry-delete btn-default btn-glyph' type='button' url='/stock/track/" + row.pk + "/delete/'><span class='fas fa-trash-alt icon-red'/></button>";
return "<div class='btn-group' role='group'>" + bEdit + bDel + "</div>";
} else {
return "";
}
}
});
table.inventreeTable({
method: 'get',
queryParams: options.params,
columns: cols,
url: options.url,
});
if (options.buttons) {
linkButtonsToSelection(table, options.buttons);
}
table.on('click', '.btn-entry-edit', function() {
var button = $(this);
launchModalForm(button.attr('url'), {
reload: true,
});
});
table.on('click', '.btn-entry-delete', function() {
var button = $(this);
launchModalForm(button.attr('url'), {
reload: true,
});
});
}