forked from OCA/account-financial-tools
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] account_reversal: add reversal_id field
This commit adds the `reversal_id` field in order to improve the UX of the move reversal functionality. The field has been added to the form view close to the `reversed_entry_id` field already present in the view. This commit also introduces a constraint preventing the user to flag a move as to be reversed when a non cancelled reversal move has already been created. This allows simplifying the domain used on the `to_be_reversed` filter. Finally, it also convert the deprecated `SavepointCase` test into a `TransactionCase` one. Fixes: OCA#1761
- Loading branch information
Laurent Stukkens
committed
Nov 14, 2023
1 parent
403c1a3
commit 668e9a4
Showing
3 changed files
with
62 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,51 @@ | |
# Copyright 2016 Antonio Espinosa <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import fields, models | ||
from odoo import _, api, fields, models | ||
from odoo.exceptions import ValidationError | ||
|
||
|
||
class MoveAlreadyReversedValidationError(ValidationError): | ||
pass | ||
|
||
|
||
class AccountMove(models.Model): | ||
_inherit = "account.move" | ||
|
||
to_be_reversed = fields.Boolean( | ||
copy=False, | ||
help="Check this box if your entry has to be reversed at the end " "of period.", | ||
help="Check this box if your entry has to be reversed at the end of period.", | ||
) | ||
reversal_id = fields.Many2one( | ||
"account.move", | ||
compute="_compute_reversal_id", | ||
string="Reversal Entry", | ||
readonly=True, | ||
) | ||
|
||
@api.depends("reversal_move_id") | ||
def _compute_reversal_id(self): | ||
for move in self: | ||
move.reversal_id = move._get_reversal_id() | ||
|
||
@api.constrains("to_be_reversed", "reversal_move_id") | ||
def _check_to_be_reversed(self): | ||
for move in self: | ||
if move.to_be_reversed and move._get_reversal_id(): | ||
raise MoveAlreadyReversedValidationError( | ||
_( | ||
"The move has already been reversed, " | ||
"so you are not allowed to mark it as to be reversed." | ||
) | ||
) | ||
|
||
def _get_reversal_id(self): | ||
# Although theoretically a o2o, reversal_move_id is technically a o2m, | ||
# which does not prevent having more than one record. That is why we are using | ||
# a slicing in order to get the first record or an empty recordset. | ||
self.ensure_one() | ||
return self.reversal_move_id.filtered(lambda m: m.state != "cancel")[:1] | ||
|
||
def _mark_as_reversed(self): | ||
self.filtered("to_be_reversed").write({"to_be_reversed": False}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters