From 7b332d93ee8bc5f1d7c9cfeb1c25a89cb4ccc58a Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Sat, 8 Aug 2020 08:48:27 +1000
Subject: [PATCH 1/9] Rearrange button layouts

---
 .../stock/templates/stock/item_base.html      | 21 ++++++++++++-------
 InvenTree/templates/js/stock.html             |  8 +------
 2 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html
index a35b5f0346..8a57c8bdaf 100644
--- a/InvenTree/stock/templates/stock/item_base.html
+++ b/InvenTree/stock/templates/stock/item_base.html
@@ -86,30 +86,35 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
             {% endif %}
         </ul>
     </div>
-    {% if item.in_stock %}
     <!-- Stock adjustment menu -->
     <div class='dropdown dropdown-buttons'>
         <button id='stock-options' title='{% trans "Stock adjustment actions" %}' class='btn btn-default dropdown-toggle' type='button' data-toggle='dropdown'><span class='fas fa-boxes'></span> <span class='caret'></span></button>
         <ul class='dropdown-menu' role='menu'>
+            {% if item.in_stock %}
             {% if not item.serialized %}
             <li><a href='#' id='stock-count' title='{% trans "Count stock" %}'><span class='fas fa-clipboard-list'></span> {% trans "Count stock" %}</a></li>
             <li><a href='#' id='stock-add' title='{% trans "Add stock" %}'><span class='fas fa-plus-circle icon-green'></span> {% trans "Add stock" %}</a></li>
             <li><a href='#' id='stock-remove' title='{% trans "Remove stock" %}'><span class='fas fa-minus-circle icon-red'></span> {% trans "Remove stock" %}</a></li>
             {% endif %}
             <li><a href='#' id='stock-move' title='{% trans "Transfer stock" %}'><span class='fas fa-exchange-alt icon-blue'></span> {% trans "Transfer stock" %}</a></li>
+            {% endif %}
+            {% if item.part.trackable and not item.serialized %}
+            <li><a href='#' id='stock-serialize' title='{% trans "Serialize stock" %}'><span class='fas fa-hashtag'></span> {% trans "Serialize stock" %}</a> </li>
+            {% endif %}
+            {% if item.part.salable %}
+            {% if item.customer %}
+            <li><a href='#' id='stock-return-from-customer' title='{% trans "Return to stock" %}'><span class='fas fa-undo'></span> {% trans "Return to stock" %}</a></li>
+            {% else %}
+            <li><a href='#' id='stock-assign-to-customer' title='{% trans "Assign to customer" %}'><span class='fas fa-user-tie'></span> {% trans "Assign to customer" %}</a></li>
+            {% endif %}
+            {% endif %}
         </ul>
     </div>
-    {% endif %}
     <!-- Edit stock item -->
     <div class='dropdown dropdown-buttons'>
         <button id='stock-edit-actions' title='{% trans "Stock actions" %}' class='btn btn-default dropdown-toggle' type='button' data-toggle='dropdown'><span class='fas fa-tools'></span> <span class='caret'></span></button>
         <ul class='dropdown-menu' role='menu'>
-            {% if item.part.trackable and not item.serialized %}
-            <li><a href='#' id='stock-serialize' title='{% trans "Serialize stock" %}'><span class='fas fa-hashtag'></span> {% trans "Serialize stock" %}</a> </li>
-            {% endif %}
-            {% if item.part.salable and not item.customer %}
-            <li><a href='#' id='stock-assign-to-customer' title='{% trans "Assign to customer" %}'><span class='fas fa-user-tie'></span> {% trans "Assign to customer" %}</a></li>
-            {% endif %}
+
             {% if item.part.has_variants %}
             <li><a href='#' id='stock-convert' title='{% trans "Convert to variant" %}'><span class='fas fa-screwdriver'></span> {% trans "Convert to variant" %}</a></li>
             {% endif %}
