From 8d729457772dfd2238fddb216a744a4480fb6d59 Mon Sep 17 00:00:00 2001 From: kaynnan Date: Wed, 29 Nov 2023 15:10:33 -0300 Subject: [PATCH] [RFC] account_consolidation_oca: method reverse_move --- .../wizard/consolidation_consolidate.py | 55 ++++++------------- 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/account_consolidation_oca/wizard/consolidation_consolidate.py b/account_consolidation_oca/wizard/consolidation_consolidate.py index b2afd703..60de67c9 100644 --- a/account_consolidation_oca/wizard/consolidation_consolidate.py +++ b/account_consolidation_oca/wizard/consolidation_consolidate.py @@ -142,15 +142,6 @@ def _prepare_rate_difference_line(self, move_lines_list): } def reverse_moves(self, subsidiary): - """ - Reverse all consolidation account moves of a subsidiary which have - the "Auto reversed" flag, and wasn't reversed before this date - - :param subsidiary: Recordset of the subsidiary - - :return: tuple with : Recordset of the reversed moves, - Recordset of the reversal moves - """ move_obj = self.env["account.move"] moves_to_reverse = move_obj.search( [ @@ -161,32 +152,18 @@ def reverse_moves(self, subsidiary): ) if not moves_to_reverse: return moves_to_reverse, False - try: - reversal_action = ( - self.env["account.move.reversal"] - .with_context( - active_ids=moves_to_reverse.ids, __conso_reversal_no_post=True - ) - .create( - { - "date": self._get_month_first_date(), - "journal_id": self.journal_id.id, - } - ) - .reverse_moves() - ) - except ValidationError as e: - raise ValidationError( - _( - "The error below appeared while trying to reverse the " - "following moves: \n %s \n %s" - ) - % ("\n".join(["- %s" % m.name for m in moves_to_reverse]), e.name) - ) - reversal_move = move_obj.browse(reversal_action.get("domain")[0][2]) + reversed_moves = self.env["account.move"] + for move in moves_to_reverse: + # Reverse the move + reversal_move = move._reverse_moves() + reversed_moves |= reversal_move + # Update the reversed_entry_id field on the original move + move.write({"reversed_entry_id": reversal_move.id}) + # Update the ref field on the reversed move + reversal_move.write({"ref": _("Reverse of %s") % move.name}) - return moves_to_reverse, reversal_move + return moves_to_reverse, reversed_moves def get_account_balance(self, account, partner=False): """ @@ -399,10 +376,10 @@ def consolidate_subsidiary(self, profile): # Now that all move lines are processed we reset the flag of processed # accounts - consolidated_move_lines = self.env["account.move.line"].search( - [("consolidated", "=", True)] + self.env.cr.execute( + "UPDATE account_move_line SET consolidated = False " + "WHERE consolidated = True;" ) - consolidated_move_lines.write({"consolidated": False}) if move_lines_to_generate: @@ -442,10 +419,10 @@ def run_consolidation(self): created_moves = self.env["account.move"] - consolidated_move_lines = self.env["account.move.line"].search( - [("consolidated", "=", True)] + self.env.cr.execute( + "UPDATE account_move_line SET consolidated = False " + "WHERE consolidated = True;" ) - consolidated_move_lines.write({"consolidated": False}) for profile in self.consolidation_profile_ids: created_moves |= self.consolidate_subsidiary(profile)