Add new option to move-stock form

- Set the destination as the default location for parts being moved
This commit is contained in:
Oliver Walters 2019-09-05 19:59:00 +10:00
parent c96c4d16a3
commit 7fd4359007
2 changed files with 26 additions and 7 deletions

View File

@ -117,10 +117,12 @@ class AdjustStockForm(forms.ModelForm):
return choices return choices
destination = forms.ChoiceField(label='Destination', required=True, help_text='Destination stock location') destination = forms.ChoiceField(label='Destination', required=True, help_text=_('Destination stock location'))
note = forms.CharField(label='Notes', required=True, help_text='Add note (required)') note = forms.CharField(label='Notes', required=True, help_text='Add note (required)')
# transaction = forms.BooleanField(required=False, initial=False, label='Create Transaction', help_text='Create a stock transaction for these parts') # transaction = forms.BooleanField(required=False, initial=False, label='Create Transaction', help_text='Create a stock transaction for these parts')
confirm = forms.BooleanField(required=False, initial=False, label='Confirm stock adjustment', help_text='Confirm movement of stock items') confirm = forms.BooleanField(required=False, initial=False, label='Confirm stock adjustment', help_text=_('Confirm movement of stock items'))
set_loc = forms.BooleanField(required=False, initial=False, label='Set Default Location', help_text=_('Set the destination as the default location for selected parts'))
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)

View File

@ -148,7 +148,15 @@ class StockAdjust(AjaxView, FormMixin):
stock_items = [] stock_items = []
def get_GET_items(self): def get_GET_items(self):
""" Return list of stock items initally requested using GET """ """ Return list of stock items initally requested using GET.
Items can be retrieved by:
a) List of stock ID - stock[]=1,2,3,4,5
b) Parent part - part=3
c) Parent location - location=78
d) Single item - item=2
"""
# Start with all 'in stock' items # Start with all 'in stock' items
items = StockItem.objects.filter(customer=None, belongs_to=None) items = StockItem.objects.filter(customer=None, belongs_to=None)
@ -224,6 +232,7 @@ class StockAdjust(AjaxView, FormMixin):
if not self.stock_action == 'move': if not self.stock_action == 'move':
form.fields.pop('destination') form.fields.pop('destination')
form.fields.pop('set_loc')
return form return form
@ -257,7 +266,7 @@ class StockAdjust(AjaxView, FormMixin):
self.request = request self.request = request
self.stock_action = request.POST.get('stock_action').lower() self.stock_action = request.POST.get('stock_action', 'invalid').lower()
# Update list of stock items # Update list of stock items
self.stock_items = self.get_POST_items() self.stock_items = self.get_POST_items()
@ -297,8 +306,9 @@ class StockAdjust(AjaxView, FormMixin):
} }
if valid: if valid:
result = self.do_action()
data['success'] = self.do_action() data['success'] = result
return self.renderJsonResponse(request, form, data=data) return self.renderJsonResponse(request, form, data=data)
@ -308,6 +318,8 @@ class StockAdjust(AjaxView, FormMixin):
if self.stock_action == 'move': if self.stock_action == 'move':
destination = None destination = None
set_default_loc = str2bool(self.request.POST.get('set_loc', False))
try: try:
destination = StockLocation.objects.get(id=self.request.POST.get('destination')) destination = StockLocation.objects.get(id=self.request.POST.get('destination'))
except StockLocation.DoesNotExist: except StockLocation.DoesNotExist:
@ -315,7 +327,7 @@ class StockAdjust(AjaxView, FormMixin):
except ValueError: except ValueError:
pass pass
return self.do_move(destination) return self.do_move(destination, set_default_loc)
elif self.stock_action == 'add': elif self.stock_action == 'add':
return self.do_add() return self.do_add()
@ -372,7 +384,7 @@ class StockAdjust(AjaxView, FormMixin):
return _("Counted stock for {n} items".format(n=count)) return _("Counted stock for {n} items".format(n=count))
def do_move(self, destination): def do_move(self, destination, set_loc=None):
""" Perform actual stock movement """ """ Perform actual stock movement """
count = 0 count = 0
@ -384,6 +396,11 @@ class StockAdjust(AjaxView, FormMixin):
if item.new_quantity <= 0: if item.new_quantity <= 0:
continue continue
# If we wish to set the destination location to the default one
if set_loc:
item.part.default_location = destination
item.part.save()
# Do not move to the same location (unless the quantity is different) # Do not move to the same location (unless the quantity is different)
if destination == item.location and item.new_quantity == item.quantity: if destination == item.location and item.new_quantity == item.quantity:
continue continue