2019-04-27 12:49:16 +00:00
|
|
|
"""
|
|
|
|
Stock database model definitions
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-04-17 06:58:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-03-27 10:04:15 +00:00
|
|
|
from __future__ import unicode_literals
|
2018-04-17 06:58:37 +00:00
|
|
|
|
2018-04-27 14:06:39 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.core.exceptions import ValidationError
|
2019-04-24 17:13:41 +00:00
|
|
|
from django.urls import reverse
|
2018-04-17 06:58:37 +00:00
|
|
|
|
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
|
2018-04-16 14:32:02 +00:00
|
|
|
from django.db.models.signals import pre_delete
|
|
|
|
from django.dispatch import receiver
|
|
|
|
|
2020-02-02 01:11:18 +00:00
|
|
|
from markdownx.models import MarkdownxField
|
|
|
|
|
2020-02-17 10:52:31 +00:00
|
|
|
from mptt.models import MPTTModel, TreeForeignKey
|
2019-09-08 09:19:39 +00:00
|
|
|
|
2019-11-18 22:18:41 +00:00
|
|
|
from decimal import Decimal, InvalidOperation
|
2018-04-16 14:32:02 +00:00
|
|
|
from datetime import datetime
|
2019-05-02 10:40:56 +00:00
|
|
|
from InvenTree import helpers
|
2017-03-27 10:04:15 +00:00
|
|
|
|
2019-06-04 13:38:52 +00:00
|
|
|
from InvenTree.status_codes import StockStatus
|
2017-03-27 11:55:21 +00:00
|
|
|
from InvenTree.models import InvenTreeTree
|
2019-09-13 14:08:49 +00:00
|
|
|
from InvenTree.fields import InvenTreeURLField
|
2017-03-28 12:25:38 +00:00
|
|
|
|
2018-04-27 14:06:39 +00:00
|
|
|
from part.models import Part
|
|
|
|
|
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):
|
2019-04-24 17:13:41 +00:00
|
|
|
return reverse('stock-location-detail', kwargs={'pk': self.id})
|
2018-04-15 13:27:56 +00:00
|
|
|
|
2019-05-02 10:50:20 +00:00
|
|
|
def format_barcode(self):
|
|
|
|
""" Return a JSON string for formatting a barcode for this StockLocation object """
|
|
|
|
|
|
|
|
return helpers.MakeBarcode(
|
|
|
|
'StockLocation',
|
2019-05-02 10:57:53 +00:00
|
|
|
self.id,
|
2019-05-02 10:50:20 +00:00
|
|
|
reverse('api-location-detail', kwargs={'pk': self.id}),
|
|
|
|
{
|
|
|
|
'name': self.name,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-09-08 09:13:13 +00:00
|
|
|
def get_stock_items(self, cascade=True):
|
|
|
|
""" Return a queryset for all stock items under this category.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
cascade: If True, also look under sublocations (default = True)
|
2019-05-09 10:30:23 +00:00
|
|
|
"""
|
|
|
|
|
2019-06-17 14:09:42 +00:00
|
|
|
if cascade:
|
2019-09-08 09:13:13 +00:00
|
|
|
query = StockItem.objects.filter(location__in=self.getUniqueChildren(include_self=True))
|
2019-06-17 14:09:42 +00:00
|
|
|
else:
|
2019-09-08 09:13:13 +00:00
|
|
|
query = StockItem.objects.filter(location=self.pk)
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
|
|
|
def stock_item_count(self, cascade=True):
|
|
|
|
""" Return the number of StockItem objects which live in or under this category
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.get_stock_items(cascade).count()
|
|
|
|
|
|
|
|
def has_items(self, cascade=True):
|
|
|
|
""" Return True if there are StockItems existing in this category.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
cascade: If True, also search an sublocations (default = True)
|
|
|
|
"""
|
|
|
|
return self.stock_item_count(cascade) > 0
|
2019-06-17 14:09:42 +00:00
|
|
|
|
2019-05-13 08:52:54 +00:00
|
|
|
@property
|
|
|
|
def item_count(self):
|
|
|
|
""" Simply returns the number of stock items in this location.
|
|
|
|
Required for tree view serializer.
|
|
|
|
"""
|
2019-06-17 14:09:42 +00:00
|
|
|
return self.stock_item_count()
|
2019-05-13 08:52:54 +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
|
2019-04-12 12:43:22 +00:00
|
|
|
for item in instance.stock_items.all():
|
2018-04-16 13:28:53 +00:00
|
|
|
item.location = instance.parent
|
|
|
|
item.save()
|
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
|
|
|
|
2020-02-17 10:52:31 +00:00
|
|
|
class StockItem(MPTTModel):
|
2018-04-16 10:08:04 +00:00
|
|
|
"""
|
2019-05-10 10:11:52 +00:00
|
|
|
A StockItem object represents a quantity of physical instances of a part.
|
|
|
|
|
|
|
|
Attributes:
|
2020-02-17 10:52:31 +00:00
|
|
|
parent: Link to another StockItem from which this StockItem was created
|
2019-05-10 10:11:52 +00:00
|
|
|
part: Link to the master abstract part that this StockItem is an instance of
|
|
|
|
supplier_part: Link to a specific SupplierPart (optional)
|
|
|
|
location: Where this StockItem is located
|
|
|
|
quantity: Number of stocked units
|
|
|
|
batch: Batch number for this StockItem
|
2019-07-22 05:55:36 +00:00
|
|
|
serial: Unique serial number for this StockItem
|
2019-05-10 10:11:52 +00:00
|
|
|
URL: Optional URL to link to external resource
|
|
|
|
updated: Date that this stock item was last updated (auto)
|
|
|
|
stocktake_date: Date of last stocktake for this item
|
|
|
|
stocktake_user: User that performed the most recent stocktake
|
|
|
|
review_needed: Flag if StockItem needs review
|
|
|
|
delete_on_deplete: If True, StockItem will be deleted when the stock level gets to zero
|
2019-06-04 13:38:52 +00:00
|
|
|
status: Status of this StockItem (ref: InvenTree.status_codes.StockStatus)
|
2019-05-10 10:11:52 +00:00
|
|
|
notes: Extra notes field
|
2019-09-01 13:09:40 +00:00
|
|
|
build: Link to a Build (if this stock item was created from a build)
|
2019-06-15 09:39:57 +00:00
|
|
|
purchase_order: Link to a PurchaseOrder (if this stock item was created from a PurchaseOrder)
|
2019-05-10 10:11:52 +00:00
|
|
|
infinite: If True this StockItem can never be exhausted
|
2018-04-16 10:08:04 +00:00
|
|
|
"""
|
2018-04-15 13:27:56 +00:00
|
|
|
|
2018-05-07 13:40:17 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.pk:
|
|
|
|
add_note = True
|
|
|
|
else:
|
|
|
|
add_note = False
|
|
|
|
|
2019-09-23 21:54:18 +00:00
|
|
|
user = kwargs.pop('user', None)
|
2019-09-23 21:59:59 +00:00
|
|
|
|
|
|
|
add_note = add_note and kwargs.pop('note', True)
|
2019-09-23 21:54:18 +00:00
|
|
|
|
2018-05-07 13:40:17 +00:00
|
|
|
super(StockItem, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
if add_note:
|
|
|
|
# This StockItem is being saved for the first time
|
2019-05-09 12:52:38 +00:00
|
|
|
self.addTransactionNote(
|
2019-04-13 23:23:24 +00:00
|
|
|
'Created stock item',
|
2019-09-23 21:54:18 +00:00
|
|
|
user,
|
2019-04-25 12:10:46 +00:00
|
|
|
notes="Created new stock item for part '{p}'".format(p=str(self.part)),
|
2019-04-13 23:23:24 +00:00
|
|
|
system=True
|
2018-05-07 13:40:17 +00:00
|
|
|
)
|
|
|
|
|
2019-09-15 10:14:27 +00:00
|
|
|
@property
|
|
|
|
def status_label(self):
|
|
|
|
|
|
|
|
return StockStatus.label(self.status)
|
|
|
|
|
2019-07-25 01:05:09 +00:00
|
|
|
@property
|
|
|
|
def serialized(self):
|
|
|
|
""" Return True if this StockItem is serialized """
|
|
|
|
return self.serial is not None and self.quantity == 1
|
|
|
|
|
2019-07-22 05:55:36 +00:00
|
|
|
@classmethod
|
|
|
|
def check_serial_number(cls, part, serial_number):
|
|
|
|
""" Check if a new stock item can be created with the provided part_id
|
|
|
|
|
|
|
|
Args:
|
|
|
|
part: The part to be checked
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not part.trackable:
|
|
|
|
return False
|
|
|
|
|
2019-08-28 11:12:16 +00:00
|
|
|
# Return False if an invalid serial number is supplied
|
|
|
|
try:
|
|
|
|
serial_number = int(serial_number)
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
2019-07-22 05:55:36 +00:00
|
|
|
items = StockItem.objects.filter(serial=serial_number)
|
|
|
|
|
|
|
|
# Is this part a variant? If so, check S/N across all sibling variants
|
|
|
|
if part.variant_of is not None:
|
|
|
|
items = items.filter(part__variant_of=part.variant_of)
|
|
|
|
else:
|
|
|
|
items = items.filter(part=part)
|
|
|
|
|
|
|
|
# An existing serial number exists
|
|
|
|
if items.exists():
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2019-05-25 13:09:04 +00:00
|
|
|
def validate_unique(self, exclude=None):
|
|
|
|
super(StockItem, self).validate_unique(exclude)
|
|
|
|
|
|
|
|
# If the Part object is a variant (of a template part),
|
|
|
|
# ensure that the serial number is unique
|
|
|
|
# across all variants of the same template part
|
|
|
|
|
|
|
|
try:
|
2019-07-22 05:55:36 +00:00
|
|
|
if self.serial is not None:
|
|
|
|
# This is a variant part (check S/N across all sibling variants)
|
|
|
|
if self.part.variant_of is not None:
|
|
|
|
if StockItem.objects.filter(part__variant_of=self.part.variant_of, serial=self.serial).exclude(id=self.id).exists():
|
|
|
|
raise ValidationError({
|
2019-09-08 22:49:27 +00:00
|
|
|
'serial': _('A stock item with this serial number already exists for template part {part}'.format(part=self.part.variant_of))
|
2019-07-22 05:55:36 +00:00
|
|
|
})
|
|
|
|
else:
|
2019-09-08 22:49:27 +00:00
|
|
|
if StockItem.objects.filter(part=self.part, serial=self.serial).exclude(id=self.id).exists():
|
2019-07-22 05:55:36 +00:00
|
|
|
raise ValidationError({
|
2019-09-08 22:49:27 +00:00
|
|
|
'serial': _('A stock item with this serial number already exists')
|
2019-07-22 05:55:36 +00:00
|
|
|
})
|
2019-05-25 13:09:04 +00:00
|
|
|
except Part.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2018-04-27 14:06:39 +00:00
|
|
|
def clean(self):
|
2019-04-28 01:46:53 +00:00
|
|
|
""" Validate the StockItem object (separate to field validation)
|
|
|
|
|
|
|
|
The following validation checks are performed:
|
|
|
|
|
|
|
|
- The 'part' and 'supplier_part.part' fields cannot point to the same Part object
|
|
|
|
- The 'part' does not belong to itself
|
|
|
|
- Quantity must be 1 if the StockItem has a serial number
|
2019-04-28 01:54:40 +00:00
|
|
|
"""
|
2018-04-27 14:06:39 +00:00
|
|
|
|
|
|
|
# The 'supplier_part' field must point to the same part!
|
|
|
|
try:
|
|
|
|
if self.supplier_part is not None:
|
|
|
|
if not self.supplier_part.part == self.part:
|
2018-04-27 15:16:47 +00:00
|
|
|
raise ValidationError({'supplier_part': _("Part type ('{pf}') must be {pe}").format(
|
|
|
|
pf=str(self.supplier_part.part),
|
|
|
|
pe=str(self.part))
|
|
|
|
})
|
|
|
|
|
2018-04-29 07:06:22 +00:00
|
|
|
if self.part is not None:
|
2019-07-25 01:04:45 +00:00
|
|
|
# A part with a serial number MUST have the quantity set to 1
|
|
|
|
if self.serial is not None:
|
|
|
|
if self.quantity > 1:
|
|
|
|
raise ValidationError({
|
|
|
|
'quantity': _('Quantity must be 1 for item with a serial number'),
|
|
|
|
'serial': _('Serial number cannot be set if quantity greater than 1')
|
|
|
|
})
|
2019-07-23 00:31:34 +00:00
|
|
|
|
2019-07-25 01:04:45 +00:00
|
|
|
if self.quantity == 0:
|
2019-08-28 10:01:38 +00:00
|
|
|
self.quantity = 1
|
|
|
|
|
|
|
|
elif self.quantity > 1:
|
2019-07-23 00:31:34 +00:00
|
|
|
raise ValidationError({
|
2019-07-25 01:04:45 +00:00
|
|
|
'quantity': _('Quantity must be 1 for item with a serial number')
|
2019-07-23 00:31:34 +00:00
|
|
|
})
|
2018-04-29 07:06:22 +00:00
|
|
|
|
2019-08-28 10:01:38 +00:00
|
|
|
# Serial numbered items cannot be deleted on depletion
|
|
|
|
self.delete_on_deplete = False
|
2019-07-25 01:04:45 +00:00
|
|
|
|
2019-05-25 12:27:36 +00:00
|
|
|
# A template part cannot be instantiated as a StockItem
|
2019-05-25 13:58:31 +00:00
|
|
|
if self.part.is_template:
|
2019-07-23 00:31:34 +00:00
|
|
|
raise ValidationError({'part': _('Stock item cannot be created for a template Part')})
|
2019-05-25 12:27:36 +00:00
|
|
|
|
2018-04-27 14:06:39 +00:00
|
|
|
except Part.DoesNotExist:
|
|
|
|
# This gets thrown if self.supplier_part is null
|
|
|
|
# TODO - Find a test than can be perfomed...
|
|
|
|
pass
|
|
|
|
|
2018-05-07 08:16:05 +00:00
|
|
|
if self.belongs_to and self.belongs_to.pk == self.pk:
|
|
|
|
raise ValidationError({
|
|
|
|
'belongs_to': _('Item cannot belong to itself')
|
|
|
|
})
|
|
|
|
|
2018-04-15 13:27:56 +00:00
|
|
|
def get_absolute_url(self):
|
2019-04-24 17:13:41 +00:00
|
|
|
return reverse('stock-item-detail', kwargs={'pk': self.id})
|
2018-04-15 13:27:56 +00:00
|
|
|
|
2019-05-28 07:21:29 +00:00
|
|
|
def get_part_name(self):
|
|
|
|
return self.part.full_name
|
|
|
|
|
2018-04-16 13:09:45 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = [
|
|
|
|
('part', 'serial'),
|
|
|
|
]
|
|
|
|
|
2019-05-02 10:40:56 +00:00
|
|
|
def format_barcode(self):
|
|
|
|
""" Return a JSON string for formatting a barcode for this StockItem.
|
|
|
|
Can be used to perform lookup of a stockitem using barcode
|
|
|
|
|
|
|
|
Contains the following data:
|
|
|
|
|
|
|
|
{ type: 'StockItem', stock_id: <pk>, part_id: <part_pk> }
|
|
|
|
|
2019-05-02 10:50:20 +00:00
|
|
|
Voltagile data (e.g. stock quantity) should be looked up using the InvenTree API (as it may change)
|
2019-05-02 10:40:56 +00:00
|
|
|
"""
|
|
|
|
|
2019-05-02 10:50:20 +00:00
|
|
|
return helpers.MakeBarcode(
|
|
|
|
'StockItem',
|
2019-05-02 10:57:53 +00:00
|
|
|
self.id,
|
2019-05-02 10:50:20 +00:00
|
|
|
reverse('api-stock-detail', kwargs={'pk': self.id}),
|
|
|
|
{
|
|
|
|
'part_id': self.part.id,
|
2019-05-12 02:16:04 +00:00
|
|
|
'part_name': self.part.full_name
|
2019-05-02 10:50:20 +00:00
|
|
|
}
|
|
|
|
)
|
2019-04-12 15:12:47 +00:00
|
|
|
|
2020-02-17 10:52:31 +00:00
|
|
|
parent = TreeForeignKey('self',
|
|
|
|
on_delete=models.DO_NOTHING,
|
|
|
|
blank=True, null=True,
|
|
|
|
related_name='children')
|
|
|
|
|
2019-05-25 12:27:36 +00:00
|
|
|
part = models.ForeignKey('part.Part', on_delete=models.CASCADE,
|
2019-11-18 21:46:25 +00:00
|
|
|
related_name='stock_items', help_text=_('Base part'),
|
2019-05-25 14:22:05 +00:00
|
|
|
limit_choices_to={
|
|
|
|
'is_template': False,
|
|
|
|
'active': True,
|
|
|
|
})
|
2018-04-14 05:26:42 +00:00
|
|
|
|
2019-05-18 13:52:22 +00:00
|
|
|
supplier_part = models.ForeignKey('company.SupplierPart', blank=True, null=True, on_delete=models.SET_NULL,
|
2019-11-18 21:46:25 +00:00
|
|
|
help_text=_('Select a matching supplier part for this stock item'))
|
2018-04-14 05:26:42 +00:00
|
|
|
|
2019-09-08 09:19:39 +00:00
|
|
|
location = TreeForeignKey(StockLocation, on_delete=models.DO_NOTHING,
|
|
|
|
related_name='stock_items', blank=True, null=True,
|
2019-11-18 21:46:25 +00:00
|
|
|
help_text=_('Where is this stock item located?'))
|
2018-04-14 05:26:42 +00:00
|
|
|
|
2018-04-16 10:08:04 +00:00
|
|
|
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,
|
2019-11-18 21:46:25 +00:00
|
|
|
help_text=_('Is this item installed in another item?'))
|
2018-04-16 10:08:04 +00:00
|
|
|
|
2018-04-18 23:01:07 +00:00
|
|
|
customer = models.ForeignKey('company.Company', on_delete=models.SET_NULL,
|
2018-04-18 22:31:31 +00:00
|
|
|
related_name='stockitems', blank=True, null=True,
|
2019-11-18 21:46:25 +00:00
|
|
|
help_text=_('Item assigned to customer?'))
|
2018-04-16 10:08:04 +00:00
|
|
|
|
2018-04-16 13:09:45 +00:00
|
|
|
serial = models.PositiveIntegerField(blank=True, null=True,
|
2019-11-18 21:46:25 +00:00
|
|
|
help_text=_('Serial number for this item'))
|
2019-05-10 10:11:52 +00:00
|
|
|
|
2019-09-13 14:08:49 +00:00
|
|
|
URL = InvenTreeURLField(max_length=125, blank=True)
|
2018-04-16 11:07:57 +00:00
|
|
|
|
2019-05-01 14:04:39 +00:00
|
|
|
batch = models.CharField(max_length=100, blank=True, null=True,
|
2019-11-18 21:46:25 +00:00
|
|
|
help_text=_('Batch code for this stock item'))
|
2018-04-16 10:08:04 +00:00
|
|
|
|
2019-11-18 21:49:54 +00:00
|
|
|
quantity = models.DecimalField(max_digits=15, decimal_places=5, validators=[MinValueValidator(0)], default=1)
|
2018-04-14 05:26:42 +00:00
|
|
|
|
2019-05-12 11:20:43 +00:00
|
|
|
updated = models.DateField(auto_now=True, null=True)
|
2017-03-29 11:55:28 +00:00
|
|
|
|
2019-09-01 13:09:40 +00:00
|
|
|
build = models.ForeignKey(
|
|
|
|
'build.Build', on_delete=models.SET_NULL,
|
|
|
|
blank=True, null=True,
|
2019-11-18 21:46:25 +00:00
|
|
|
help_text=_('Build for this stock item'),
|
2019-09-01 13:09:40 +00:00
|
|
|
related_name='build_outputs',
|
|
|
|
)
|
|
|
|
|
2019-06-15 09:39:57 +00:00
|
|
|
purchase_order = models.ForeignKey(
|
2019-06-15 09:42:09 +00:00
|
|
|
'order.PurchaseOrder',
|
2019-06-15 09:39:57 +00:00
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
related_name='stock_items',
|
|
|
|
blank=True, null=True,
|
2019-11-18 21:46:25 +00:00
|
|
|
help_text=_('Purchase order for this stock item')
|
2019-06-15 09:39:57 +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
|
|
|
|
2019-04-17 08:34:21 +00:00
|
|
|
stocktake_user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True,
|
|
|
|
related_name='stocktake_stock')
|
2017-03-29 12:36:06 +00:00
|
|
|
|
|
|
|
review_needed = models.BooleanField(default=False)
|
|
|
|
|
2019-11-18 21:46:25 +00:00
|
|
|
delete_on_deplete = models.BooleanField(default=True, help_text=_('Delete this Stock Item when stock is depleted'))
|
2019-05-09 12:52:38 +00:00
|
|
|
|
2017-03-29 11:55:28 +00:00
|
|
|
status = models.PositiveIntegerField(
|
2019-06-04 13:38:52 +00:00
|
|
|
default=StockStatus.OK,
|
|
|
|
choices=StockStatus.items(),
|
2017-04-16 07:05:02 +00:00
|
|
|
validators=[MinValueValidator(0)])
|
2017-03-29 11:55:28 +00:00
|
|
|
|
2020-02-06 12:22:55 +00:00
|
|
|
notes = MarkdownxField(blank=True, null=True, help_text=_('Stock Item Notes'))
|
2017-04-15 15:43:30 +00:00
|
|
|
|
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-05-06 12:38:39 +00:00
|
|
|
def can_delete(self):
|
2019-07-24 10:24:12 +00:00
|
|
|
""" Can this stock item be deleted? It can NOT be deleted under the following circumstances:
|
|
|
|
|
|
|
|
- Has a serial number and is tracked
|
|
|
|
- Is installed inside another StockItem
|
|
|
|
"""
|
|
|
|
|
2019-07-25 00:36:59 +00:00
|
|
|
if self.part.trackable and self.serial is not None:
|
2019-07-24 10:24:12 +00:00
|
|
|
return False
|
|
|
|
|
2018-05-06 12:38:39 +00:00
|
|
|
return True
|
|
|
|
|
2018-04-29 14:59:36 +00:00
|
|
|
@property
|
|
|
|
def in_stock(self):
|
|
|
|
|
|
|
|
if self.belongs_to or self.customer:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2018-04-16 10:17:58 +00:00
|
|
|
@property
|
|
|
|
def has_tracking_info(self):
|
2018-04-16 13:26:02 +00:00
|
|
|
return self.tracking_info.count() > 0
|
2018-04-16 10:17:58 +00:00
|
|
|
|
2019-07-15 14:10:24 +00:00
|
|
|
def addTransactionNote(self, title, user, notes='', url='', system=True):
|
2019-05-09 12:52:38 +00:00
|
|
|
""" Generation a stock transaction note for this item.
|
|
|
|
|
|
|
|
Brief automated note detailing a movement or quantity change.
|
|
|
|
"""
|
2019-08-28 21:37:44 +00:00
|
|
|
|
2018-04-30 11:36:50 +00:00
|
|
|
track = StockItemTracking.objects.create(
|
|
|
|
item=self,
|
|
|
|
title=title,
|
|
|
|
user=user,
|
2019-04-12 14:14:10 +00:00
|
|
|
quantity=self.quantity,
|
2018-04-30 11:36:50 +00:00
|
|
|
date=datetime.now().date(),
|
|
|
|
notes=notes,
|
2019-07-15 14:10:24 +00:00
|
|
|
URL=url,
|
2018-04-30 11:36:50 +00:00
|
|
|
system=system
|
|
|
|
)
|
|
|
|
|
|
|
|
track.save()
|
|
|
|
|
2019-07-25 01:05:09 +00:00
|
|
|
@transaction.atomic
|
2019-08-28 21:37:44 +00:00
|
|
|
def serializeStock(self, quantity, serials, user, notes='', location=None):
|
2019-07-25 01:05:09 +00:00
|
|
|
""" Split this stock item into unique serial numbers.
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
- Quantity can be less than or equal to the quantity of the stock item
|
|
|
|
- Number of serial numbers must match the quantity
|
|
|
|
- Provided serial numbers must not already be in use
|
|
|
|
|
|
|
|
Args:
|
|
|
|
quantity: Number of items to serialize (integer)
|
|
|
|
serials: List of serial numbers (list<int>)
|
|
|
|
user: User object associated with action
|
2019-08-28 12:47:46 +00:00
|
|
|
notes: Optional notes for tracking
|
2019-08-28 11:12:16 +00:00
|
|
|
location: If specified, serialized items will be placed in the given location
|
2019-07-25 01:05:09 +00:00
|
|
|
"""
|
|
|
|
|
2019-08-28 11:12:16 +00:00
|
|
|
# Cannot serialize stock that is already serialized!
|
|
|
|
if self.serialized:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Quantity must be a valid integer value
|
|
|
|
try:
|
|
|
|
quantity = int(quantity)
|
|
|
|
except ValueError:
|
2019-08-28 12:47:46 +00:00
|
|
|
raise ValidationError({"quantity": _("Quantity must be integer")})
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
if quantity <= 0:
|
2019-08-28 12:47:46 +00:00
|
|
|
raise ValidationError({"quantity": _("Quantity must be greater than zero")})
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
if quantity > self.quantity:
|
2019-08-28 12:47:46 +00:00
|
|
|
raise ValidationError({"quantity": _("Quantity must not exceed available stock quantity ({n})".format(n=self.quantity))})
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
if not type(serials) in [list, tuple]:
|
2019-08-28 12:47:46 +00:00
|
|
|
raise ValidationError({"serial_numbers": _("Serial numbers must be a list of integers")})
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
if any([type(i) is not int for i in serials]):
|
2019-08-28 12:47:46 +00:00
|
|
|
raise ValidationError({"serial_numbers": _("Serial numbers must be a list of integers")})
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
if not quantity == len(serials):
|
2019-08-28 12:47:46 +00:00
|
|
|
raise ValidationError({"quantity": _("Quantity does not match serial numbers")})
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
# Test if each of the serial numbers are valid
|
|
|
|
existing = []
|
|
|
|
|
|
|
|
for serial in serials:
|
|
|
|
if not StockItem.check_serial_number(self.part, serial):
|
|
|
|
existing.append(serial)
|
|
|
|
|
|
|
|
if len(existing) > 0:
|
2019-08-28 12:47:46 +00:00
|
|
|
raise ValidationError({"serial_numbers": _("Serial numbers already exist: ") + str(existing)})
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
# Create a new stock item for each unique serial number
|
|
|
|
for serial in serials:
|
|
|
|
|
|
|
|
# Create a copy of this StockItem
|
|
|
|
new_item = StockItem.objects.get(pk=self.pk)
|
|
|
|
new_item.quantity = 1
|
|
|
|
new_item.serial = serial
|
|
|
|
new_item.pk = None
|
|
|
|
|
|
|
|
if location:
|
|
|
|
new_item.location = location
|
|
|
|
|
2019-09-23 21:59:59 +00:00
|
|
|
# The item already has a transaction history, don't create a new note
|
|
|
|
new_item.save(user=user, note=False)
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
# Copy entire transaction history
|
|
|
|
new_item.copyHistoryFrom(self)
|
|
|
|
|
|
|
|
# Create a new stock tracking item
|
2019-08-28 12:47:46 +00:00
|
|
|
new_item.addTransactionNote(_('Add serial number'), user, notes=notes)
|
2019-08-28 11:12:16 +00:00
|
|
|
|
|
|
|
# Remove the equivalent number of items
|
|
|
|
self.take_stock(quantity, user, notes=_('Serialized {n} items'.format(n=quantity)))
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def copyHistoryFrom(self, other):
|
|
|
|
""" Copy stock history from another part """
|
|
|
|
|
|
|
|
for item in other.tracking_info.all():
|
|
|
|
|
|
|
|
item.item = self
|
|
|
|
item.pk = None
|
|
|
|
item.save()
|
2019-07-25 01:05:09 +00:00
|
|
|
|
2018-05-06 11:39:33 +00:00
|
|
|
@transaction.atomic
|
2019-05-10 14:37:54 +00:00
|
|
|
def splitStock(self, quantity, user):
|
|
|
|
""" Split this stock item into two items, in the same location.
|
|
|
|
Stock tracking notes for this StockItem will be duplicated,
|
|
|
|
and added to the new StockItem.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
quantity: Number of stock items to remove from this entity, and pass to the next
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
The provided quantity will be subtracted from this item and given to the new one.
|
|
|
|
The new item will have a different StockItem ID, while this will remain the same.
|
|
|
|
"""
|
|
|
|
|
2019-07-25 01:05:09 +00:00
|
|
|
# Do not split a serialized part
|
|
|
|
if self.serialized:
|
|
|
|
return
|
|
|
|
|
2019-11-18 23:17:20 +00:00
|
|
|
try:
|
|
|
|
quantity = Decimal(quantity)
|
|
|
|
except (InvalidOperation, ValueError):
|
|
|
|
return
|
|
|
|
|
2019-05-10 14:37:54 +00:00
|
|
|
# Doesn't make sense for a zero quantity
|
|
|
|
if quantity <= 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Also doesn't make sense to split the full amount
|
|
|
|
if quantity >= self.quantity:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Create a new StockItem object, duplicating relevant fields
|
2019-09-01 13:18:08 +00:00
|
|
|
# Nullify the PK so a new record is created
|
|
|
|
new_stock = StockItem.objects.get(pk=self.pk)
|
2019-09-01 13:28:28 +00:00
|
|
|
new_stock.pk = None
|
2020-02-17 10:52:31 +00:00
|
|
|
new_stock.parent = self
|
2019-09-01 13:18:08 +00:00
|
|
|
new_stock.quantity = quantity
|
2019-05-10 14:37:54 +00:00
|
|
|
new_stock.save()
|
|
|
|
|
2019-09-01 13:18:08 +00:00
|
|
|
# Copy the transaction history of this part into the new one
|
2019-08-28 11:12:16 +00:00
|
|
|
new_stock.copyHistoryFrom(self)
|
|
|
|
|
2019-05-10 14:37:54 +00:00
|
|
|
# Add a new tracking item for the new stock item
|
|
|
|
new_stock.addTransactionNote(
|
|
|
|
"Split from existing stock",
|
|
|
|
user,
|
|
|
|
"Split {n} from existing stock item".format(n=quantity))
|
|
|
|
|
|
|
|
# Remove the specified quantity from THIS stock item
|
|
|
|
self.take_stock(quantity, user, 'Split {n} items into new stock item'.format(n=quantity))
|
2018-05-06 11:39:33 +00:00
|
|
|
|
2019-05-10 14:40:37 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def move(self, location, notes, user, **kwargs):
|
|
|
|
""" Move part to a new location.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
location: Destination location (cannot be null)
|
|
|
|
notes: User notes
|
|
|
|
user: Who is performing the move
|
|
|
|
kwargs:
|
|
|
|
quantity: If provided, override the quantity (default = total stock quantity)
|
|
|
|
"""
|
|
|
|
|
2019-11-18 22:18:41 +00:00
|
|
|
try:
|
|
|
|
quantity = Decimal(kwargs.get('quantity', self.quantity))
|
|
|
|
except InvalidOperation:
|
|
|
|
return False
|
2019-05-10 14:40:37 +00:00
|
|
|
|
|
|
|
if quantity <= 0:
|
|
|
|
return False
|
|
|
|
|
2019-04-25 13:01:03 +00:00
|
|
|
if location is None:
|
|
|
|
# TODO - Raise appropriate error (cannot move to blank location)
|
|
|
|
return False
|
2019-07-25 01:04:45 +00:00
|
|
|
elif self.location and (location.pk == self.location.pk) and (quantity == self.quantity):
|
2019-04-25 13:01:03 +00:00
|
|
|
# TODO - Raise appropriate error (cannot move to same location)
|
|
|
|
return False
|
2018-05-06 11:39:33 +00:00
|
|
|
|
2019-05-10 14:40:37 +00:00
|
|
|
# Test for a partial movement
|
|
|
|
if quantity < self.quantity:
|
|
|
|
# We need to split the stock!
|
|
|
|
|
|
|
|
# Leave behind certain quantity
|
|
|
|
self.splitStock(self.quantity - quantity, user)
|
|
|
|
|
2019-04-25 13:01:03 +00:00
|
|
|
msg = "Moved to {loc}".format(loc=str(location))
|
|
|
|
|
|
|
|
if self.location:
|
|
|
|
msg += " (from {loc})".format(loc=str(self.location))
|
2018-05-06 11:39:33 +00:00
|
|
|
|
|
|
|
self.location = location
|
|
|
|
|
2019-05-09 12:52:38 +00:00
|
|
|
self.addTransactionNote(msg,
|
2019-05-09 13:06:19 +00:00
|
|
|
user,
|
|
|
|
notes=notes,
|
|
|
|
system=True)
|
2018-05-06 11:39:33 +00:00
|
|
|
|
2019-05-09 13:01:32 +00:00
|
|
|
self.save()
|
2018-05-06 11:39:33 +00:00
|
|
|
|
2019-04-12 14:08:13 +00:00
|
|
|
return True
|
|
|
|
|
2019-05-09 13:01:32 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def updateQuantity(self, quantity):
|
2019-05-09 13:06:19 +00:00
|
|
|
""" Update stock quantity for this item.
|
|
|
|
|
2019-05-09 13:01:32 +00:00
|
|
|
If the quantity has reached zero, this StockItem will be deleted.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
- True if the quantity was saved
|
|
|
|
- False if the StockItem was deleted
|
|
|
|
"""
|
|
|
|
|
2019-07-25 01:05:09 +00:00
|
|
|
# Do not adjust quantity of a serialized part
|
|
|
|
if self.serialized:
|
|
|
|
return
|
|
|
|
|
2019-11-18 23:17:20 +00:00
|
|
|
try:
|
|
|
|
self.quantity = Decimal(quantity)
|
|
|
|
except (InvalidOperation, ValueError):
|
|
|
|
return
|
|
|
|
|
2019-05-09 13:01:32 +00:00
|
|
|
if quantity < 0:
|
|
|
|
quantity = 0
|
|
|
|
|
2019-11-18 23:19:52 +00:00
|
|
|
self.quantity = quantity
|
|
|
|
|
2019-11-18 23:17:20 +00:00
|
|
|
if quantity == 0 and self.delete_on_deplete and self.can_delete():
|
|
|
|
|
|
|
|
# TODO - Do not actually "delete" stock at this point - instead give it a "DELETED" flag
|
2019-05-09 13:01:32 +00:00
|
|
|
self.delete()
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
self.save()
|
|
|
|
return True
|
|
|
|
|
2017-04-21 13:47:04 +00:00
|
|
|
@transaction.atomic
|
2018-05-08 12:06:28 +00:00
|
|
|
def stocktake(self, count, user, notes=''):
|
2017-04-20 12:08:27 +00:00
|
|
|
""" Perform item stocktake.
|
|
|
|
When the quantity of an item is counted,
|
|
|
|
record the date of stocktake
|
|
|
|
"""
|
|
|
|
|
2019-11-18 22:18:41 +00:00
|
|
|
try:
|
|
|
|
count = Decimal(count)
|
|
|
|
except InvalidOperation:
|
|
|
|
return False
|
2017-04-20 12:08:27 +00:00
|
|
|
|
|
|
|
if count < 0 or self.infinite:
|
2019-04-25 07:30:44 +00:00
|
|
|
return False
|
2017-04-20 12:08:27 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-05-09 13:01:32 +00:00
|
|
|
if self.updateQuantity(count):
|
|
|
|
|
|
|
|
self.addTransactionNote('Stocktake - counted {n} items'.format(n=count),
|
|
|
|
user,
|
|
|
|
notes=notes,
|
|
|
|
system=True)
|
2018-04-30 11:36:50 +00:00
|
|
|
|
2019-04-25 07:30:44 +00:00
|
|
|
return True
|
|
|
|
|
2017-04-21 13:47:04 +00:00
|
|
|
@transaction.atomic
|
2018-05-08 12:06:28 +00:00
|
|
|
def add_stock(self, quantity, user, notes=''):
|
2017-04-20 12:08:27 +00:00
|
|
|
""" Add items to stock
|
|
|
|
This function can be called by initiating a ProjectRun,
|
|
|
|
or by manually adding the items to the stock location
|
|
|
|
"""
|
|
|
|
|
2019-07-25 01:05:09 +00:00
|
|
|
# Cannot add items to a serialized part
|
|
|
|
if self.serialized:
|
|
|
|
return False
|
|
|
|
|
2019-11-18 22:18:41 +00:00
|
|
|
try:
|
|
|
|
quantity = Decimal(quantity)
|
|
|
|
except InvalidOperation:
|
|
|
|
return False
|
2017-04-20 12:08:27 +00:00
|
|
|
|
2018-05-08 12:06:28 +00:00
|
|
|
# Ignore amounts that do not make sense
|
|
|
|
if quantity <= 0 or self.infinite:
|
2019-04-25 07:30:44 +00:00
|
|
|
return False
|
2017-04-20 12:08:27 +00:00
|
|
|
|
2019-05-09 13:01:32 +00:00
|
|
|
if self.updateQuantity(self.quantity + quantity):
|
|
|
|
|
|
|
|
self.addTransactionNote('Added {n} items to stock'.format(n=quantity),
|
|
|
|
user,
|
|
|
|
notes=notes,
|
|
|
|
system=True)
|
2018-05-08 12:06:28 +00:00
|
|
|
|
2019-04-25 07:30:44 +00:00
|
|
|
return True
|
|
|
|
|
2017-04-21 13:47:04 +00:00
|
|
|
@transaction.atomic
|
2018-05-08 12:06:28 +00:00
|
|
|
def take_stock(self, quantity, user, notes=''):
|
|
|
|
""" Remove items from stock
|
|
|
|
"""
|
|
|
|
|
2019-07-25 01:05:09 +00:00
|
|
|
# Cannot remove items from a serialized part
|
|
|
|
if self.serialized:
|
|
|
|
return False
|
|
|
|
|
2019-11-18 22:18:41 +00:00
|
|
|
try:
|
|
|
|
quantity = Decimal(quantity)
|
|
|
|
except InvalidOperation:
|
|
|
|
return False
|
2018-05-08 12:06:28 +00:00
|
|
|
|
|
|
|
if quantity <= 0 or self.infinite:
|
2019-04-25 07:30:44 +00:00
|
|
|
return False
|
2018-05-08 12:06:28 +00:00
|
|
|
|
2019-05-09 13:01:32 +00:00
|
|
|
if self.updateQuantity(self.quantity - quantity):
|
2018-05-08 12:06:28 +00:00
|
|
|
|
2019-05-09 13:01:32 +00:00
|
|
|
self.addTransactionNote('Removed {n} items from stock'.format(n=quantity),
|
|
|
|
user,
|
|
|
|
notes=notes,
|
|
|
|
system=True)
|
2017-04-20 12:20:41 +00:00
|
|
|
|
2019-04-25 07:30:44 +00:00
|
|
|
return True
|
|
|
|
|
2017-03-27 10:04:15 +00:00
|
|
|
def __str__(self):
|
2019-07-23 01:55:51 +00:00
|
|
|
if self.part.trackable and self.serial:
|
|
|
|
s = '{part} #{sn}'.format(
|
|
|
|
part=self.part.full_name,
|
|
|
|
sn=self.serial)
|
|
|
|
else:
|
|
|
|
s = '{n} x {part}'.format(
|
2019-11-18 21:59:56 +00:00
|
|
|
n=helpers.decimal2string(self.quantity),
|
2019-07-23 01:55:51 +00:00
|
|
|
part=self.part.full_name)
|
2018-04-26 23:33:05 +00:00
|
|
|
|
|
|
|
if self.location:
|
|
|
|
s += ' @ {loc}'.format(loc=self.location.name)
|
|
|
|
|
|
|
|
return s
|
2018-04-16 10:08:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StockItemTracking(models.Model):
|
2019-05-10 10:11:52 +00:00
|
|
|
""" Stock tracking entry - breacrumb for keeping track of automated stock transactions
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
item: Link to StockItem
|
|
|
|
date: Date that this tracking info was created
|
|
|
|
title: Title of this tracking info (generated by system)
|
|
|
|
notes: Associated notes (input by user)
|
2019-07-15 12:33:00 +00:00
|
|
|
URL: Optional URL to external page
|
2019-05-10 10:11:52 +00:00
|
|
|
user: The user associated with this tracking info
|
|
|
|
quantity: The StockItem quantity at this point in time
|
2018-04-16 10:08:04 +00:00
|
|
|
"""
|
|
|
|
|
2018-05-08 12:30:32 +00:00
|
|
|
def get_absolute_url(self):
|
2019-04-25 12:11:10 +00:00
|
|
|
return '/stock/track/{pk}'.format(pk=self.id)
|
2019-04-25 13:25:52 +00:00
|
|
|
# return reverse('stock-tracking-detail', kwargs={'pk': self.id})
|
2018-05-08 12:30:32 +00:00
|
|
|
|
2018-04-16 10:08:04 +00:00
|
|
|
item = models.ForeignKey(StockItem, on_delete=models.CASCADE,
|
|
|
|
related_name='tracking_info')
|
|
|
|
|
2018-05-10 11:13:36 +00:00
|
|
|
date = models.DateTimeField(auto_now_add=True, editable=False)
|
2018-04-16 10:08:04 +00:00
|
|
|
|
2019-11-18 21:46:25 +00:00
|
|
|
title = models.CharField(blank=False, max_length=250, help_text=_('Tracking entry title'))
|
2018-04-16 10:08:04 +00:00
|
|
|
|
2019-11-18 21:46:25 +00:00
|
|
|
notes = models.CharField(blank=True, max_length=512, help_text=_('Entry notes'))
|
2018-04-16 10:08:04 +00:00
|
|
|
|
2019-11-18 21:46:25 +00:00
|
|
|
URL = InvenTreeURLField(blank=True, help_text=_('Link to external page for further information'))
|
2019-07-15 12:33:00 +00:00
|
|
|
|
2018-04-16 10:08:04 +00:00
|
|
|
user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
|
|
|
|
|
2018-04-17 22:44:08 +00:00
|
|
|
system = models.BooleanField(default=False)
|
|
|
|
|
2019-11-18 23:17:20 +00:00
|
|
|
quantity = models.DecimalField(max_digits=15, decimal_places=5, validators=[MinValueValidator(0)], default=1)
|
2019-04-12 14:14:10 +00:00
|
|
|
|
2018-04-16 10:08:04 +00:00
|
|
|
# TODO
|
|
|
|
# image = models.ImageField(upload_to=func, max_length=255, null=True, blank=True)
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
# file = models.FileField()
|