From ba1b8d9181f235b1f96086dc9bac4ab7654145d0 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 17 Apr 2019 22:59:52 +1000 Subject: [PATCH 1/3] Created js function to load stock table - Avoid duplication of code --- InvenTree/part/templates/part/stock.html | 47 ++----------- InvenTree/static/script/inventree/stock.js | 62 ++++++++++++++++- InvenTree/stock/templates/stock/location.html | 66 ++----------------- 3 files changed, 71 insertions(+), 104 deletions(-) diff --git a/InvenTree/part/templates/part/stock.html b/InvenTree/part/templates/part/stock.html index 5bece01f2e..d41a735989 100644 --- a/InvenTree/part/templates/part/stock.html +++ b/InvenTree/part/templates/part/stock.html @@ -45,50 +45,11 @@ }); }); - $("#stock-table").bootstrapTable({ - sortable: true, - search: true, - pagination: true, - queryParams: function(p) { - return { - part: {{ part.id }}, - in_stock: true, - } + loadStockTable($("#stock-table"), { + params: { + part: {{ part.id }}, }, - columns: [ - { - field: 'pk', - title: 'ID', - visible: false, - }, - { - checkbox: true, - }, - { - field: 'location', - title: 'Location', - sortable: true, - formatter: function(value, row, index, field){ - return renderLink(value.pathstring, value.url); - } - }, - { - field: 'quantity', - title: 'Stock', - searchable: false, - sortable: true, - formatter: function(value, row, index, field) { - var text = renderLink(value, row.url) - text = text + "" + row.status + ""; - return text; - } - }, - { - field: 'notes', - title: 'Notes', - }, - ], - url: "{% url 'api-stock-list' %}" + url: "{% url 'api-stock-list' %}", }); function selectedStock() { diff --git a/InvenTree/static/script/inventree/stock.js b/InvenTree/static/script/inventree/stock.js index 4f008aec0c..d08b705023 100644 --- a/InvenTree/static/script/inventree/stock.js +++ b/InvenTree/static/script/inventree/stock.js @@ -277,4 +277,64 @@ function deleteStockItems(items, options) { modal: modal, title: 'Delete ' + items.length + ' stock items' }); -} \ No newline at end of file +} + + +function loadStockTable(modal, options) { + modal.bootstrapTable({ + sortable: true, + search: true, + method: 'get', + pagination: true, + rememberOrder: true, + queryParams: options.params, + columns: [ + { + checkbox: true, + title: 'Select', + searchable: false, + }, + { + field: 'pk', + title: 'ID', + visible: false, + }, + { + field: 'part.name', + title: 'Part', + sortable: true, + formatter: function(value, row, index, field) { + return renderLink(value, row.part.url); + } + }, + { + field: 'location', + title: 'Location', + sortable: true, + formatter: function(value, row, index, field) { + if (row.location) { + return renderLink(row.location.pathstring, row.location.url); + } + else { + return ''; + } + } + }, + { + field: 'quantity', + title: 'Quantity', + sortable: true, + formatter: function(value, row, index, field) { + var text = renderLink(value, row.url); + text = text + "" + row.status + ""; + return text; + } + }, + { + field: 'notes', + title: 'Notes', + } + ], + url: options.url, + }); +}; \ No newline at end of file diff --git a/InvenTree/stock/templates/stock/location.html b/InvenTree/stock/templates/stock/location.html index 5a232ec74f..dc68a198b5 100644 --- a/InvenTree/stock/templates/stock/location.html +++ b/InvenTree/stock/templates/stock/location.html @@ -9,6 +9,7 @@

{{ location.description }}

{% else %}

Stock

+

All stock items

{% endif %}
@@ -160,67 +161,12 @@ return false; }); - $("#stock-table").bootstrapTable({ - sortable: true, - search: true, - method: 'get', - pagination: true, - rememberOrder: true, - {% if location %} - queryParams: function(p) { - return { - location: {{ location.id }} - } - }, - {% endif %} - columns: [ - { - checkbox: true, - title: 'Select', - searchable: false, - }, - { - field: 'pk', - title: 'ID', - visible: false, - }, - { - field: 'part.name', - title: 'Part', - sortable: true, - formatter: function(value, row, index, field) { - return renderLink(value, row.part.url); - } - }, - {% if location == None %} - { - field: 'location', - title: 'Location', - sortable: true, - formatter: function(value, row, index, field) { - if (row.location) { - return renderLink(row.location.name, row.location.url); - } - else { - return ''; - } - } - }, + loadStockTable($("#stock-table"), { + params: { + {% if location %} + location: {{ location.id }} {% endif %} - { - field: 'quantity', - title: 'Stock', - sortable: true, - formatter: function(value, row, index, field) { - return renderLink(value, row.url); - } - }, - { - field: 'status', - title: 'Status', - sortable: true, - } - ], + }, url: "{% url 'api-stock-list' %}", }); From 963883acd65474e9be52100611cc19d16fdca0f2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 17 Apr 2019 23:17:04 +1000 Subject: [PATCH 2/3] Consolidated function to adjust stock - Add / remove / stocktake - Handle forms and tables properly --- InvenTree/part/templates/part/stock.html | 22 +++++------ InvenTree/static/script/inventree/stock.js | 39 +++++++++++++++++++ InvenTree/stock/templates/stock/location.html | 22 +++++------ 3 files changed, 57 insertions(+), 26 deletions(-) diff --git a/InvenTree/part/templates/part/stock.html b/InvenTree/part/templates/part/stock.html index d41a735989..9f628fef1c 100644 --- a/InvenTree/part/templates/part/stock.html +++ b/InvenTree/part/templates/part/stock.html @@ -70,28 +70,24 @@ return false; }); - function _stock(action) { - adjustStock({ - action: action, - items: selectedStock(), - success: function() { - $('#stock-table').bootstrapTable('refresh'); - } - }); - } - $("#multi-item-stocktake").click(function() { - _stock('stocktake'); + updateStockItems({ + action: 'stocktake' + }); return false; }); $("#multi-item-take").click(function() { - _stock('remove'); + updateStockItems({ + action: 'remove', + }); return false; }); $("#multi-item-give").click(function() { - _stock('add'); + updateStockItems({ + action: 'add', + }); return false; }) diff --git a/InvenTree/static/script/inventree/stock.js b/InvenTree/static/script/inventree/stock.js index d08b705023..de383867f1 100644 --- a/InvenTree/static/script/inventree/stock.js +++ b/InvenTree/static/script/inventree/stock.js @@ -153,6 +153,19 @@ function updateStock(items, options={}) { }); } + +function selectStockItems(options) { + /* Return list of selections from stock table + * If options.table not provided, assumed to be '#stock-table' + */ + + var table_name = options.table || '#stock-table'; + + // Return list of selected items from the bootstrap table + return $(table_name).bootstrapTable('getSelections'); +} + + function adjustStock(options) { if (options.items) { updateStock(options.items, options); @@ -172,6 +185,32 @@ function adjustStock(options) { } } + +function updateStockItems(options) { + /* Update one or more stock items selected from a stock-table + * Options available: + * 'action' - Action to perform - 'add' / 'remove' / 'stocktake' + * 'table' - ID of the stock table (default = '#stock-table' + */ + + var table = options.table || '#stock-table'; + + var items = selectStockItems({ + table: table, + }); + + // Pass items through + options.items = items; + options.table = table; + + // On success, reload the table + options.success = function() { + $(table).bootstrapTable('refresh'); + }; + + adjustStock(options); +} + function moveStockItems(items, options) { var modal = options.modal || '#modal-form'; diff --git a/InvenTree/stock/templates/stock/location.html b/InvenTree/stock/templates/stock/location.html index dc68a198b5..79ae9ecd15 100644 --- a/InvenTree/stock/templates/stock/location.html +++ b/InvenTree/stock/templates/stock/location.html @@ -136,28 +136,24 @@ return false; }); - function _stock(action) { - adjustStock({ - action: action, - items: selectedStock(), - success: function() { - $('#stock-table').bootstrapTable('refresh'); - } - }) - } - $('#multi-item-stocktake').click(function() { - _stock('stocktake'); + updateStockItems({ + action: 'stocktake', + }); return false; }); $('#multi-item-remove').click(function() { - _stock('remove'); + updateStockItems({ + action: 'remove', + }); return false; }); $('#multi-item-add').click(function() { - _stock('add'); + updateStockItems({ + action: 'add', + }); return false; }); From ebcbf9a90edcb97d805b7ba5d4ed107e46dae31d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 17 Apr 2019 23:33:19 +1000 Subject: [PATCH 3/3] Add status_text to API --- InvenTree/build/serializers.py | 3 +++ InvenTree/static/script/inventree/stock.js | 2 +- InvenTree/stock/serializers.py | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index f28c9fcf89..ac36fd5b41 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -9,6 +9,7 @@ from .models import Build class BuildSerializer(serializers.ModelSerializer): url = serializers.CharField(source='get_absolute_url', read_only=True) + status_text = serializers.CharField(source='get_status_display', read_only=True) class Meta: model = Build @@ -20,4 +21,6 @@ class BuildSerializer(serializers.ModelSerializer): 'completion_date', 'part', 'quantity', + 'status', + 'status_text', 'notes'] diff --git a/InvenTree/static/script/inventree/stock.js b/InvenTree/static/script/inventree/stock.js index de383867f1..c0a931cdbf 100644 --- a/InvenTree/static/script/inventree/stock.js +++ b/InvenTree/static/script/inventree/stock.js @@ -365,7 +365,7 @@ function loadStockTable(modal, options) { sortable: true, formatter: function(value, row, index, field) { var text = renderLink(value, row.url); - text = text + "" + row.status + ""; + text = text + "" + row.status_text + ""; return text; } }, diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index 104737c437..f79ceaa3b1 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -63,6 +63,7 @@ class StockItemSerializer(serializers.ModelSerializer): part = PartBriefSerializer(many=False, read_only=True) location = LocationBriefSerializer(many=False, read_only=True) + status_text = serializers.CharField(source='get_status_display', read_only=True) class Meta: model = StockItem @@ -78,6 +79,7 @@ class StockItemSerializer(serializers.ModelSerializer): 'serial', 'batch', 'status', + 'status_text', 'notes', ]