From 317cc0358c0e3cb07da0d72657dddeb9ef7ee546 Mon Sep 17 00:00:00 2001 From: Khushi Rawat <142375893+khushi8112@users.noreply.github.com> Date: Mon, 1 Jul 2024 16:55:49 +0530 Subject: [PATCH] fix: add auto-update for overdue status (#42105) * fix: auto-update for overdue status * chore: use qb.update (cherry picked from commit c5e474f4f52019d431da6b3ed6b3ad4368a0f3f1) --- .../doctype/asset_maintenance/asset_maintenance.py | 9 ++++++--- .../asset_maintenance_log/asset_maintenance_log.py | 14 +++++++++++++- erpnext/hooks.py | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/erpnext/assets/doctype/asset_maintenance/asset_maintenance.py b/erpnext/assets/doctype/asset_maintenance/asset_maintenance.py index 570c9751a572..b44164f2dae3 100644 --- a/erpnext/assets/doctype/asset_maintenance/asset_maintenance.py +++ b/erpnext/assets/doctype/asset_maintenance/asset_maintenance.py @@ -18,9 +18,7 @@ class AssetMaintenance(Document): if TYPE_CHECKING: from frappe.types import DF - from erpnext.assets.doctype.asset_maintenance_task.asset_maintenance_task import ( - AssetMaintenanceTask, - ) + from erpnext.assets.doctype.asset_maintenance_task.asset_maintenance_task import AssetMaintenanceTask asset_category: DF.ReadOnly | None asset_maintenance_tasks: DF.Table[AssetMaintenanceTask] @@ -47,6 +45,11 @@ def on_update(self): assign_tasks(self.name, task.assign_to, task.maintenance_task, task.next_due_date) self.sync_maintenance_tasks() + def after_delete(self): + asset = frappe.get_doc("Asset", self.asset_name) + if asset.status == "In Maintenance": + asset.set_status() + def sync_maintenance_tasks(self): tasks_names = [] for task in self.get("asset_maintenance_tasks"): diff --git a/erpnext/assets/doctype/asset_maintenance_log/asset_maintenance_log.py b/erpnext/assets/doctype/asset_maintenance_log/asset_maintenance_log.py index 009bcc3e69ab..95d02714c5b1 100644 --- a/erpnext/assets/doctype/asset_maintenance_log/asset_maintenance_log.py +++ b/erpnext/assets/doctype/asset_maintenance_log/asset_maintenance_log.py @@ -5,7 +5,8 @@ import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import getdate, nowdate +from frappe.query_builder import DocType +from frappe.utils import getdate, nowdate, today from erpnext.assets.doctype.asset_maintenance.asset_maintenance import calculate_next_due_date @@ -75,6 +76,17 @@ def update_maintenance_task(self): asset_maintenance_doc.save() +def update_asset_maintenance_log_status(): + AssetMaintenanceLog = DocType("Asset Maintenance Log") + ( + frappe.qb.update(AssetMaintenanceLog) + .set(AssetMaintenanceLog.maintenance_status, "Overdue") + .where( + (AssetMaintenanceLog.maintenance_status == "Planned") & (AssetMaintenanceLog.due_date < today()) + ) + ).run() + + @frappe.whitelist() @frappe.validate_and_sanitize_search_inputs def get_maintenance_tasks(doctype, txt, searchfield, start, page_len, filters): diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 64cbc297d207..a080af9a8270 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -442,6 +442,7 @@ "erpnext.accounts.doctype.process_statement_of_accounts.process_statement_of_accounts.send_auto_email", "erpnext.accounts.utils.auto_create_exchange_rate_revaluation_daily", "erpnext.accounts.utils.run_ledger_health_checks", + "erpnext.assets.doctype.asset.asset_maintenance_log.update_asset_maintenance_log_status", ], "weekly": [ "erpnext.accounts.utils.auto_create_exchange_rate_revaluation_weekly",