From 489cb36633cdbba3a5fc1832ab3096b37e5e068a Mon Sep 17 00:00:00 2001 From: Jacques-Etienne Baudoux Date: Wed, 25 Oct 2023 11:09:57 +0200 Subject: [PATCH] [IMP] account_cutoff_base: auto reverse Allow to automatically reverse generated moves --- account_cutoff_base/models/account_cutoff.py | 32 ++++++++++++++++++-- account_cutoff_base/readme/CONTRIBUTORS.rst | 1 + 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/account_cutoff_base/models/account_cutoff.py b/account_cutoff_base/models/account_cutoff.py index 4118e5d448d..8b03fd5f2c3 100644 --- a/account_cutoff_base/models/account_cutoff.py +++ b/account_cutoff_base/models/account_cutoff.py @@ -1,5 +1,5 @@ -# Copyright 2013-2021 Akretion (http://www.akretion.com/) -# @author: Alexis de Lattre +# Copyright 2013 Alexis de Lattre (Akretion) +# Copyright 2018 Jacques-Etienne Baudoux (BCIM) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). import json @@ -109,6 +109,17 @@ def _default_cutoff_account_id(self): copy=False, check_company=True, ) + auto_reverse = fields.Boolean( + help="Automatically reverse created move on following day. Use this " + "if you accrue a value end of period that you want to reverse " + "begin of next period", + ) + move_reversal_id = fields.Many2one( + "account.move", + string="Cut-off Journal Entry Reversal", + readonly=True, + copy=False, + ) move_ref = fields.Char( string="Reference of the Cut-off Journal Entry", states={"done": [("readonly", True)]}, @@ -191,6 +202,10 @@ def name_get(self): def back2draft(self): self.ensure_one() + if self.move_reversal_id: + self.move_reversal_id.line_ids.remove_move_reconcile() + self.move_reversal_id.unlink() + self.move_id.line_ids.remove_move_reconcile() if self.move_id: self.move_id.unlink() self.write({"state": "draft"}) @@ -325,7 +340,18 @@ def create_move(self): move = move_obj.create(vals) if self.company_id.post_cutoff_move: move._post(soft=False) - self.write({"move_id": move.id, "state": "done"}) + + data = {"move_id": move.id, "state": "done"} + + if self.auto_reverse: + next_day = fields.Date.from_string(self.cutoff_date) + relativedelta(days=1) + rev_move = move._reverse_move(next_day, move.journal_id) + rev_move.ref = _("reversal of: ") + move.ref + data["move_reversal_id"] = rev_move.id + if self.company_id.post_cutoff_move: + rev_move._post(soft=False) + + self.write(data) self.message_post(body=_("Journal entry generated")) action = self.env.ref("account.action_move_journal_line").sudo().read()[0] diff --git a/account_cutoff_base/readme/CONTRIBUTORS.rst b/account_cutoff_base/readme/CONTRIBUTORS.rst index 740c34299d1..6a7d85d9f00 100644 --- a/account_cutoff_base/readme/CONTRIBUTORS.rst +++ b/account_cutoff_base/readme/CONTRIBUTORS.rst @@ -7,3 +7,4 @@ * Jim Hoefnagels * `Trobz `_: * Dzung Tran +* Jacques-Etienne Baudoux (BCIM)