From aac835f634e8427c7c9689523a5d9e8ed8bef867 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Thu, 14 Jan 2021 13:41:38 +1100
Subject: [PATCH 1/2] Add menu item to set stock status for multiple items

---
 InvenTree/templates/js/stock.js      | 8 ++++++++
 InvenTree/templates/stock_table.html | 1 +
 2 files changed, 9 insertions(+)

diff --git a/InvenTree/templates/js/stock.js b/InvenTree/templates/js/stock.js
index 9ce395db57..bc2db4098a 100644
--- a/InvenTree/templates/js/stock.js
+++ b/InvenTree/templates/js/stock.js
@@ -682,6 +682,14 @@ function loadStockTable(table, options) {
         });
     });
 
+    $("#multi-item-set-status").click(function() {
+        var selections = $("#stock-table").bootstrapTable('getSelections');
+
+        selections.forEach(function(item) {
+            // TODO
+        });
+    });
+
     $("#multi-item-delete").click(function() {
         var selections = $("#stock-table").bootstrapTable("getSelections");
 
diff --git a/InvenTree/templates/stock_table.html b/InvenTree/templates/stock_table.html
index 51f7c277db..f39f9c733a 100644
--- a/InvenTree/templates/stock_table.html
+++ b/InvenTree/templates/stock_table.html
@@ -23,6 +23,7 @@
                     <li><a href="#" id='multi-item-stocktake' title='{% trans "Stocktake selected stock items" %}'><span class='fas fa-check-circle'></span> {% trans "Count stock" %}</a></li>
                     <li><a href='#' id='multi-item-move' title='{% trans "Move selected stock items" %}'><span class='fas fa-exchange-alt'></span> {% trans "Move stock" %}</a></li>
                     <li><a href='#' id='multi-item-order' title='{% trans "Order selected items" %}'><span class='fas fa-shopping-cart'></span> {% trans "Order stock" %}</a></li>
+                    <li><a href='#' id='multi-item-set-status' title='{% trans "Change status" %}'><span class='fas fa-exclamation-circle'></span> {% trans "Change stock status" %}</a></li>
                     {% endif %}
                     {% if roles.stock.delete %}
                     <li><a href='#' id='multi-item-delete' title='{% trans "Delete selected items" %}'><span class='fas fa-trash-alt'></span> {% trans "Delete Stock" %}</a></li>

From bb9fe98a7edd50d07b41e02730549f6e358f1b92 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Thu, 14 Jan 2021 14:04:24 +1100
Subject: [PATCH 2/2] Set status for multiple stock items at once

---
 InvenTree/templates/js/stock.js | 97 +++++++++++++++++++++++++++++++--
 1 file changed, 93 insertions(+), 4 deletions(-)

diff --git a/InvenTree/templates/js/stock.js b/InvenTree/templates/js/stock.js
index bc2db4098a..e9cb5e2696 100644
--- a/InvenTree/templates/js/stock.js
+++ b/InvenTree/templates/js/stock.js
@@ -6,8 +6,18 @@
  * Requires api.js to be loaded first
  */
 
-/* Functions for interacting with stock management forms
- */
+
+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
@@ -683,11 +693,90 @@ function loadStockTable(table, options) {
     });
 
     $("#multi-item-set-status").click(function() {
+        // Select and set the STATUS field for selected stock items
         var selections = $("#stock-table").bootstrapTable('getSelections');
 
-        selections.forEach(function(item) {
-            // TODO
+        // Select stock status
+        var modal = '#modal-form';
+
+        var status_list = makeOptionsList(
+            stockStatusCodes(),
+            function(item) {
+                return item.text;
+            },
+            function (item) {
+                return item.key;
+            }
+        );
+
+        // Add an empty option at the start of the list
+        status_list.unshift('<option value="">---------</option>');
+
+        // Construct form
+        var html = `
+        <form method='post' action='' class='js-modal-form' enctype='multipart/form-data'>
+            <div class='form-group'>
+                <label class='control-label requiredField' for='id_status'>
+                {% trans "Stock Status" %}
+                </label>
+                <div class='controls'>
+                    <select id='id_status' class='select form-control' name='label'>
+                        ${status_list}
+                    </select>
+                </div>
+            </div>
+        </form>`;
+
+        openModal({
+            modal: modal,
         });
+
+        modalEnable(modal, true);
+        modalSetTitle(modal, '{% trans "Set Stock Status" %}');
+        modalSetContent(modal, html);
+
+        attachSelect(modal);
+
+        modalSubmit(modal, function() {
+            var label = $(modal).find('#id_status');
+
+            var status_code = label.val();
+
+            closeModal(modal);
+            
+            if (!status_code) {
+                showAlertDialog(
+                    '{% trans "Select Status Code" %}',
+                    '{% trans "Status code must be selected" %}'
+                );
+
+                return;
+            }
+
+            var requests = [];
+
+            selections.forEach(function(item) {
+                var url = `/api/stock/${item.pk}/`;
+
+                requests.push(
+                    inventreePut(
+                        url,
+                        {
+                            status: status_code,
+                        },
+                        {
+                            method: 'PATCH',
+                            success: function() {
+                            }
+                        }
+                    )
+                );
+            });
+
+            $.when.apply($, requests).then(function() {
+                $("#stock-table").bootstrapTable('refresh');
+            });
+        })
     });
 
     $("#multi-item-delete").click(function() {