2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-05 13:10:57 +00:00

Re-enabled installing stock items into others

This commit is contained in:
eeintech
2021-08-02 15:05:24 -04:00
parent 9aa5086067
commit ac3dcac641
6 changed files with 146 additions and 38 deletions

View File

@ -518,36 +518,73 @@ class StockItemInstall(AjaxUpdateView):
part = None
def get_params(self):
""" Retrieve GET parameters """
# Look at GET params
self.part_id = self.request.GET.get('part', None)
self.install_in = self.request.GET.get('install_in', False)
self.install_item = self.request.GET.get('install_item', False)
if self.part_id is None:
# Look at POST params
self.part_id = self.request.POST.get('part', None)
try:
self.part = Part.objects.get(pk=self.part_id)
except (ValueError, Part.DoesNotExist):
self.part = None
def get_stock_items(self):
"""
Return a list of stock items suitable for displaying to the user.
Requirements:
- Items must be in stock
Filters:
- Items can be filtered by Part reference
- Items must be in BOM of stock item
- Items must be serialized
"""
# Filter items in stock
items = StockItem.objects.filter(StockItem.IN_STOCK_FILTER)
# Filter by Part association
# Filter serialized stock items
items = items.exclude(serial__isnull=True).exclude(serial__exact='')
# Look at GET params
part_id = self.request.GET.get('part', None)
if self.part:
# Filter for parts to install this item in
if self.install_in:
# Get parts using this part
allowed_parts = self.part.get_used_in()
# Filter
items = items.filter(part__in=allowed_parts)
if part_id is None:
# Look at POST params
part_id = self.request.POST.get('part', None)
try:
self.part = Part.objects.get(pk=part_id)
items = items.filter(part=self.part)
except (ValueError, Part.DoesNotExist):
self.part = None
# Filter for parts to install in this item
if self.install_item:
# Get parts used in this part's BOM
bom_items = self.part.get_bom_items()
allowed_parts = [item.sub_part for item in bom_items]
# Filter
items = items.filter(part__in=allowed_parts)
return items
def get_context_data(self, **kwargs):
""" Retrieve parameters and update context """
ctx = super().get_context_data(**kwargs)
# Get request parameters
self.get_params()
ctx.update({
'part': self.part,
'install_in': self.install_in,
'install_item': self.install_item,
})
return ctx
def get_initial(self):
initials = super().get_initial()
@ -558,11 +595,17 @@ class StockItemInstall(AjaxUpdateView):
if items.count() == 1:
item = items.first()
initials['stock_item'] = item.pk
initials['quantity_to_install'] = item.quantity
# initials['quantity_to_install'] = item.quantity
if self.part:
initials['part'] = self.part
try:
# Is this stock item being installed in the other stock item?
initials['to_install'] = self.install_in or not self.install_item
except AttributeError:
pass
return initials
def get_form(self):
@ -575,6 +618,8 @@ class StockItemInstall(AjaxUpdateView):
def post(self, request, *args, **kwargs):
self.get_params()
form = self.get_form()
valid = form.is_valid()
@ -584,13 +629,20 @@ class StockItemInstall(AjaxUpdateView):
data = form.cleaned_data
other_stock_item = data['stock_item']
quantity = data['quantity_to_install']
# quantity = data['quantity_to_install']
# Quantity will always be 1 for serialized item
quantity = 1
notes = data['notes']
# Install the other stock item into this one
# Get stock item
this_stock_item = self.get_object()
this_stock_item.installStockItem(other_stock_item, quantity, request.user, notes)
if data['to_install']:
# Install this stock item into the other stock item
other_stock_item.installStockItem(this_stock_item, quantity, request.user, notes)
else:
# Install the other stock item into this one
this_stock_item.installStockItem(other_stock_item, quantity, request.user, notes)
data = {
'form_valid': valid,