From 61f05132dbd0cd3d0ed76e04f2d45a8a6ebf1c66 Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Thu, 17 Nov 2022 11:00:01 +0100 Subject: [PATCH 1/4] feat: validate repost item valuation against accounts freeze date --- .../repost_item_valuation.py | 19 +++++++++++++-- .../test_repost_item_valuation.py | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 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 d6f9bae5da..5fc0ee094f 100644 --- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py @@ -5,7 +5,7 @@ import frappe from frappe import _ from frappe.exceptions import QueryDeadlockError, QueryTimeoutError from frappe.model.document import Document -from frappe.utils import cint, get_link_to_form, get_weekday, now, nowtime +from frappe.utils import cint, get_link_to_form, get_weekday, getdate, now, nowtime from frappe.utils.user import get_users_with_role from rq.timeouts import JobTimeoutException @@ -25,6 +25,21 @@ class RepostItemValuation(Document): self.set_status(write=False) self.reset_field_values() self.set_company() + self.validate_accounts_freeze() + + def validate_accounts_freeze(self): + acc_settings = frappe.db.get_value( + 'Accounts Settings', + 'Accounts Settings', + ['acc_frozen_upto', 'frozen_accounts_modifier'], + as_dict=1 + ) + if not acc_settings.acc_frozen_upto: + return + if acc_settings.frozen_accounts_modifier and self.owner in get_users_with_role(acc_settings.frozen_accounts_modifier): + return + if getdate(self.posting_date) <= getdate(acc_settings.acc_frozen_upto): + frappe.throw(_("You cannot repost item valuation before {}").format(acc_settings.acc_frozen_upto)) def reset_field_values(self): if self.based_on == "Transaction": @@ -240,7 +255,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: - get_users_with_role("System Manager") + recipients = get_users_with_role("System Manager") subject = _("Error while reposting item valuation") message = ( diff --git a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py index e0f2479710..dc03c79b33 100644 --- a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py @@ -327,3 +327,26 @@ class TestRepostItemValuation(FrappeTestCase, StockTestMixin): # outstanding should not be affected sinv.reload() self.assertEqual(sinv.outstanding_amount, 100) + + def test_account_freeze_validation(self): + today = nowdate() + + riv = frappe.get_doc( + doctype="Repost Item Valuation", + item_code="_Test Item", + warehouse="_Test Warehouse - _TC", + based_on="Item and Warehouse", + posting_date=today, + posting_time="00:01:00", + ) + riv.flags.dont_run_in_test = True # keep it queued + + accounts_settings = frappe.get_doc("Accounts Settings") + accounts_settings.acc_frozen_upto = today + accounts_settings.frozen_accounts_modifier = '' + accounts_settings.save() + + self.assertRaises(frappe.ValidationError, riv.save) + + accounts_settings.acc_frozen_upto = '' + accounts_settings.save() \ No newline at end of file From be15419bd5d3481dd83c7c4fedbb010d3e0e13d9 Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Thu, 17 Nov 2022 11:00:34 +0100 Subject: [PATCH 2/4] chore: pre-commit --- .../repost_item_valuation.py | 16 ++++++++++------ .../test_repost_item_valuation.py | 6 +++--- 2 files changed, 13 insertions(+), 9 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 5fc0ee094f..b62933821c 100644 --- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py @@ -29,17 +29,21 @@ class RepostItemValuation(Document): def validate_accounts_freeze(self): acc_settings = frappe.db.get_value( - 'Accounts Settings', - 'Accounts Settings', - ['acc_frozen_upto', 'frozen_accounts_modifier'], - as_dict=1 + "Accounts Settings", + "Accounts Settings", + ["acc_frozen_upto", "frozen_accounts_modifier"], + as_dict=1, ) if not acc_settings.acc_frozen_upto: return - if acc_settings.frozen_accounts_modifier and self.owner in get_users_with_role(acc_settings.frozen_accounts_modifier): + if acc_settings.frozen_accounts_modifier and self.owner in get_users_with_role( + acc_settings.frozen_accounts_modifier + ): return if getdate(self.posting_date) <= getdate(acc_settings.acc_frozen_upto): - frappe.throw(_("You cannot repost item valuation before {}").format(acc_settings.acc_frozen_upto)) + frappe.throw( + _("You cannot repost item valuation before {}").format(acc_settings.acc_frozen_upto) + ) def reset_field_values(self): if self.based_on == "Transaction": diff --git a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py index dc03c79b33..f1667757a7 100644 --- a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py @@ -343,10 +343,10 @@ class TestRepostItemValuation(FrappeTestCase, StockTestMixin): accounts_settings = frappe.get_doc("Accounts Settings") accounts_settings.acc_frozen_upto = today - accounts_settings.frozen_accounts_modifier = '' + accounts_settings.frozen_accounts_modifier = "" accounts_settings.save() self.assertRaises(frappe.ValidationError, riv.save) - accounts_settings.acc_frozen_upto = '' - accounts_settings.save() \ No newline at end of file + accounts_settings.acc_frozen_upto = "" + accounts_settings.save() From b482e3876dccb2547330b95c1c8d4f2cf7039918 Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Wed, 23 Nov 2022 18:59:15 +0530 Subject: [PATCH 3/4] fix: check for session user rather than owner --- .../repost_item_valuation/repost_item_valuation.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 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 b62933821c..a98d16e390 100644 --- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py @@ -36,11 +36,12 @@ class RepostItemValuation(Document): ) if not acc_settings.acc_frozen_upto: return - if acc_settings.frozen_accounts_modifier and self.owner in get_users_with_role( - acc_settings.frozen_accounts_modifier - ): - return if getdate(self.posting_date) <= getdate(acc_settings.acc_frozen_upto): + if acc_settings.frozen_accounts_modifier and frappe.session.user in get_users_with_role( + acc_settings.frozen_accounts_modifier + ): + frappe.msgprint(_("Caution: This might alter frozen accounts.")) + return frappe.throw( _("You cannot repost item valuation before {}").format(acc_settings.acc_frozen_upto) ) From 88a0aa4077b6cd96800a813e5ba163b573d01f47 Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Wed, 23 Nov 2022 19:04:11 +0530 Subject: [PATCH 4/4] chore: pre-commit --- .../doctype/repost_item_valuation/repost_item_valuation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 a98d16e390..8e914e6b80 100644 --- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py @@ -37,8 +37,9 @@ class RepostItemValuation(Document): if not acc_settings.acc_frozen_upto: return if getdate(self.posting_date) <= getdate(acc_settings.acc_frozen_upto): - if acc_settings.frozen_accounts_modifier and frappe.session.user in get_users_with_role( + if ( acc_settings.frozen_accounts_modifier + and frappe.session.user in get_users_with_role(acc_settings.frozen_accounts_modifier) ): frappe.msgprint(_("Caution: This might alter frozen accounts.")) return