From 963883acd65474e9be52100611cc19d16fdca0f2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 17 Apr 2019 23:17:04 +1000 Subject: [PATCH] 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; });