mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-05 13:10:57 +00:00
Merge pull request #1892 from eeintech/stock_installed_items
Re-enabled installing stock items into others
This commit is contained in:
@ -536,36 +536,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()
|
||||
@ -576,11 +613,16 @@ class StockItemInstall(AjaxUpdateView):
|
||||
if items.count() == 1:
|
||||
item = items.first()
|
||||
initials['stock_item'] = item.pk
|
||||
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):
|
||||
@ -593,6 +635,8 @@ class StockItemInstall(AjaxUpdateView):
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
|
||||
self.get_params()
|
||||
|
||||
form = self.get_form()
|
||||
|
||||
valid = form.is_valid()
|
||||
@ -602,13 +646,19 @@ class StockItemInstall(AjaxUpdateView):
|
||||
data = form.cleaned_data
|
||||
|
||||
other_stock_item = data['stock_item']
|
||||
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,
|
||||
|
Reference in New Issue
Block a user