From e4878bfae4c39a9b53e2e1de67983fbd4640ea73 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Tue, 1 Dec 2020 18:06:39 +0100 Subject: [PATCH] [FIX] account_lock_to_date. No crash on error. Use ValidationError instead of UserError to prevent unfriendly message; Prevent crash when a lock to date not yet set in company. --- account_lock_to_date/models/account_move.py | 32 +++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/account_lock_to_date/models/account_move.py b/account_lock_to_date/models/account_move.py index 574ef9db8..d61cc7575 100644 --- a/account_lock_to_date/models/account_move.py +++ b/account_lock_to_date/models/account_move.py @@ -1,25 +1,33 @@ # Copyright 2019 ForgeFlow S.L. -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import _, models -from odoo.exceptions import UserError +from odoo.exceptions import ValidationError class AccountMove(models.Model): _inherit = "account.move" def _check_lock_to_dates(self): + """Prevent moves that are on or after lock to date. + + Advisors have more freedom then other users and are only constrained + by the ficalyear_lock_to_date. + Other users will also be restricted by the period_lock_to_date. + """ + is_advisor = self.user_has_groups("account.group_account_manager") for move in self: - lock_to_date = ( - min( - move.company_id.period_lock_to_date, - move.company_id.fiscalyear_lock_to_date, + advisor_lock_to_date = move.company_id.fiscalyear_lock_to_date + user_lock_to_date = move.company_id.period_lock_to_date + if is_advisor: + lock_to_date = advisor_lock_to_date or False + else: + lock_to_date = ( + min(user_lock_to_date, advisor_lock_to_date) + if user_lock_to_date and advisor_lock_to_date + else user_lock_to_date or advisor_lock_to_date or False ) - or False - ) - if self.user_has_groups("account.group_account_manager"): - lock_to_date = move.company_id.fiscalyear_lock_to_date or False if lock_to_date and move.date >= lock_to_date: - if self.user_has_groups("account.group_account_manager"): + if is_advisor: message = _( "You cannot add/modify entries after and " "inclusive of the lock to date %s" @@ -31,7 +39,7 @@ class AccountMove(models.Model): "Check the company settings or ask someone " "with the 'Adviser' role" ) % (lock_to_date) - raise UserError(message) + raise ValidationError(message) def action_post(self): self._check_lock_to_dates()