InvenTree/InvenTree/stock/models.py

221 lines
7.1 KiB
Python
Raw Normal View History

2017-03-27 10:04:15 +00:00
from __future__ import unicode_literals
2017-03-29 12:19:53 +00:00
from django.utils.translation import ugettext as _
from django.db import models, transaction
2017-04-16 07:05:02 +00:00
from django.core.validators import MinValueValidator
2017-04-20 12:40:59 +00:00
from django.contrib.auth.models import User
2017-03-27 10:04:15 +00:00
2017-04-15 15:43:30 +00:00
from supplier.models import SupplierPart
from supplier.models import Customer
2017-03-27 10:04:15 +00:00
from part.models import Part
2017-03-27 11:55:21 +00:00
from InvenTree.models import InvenTreeTree
2017-03-27 10:04:15 +00:00
from datetime import datetime
from django.db.models.signals import pre_delete
from django.dispatch import receiver
2017-03-28 12:25:38 +00:00
2018-04-15 15:02:17 +00:00
2017-04-11 08:58:44 +00:00
class StockLocation(InvenTreeTree):
""" Organization tree for StockItem objects
A "StockLocation" can be considered a warehouse, or storage location
Stock locations can be heirarchical as required
2017-04-11 08:58:44 +00:00
"""
def get_absolute_url(self):
return '/stock/location/{id}/'.format(id=self.id)
2017-04-11 08:58:44 +00:00
@property
def items(self):
stock_list = self.stockitem_set.all()
return stock_list
2017-03-29 11:55:28 +00:00
2017-03-28 12:25:38 +00:00
@receiver(pre_delete, sender=StockLocation, dispatch_uid='stocklocation_delete_log')
def before_delete_stock_location(sender, instance, using, **kwargs):
# Update each part in the stock location
for item in instance.items.all():
# If this location has a parent, move the child stock items to the parent
if instance.parent:
item.location = instance.parent
item.save()
# No parent location? Delete the stock items
else:
item.delete()
# Update each child category
for child in instance.children.all():
child.parent = instance.parent
child.save()
2018-04-15 15:02:17 +00:00
2017-03-27 10:04:15 +00:00
class StockItem(models.Model):
"""
A 'StockItem' instance represents a quantity of physical instances of a part.
It may exist in a StockLocation, or as part of a sub-assembly installed into another StockItem
StockItems may be tracked using batch or serial numbers.
If a serial number is assigned, then StockItem cannot have a quantity other than 1
"""
def get_absolute_url(self):
return '/stock/item/{id}/'.format(id=self.id)
2018-04-16 13:09:45 +00:00
class Meta:
unique_together = [
('part', 'serial'),
]
# The 'master' copy of the part of which this stock item is an instance
2017-04-15 15:24:00 +00:00
part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='locations')
2018-04-14 05:26:42 +00:00
# The 'supplier part' used in this instance. May be null if no supplier parts are defined the master part
2017-04-15 15:43:30 +00:00
supplier_part = models.ForeignKey(SupplierPart, blank=True, null=True, on_delete=models.SET_NULL)
2018-04-14 05:26:42 +00:00
# Where the part is stored. If the part has been used to build another stock item, the location may not make sense
location = models.ForeignKey(StockLocation, on_delete=models.DO_NOTHING,
2018-04-16 13:09:45 +00:00
related_name='items', blank=True, null=True,
help_text='Where is this stock item located?')
2018-04-14 05:26:42 +00:00
# If this StockItem belongs to another StockItem (e.g. as part of a sub-assembly)
belongs_to = models.ForeignKey('self', on_delete=models.DO_NOTHING,
2018-04-16 13:09:45 +00:00
related_name='owned_parts', blank=True, null=True,
help_text='Is this item installed in another item?')
# The StockItem may be assigned to a particular customer
2018-04-16 13:09:45 +00:00
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL,
related_name='stockitems', blank=True, null=True,
help_text='Item assigned to customer?')
# Optional serial number
2018-04-16 13:09:45 +00:00
serial = models.PositiveIntegerField(blank=True, null=True,
help_text='Serial number for this item')
2018-04-16 11:07:57 +00:00
# Optional URL to link to external resource
URL = models.URLField(max_length=125, blank=True)
# Optional batch information
2018-04-16 13:09:45 +00:00
batch = models.CharField(max_length=100, blank=True,
help_text='Batch code for this stock item')
# Quantity of this stock item. Value may be overridden by other settings
2017-04-16 07:05:02 +00:00
quantity = models.PositiveIntegerField(validators=[MinValueValidator(0)])
2018-04-14 05:26:42 +00:00
# Last time this item was updated (set automagically)
2017-03-27 10:04:15 +00:00
updated = models.DateField(auto_now=True)
2017-03-29 11:55:28 +00:00
2017-03-29 12:36:06 +00:00
# last time the stock was checked / counted
stocktake_date = models.DateField(blank=True, null=True)
2018-04-14 05:26:42 +00:00
2017-04-20 12:40:59 +00:00
stocktake_user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
2017-03-29 12:36:06 +00:00
review_needed = models.BooleanField(default=False)
2017-03-28 11:27:46 +00:00
# Stock status types
ITEM_OK = 10
2017-03-29 12:19:53 +00:00
ITEM_ATTENTION = 50
ITEM_DAMAGED = 55
ITEM_DESTROYED = 60
ITEM_STATUS_CODES = {
ITEM_OK: _("OK"),
2017-03-29 12:19:53 +00:00
ITEM_ATTENTION: _("Attention needed"),
ITEM_DAMAGED: _("Damaged"),
ITEM_DESTROYED: _("Destroyed")
}
2017-03-29 11:55:28 +00:00
status = models.PositiveIntegerField(
default=ITEM_OK,
2017-04-16 07:05:02 +00:00
choices=ITEM_STATUS_CODES.items(),
validators=[MinValueValidator(0)])
2017-03-29 11:55:28 +00:00
2017-04-15 15:43:30 +00:00
notes = models.CharField(max_length=100, blank=True)
2017-03-29 11:55:28 +00:00
# If stock item is incoming, an (optional) ETA field
# expected_arrival = models.DateField(null=True, blank=True)
2017-03-29 11:55:28 +00:00
infinite = models.BooleanField(default=False)
@property
def has_tracking_info(self):
return self.tracking_info.all().count() > 0
@transaction.atomic
2017-04-20 12:40:59 +00:00
def stocktake(self, count, user):
""" Perform item stocktake.
When the quantity of an item is counted,
record the date of stocktake
"""
count = int(count)
if count < 0 or self.infinite:
return
self.quantity = count
self.stocktake_date = datetime.now().date()
2017-04-20 12:40:59 +00:00
self.stocktake_user = user
self.save()
@transaction.atomic
def add_stock(self, amount):
""" 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)
if self.infinite or amount == 0:
return
amount = int(amount)
q = self.quantity + amount
if q < 0:
q = 0
2017-04-20 12:10:13 +00:00
self.quantity = q
self.save()
@transaction.atomic
2017-04-20 12:20:41 +00:00
def take_stock(self, amount):
self.add_stock(-amount)
2017-03-27 10:04:15 +00:00
def __str__(self):
return "{n} x {part} @ {loc}".format(
2017-03-28 12:25:38 +00:00
n=self.quantity,
part=self.part.name,
loc=self.location.name)
2018-04-16 12:23:29 +00:00
@property
def is_trackable(self):
return self.part.trackable
class StockItemTracking(models.Model):
""" Stock tracking entry
"""
# Stock item
item = models.ForeignKey(StockItem, on_delete=models.CASCADE,
related_name='tracking_info')
# Date this entry was created (cannot be edited)
date = models.DateField(auto_now_add=True, editable=False)
# Short-form title for this tracking entry
title = models.CharField(max_length=250)
# Optional longer description
description = models.CharField(max_length=1024, blank=True)
# Which user created this tracking entry?
user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
# TODO
# image = models.ImageField(upload_to=func, max_length=255, null=True, blank=True)
# TODO
# file = models.FileField()