diff --git a/InvenTree/templates/js/stock.html b/InvenTree/templates/js/stock.html
index 7a92d0df39..adaf07b4f6 100644
--- a/InvenTree/templates/js/stock.html
+++ b/InvenTree/templates/js/stock.html
@@ -425,16 +425,10 @@ function loadStockTable(table, options) {
                 sortable: true,
                 formatter: function(value, row, index, field) {
 
-                    var url = '';
+                    var url = `/stock/item/${row.pk}/`;
                     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;

From bdea29df044522258bdd5920b05176f082759ac8 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Sat, 8 Aug 2020 09:05:33 +1000
Subject: [PATCH 2/9] Add functionality to return stock item from customer

---
 InvenTree/stock/forms.py                      | 12 +++++++
 InvenTree/stock/models.py                     | 17 +++++++++
 .../stock/templates/stock/item_base.html      | 12 +++++--
 InvenTree/stock/urls.py                       |  1 +
 InvenTree/stock/views.py                      | 35 +++++++++++++++++++
 5 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py
index bb403d837d..06d90e33df 100644
--- a/InvenTree/stock/forms.py
+++ b/InvenTree/stock/forms.py
@@ -46,6 +46,18 @@ class AssignStockItemToCustomerForm(HelperForm):
         ]
 
 
+class ReturnStockItemForm(HelperForm):
+    """
+    Form for manually returning a StockItem into stock
+    """
+
+    class Meta:
+        model = StockItem
+        fields = [
+            'location',
+        ]
+
+
 class EditStockItemTestResultForm(HelperForm):
     """
     Form for creating / editing a StockItemTestResult object.
diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py
index 736e2218bf..38e2a35fff 100644
--- a/InvenTree/stock/models.py
+++ b/InvenTree/stock/models.py
@@ -495,6 +495,23 @@ class StockItem(MPTTModel):
         # Return the reference to the stock item
         return item
 
+    def returnFromCustomer(self, location, user=None):
+        """
+        Return stock item from customer, back into the specified location.
+        """
+
+        self.addTransactionNote(
+            _("Returned from customer") + " " + self.customer.name,
+            user,
+            notes=_("Returned to location") + " " + location.name,
+            system=True
+        )
+
+        self.customer = None
+        self.location = location
+
+        self.save()
+
     # If stock item is incoming, an (optional) ETA field
     # expected_arrival = models.DateField(null=True, blank=True)
 
diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html
index 8a57c8bdaf..00ad0063d5 100644
--- a/InvenTree/stock/templates/stock/item_base.html
+++ b/InvenTree/stock/templates/stock/item_base.html
@@ -354,7 +354,6 @@ $("#unlink-barcode").click(function() {
 
 {% if item.in_stock %}
 
-{% if item.part.salable %}
 $("#stock-assign-to-customer").click(function() {
     launchModalForm("{% url 'stock-item-assign' item.id %}",
         {
@@ -362,7 +361,6 @@ $("#stock-assign-to-customer").click(function() {
         }
     );
 });
-{% endif %}
 
 function itemAdjust(action) {
     launchModalForm("/stock/adjust/", 
@@ -403,6 +401,16 @@ $('#stock-add').click(function() {
     itemAdjust('add');
 });
 
+{% else %}
+
+$("#stock-return-from-customer").click(function() {
+    launchModalForm("{% url 'stock-item-return' item.id %}",
+        {
+            reload: true,
+        }
+    );
+});
+
 {% endif %}
 
 $("#stock-delete").click(function () {
diff --git a/InvenTree/stock/urls.py b/InvenTree/stock/urls.py
index 65e9c6742b..b48795b86e 100644
--- a/InvenTree/stock/urls.py
+++ b/InvenTree/stock/urls.py
@@ -24,6 +24,7 @@ stock_item_detail_urls = [
     url(r'^qr_code/', views.StockItemQRCode.as_view(), name='stock-item-qr'),
     url(r'^delete_test_data/', views.StockItemDeleteTestData.as_view(), name='stock-item-delete-test-data'),
     url(r'^assign/', views.StockItemAssignToCustomer.as_view(), name='stock-item-assign'),
+    url(r'^return/', views.StockItemReturnToStock.as_view(), name='stock-item-return'),
 
     url(r'^add_tracking/', views.StockItemTrackingCreate.as_view(), name='stock-tracking-create'),
 
diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py
index 672f0a3892..56a3abaaad 100644
--- a/InvenTree/stock/views.py
+++ b/InvenTree/stock/views.py
@@ -260,6 +260,41 @@ class StockItemAssignToCustomer(AjaxUpdateView):
         return self.renderJsonResponse(request, self.get_form(), data)
 
 
+class StockItemReturnToStock(AjaxUpdateView):
+    """
+    View for returning a stock item (which is assigned to a customer) to stock.
+    """
+
+    model = StockItem
+    ajax_form_title = _("Return to Stock")
+    context_object_name = "item"
+    form_class = StockForms.ReturnStockItemForm
+
+    def post(self, request, *args, **kwargs):
+
+        location = request.POST.get('location', None)
+
+        if location:
+            try:
+                location = StockLocation.objects.get(pk=location)
+            except (ValueError, StockLocation.DoesNotExist):
+                location = None
+
+        if location:
+            stock_item = self.get_object()
+
+            stock_item.returnFromCustomer(location, request.user)
+        else:
+            raise ValidationError({'location': _("Specify a valid location")})
+
+        data = {
+            'form_valid': True,
+            'success': _("Stock item returned from customer")
+        }
+
+        return self.renderJsonResponse(request, self.get_form(), data)
+
+
 class StockItemDeleteTestData(AjaxUpdateView):
     """
     View for deleting all test data

From 80173a9d43a052fc3227efc8493539bb64cb51e7 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Sat, 8 Aug 2020 09:07:25 +1000
Subject: [PATCH 3/9] Remove ASSIGNED_TO_OTHER_ITEM status code

---
 InvenTree/InvenTree/status_codes.py | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/InvenTree/InvenTree/status_codes.py b/InvenTree/InvenTree/status_codes.py
index f8089cb5de..3af1b55a45 100644
--- a/InvenTree/InvenTree/status_codes.py
+++ b/InvenTree/InvenTree/status_codes.py
@@ -169,7 +169,6 @@ class StockStatus(StatusCode):
 
     SHIPPED = 110  # Item has been shipped to a customer
     ASSIGNED_TO_BUILD = 120
-    ASSIGNED_TO_OTHER_ITEM = 130
 
     options = {
         OK: _("OK"),
@@ -181,7 +180,6 @@ class StockStatus(StatusCode):
         RETURNED: _("Returned"),
         SHIPPED: _('Shipped'),
         ASSIGNED_TO_BUILD: _("Used for Build"),
-        ASSIGNED_TO_OTHER_ITEM: _("Installed in Stock Item")
     }
 
     colors = {
@@ -192,7 +190,6 @@ class StockStatus(StatusCode):
         REJECTED: 'red',
         SHIPPED: 'green',
         ASSIGNED_TO_BUILD: 'blue',
-        ASSIGNED_TO_OTHER_ITEM: 'blue',
     }
 
     # The following codes correspond to parts that are 'available' or 'in stock'
@@ -210,7 +207,6 @@ class StockStatus(StatusCode):
         REJECTED,
         SHIPPED,
         ASSIGNED_TO_BUILD,
-        ASSIGNED_TO_OTHER_ITEM,
     ]
 
     # The following codes are available for receiving goods

From 459cc03aae21a699e888c45922addffbd0b55ef7 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Sat, 8 Aug 2020 09:08:15 +1000
Subject: [PATCH 4/9] Remove "ASSIGNED_TO_BUILD" status code

---
 InvenTree/InvenTree/status_codes.py | 4 ----
 InvenTree/build/models.py           | 1 -
 InvenTree/build/test_build.py       | 3 ---
 3 files changed, 8 deletions(-)

diff --git a/InvenTree/InvenTree/status_codes.py b/InvenTree/InvenTree/status_codes.py
index 3af1b55a45..d26be11333 100644
--- a/InvenTree/InvenTree/status_codes.py
+++ b/InvenTree/InvenTree/status_codes.py
@@ -168,7 +168,6 @@ class StockStatus(StatusCode):
     NOT_IN_STOCK = 100
 
     SHIPPED = 110  # Item has been shipped to a customer
-    ASSIGNED_TO_BUILD = 120
 
     options = {
         OK: _("OK"),
@@ -179,7 +178,6 @@ class StockStatus(StatusCode):
         REJECTED: _("Rejected"),
         RETURNED: _("Returned"),
         SHIPPED: _('Shipped'),
-        ASSIGNED_TO_BUILD: _("Used for Build"),
     }
 
     colors = {
@@ -189,7 +187,6 @@ class StockStatus(StatusCode):
         DESTROYED: 'red',
         REJECTED: 'red',
         SHIPPED: 'green',
-        ASSIGNED_TO_BUILD: 'blue',
     }
 
     # The following codes correspond to parts that are 'available' or 'in stock'
