2017-03-25 12:07:43 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-03-29 12:19:53 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2017-03-25 12:07:43 +00:00
|
|
|
from django.db import models
|
2017-03-28 10:24:00 +00:00
|
|
|
from django.db.models import Sum
|
2017-04-16 07:05:02 +00:00
|
|
|
from django.core.validators import MinValueValidator
|
2017-03-25 12:07:43 +00:00
|
|
|
|
2017-03-27 10:03:46 +00:00
|
|
|
from InvenTree.models import InvenTreeTree
|
2017-03-26 22:05:54 +00:00
|
|
|
|
2018-04-14 04:11:46 +00:00
|
|
|
#from django.db.models.signals.pre_delete
|
|
|
|
#from django.dispatch import receiver
|
2017-03-28 12:17:56 +00:00
|
|
|
|
2017-03-28 12:31:41 +00:00
|
|
|
class PartCategory(InvenTreeTree):
|
2017-03-27 11:55:21 +00:00
|
|
|
""" PartCategory provides hierarchical organization of Part objects.
|
|
|
|
"""
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-27 10:03:46 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "Part Category"
|
|
|
|
verbose_name_plural = "Part Categories"
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-04-10 23:41:03 +00:00
|
|
|
@property
|
|
|
|
def parts(self):
|
2017-04-11 13:07:02 +00:00
|
|
|
return self.part_set.all()
|
2017-04-10 23:41:03 +00:00
|
|
|
|
2018-04-14 04:11:46 +00:00
|
|
|
"""
|
|
|
|
@receiver(pre_delete, sender=PartCategory)
|
|
|
|
def reset_tag(sender, **kwargs):
|
|
|
|
cat = kwargs['instance']
|
|
|
|
|
|
|
|
for book in books.filter(tag=tag):
|
|
|
|
book.tag = book.author.tags.first()
|
|
|
|
book.save()
|
|
|
|
"""
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-25 12:19:49 +00:00
|
|
|
class Part(models.Model):
|
2018-04-12 06:27:26 +00:00
|
|
|
""" Represents an abstract part
|
|
|
|
Parts can be "stocked" in multiple warehouses,
|
|
|
|
and can be combined to form other parts
|
|
|
|
"""
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-04-01 02:31:48 +00:00
|
|
|
# Short name of the part
|
2017-03-25 12:19:49 +00:00
|
|
|
name = models.CharField(max_length=100)
|
2017-04-01 02:31:48 +00:00
|
|
|
|
|
|
|
# Longer description of the part (optional)
|
2017-03-25 12:19:49 +00:00
|
|
|
description = models.CharField(max_length=250, blank=True)
|
2017-04-01 02:31:48 +00:00
|
|
|
|
|
|
|
# Internal Part Number (optional)
|
2017-03-25 12:19:49 +00:00
|
|
|
IPN = models.CharField(max_length=100, blank=True)
|
2017-04-01 02:31:48 +00:00
|
|
|
|
2018-04-14 06:32:41 +00:00
|
|
|
# Provide a URL for an external link
|
|
|
|
URL = models.URLField(blank=True)
|
|
|
|
|
2017-04-01 02:31:48 +00:00
|
|
|
# Part category - all parts must be assigned to a category
|
2018-04-14 04:11:46 +00:00
|
|
|
category = models.ForeignKey(PartCategory, related_name='parts',
|
|
|
|
null=True, blank=True,
|
|
|
|
on_delete=models.SET_NULL)
|
2017-04-01 02:31:48 +00:00
|
|
|
|
|
|
|
# Minimum "allowed" stock level
|
2017-04-16 07:05:02 +00:00
|
|
|
minimum_stock = models.PositiveIntegerField(default=0, validators=[MinValueValidator(0)])
|
2017-04-01 02:31:48 +00:00
|
|
|
|
|
|
|
# Units of quantity for this part. Default is "pcs"
|
2017-03-28 06:49:01 +00:00
|
|
|
units = models.CharField(max_length=20, default="pcs", blank=True)
|
2017-04-01 02:31:48 +00:00
|
|
|
|
|
|
|
# Is this part "trackable"?
|
|
|
|
# Trackable parts can have unique instances which are assigned serial numbers
|
|
|
|
# and can have their movements tracked
|
2017-03-28 07:17:32 +00:00
|
|
|
trackable = models.BooleanField(default=False)
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-25 12:19:49 +00:00
|
|
|
def __str__(self):
|
|
|
|
if self.IPN:
|
|
|
|
return "{name} ({ipn})".format(
|
2017-03-28 12:31:41 +00:00
|
|
|
ipn=self.IPN,
|
|
|
|
name=self.name)
|
2017-03-25 12:07:43 +00:00
|
|
|
else:
|
2017-03-25 23:06:00 +00:00
|
|
|
return self.name
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-27 10:03:46 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "Part"
|
|
|
|
verbose_name_plural = "Parts"
|
2018-04-12 09:14:07 +00:00
|
|
|
unique_together = (("name", "category"),)
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-28 10:24:00 +00:00
|
|
|
@property
|
2017-03-28 11:27:46 +00:00
|
|
|
def stock(self):
|
2017-03-28 10:24:00 +00:00
|
|
|
""" Return the total stock quantity for this part.
|
|
|
|
Part may be stored in multiple locations
|
|
|
|
"""
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-28 21:37:41 +00:00
|
|
|
stocks = self.locations.all()
|
2017-03-28 10:24:00 +00:00
|
|
|
if len(stocks) == 0:
|
|
|
|
return 0
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-28 10:24:00 +00:00
|
|
|
result = stocks.aggregate(total=Sum('quantity'))
|
|
|
|
return result['total']
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2018-04-13 12:36:59 +00:00
|
|
|
@property
|
|
|
|
def bomItemCount(self):
|
|
|
|
return self.bom_items.all().count()
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def usedInCount(self):
|
|
|
|
return self.used_in.all().count()
|
|
|
|
|
|
|
|
"""
|
2017-03-28 10:24:00 +00:00
|
|
|
@property
|
|
|
|
def projects(self):
|
2018-04-13 12:36:59 +00:00
|
|
|
" Return a list of unique projects that this part is associated with.
|
2017-04-01 02:31:48 +00:00
|
|
|
A part may be used in zero or more projects.
|
2018-04-13 12:36:59 +00:00
|
|
|
"
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-28 10:24:00 +00:00
|
|
|
project_ids = set()
|
|
|
|
project_parts = self.projectpart_set.all()
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-28 10:24:00 +00:00
|
|
|
projects = []
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-28 10:24:00 +00:00
|
|
|
for pp in project_parts:
|
|
|
|
if pp.project.id not in project_ids:
|
|
|
|
project_ids.add(pp.project.id)
|
|
|
|
projects.append(pp.project)
|
2017-03-29 12:19:53 +00:00
|
|
|
|
2017-03-28 10:24:00 +00:00
|
|
|
return projects
|
2018-04-13 12:36:59 +00:00
|
|
|
"""
|
2017-03-29 04:12:14 +00:00
|
|
|
|
2017-03-29 04:45:50 +00:00
|
|
|
|
2018-04-14 04:19:03 +00:00
|
|
|
class BomItem(models.Model):
|
|
|
|
""" A BomItem links a part to its component items.
|
|
|
|
A part can have a BOM (bill of materials) which defines
|
|
|
|
which parts are required (and in what quatity) to make it
|
|
|
|
"""
|
|
|
|
|
|
|
|
# A link to the parent part
|
|
|
|
# Each part will get a reverse lookup field 'bom_items'
|
|
|
|
part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='bom_items')
|
|
|
|
|
|
|
|
# A link to the child item (sub-part)
|
|
|
|
# Each part will get a reverse lookup field 'used_in'
|
|
|
|
sub_part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='used_in')
|
|
|
|
|
|
|
|
# Quantity required
|
|
|
|
quantity = models.PositiveIntegerField(default=1, validators=[MinValueValidator(0)])
|
|
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = "BOM Item"
|
|
|
|
|
|
|
|
# Prevent duplication of parent/child rows
|
|
|
|
unique_together = ('part', 'sub_part')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{par} -> {child} ({n})".format(
|
|
|
|
par=self.part.name,
|
|
|
|
child=self.sub_part.name,
|
|
|
|
n=self.quantity)
|