Remove 'destination' field if we are not moving stock

- Allow different stock adjustment actions
This commit is contained in:
Oliver Walters 2019-06-02 12:00:39 +10:00
parent d1ff115f74
commit 8dd9034563
3 changed files with 83 additions and 28 deletions

View File

@ -578,10 +578,7 @@ function loadStockTable(table, options) {
});
$('#multi-item-add').click(function() {
updateStockItems({
action: 'add',
});
return false;
stockAdjustment('add');
});
$("#multi-item-move").click(function() {

View File

@ -21,7 +21,10 @@
{{ item.part.full_name }} <small><i>{{ item.part.description }}</i></small></td>
<td>{{ item.location.pathstring }}</td>
<td>
<input class='numberinput' min='0' max='{{ item.quantity }}' value='{{ item.new_quantity }}' type='number' name='stock-id-{{ item.id }}' id='stock-id-{{ item.id }}'/>
<input class='numberinput'
min='0'
{% if stock_action == 'move' %} max='{{ item.quantity }}' {% endif %}
value='{{ item.new_quantity }}' type='number' name='stock-id-{{ item.id }}' id='stock-id-{{ item.id }}'/>
{% if item.error %}
<br><span class='help-inline'>{{ item.error }}</span>
{% endif %}

View File

@ -174,7 +174,7 @@ class StockAdjust(AjaxView, FormMixin):
for item in items:
# Initialize quantity to zero for addition/removal
if self.stock_action in ['take', 'give']:
if self.stock_action in ['take', 'add']:
item.new_quantity = 0
# Initialize quantity at full amount for counting or moving
else:
@ -216,6 +216,15 @@ class StockAdjust(AjaxView, FormMixin):
return context
def get_form(self):
form = super().get_form()
if not self.stock_action == 'move':
form.fields.pop('destination')
return form
def get(self, request, *args, **kwargs):
self.request = request
@ -224,7 +233,7 @@ class StockAdjust(AjaxView, FormMixin):
self.stock_action = request.GET.get('action', '').lower()
# Pick a default action...
if self.stock_action not in ['move', 'count', 'take', 'give']:
if self.stock_action not in ['move', 'count', 'take', 'add']:
self.stock_action = 'count'
# Choose the form title based on the action
@ -232,7 +241,7 @@ class StockAdjust(AjaxView, FormMixin):
'move': 'Move Stock',
'count': 'Count Stock',
'take': 'Remove Stock',
'give': 'Add Stock'
'add': 'Add Stock'
}
self.ajax_form_title = titles[self.stock_action]
@ -246,6 +255,8 @@ class StockAdjust(AjaxView, FormMixin):
self.request = request
self.stock_action = request.POST.get('stock_action').lower()
# Update list of stock items
self.stock_items = self.get_POST_items()
@ -265,11 +276,13 @@ class StockAdjust(AjaxView, FormMixin):
item.error = _('Quantity must be positive')
valid = False
continue
if item.new_quantity > item.quantity:
item.error = _('Quantity must not exceed {x}'.format(x=item.quantity))
valid = False
continue
if self.stock_action in ['move']:
if item.new_quantity > item.quantity:
item.error = _('Quantity must not exceed {x}'.format(x=item.quantity))
valid = False
continue
confirmed = str2bool(request.POST.get('confirm'))
@ -281,26 +294,68 @@ class StockAdjust(AjaxView, FormMixin):
'form_valid': valid,
}
self.stock_action = request.POST.get('stock_action').lower()
if valid:
if self.stock_action == 'move':
destination = None
try:
destination = StockLocation.objects.get(id=form['destination'].value())
except StockLocation.DoesNotExist:
pass
except ValueError:
pass
if destination:
data['success'] = self.do_move(destination)
data['success'] = self.do_action()
return self.renderJsonResponse(request, form, data=data)
def do_action(self):
""" Perform stock adjustment action """
if self.stock_action == 'move':
destination = None
try:
destination = StockLocation.objects.get(id=self.request.POST.get('destination'))
except StockLocation.DoesNotExist:
pass
except ValueError:
pass
return self.do_move(destination)
elif self.stock_action == 'add':
return self.do_add()
elif self.stock_action == 'take':
return self.do_take()
elif self.stock_action == 'count':
return self.do_count()
else:
return 'No action performed'
def do_add(self):
count = 0
note = self.request.POST['note']
for item in self.stock_items:
if item.new_quantity <= 0:
continue
count += 1
return _("Added stock to {n} items".format(n=count))
def do_take(self):
count = 0
note = self.request.POST['note']
for item in self.stock_items:
if item.new_quantity <= 0:
continue
count += 1
return _("Removed stock from {n} items".format(n=count))
def do_count(self):
pass
def do_move(self, destination):
""" Perform actual stock movement """