2019-06-10 12:14:23 +00:00
|
|
|
"""
|
|
|
|
Order model definitions
|
|
|
|
"""
|
|
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-06-04 14:21:19 +00:00
|
|
|
from django.db import models
|
2019-06-04 12:19:04 +00:00
|
|
|
from django.core.validators import MinValueValidator
|
2019-06-13 11:17:06 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2019-06-04 12:19:04 +00:00
|
|
|
from django.contrib.auth.models import User
|
2019-06-10 12:56:34 +00:00
|
|
|
from django.urls import reverse
|
2019-06-04 12:19:04 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2019-06-10 12:14:23 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2019-06-05 10:59:30 +00:00
|
|
|
from company.models import Company, SupplierPart
|
2019-06-04 12:19:04 +00:00
|
|
|
|
2019-06-04 13:09:51 +00:00
|
|
|
from InvenTree.status_codes import OrderStatus
|
|
|
|
|
2019-06-04 12:19:04 +00:00
|
|
|
|
|
|
|
class Order(models.Model):
|
|
|
|
""" Abstract model for an order.
|
|
|
|
|
|
|
|
Instances of this class:
|
|
|
|
|
|
|
|
- PuchaseOrder
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
reference: Unique order number / reference / code
|
|
|
|
description: Long form description (required)
|
|
|
|
notes: Extra note field (optional)
|
|
|
|
creation_date: Automatic date of order creation
|
|
|
|
created_by: User who created this order (automatically captured)
|
|
|
|
issue_date: Date the order was issued
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-06-04 13:09:51 +00:00
|
|
|
ORDER_PREFIX = ""
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
el = []
|
|
|
|
|
|
|
|
if self.ORDER_PREFIX:
|
|
|
|
el.append(self.ORDER_PREFIX)
|
|
|
|
|
|
|
|
el.append(self.reference)
|
|
|
|
|
|
|
|
return " ".join(el)
|
2019-06-04 12:19:04 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
reference = models.CharField(unique=True, max_length=64, blank=False, help_text=_('Order reference'))
|
|
|
|
|
2019-06-06 11:39:04 +00:00
|
|
|
description = models.CharField(max_length=250, help_text=_('Order description'))
|
2019-06-04 12:26:19 +00:00
|
|
|
|
|
|
|
URL = models.URLField(blank=True, help_text=_('Link to external page'))
|
2019-06-04 12:19:04 +00:00
|
|
|
|
|
|
|
creation_date = models.DateField(auto_now=True, editable=False)
|
|
|
|
|
2019-06-04 13:09:51 +00:00
|
|
|
status = models.PositiveIntegerField(default=OrderStatus.PENDING, choices=OrderStatus.items(),
|
|
|
|
help_text='Order status')
|
|
|
|
|
2019-06-04 12:19:04 +00:00
|
|
|
created_by = models.ForeignKey(User,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
blank=True, null=True,
|
|
|
|
related_name='+'
|
|
|
|
)
|
|
|
|
|
|
|
|
issue_date = models.DateField(blank=True, null=True)
|
|
|
|
|
|
|
|
notes = models.TextField(blank=True, help_text=_('Order notes'))
|
|
|
|
|
2019-06-10 12:14:23 +00:00
|
|
|
def place_order(self):
|
|
|
|
""" Marks the order as PLACED. Order must be currently PENDING. """
|
|
|
|
|
|
|
|
if self.status == OrderStatus.PENDING:
|
|
|
|
self.status = OrderStatus.PLACED
|
|
|
|
self.issue_date = datetime.now().date()
|
|
|
|
self.save()
|
|
|
|
|
2019-06-04 12:19:04 +00:00
|
|
|
|
|
|
|
class PurchaseOrder(Order):
|
|
|
|
""" A PurchaseOrder represents goods shipped inwards from an external supplier.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
supplier: Reference to the company supplying the goods in the order
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-06-04 13:09:51 +00:00
|
|
|
ORDER_PREFIX = "PO"
|
|
|
|
|
2019-06-04 14:21:19 +00:00
|
|
|
supplier = models.ForeignKey(
|
|
|
|
Company, on_delete=models.CASCADE,
|
|
|
|
limit_choices_to={
|
|
|
|
'is_supplier': True,
|
|
|
|
},
|
|
|
|
related_name='purchase_orders',
|
|
|
|
help_text=_('Company')
|
|
|
|
)
|
|
|
|
|
2019-06-10 12:56:34 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('purchase-order-detail', kwargs={'pk': self.id})
|
|
|
|
|
2019-06-13 11:17:06 +00:00
|
|
|
def add_line_item(self, supplier_part, quantity, group=True, reference=''):
|
|
|
|
""" Add a new line item to this purchase order.
|
|
|
|
This function will check that:
|
|
|
|
|
|
|
|
* The supplier part matches the supplier specified for this purchase order
|
|
|
|
* The quantity is greater than zero
|
|
|
|
|
|
|
|
Args:
|
|
|
|
supplier_part - The supplier_part to add
|
|
|
|
quantity - The number of items to add
|
|
|
|
group - If True, this new quantity will be added to an existing line item for the same supplier_part (if it exists)
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
quantity = int(quantity)
|
|
|
|
if quantity <= 0:
|
|
|
|
raise ValidationError({
|
|
|
|
'quantity': _("Quantity must be greater than zero")})
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError({'quantity': _("Invalid quantity provided")})
|
|
|
|
|
|
|
|
if not supplier_part.supplier == self.supplier:
|
|
|
|
raise ValidationError({'supplier': _("Part supplier must match PO supplier")})
|
|
|
|
|
|
|
|
if group:
|
|
|
|
# Check if there is already a matching line item
|
|
|
|
matches = PurchaseOrderLineItem.objects.filter(part=supplier_part)
|
|
|
|
|
|
|
|
if matches.count() > 0:
|
|
|
|
line = matches.first()
|
|
|
|
|
|
|
|
line.quantity += quantity
|
|
|
|
line.save()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
line = PurchaseOrderLineItem(
|
|
|
|
order=self,
|
|
|
|
part=supplier_part,
|
|
|
|
quantity=quantity,
|
|
|
|
reference=reference)
|
|
|
|
|
|
|
|
line.save()
|
|
|
|
|
2019-06-04 12:19:04 +00:00
|
|
|
|
|
|
|
class OrderLineItem(models.Model):
|
2019-06-04 14:21:19 +00:00
|
|
|
""" Abstract model for an order line item
|
2019-06-04 12:19:04 +00:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
quantity: Number of items
|
|
|
|
note: Annotation for the item
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
quantity = models.PositiveIntegerField(validators=[MinValueValidator(0)], default=1, help_text=_('Item quantity'))
|
|
|
|
|
|
|
|
reference = models.CharField(max_length=100, blank=True, help_text=_('Line item reference'))
|
|
|
|
|
|
|
|
|
|
|
|
class PurchaseOrderLineItem(OrderLineItem):
|
2019-06-04 14:21:19 +00:00
|
|
|
""" Model for a purchase order line item.
|
2019-06-04 12:19:04 +00:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
order: Reference to a PurchaseOrder object
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-06-05 10:59:30 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = (
|
|
|
|
('order', 'part')
|
|
|
|
)
|
|
|
|
|
|
|
|
order = models.ForeignKey(
|
|
|
|
PurchaseOrder, on_delete=models.CASCADE,
|
|
|
|
related_name='lines',
|
|
|
|
help_text=_('Purchase Order')
|
|
|
|
)
|
2019-06-04 12:19:04 +00:00
|
|
|
|
2019-06-06 11:56:20 +00:00
|
|
|
# TODO - Function callback for when the SupplierPart is deleted?
|
2019-06-04 12:19:04 +00:00
|
|
|
|
2019-06-05 10:59:30 +00:00
|
|
|
part = models.ForeignKey(
|
|
|
|
SupplierPart, on_delete=models.SET_NULL,
|
|
|
|
blank=True, null=True,
|
2019-06-05 11:47:22 +00:00
|
|
|
related_name='purchase_order_line_items',
|
2019-06-05 10:59:30 +00:00
|
|
|
help_text=_("Supplier part"),
|
|
|
|
)
|
|
|
|
|
2019-06-04 12:19:04 +00:00
|
|
|
received = models.PositiveIntegerField(default=0, help_text=_('Number of items received'))
|