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 _
|
2017-04-21 13:47:04 +00:00
|
|
|
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
|
2018-04-16 10:08:04 +00:00
|
|
|
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
|
|
|
|
2017-04-20 12:08:27 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2018-04-14 10:33:53 +00:00
|
|
|
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
|
2018-04-12 06:27:26 +00:00
|
|
|
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
|
|
|
"""
|
|
|
|
|
2018-04-15 13:27:56 +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
|
|
|
|
2018-04-14 10:33:53 +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():
|
2018-04-15 14:30:57 +00:00
|
|
|
# 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()
|
2018-04-14 10:33:53 +00:00
|
|
|
|
2018-04-15 13:27:56 +00:00
|
|
|
# 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):
|
2018-04-16 10:08:04 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2018-04-15 13:27:56 +00:00
|
|
|
|
|
|
|
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'),
|
|
|
|
]
|
|
|
|
|
2018-04-16 10:08:04 +00:00
|
|
|
# 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
|
|
|
|
2018-04-16 10:08:04 +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
|
|
|
|
2018-04-16 10:08:04 +00:00
|
|
|
# Where the part is stored. If the part has been used to build another stock item, the location may not make sense
|
2018-04-14 10:33:53 +00:00
|
|
|
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
|
|
|
|
2018-04-16 10:08:04 +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?')
|
2018-04-16 10:08:04 +00:00
|
|
|
|
|
|
|
# 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?')
|
2018-04-16 10:08:04 +00:00
|
|
|
|
|
|
|
# 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 10:08:04 +00:00
|
|
|
|
2018-04-16 11:07:57 +00:00
|
|
|
# Optional URL to link to external resource
|
|
|
|
URL = models.URLField(max_length=125, blank=True)
|
|
|
|
|
2018-04-16 10:08:04 +00:00
|
|
|
# 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')
|
2018-04-16 10:08:04 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2018-04-16 10:08:04 +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
|
2017-04-20 12:08:27 +00:00
|
|
|
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
|
2018-04-12 06:27:26 +00:00
|
|
|
ITEM_OK = 10
|
2017-03-29 12:19:53 +00:00
|
|
|
ITEM_ATTENTION = 50
|
|
|
|
ITEM_DAMAGED = 55
|
|
|
|
ITEM_DESTROYED = 60
|
|
|
|
|
|
|
|
ITEM_STATUS_CODES = {
|
2018-04-12 06:27:26 +00:00
|
|
|
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(
|
2018-04-12 06:27:26 +00:00
|
|
|
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
|
2018-04-12 06:27:26 +00:00
|
|
|
# expected_arrival = models.DateField(null=True, blank=True)
|
2017-03-29 11:55:28 +00:00
|
|
|
|
2017-04-20 12:08:27 +00:00
|
|
|
infinite = models.BooleanField(default=False)
|
|
|
|
|
2018-04-16 10:17:58 +00:00
|
|
|
@property
|
|
|
|
def has_tracking_info(self):
|
|
|
|
return self.tracking_info.all().count() > 0
|
|
|
|
|
2017-04-21 13:47:04 +00:00
|
|
|
@transaction.atomic
|
2017-04-20 12:40:59 +00:00
|
|
|
def stocktake(self, count, user):
|
2017-04-20 12:08:27 +00:00
|
|
|
""" 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
|
2017-04-20 12:08:27 +00:00
|
|
|
self.save()
|
|
|
|
|
2017-04-21 13:47:04 +00:00
|
|
|
@transaction.atomic
|
2017-04-20 12:08:27 +00:00
|
|
|
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
|
|
|
|
2017-04-20 12:08:27 +00:00
|
|
|
self.quantity = q
|
|
|
|
self.save()
|
|
|
|
|
2017-04-21 13:47:04 +00:00
|
|
|
@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 10:08:04 +00:00
|
|
|
|
2018-04-16 12:23:29 +00:00
|
|
|
@property
|
|
|
|
def is_trackable(self):
|
|
|
|
return self.part.trackable
|
|
|
|
|
2018-04-16 10:08:04 +00:00
|
|
|
|
|
|
|
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()
|