From ab114b52e9a80398863b1af51faf9754cbc769b8 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 19 Apr 2022 12:46:08 +1000 Subject: [PATCH 1/6] Add new CONVERTED_TO_VARIANT tracking code --- InvenTree/InvenTree/status_codes.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/InvenTree/InvenTree/status_codes.py b/InvenTree/InvenTree/status_codes.py index ffe22039c9..93f213445c 100644 --- a/InvenTree/InvenTree/status_codes.py +++ b/InvenTree/InvenTree/status_codes.py @@ -255,6 +255,9 @@ class StockHistoryCode(StatusCode): # Stock merging operations MERGED_STOCK_ITEMS = 45 + # Convert stock item to variant + CONVERTED_TO_VARIANT = 48 + # Build order codes BUILD_OUTPUT_CREATED = 50 BUILD_OUTPUT_COMPLETED = 55 @@ -294,6 +297,8 @@ class StockHistoryCode(StatusCode): MERGED_STOCK_ITEMS: _('Merged stock items'), + CONVERTED_TO_VARIANT: _('Converted to variant'), + SENT_TO_CUSTOMER: _('Sent to customer'), RETURNED_FROM_CUSTOMER: _('Returned from customer'), From 87de7e4beb29d65fcd96236d2b9ba622845625c6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 19 Apr 2022 12:52:36 +1000 Subject: [PATCH 2/6] Adds "convert_to_variant" function for the StockItem model --- InvenTree/stock/models.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 171ee7e0a3..4c221e92b9 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -718,6 +718,30 @@ class StockItem(MPTTModel): help_text=_('Select Owner'), related_name='stock_items') + @transaction.atomic + def convert_to_variant(self, variant, user, notes=None): + """ + Convert this StockItem instance to a "variant", + i.e. change the "part" reference field + """ + + if not variant: + # Ignore null values + return + + if variant == self.part: + # Variant is the same as the current part + return + + self.part = variant + self.save() + + self.add_tracking_entry( + StockHistoryCode.CONVERTED_TO_VARIANT, + user, + notes=notes + ) + def get_item_owner(self): """ Return the closest "owner" for this StockItem. From 48170ee0960ac9d45cd8e12ce8ede0216a3d3b63 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 19 Apr 2022 13:04:17 +1000 Subject: [PATCH 3/6] Update stock item variant conversion form to use new function --- InvenTree/stock/models.py | 5 ++++- InvenTree/stock/views.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 4c221e92b9..0a5b1652e4 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -739,7 +739,10 @@ class StockItem(MPTTModel): self.add_tracking_entry( StockHistoryCode.CONVERTED_TO_VARIANT, user, - notes=notes + deltas={ + 'part': variant.pk, + }, + notes=_('Converted to part') + ': ' + variant.full_name, ) def get_item_owner(self): diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 079f9c2dc9..5781f69db7 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -643,6 +643,16 @@ class StockItemConvert(AjaxUpdateView): form.fields['part'].queryset = item.part.get_conversion_options() return form + + def save(self, obj, form): + + stock_item = self.get_object() + + variant = form.cleaned_data.get('part', None) + + stock_item.convert_to_variant(variant, user=self.request.user) + + return stock_item class StockLocationCreate(AjaxCreateView): From e5eb1f45136cd9653b90a70798d1d4db627e304a Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 19 Apr 2022 13:09:14 +1000 Subject: [PATCH 4/6] Adds button to refresh stock item tracking table --- InvenTree/templates/js/translated/stock.js | 38 +++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js index 42ed04265d..ff2240b007 100644 --- a/InvenTree/templates/js/translated/stock.js +++ b/InvenTree/templates/js/translated/stock.js @@ -2319,6 +2319,23 @@ function loadStockTrackingTable(table, options) { var cols = []; + var filterTarget = '#filter-list-stocktracking'; + + var filterKey = 'stocktracking'; + + var filters = loadTableFilters(filterKey); + + var params = options.params; + + var original = {}; + + for (var k in params) { + original[k] = params[k]; + filters[k] = params[k]; + } + + setupFilterList(filterKey, table, filterTarget); + // Date cols.push({ field: 'date', @@ -2493,27 +2510,10 @@ function loadStockTrackingTable(table, options) { } }); - /* - // 2021-05-11 - Ability to edit or delete StockItemTracking entries is now removed - cols.push({ - sortable: false, - formatter: function(value, row, index, field) { - // Manually created entries can be edited or deleted - if (false && !row.system) { - var bEdit = ""; - var bDel = ""; - - return "
" + bEdit + bDel + "
"; - } else { - return ""; - } - } - }); - */ - table.inventreeTable({ method: 'get', - queryParams: options.params, + queryParams: filters, + original: original, columns: cols, url: options.url, }); From 5240c60e0b704eab330bfaa15f76287ce7d06f2a Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 19 Apr 2022 13:14:54 +1000 Subject: [PATCH 5/6] Render part information in stock item tracking table (if provided) --- InvenTree/stock/api.py | 9 +++++++++ InvenTree/stock/templates/stock/item.html | 6 +++--- InvenTree/templates/js/translated/stock.js | 13 +++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index e176948599..3768cd8787 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -1234,6 +1234,15 @@ class StockTrackingList(generics.ListAPIView): if not deltas: deltas = {} + # Add part detail + if 'part' in deltas: + try: + part = Part.objects.get(pk=deltas['part']) + serializer = PartBriefSerializer(part) + deltas['part_detail'] = serializer.data + except: + pass + # Add location detail if 'location' in deltas: try: diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index ae0742e99e..75e53d6758 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -26,11 +26,12 @@
-
+
+ {% include "filter_list.html" with id="stocktracking" %}
- +
@@ -342,7 +343,6 @@ ); }); - loadStockTrackingTable($("#track-table"), { params: { ordering: '-date', diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js index ff2240b007..c13ea41f99 100644 --- a/InvenTree/templates/js/translated/stock.js +++ b/InvenTree/templates/js/translated/stock.js @@ -2373,6 +2373,19 @@ function loadStockTrackingTable(table, options) { return html; } + // Part information + if (details.part) { + html += `{% trans "Part" %}`; + + if (details.part_detail) { + html += renderLink(details.part_detail.full_name, `/part/${details.part}/`); + } else { + html += `{% trans "Part information unavailable" %}`; + } + + html += ``; + } + // Location information if (details.location) { From 5f503c7e0f718701e513f5933df3f0ab9b1bae73 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 19 Apr 2022 13:16:45 +1000 Subject: [PATCH 6/6] PEP fxies --- InvenTree/stock/models.py | 2 +- InvenTree/stock/views.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 0a5b1652e4..dc93f61e81 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -728,7 +728,7 @@ class StockItem(MPTTModel): if not variant: # Ignore null values return - + if variant == self.part: # Variant is the same as the current part return diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 5781f69db7..b0661dd0e3 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -643,7 +643,7 @@ class StockItemConvert(AjaxUpdateView): form.fields['part'].queryset = item.part.get_conversion_options() return form - + def save(self, obj, form): stock_item = self.get_object()