mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-01 03:00:54 +00:00
* add file for states * move general definition out * add some tests and docs * add tests for invalid definitions * make status_label tag generic * move templatetags * remove unused tag * rename test file * make status label a lookup * rename tags * move import structure * add missing tag * collect states dynamically * fix context function * move api function out * add tests for tags * rename tests * refactor imports * Add test for API function * improve errors and add tests for imporved errors * make test calls simpler * refactor definitions to use enums * switch to enum * refactor definitions to use enums * fix lookup * fix tag name * make _TAG lookup a function * cleanup BaseEnum * make _TAG definition simpler * restructure status codes to enum * reduce LoC * type status codes as int * add specific function for template context * Add definition for lookups * fix filter lookup * TEST: "fix" action lookup * Add missing migrations * Make all group code references explict * change default on models to value * switch to IntEnum * move groups into a seperate class * only request _TAG if it exsists * use value and list * use dedicated groups * fix stock assigment * fix order code * more fixes * fix borked change * fix render lookup * add group * fix import * fix syntax * clenup * fix migrations * fix typo * fix wrong value usage * fix test * remove group section * remove group section * add more test cases * Add more docstring * move choices out of migrations * change import ordeR? * last try before I revert * Update part.migrations.0112 - Add custom migration class which handles errors * Add unit test for migration - Ensure that the new fields are added to the model * Update reference to PR --------- Co-authored-by: Oliver Walters <oliver.henry.walters@gmail.com>
140 lines
3.7 KiB
Python
140 lines
3.7 KiB
Python
"""Background tasks for the 'order' app"""
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
import common.notifications
|
|
import InvenTree.helpers_model
|
|
import order.models
|
|
from InvenTree.status_codes import (PurchaseOrderStatusGroups,
|
|
SalesOrderStatusGroups)
|
|
from InvenTree.tasks import ScheduledTask, scheduled_task
|
|
from plugin.events import trigger_event
|
|
|
|
|
|
def notify_overdue_purchase_order(po: order.models.PurchaseOrder):
|
|
"""Notify users that a PurchaseOrder has just become 'overdue'"""
|
|
|
|
targets = []
|
|
|
|
if po.created_by:
|
|
targets.append(po.created_by)
|
|
|
|
if po.responsible:
|
|
targets.append(po.responsible)
|
|
|
|
name = _('Overdue Purchase Order')
|
|
|
|
context = {
|
|
'order': po,
|
|
'name': name,
|
|
'message': _(f'Purchase order {po} is now overdue'),
|
|
'link': InvenTree.helpers_model.construct_absolute_url(
|
|
po.get_absolute_url(),
|
|
),
|
|
'template': {
|
|
'html': 'email/overdue_purchase_order.html',
|
|
'subject': name,
|
|
}
|
|
}
|
|
|
|
event_name = 'order.overdue_purchase_order'
|
|
|
|
# Send a notification to the appropriate users
|
|
common.notifications.trigger_notification(
|
|
po,
|
|
event_name,
|
|
targets=targets,
|
|
context=context,
|
|
)
|
|
|
|
# Register a matching event to the plugin system
|
|
trigger_event(
|
|
event_name,
|
|
purchase_order=po.pk,
|
|
)
|
|
|
|
|
|
@scheduled_task(ScheduledTask.DAILY)
|
|
def check_overdue_purchase_orders():
|
|
"""Check if any outstanding PurchaseOrders have just become overdue:
|
|
|
|
- This check is performed daily
|
|
- Look at the 'target_date' of any outstanding PurchaseOrder objects
|
|
- If the 'target_date' expired *yesterday* then the order is just out of date
|
|
"""
|
|
|
|
yesterday = datetime.now().date() - timedelta(days=1)
|
|
|
|
overdue_orders = order.models.PurchaseOrder.objects.filter(
|
|
target_date=yesterday,
|
|
status__in=PurchaseOrderStatusGroups.OPEN,
|
|
)
|
|
|
|
for po in overdue_orders:
|
|
notify_overdue_purchase_order(po)
|
|
|
|
|
|
def notify_overdue_sales_order(so: order.models.SalesOrder):
|
|
"""Notify appropriate users that a SalesOrder has just become 'overdue'"""
|
|
|
|
targets = []
|
|
|
|
if so.created_by:
|
|
targets.append(so.created_by)
|
|
|
|
if so.responsible:
|
|
targets.append(so.responsible)
|
|
|
|
name = _('Overdue Sales Order')
|
|
|
|
context = {
|
|
'order': so,
|
|
'name': name,
|
|
'message': _(f"Sales order {so} is now overdue"),
|
|
'link': InvenTree.helpers_model.construct_absolute_url(
|
|
so.get_absolute_url(),
|
|
),
|
|
'template': {
|
|
'html': 'email/overdue_sales_order.html',
|
|
'subject': name,
|
|
}
|
|
}
|
|
|
|
event_name = 'order.overdue_sales_order'
|
|
|
|
# Send a notification to the appropriate users
|
|
common.notifications.trigger_notification(
|
|
so,
|
|
event_name,
|
|
targets=targets,
|
|
context=context,
|
|
)
|
|
|
|
# Register a matching event to the plugin system
|
|
trigger_event(
|
|
event_name,
|
|
sales_order=so.pk,
|
|
)
|
|
|
|
|
|
@scheduled_task(ScheduledTask.DAILY)
|
|
def check_overdue_sales_orders():
|
|
"""Check if any outstanding SalesOrders have just become overdue
|
|
|
|
- This check is performed daily
|
|
- Look at the 'target_date' of any outstanding SalesOrder objects
|
|
- If the 'target_date' expired *yesterday* then the order is just out of date
|
|
"""
|
|
|
|
yesterday = datetime.now().date() - timedelta(days=1)
|
|
|
|
overdue_orders = order.models.SalesOrder.objects.filter(
|
|
target_date=yesterday,
|
|
status__in=SalesOrderStatusGroups.OPEN,
|
|
)
|
|
|
|
for po in overdue_orders:
|
|
notify_overdue_sales_order(po)
|