From 2370e4196113d2082311811a6ad55c74f8f0309a Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 10 Jan 2022 09:01:17 +1100 Subject: [PATCH] Add multiple triggered events for the "order" app --- InvenTree/order/models.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index e798ee4e30..a331ddf491 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -11,6 +11,8 @@ from decimal import Decimal from django.db import models, transaction from django.db.models import Q, F, Sum from django.db.models.functions import Coalesce +from django.db.models.signals import post_save +from django.dispatch.dispatcher import receiver from django.core.validators import MinValueValidator from django.core.exceptions import ValidationError from django.contrib.auth.models import User @@ -24,6 +26,7 @@ from users import models as UserModels from part import models as PartModels from stock import models as stock_models from company.models import Company, SupplierPart +from plugin.events import trigger_event from InvenTree.fields import InvenTreeModelMoneyField, RoundingDecimalField from InvenTree.helpers import decimal2string, increment, getSetting @@ -317,6 +320,8 @@ class PurchaseOrder(Order): self.issue_date = datetime.now().date() self.save() + trigger_event('purchaseorder.placed', order_id=self.pk) + @transaction.atomic def complete_order(self): """ Marks the PurchaseOrder as COMPLETE. Order must be currently PLACED. """ @@ -326,6 +331,8 @@ class PurchaseOrder(Order): self.complete_date = datetime.now().date() self.save() + trigger_event('purchaseorder.completed', order_id=self.pk) + @property def is_overdue(self): """ @@ -356,6 +363,8 @@ class PurchaseOrder(Order): self.status = PurchaseOrderStatus.CANCELLED self.save() + trigger_event('purchaseorder.cancelled', order_id=self.pk) + def pending_line_items(self): """ Return a list of pending line items for this order. Any line item where 'received' < 'quantity' will be returned. @@ -461,6 +470,15 @@ class PurchaseOrder(Order): self.complete_order() # This will save the model +@receiver(post_save, sender=PurchaseOrder, dispatch_uid='po_post_save_log') +def after_save_build(sender, instance: PurchaseOrder, created: bool, **kwargs): + + if created: + trigger_event('purchaseorder.created', order_id=instance.pk) + else: + trigger_event('purchasesorder.saved', order_id=instance.pk) + + class SalesOrder(Order): """ A SalesOrder represents a list of goods shipped outwards to a customer. @@ -667,6 +685,8 @@ class SalesOrder(Order): self.save() + trigger_event('salesorder.completed', order_id=self.pk) + return True def can_cancel(self): @@ -698,6 +718,8 @@ class SalesOrder(Order): for allocation in line.allocations.all(): allocation.delete() + trigger_event('salesorder.cancelled', order_id=self.pk) + return True @property @@ -750,6 +772,15 @@ class SalesOrder(Order): return self.pending_shipments().count() +@receiver(post_save, sender=SalesOrder, dispatch_uid='so_post_save_log') +def after_save_build(sender, instance: SalesOrder, created: bool, **kwargs): + + if created: + trigger_event('salesorder.created', order_id=instance.pk) + else: + trigger_event('salesorder.saved', order_id=instance.pk) + + class PurchaseOrderAttachment(InvenTreeAttachment): """ Model for storing file attachments against a PurchaseOrder object @@ -1104,6 +1135,8 @@ class SalesOrderShipment(models.Model): self.save() + trigger_event('salesordershipment.completed', shipment_id=self.pk) + class SalesOrderAllocation(models.Model): """