mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Auto delete stock items when they are depleted
This commit is contained in:
parent
61351a8f8d
commit
ac326c135f
@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 2.2 on 2019-05-09 12:59
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('stock', '0014_auto_20190508_2332'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='stockitem',
|
||||||
|
name='delete_on_deplete',
|
||||||
|
field=models.BooleanField(default=True, help_text='Delete this Stock Item when stock is depleted'),
|
||||||
|
),
|
||||||
|
]
|
@ -284,13 +284,37 @@ class StockItem(models.Model):
|
|||||||
msg += " (from {loc})".format(loc=str(self.location))
|
msg += " (from {loc})".format(loc=str(self.location))
|
||||||
|
|
||||||
self.location = location
|
self.location = location
|
||||||
self.save()
|
|
||||||
|
|
||||||
self.addTransactionNote(msg,
|
self.addTransactionNote(msg,
|
||||||
user,
|
user,
|
||||||
notes=notes,
|
notes=notes,
|
||||||
system=True)
|
system=True)
|
||||||
|
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def updateQuantity(self, quantity):
|
||||||
|
""" Update stock quantity for this item.
|
||||||
|
|
||||||
|
If the quantity has reached zero, this StockItem will be deleted.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- True if the quantity was saved
|
||||||
|
- False if the StockItem was deleted
|
||||||
|
"""
|
||||||
|
|
||||||
|
if quantity < 0:
|
||||||
|
quantity = 0
|
||||||
|
|
||||||
|
self.quantity = quantity
|
||||||
|
|
||||||
|
if quantity <= 0 and self.delete_on_deplete:
|
||||||
|
self.delete()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
self.save()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
@ -305,10 +329,10 @@ class StockItem(models.Model):
|
|||||||
if count < 0 or self.infinite:
|
if count < 0 or self.infinite:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.quantity = count
|
|
||||||
self.stocktake_date = datetime.now().date()
|
self.stocktake_date = datetime.now().date()
|
||||||
self.stocktake_user = user
|
self.stocktake_user = user
|
||||||
self.save()
|
|
||||||
|
if self.updateQuantity(count):
|
||||||
|
|
||||||
self.addTransactionNote('Stocktake - counted {n} items'.format(n=count),
|
self.addTransactionNote('Stocktake - counted {n} items'.format(n=count),
|
||||||
user,
|
user,
|
||||||
@ -330,9 +354,7 @@ class StockItem(models.Model):
|
|||||||
if quantity <= 0 or self.infinite:
|
if quantity <= 0 or self.infinite:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.quantity += quantity
|
if self.updateQuantity(self.quantity + quantity):
|
||||||
|
|
||||||
self.save()
|
|
||||||
|
|
||||||
self.addTransactionNote('Added {n} items to stock'.format(n=quantity),
|
self.addTransactionNote('Added {n} items to stock'.format(n=quantity),
|
||||||
user,
|
user,
|
||||||
@ -354,12 +376,7 @@ class StockItem(models.Model):
|
|||||||
if quantity <= 0 or self.infinite:
|
if quantity <= 0 or self.infinite:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.quantity -= quantity
|
if self.updateQuantity(self.quantity - quantity):
|
||||||
|
|
||||||
if self.quantity < 0:
|
|
||||||
self.quantity = 0
|
|
||||||
|
|
||||||
self.save()
|
|
||||||
|
|
||||||
self.addTransactionNote('Removed {n} items from stock'.format(n=quantity),
|
self.addTransactionNote('Removed {n} items from stock'.format(n=quantity),
|
||||||
user,
|
user,
|
||||||
|
Loading…
Reference in New Issue
Block a user