Skip to content

Commit

Permalink
[IMP] account_cutoff_base: auto reverse
Browse files Browse the repository at this point in the history
Allow to automatically reverse generated moves
  • Loading branch information
jbaudoux committed Oct 25, 2023
1 parent e5e0bca commit 489cb36
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
32 changes: 29 additions & 3 deletions account_cutoff_base/models/account_cutoff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2013-2021 Akretion (http://www.akretion.com/)
# @author: Alexis de Lattre <[email protected]>
# Copyright 2013 Alexis de Lattre (Akretion) <alexis.delattre@akretion.com>
# Copyright 2018 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import json
Expand Down Expand Up @@ -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)]},
Expand Down Expand Up @@ -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()

Check warning on line 208 in account_cutoff_base/models/account_cutoff.py

View check run for this annotation

Codecov / codecov/patch

account_cutoff_base/models/account_cutoff.py#L206-L208

Added lines #L206 - L208 were not covered by tests
if self.move_id:
self.move_id.unlink()
self.write({"state": "draft"})
Expand Down Expand Up @@ -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

Check warning on line 350 in account_cutoff_base/models/account_cutoff.py

View check run for this annotation

Codecov / codecov/patch

account_cutoff_base/models/account_cutoff.py#L347-L350

Added lines #L347 - L350 were not covered by tests
if self.company_id.post_cutoff_move:
rev_move._post(soft=False)

Check warning on line 352 in account_cutoff_base/models/account_cutoff.py

View check run for this annotation

Codecov / codecov/patch

account_cutoff_base/models/account_cutoff.py#L352

Added line #L352 was not covered by tests

self.write(data)
self.message_post(body=_("Journal entry generated"))

action = self.env.ref("account.action_move_journal_line").sudo().read()[0]
Expand Down
1 change: 1 addition & 0 deletions account_cutoff_base/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
* Jim Hoefnagels <[email protected]>
* `Trobz <https://trobz.com>`_:
* Dzung Tran <[email protected]>
* Jacques-Etienne Baudoux (BCIM) <[email protected]>

0 comments on commit 489cb36

Please sign in to comment.