@@ -206,7 +203,6 @@ class StockStatus(StatusCode):
         LOST,
         REJECTED,
         SHIPPED,
-        ASSIGNED_TO_BUILD,
     ]
 
     # The following codes are available for receiving goods
diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py
index 23043d077f..d89b2feccf 100644
--- a/InvenTree/build/models.py
+++ b/InvenTree/build/models.py
@@ -501,7 +501,6 @@ class BuildItem(models.Model):
 
         # TODO - If the item__part object is not trackable, delete the stock item here
         
-        item.status = StockStatus.ASSIGNED_TO_BUILD
         item.build_order = self.build
         item.save()
 
diff --git a/InvenTree/build/test_build.py b/InvenTree/build/test_build.py
index c1fb4a5efd..32ad33dab3 100644
--- a/InvenTree/build/test_build.py
+++ b/InvenTree/build/test_build.py
@@ -211,15 +211,12 @@ class BuildTest(TestCase):
         # New stock items created and assigned to the build
         self.assertEqual(StockItem.objects.get(pk=4).quantity, 50)
         self.assertEqual(StockItem.objects.get(pk=4).build_order, self.build)
-        self.assertEqual(StockItem.objects.get(pk=4).status, status.StockStatus.ASSIGNED_TO_BUILD)
 
         self.assertEqual(StockItem.objects.get(pk=5).quantity, 50)
         self.assertEqual(StockItem.objects.get(pk=5).build_order, self.build)
-        self.assertEqual(StockItem.objects.get(pk=5).status, status.StockStatus.ASSIGNED_TO_BUILD)
 
         self.assertEqual(StockItem.objects.get(pk=6).quantity, 250)
         self.assertEqual(StockItem.objects.get(pk=6).build_order, self.build)
-        self.assertEqual(StockItem.objects.get(pk=6).status, status.StockStatus.ASSIGNED_TO_BUILD)
         
         # And a new stock item created for the build output
         self.assertEqual(StockItem.objects.get(pk=7).quantity, 1)

From 54c1f2d9a233daad636613a37f9be46975e1f329 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Sat, 8 Aug 2020 09:15:45 +1000
Subject: [PATCH 5/9] Upgrade to latest flake8

---
 requirements.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/requirements.txt b/requirements.txt
index 362f1bd074..f09cc35167 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -15,7 +15,7 @@ django-crispy-forms==1.8.1      # Form helpers
 django-import-export==2.0.0     # Data import / export for admin interface
 django-cleanup==4.0.0           # Manage deletion of old / unused uploaded files
 django-qr-code==1.2.0           # Generate QR codes
-flake8==3.3.0                   # PEP checking
+flake8==3.8.3                   # PEP checking
 coverage==4.0.3                 # Unit test coverage
 python-coveralls==2.9.1         # Coveralls linking (for Travis)
 rapidfuzz==0.7.6                # Fuzzy string matching

From 861a2982dbdbb6e33e120364c241706a829ce001 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Sat, 8 Aug 2020 09:16:53 +1000
Subject: [PATCH 6/9] Removed "SHIPPED" stock status code

---
 InvenTree/InvenTree/status_codes.py | 5 -----
 InvenTree/build/models.py           | 2 +-
 InvenTree/stock/models.py           | 7 -------
 3 files changed, 1 insertion(+), 13 deletions(-)

diff --git a/InvenTree/InvenTree/status_codes.py b/InvenTree/InvenTree/status_codes.py
index d26be11333..2032ec75d8 100644
--- a/InvenTree/InvenTree/status_codes.py
+++ b/InvenTree/InvenTree/status_codes.py
@@ -167,8 +167,6 @@ class StockStatus(StatusCode):
     # This can be used as a quick check for filtering
     NOT_IN_STOCK = 100
 
