2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-12 07:54:14 +00:00

Vast improvements to stocktake API endpoint

- Also acts to ADD and REMOVE stock
- Send 'action' field to specify which one to perform
- Fixed add_stock and remove_stock funcs for StockItem model
- Autoatically add transaction notes for all actions
This commit is contained in:
Oliver
2018-05-08 22:06:28 +10:00
parent ca2d3a1a7b
commit 25e0de1ce7
5 changed files with 252 additions and 110 deletions

View File

@ -52,6 +52,13 @@ class StockFilter(FilterSet):
class StockStocktake(APIView):
"""
Stocktake API endpoint provides stock update of multiple items simultaneously
The 'action' field tells the type of stock action to perform:
* 'stocktake' - Count the stock item(s)
* 'remove' - Remove the quantity provided from stock
* 'add' - Add the quantity provided from stock
"""
permission_classes = [
permissions.IsAuthenticatedOrReadOnly,
@ -59,6 +66,16 @@ class StockStocktake(APIView):
def post(self, request, *args, **kwargs):
if not 'action' in request.data:
raise ValidationError({'action': 'Stocktake action must be provided'})
action = request.data['action']
ACTIONS = ['stocktake', 'remove', 'add']
if not action in ACTIONS:
raise ValidationError({'action': 'Action must be one of ' + ','.join(ACTIONS)})
if not 'items[]' in request.data:
raise ValidationError({'items[]:' 'Request must contain list of items'})
@ -86,8 +103,22 @@ class StockStocktake(APIView):
items.append(item)
# Stocktake notes
notes = ''
if 'notes' in request.data:
notes = request.data['notes']
for item in items:
item['item'].stocktake(item['quantity'], request.user)
quantity = int(item['quantity'])
if action == u'stocktake':
item['item'].stocktake(quantity, request.user, notes=notes)
elif action == u'remove':
item['item'].take_stock(quantity, request.user, notes=notes)
elif action == u'add':
item['item'].add_stock(quantity, request.user, notes=notes)
return Response({'success': 'success'})