From c7b62011db5561ed2a3efacec26eb55b3f38eb43 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 4 May 2023 16:49:14 +0530 Subject: [PATCH] feat: configuration to notify reposting errors to specific role --- .../repost_item_valuation.py | 15 ++++++-- .../stock_reposting_settings.json | 18 ++++++++-- .../test_stock_reposting_settings.py | 35 +++++++++++++++++-- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py index aabc6fcfe3..50534605fe 100644 --- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py @@ -300,9 +300,7 @@ def _get_directly_dependent_vouchers(doc): def notify_error_to_stock_managers(doc, traceback): - recipients = get_users_with_role("Stock Manager") - if not recipients: - recipients = get_users_with_role("System Manager") + recipients = get_recipients() subject = _("Error while reposting item valuation") message = ( @@ -319,6 +317,17 @@ def notify_error_to_stock_managers(doc, traceback): frappe.sendmail(recipients=recipients, subject=subject, message=message) +def get_recipients(): + role = ( + frappe.db.get_single_value("Stock Reposting Settings", "notify_reposting_error_to_role") + or "Stock Manager" + ) + + recipients = get_users_with_role(role) + + return recipients + + def repost_entries(): """ Reposts 'Repost Item Valuation' entries in queue. diff --git a/erpnext/stock/doctype/stock_reposting_settings/stock_reposting_settings.json b/erpnext/stock/doctype/stock_reposting_settings/stock_reposting_settings.json index 0facae8d3b..7c712ce225 100644 --- a/erpnext/stock/doctype/stock_reposting_settings/stock_reposting_settings.json +++ b/erpnext/stock/doctype/stock_reposting_settings/stock_reposting_settings.json @@ -12,7 +12,9 @@ "start_time", "end_time", "limits_dont_apply_on", - "item_based_reposting" + "item_based_reposting", + "errors_notification_section", + "notify_reposting_error_to_role" ], "fields": [ { @@ -52,12 +54,23 @@ "fieldname": "item_based_reposting", "fieldtype": "Check", "label": "Use Item based reposting" + }, + { + "fieldname": "notify_reposting_error_to_role", + "fieldtype": "Link", + "label": "Notify Reposting Error to Role", + "options": "Role" + }, + { + "fieldname": "errors_notification_section", + "fieldtype": "Section Break", + "label": "Errors Notification" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-11-02 01:22:45.155841", + "modified": "2023-05-04 16:14:29.080697", "modified_by": "Administrator", "module": "Stock", "name": "Stock Reposting Settings", @@ -76,5 +89,6 @@ ], "sort_field": "modified", "sort_order": "DESC", + "states": [], "track_changes": 1 } \ No newline at end of file diff --git a/erpnext/stock/doctype/stock_reposting_settings/test_stock_reposting_settings.py b/erpnext/stock/doctype/stock_reposting_settings/test_stock_reposting_settings.py index fad74d355c..a6dc72d7a4 100644 --- a/erpnext/stock/doctype/stock_reposting_settings/test_stock_reposting_settings.py +++ b/erpnext/stock/doctype/stock_reposting_settings/test_stock_reposting_settings.py @@ -1,9 +1,40 @@ # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors # See license.txt -# import frappe import unittest +import frappe + +from erpnext.stock.doctype.repost_item_valuation.repost_item_valuation import get_recipients + class TestStockRepostingSettings(unittest.TestCase): - pass + def test_notify_reposting_error_to_role(self): + role = "Notify Reposting Role" + + if not frappe.db.exists("Role", role): + frappe.get_doc({"doctype": "Role", "role_name": role}).insert(ignore_permissions=True) + + user = "notify_reposting_error@test.com" + if not frappe.db.exists("User", user): + frappe.get_doc( + { + "doctype": "User", + "email": user, + "first_name": "Test", + "language": "en", + "time_zone": "Asia/Kolkata", + "send_welcome_email": 0, + "roles": [{"role": role}], + } + ).insert(ignore_permissions=True) + + frappe.db.set_single_value("Stock Reposting Settings", "notify_reposting_error_to_role", "") + + users = get_recipients() + self.assertFalse(user in users) + + frappe.db.set_single_value("Stock Reposting Settings", "notify_reposting_error_to_role", role) + + users = get_recipients() + self.assertTrue(user in users)