mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Replace other choice fields with commonized status code
This commit is contained in:
parent
8d70d2f28a
commit
f731c45ce8
@ -7,6 +7,11 @@ class StatusCode:
|
||||
def items(cls):
|
||||
return cls.options.items()
|
||||
|
||||
@classmethod
|
||||
def label(cls, value):
|
||||
""" Return the status code label associated with the provided value """
|
||||
return cls.options.get(value, '')
|
||||
|
||||
|
||||
class OrderStatus(StatusCode):
|
||||
|
||||
@ -26,3 +31,48 @@ class OrderStatus(StatusCode):
|
||||
LOST: _("Lost"),
|
||||
RETURNED: _("Returned"),
|
||||
}
|
||||
|
||||
|
||||
class StockStatus(StatusCode):
|
||||
|
||||
OK = 10 # Item is OK
|
||||
ATTENTION = 50 # Item requires attention
|
||||
DAMAGED = 55 # Item is damaged
|
||||
DESTROYED = 60 # Item is destroyed
|
||||
LOST = 70 # Item has been lost
|
||||
|
||||
options = {
|
||||
OK: _("OK"),
|
||||
ATTENTION: _("Attention needed"),
|
||||
DAMAGED: _("Damaged"),
|
||||
DESTROYED: _("Destroyed"),
|
||||
LOST: _("Lost"),
|
||||
}
|
||||
|
||||
# The following codes correspond to parts that are 'available'
|
||||
AVAILABLE_CODES = [
|
||||
OK,
|
||||
ATTENTION,
|
||||
DAMAGED
|
||||
]
|
||||
|
||||
|
||||
class BuildStatus(StatusCode):
|
||||
|
||||
# Build status codes
|
||||
PENDING = 10 # Build is pending / active
|
||||
ALLOCATED = 20 # Parts have been removed from stock
|
||||
CANCELLED = 30 # Build was cancelled
|
||||
COMPLETE = 40 # Build is complete
|
||||
|
||||
options = {
|
||||
PENDING: _("Pending"),
|
||||
ALLOCATED: _("Allocated"),
|
||||
CANCELLED: _("Cancelled"),
|
||||
COMPLETE: _("Complete"),
|
||||
}
|
||||
|
||||
ACTIVE_CODES = [
|
||||
PENDING,
|
||||
ALLOCATED
|
||||
]
|
||||
|
@ -16,6 +16,8 @@ from django.db import models, transaction
|
||||
from django.db.models import Sum
|
||||
from django.core.validators import MinValueValidator
|
||||
|
||||
from InvenTree.status_codes import BuildStatus
|
||||
|
||||
from stock.models import StockItem
|
||||
from part.models import Part, BomItem
|
||||
|
||||
@ -68,22 +70,10 @@ class Build(models.Model):
|
||||
validators=[MinValueValidator(1)],
|
||||
help_text='Number of parts to build'
|
||||
)
|
||||
|
||||
# Build status codes
|
||||
PENDING = 10 # Build is pending / active
|
||||
ALLOCATED = 20 # Parts have been removed from stock
|
||||
CANCELLED = 30 # Build was cancelled
|
||||
COMPLETE = 40 # Build is complete
|
||||
|
||||
#: Build status codes
|
||||
BUILD_STATUS_CODES = {PENDING: _("Pending"),
|
||||
ALLOCATED: _("Allocated"),
|
||||
CANCELLED: _("Cancelled"),
|
||||
COMPLETE: _("Complete"),
|
||||
}
|
||||
|
||||
status = models.PositiveIntegerField(default=PENDING,
|
||||
choices=BUILD_STATUS_CODES.items(),
|
||||
status = models.PositiveIntegerField(default=BuildStatus.PENDING,
|
||||
choices=BuildStatus.items(),
|
||||
validators=[MinValueValidator(0)],
|
||||
help_text='Build status')
|
||||
|
||||
@ -325,14 +315,12 @@ class Build(models.Model):
|
||||
- HOLDING
|
||||
"""
|
||||
|
||||
return self.status in [
|
||||
self.PENDING,
|
||||
]
|
||||
return self.status in BuildStatus.ACTIVE_CODES
|
||||
|
||||
@property
|
||||
def is_complete(self):
|
||||
""" Returns True if the build status is COMPLETE """
|
||||
return self.status == self.COMPLETE
|
||||
return self.status == self.BuildStatus.COMPLETE
|
||||
|
||||
|
||||
class BuildItem(models.Model):
|
||||
|
@ -33,6 +33,8 @@ from InvenTree import helpers
|
||||
from InvenTree import validators
|
||||
from InvenTree.models import InvenTreeTree
|
||||
|
||||
from InvenTree.status_codes import BuildStatus, StockStatus
|
||||
|
||||
from company.models import SupplierPart
|
||||
|
||||
|
||||
@ -454,14 +456,14 @@ class Part(models.Model):
|
||||
Builds marked as 'complete' or 'cancelled' are ignored
|
||||
"""
|
||||
|
||||
return [b for b in self.builds.all() if b.is_active]
|
||||
return self.builds.filter(status__in=BuildStatus.ACTIVE_CODES)
|
||||
|
||||
@property
|
||||
def inactive_builds(self):
|
||||
""" Return a list of inactive builds
|
||||
"""
|
||||
|
||||
return [b for b in self.builds.all() if not b.is_active]
|
||||
return self.builds.exclude(status__in=BuildStatus.ACTIVE_CODES)
|
||||
|
||||
@property
|
||||
def quantity_being_built(self):
|
||||
@ -531,7 +533,7 @@ class Part(models.Model):
|
||||
if self.is_template:
|
||||
total = sum([variant.total_stock for variant in self.variants.all()])
|
||||
else:
|
||||
total = self.stock_entries.aggregate(total=Sum('quantity'))['total']
|
||||
total = self.stock_entries.filter(status__in=StockStatus.AVAILABLE_CODES).aggregate(total=Sum('quantity'))['total']
|
||||
|
||||
if total:
|
||||
return total
|
||||
|
@ -20,6 +20,7 @@ from .serializers import StockTrackingSerializer
|
||||
|
||||
from InvenTree.views import TreeSerializer
|
||||
from InvenTree.helpers import str2bool
|
||||
from InvenTree.status_codes import StockStatus
|
||||
|
||||
import os
|
||||
|
||||
@ -311,7 +312,7 @@ class StockList(generics.ListCreateAPIView):
|
||||
else:
|
||||
item['location__path'] = None
|
||||
|
||||
item['status_text'] = StockItem.ITEM_STATUS_CODES[item['status']]
|
||||
item['status_text'] = StockStatus.label(item['status'])
|
||||
|
||||
return Response(data)
|
||||
|
||||
|
@ -19,6 +19,7 @@ from django.dispatch import receiver
|
||||
from datetime import datetime
|
||||
from InvenTree import helpers
|
||||
|
||||
from InvenTree.status_codes import StockStatus
|
||||
from InvenTree.models import InvenTreeTree
|
||||
|
||||
from part.models import Part
|
||||
@ -93,7 +94,7 @@ class StockItem(models.Model):
|
||||
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
|
||||
status: Status of this StockItem (ref: ITEM_STATUS_CODES)
|
||||
status: Status of this StockItem (ref: InvenTree.status_codes.StockStatus)
|
||||
notes: Extra notes field
|
||||
infinite: If True this StockItem can never be exhausted
|
||||
"""
|
||||
@ -256,23 +257,9 @@ class StockItem(models.Model):
|
||||
|
||||
delete_on_deplete = models.BooleanField(default=True, help_text='Delete this Stock Item when stock is depleted')
|
||||
|
||||
ITEM_OK = 10
|
||||
ITEM_ATTENTION = 50
|
||||
ITEM_DAMAGED = 55
|
||||
ITEM_DESTROYED = 60
|
||||
ITEM_LOST = 70
|
||||
|
||||
ITEM_STATUS_CODES = {
|
||||
ITEM_OK: _("OK"),
|
||||
ITEM_ATTENTION: _("Attention needed"),
|
||||
ITEM_DAMAGED: _("Damaged"),
|
||||
ITEM_DESTROYED: _("Destroyed"),
|
||||
ITEM_LOST: _("Lost")
|
||||
}
|
||||
|
||||
status = models.PositiveIntegerField(
|
||||
default=ITEM_OK,
|
||||
choices=ITEM_STATUS_CODES.items(),
|
||||
default=StockStatus.OK,
|
||||
choices=StockStatus.items(),
|
||||
validators=[MinValueValidator(0)])
|
||||
|
||||
notes = models.CharField(max_length=250, blank=True, help_text='Stock Item Notes')
|
||||
|
Loading…
Reference in New Issue
Block a user