-    SHIPPED = 110  # Item has been shipped to a customer
-
     options = {
         OK: _("OK"),
         ATTENTION: _("Attention needed"),
@@ -177,7 +175,6 @@ class StockStatus(StatusCode):
         LOST: _("Lost"),
         REJECTED: _("Rejected"),
         RETURNED: _("Returned"),
-        SHIPPED: _('Shipped'),
     }
 
     colors = {
@@ -186,7 +183,6 @@ class StockStatus(StatusCode):
         DAMAGED: 'red',
         DESTROYED: 'red',
         REJECTED: 'red',
-        SHIPPED: 'green',
     }
 
     # The following codes correspond to parts that are 'available' or 'in stock'
@@ -202,7 +198,6 @@ class StockStatus(StatusCode):
         DESTROYED,
         LOST,
         REJECTED,
-        SHIPPED,
     ]
 
     # The following codes are available for receiving goods
diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py
index d89b2feccf..89e79761e8 100644
--- a/InvenTree/build/models.py
+++ b/InvenTree/build/models.py
@@ -21,7 +21,7 @@ from markdownx.models import MarkdownxField
 
 from mptt.models import MPTTModel, TreeForeignKey
 
-from InvenTree.status_codes import BuildStatus, StockStatus
+from InvenTree.status_codes import BuildStatus
 from InvenTree.fields import InvenTreeURLField
 from InvenTree.helpers import decimal2string
 
diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py
index 38e2a35fff..8f4b04e54f 100644
--- a/InvenTree/stock/models.py
+++ b/InvenTree/stock/models.py
@@ -219,12 +219,6 @@ class StockItem(MPTTModel):
 
         super().clean()
 
-        if self.status == StockStatus.ASSIGNED_TO_OTHER_ITEM and self.belongs_to is None:
-            raise ValidationError({
-                'belongs_to': "Belongs_to field must be specified as statis is marked as ASSIGNED_TO_OTHER_ITEM",
-                'status': 'Status cannot be marked as ASSIGNED_TO_OTHER_ITEM if the belongs_to field is not set',
-            })
-
         try:
             if self.part.trackable:
                 # Trackable parts must have integer values for quantity field!
@@ -477,7 +471,6 @@ class StockItem(MPTTModel):
 
         # Update StockItem fields with new information
         item.sales_order = order
-        item.status = StockStatus.SHIPPED
         item.customer = customer
         item.location = None
 

From ff71fe6e93f1f4d939323a5a69c98d77f2d1b80a Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Sat, 8 Aug 2020 09:20:42 +1000
Subject: [PATCH 7/9] Fix "in_stock" logic for StockItem

- Not in stock if it is at a customer!
---
 InvenTree/stock/models.py                      | 5 +++++
 InvenTree/stock/templates/stock/item_base.html | 9 ++++-----
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py
index 8f4b04e54f..0a9fc6b8bd 100644
--- a/InvenTree/stock/models.py
+++ b/InvenTree/stock/models.py
@@ -140,6 +140,7 @@ class StockItem(MPTTModel):
         sales_order=None,
         build_order=None,
         belongs_to=None,
+        customer=None,
         status__in=StockStatus.AVAILABLE_CODES
     )
 
@@ -609,6 +610,10 @@ class StockItem(MPTTModel):
         if self.build_order is not None:
             return False
 
+        # Not 'in stock' if it has been assigned to a customer
+        if self.customer is not None:
+            return False
+
         # Not 'in stock' if the status code makes it unavailable
         if self.status in StockStatus.UNAVAILABLE_CODES:
             return False
diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html
index 00ad0063d5..9b0b182848 100644
--- a/InvenTree/stock/templates/stock/item_base.html
+++ b/InvenTree/stock/templates/stock/item_base.html
@@ -97,16 +97,15 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
             <li><a href='#' id='stock-remove' title='{% trans "Remove stock" %}'><span class='fas fa-minus-circle icon-red'></span> {% trans "Remove stock" %}</a></li>
             {% endif %}
             <li><a href='#' id='stock-move' title='{% trans "Transfer stock" %}'><span class='fas fa-exchange-alt icon-blue'></span> {% trans "Transfer stock" %}</a></li>
