mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-14 19:15:41 +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:
@ -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'})
|
||||
|
||||
|
@ -236,7 +236,7 @@ class StockItem(models.Model):
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def stocktake(self, count, user):
|
||||
def stocktake(self, count, user, notes=''):
|
||||
""" Perform item stocktake.
|
||||
When the quantity of an item is counted,
|
||||
record the date of stocktake
|
||||
@ -252,35 +252,54 @@ class StockItem(models.Model):
|
||||
self.stocktake_user = user
|
||||
self.save()
|
||||
|
||||
self.add_transaction_note('Stocktake',
|
||||
self.add_transaction_note('Stocktake - counted {n} items'.format(n=count),
|
||||
user,
|
||||
notes='Counted {n} items'.format(n=count),
|
||||
notes=notes,
|
||||
system=True)
|
||||
|
||||
@transaction.atomic
|
||||
def add_stock(self, amount):
|
||||
def add_stock(self, quantity, user, notes=''):
|
||||
""" Add items to stock
|
||||
This function can be called by initiating a ProjectRun,
|
||||
or by manually adding the items to the stock location
|
||||
"""
|
||||
|
||||
amount = int(amount)
|
||||
quantity = int(quantity)
|
||||
|
||||
if self.infinite or amount == 0:
|
||||
# Ignore amounts that do not make sense
|
||||
if quantity <= 0 or self.infinite:
|
||||
return
|
||||
|
||||
amount = int(amount)
|
||||
self.quantity += quantity
|
||||
|
||||
q = self.quantity + amount
|
||||
if q < 0:
|
||||
q = 0
|
||||
|
||||
self.quantity = q
|
||||
self.save()
|
||||
|
||||
self.add_transaction_note('Added {n} items to stock'.format(n=quantity),
|
||||
user,
|
||||
notes=notes,
|
||||
system=True)
|
||||
|
||||
@transaction.atomic
|
||||
def take_stock(self, amount):
|
||||
self.add_stock(-amount)
|
||||
def take_stock(self, quantity, user, notes=''):
|
||||
""" Remove items from stock
|
||||
"""
|
||||
|
||||
quantity = int(quantity)
|
||||
|
||||
if quantity <= 0 or self.infinite:
|
||||
return
|
||||
|
||||
self.quantity -= quantity
|
||||
|
||||
if self.quantity < 0:
|
||||
self.quantity = 0
|
||||
|
||||
self.save()
|
||||
|
||||
self.add_transaction_note('Removed {n} items from stock'.format(n=quantity),
|
||||
user,
|
||||
notes=notes,
|
||||
system=True)
|
||||
|
||||
def __str__(self):
|
||||
s = '{n} x {part}'.format(
|
||||
|
Reference in New Issue
Block a user