-            {% endif %}
             {% if item.part.trackable and not item.serialized %}
             <li><a href='#' id='stock-serialize' title='{% trans "Serialize stock" %}'><span class='fas fa-hashtag'></span> {% trans "Serialize stock" %}</a> </li>
             {% endif %}
-            {% if item.part.salable %}
-            {% if item.customer %}
-            <li><a href='#' id='stock-return-from-customer' title='{% trans "Return to stock" %}'><span class='fas fa-undo'></span> {% trans "Return to stock" %}</a></li>
-            {% else %}
+            {% endif %}
+            {% if item.part.salable and not item.customer %}
             <li><a href='#' id='stock-assign-to-customer' title='{% trans "Assign to customer" %}'><span class='fas fa-user-tie'></span> {% trans "Assign to customer" %}</a></li>
             {% endif %}
+            {% if item.customer %}
+            <li><a href='#' id='stock-return-from-customer' title='{% trans "Return to stock" %}'><span class='fas fa-undo'></span> {% trans "Return to stock" %}</a></li>
             {% endif %}
         </ul>
     </div>

From a2c3c1086cea8515bc6cd9976531b5a73aba99bb Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Sat, 8 Aug 2020 09:31:57 +1000
Subject: [PATCH 8/9] Catch unhandled javascript errors

---
 InvenTree/stock/templates/stock/item_base.html |  2 +-
 InvenTree/templates/status_codes.html          | 12 ++++++++----
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html
index 9b0b182848..ce055d65c5 100644
--- a/InvenTree/stock/templates/stock/item_base.html
+++ b/InvenTree/stock/templates/stock/item_base.html
@@ -161,7 +161,7 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
     <tr>
         <td><span class='fas fa-user-tie'></span></td>
         <td>{% trans "Customer" %}</td>
-        <td><a href="{% url 'company-detail' item.customer.id %}">{{ item.customer.name }}</a></td>
+        <td><a href="{% url 'company-detail-assigned-stock' item.customer.id %}">{{ item.customer.name }}</a></td>
     </tr>
     {% endif %}
     {% if item.belongs_to %}
diff --git a/InvenTree/templates/status_codes.html b/InvenTree/templates/status_codes.html
index 029252a842..f032f97309 100644
--- a/InvenTree/templates/status_codes.html
+++ b/InvenTree/templates/status_codes.html
@@ -18,14 +18,18 @@ function {{ label }}StatusDisplay(key) {
 
     key = String(key);
 
-    var value = {{ label }}Codes[key].value;
+    var value = null;
+    var label = null;
+
+    if (key in {{ label }}Codes) {
+        value = {{ label }}Codes[key].value;
+        label = {{ label }}Codes[key].label;
+    }
 
     if (value == null || value.length == 0) {
         value = key;
+        label = '';
     }
 
-    // Select the label color
-    var label = {{ label }}Codes[key].label ?? '';
-
     return `<span class='label ${label}'>${value}</span>`;
 }

From 8d9cfd3678f722c2454a801a08c86da21d49acf6 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Sat, 8 Aug 2020 09:44:21 +1000
Subject: [PATCH 9/9] Migration file for StockStatus codes

---
 .../migrations/0048_auto_20200807_2344.py     | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 InvenTree/stock/migrations/0048_auto_20200807_2344.py

diff --git a/InvenTree/stock/migrations/0048_auto_20200807_2344.py b/InvenTree/stock/migrations/0048_auto_20200807_2344.py
new file mode 100644
index 0000000000..b859344bb0
--- /dev/null
+++ b/InvenTree/stock/migrations/0048_auto_20200807_2344.py
@@ -0,0 +1,19 @@
+# Generated by Django 3.0.7 on 2020-08-07 23:44
+
+import django.core.validators
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('stock', '0047_auto_20200605_0932'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='stockitem',
+            name='status',
+            field=models.PositiveIntegerField(choices=[(10, 'OK'), (50, 'Attention needed'), (55, 'Damaged'), (60, 'Destroyed'), (70, 'Lost'), (65, 'Rejected'), (85, 'Returned')], default=10, validators=[django.core.validators.MinValueValidator(0)]),
+        ),
+    ]