From ab8e59d69d993fea3dba32c324035b43f0cd3840 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Fri, 2 Dec 2022 12:57:26 +0100 Subject: [PATCH] [FIX] account_move_name_sequence: call flush before _fetch_duplicate_supplier_reference method same issue https://github.com/OCA/account-financial-tools/issues/1501 this fix not working for v16 https://github.com/OCA/account-financial-tools/pull/1514 --- .../models/account_move.py | 6 ++ .../tests/test_account_move_name_seq.py | 96 +++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/account_move_name_sequence/models/account_move.py b/account_move_name_sequence/models/account_move.py index 8813263f..8b43a629 100644 --- a/account_move_name_sequence/models/account_move.py +++ b/account_move_name_sequence/models/account_move.py @@ -64,3 +64,9 @@ class AccountMove(models.Model): if not invoices_other_sequences and invoices_no_gap_sequences: return False return super(AccountMove, invoices_other_sequences)._is_end_of_seq_chain() + + def _fetch_duplicate_supplier_reference(self, only_posted=False): + moves = self.filtered(lambda m: m.is_purchase_document() and m.ref) + if moves: + self.flush_model(["name", "journal_id", "move_type", "state"]) + return super()._fetch_duplicate_supplier_reference(only_posted=only_posted) diff --git a/account_move_name_sequence/tests/test_account_move_name_seq.py b/account_move_name_sequence/tests/test_account_move_name_seq.py index 4538ccd5..4457da0c 100644 --- a/account_move_name_sequence/tests/test_account_move_name_seq.py +++ b/account_move_name_sequence/tests/test_account_move_name_seq.py @@ -138,6 +138,102 @@ class TestAccountMoveNameSequence(TransactionCase): move.action_post() self.assertEqual(move.name, "TEST-2022-07-0001") + def test_in_invoice_and_refund(self): + in_invoice = self.env["account.move"].create( + { + "journal_id": self.purchase_journal.id, + "invoice_date": self.date, + "partner_id": self.env.ref("base.res_partner_3").id, + "move_type": "in_invoice", + "invoice_line_ids": [ + ( + 0, + 0, + { + "account_id": self.account1.id, + "price_unit": 42.0, + "quantity": 12, + }, + ), + ( + 0, + 0, + { + "account_id": self.account1.id, + "price_unit": 48.0, + "quantity": 10, + }, + ), + ], + } + ) + self.assertEqual(in_invoice.name, "/") + in_invoice.action_post() + + move_reversal = ( + self.env["account.move.reversal"] + .with_context(active_model="account.move", active_ids=in_invoice.ids) + .create( + { + "journal_id": in_invoice.journal_id.id, + "reason": "no reason", + "refund_method": "cancel", + } + ) + ) + reversal = move_reversal.reverse_moves() + reversed_move = self.env["account.move"].browse(reversal["res_id"]) + self.assertTrue(reversed_move) + self.assertEqual(reversed_move.state, "posted") + + in_invoice = in_invoice.copy( + { + "invoice_date": self.date, + } + ) + in_invoice.action_post() + + move_reversal = ( + self.env["account.move.reversal"] + .with_context(active_model="account.move", active_ids=in_invoice.ids) + .create( + { + "journal_id": in_invoice.journal_id.id, + "reason": "no reason", + "refund_method": "modify", + } + ) + ) + reversal = move_reversal.reverse_moves() + draft_invoice = self.env["account.move"].browse(reversal["res_id"]) + self.assertTrue(draft_invoice) + self.assertEqual(draft_invoice.state, "draft") + self.assertEqual(draft_invoice.move_type, "in_invoice") + + in_invoice = in_invoice.copy( + { + "invoice_date": self.date, + } + ) + in_invoice.action_post() + + move_reversal = ( + self.env["account.move.reversal"] + .with_context(active_model="account.move", active_ids=in_invoice.ids) + .create( + { + "journal_id": in_invoice.journal_id.id, + "reason": "no reason", + "refund_method": "refund", + } + ) + ) + reversal = move_reversal.reverse_moves() + draft_reversed_move = self.env["account.move"].browse(reversal["res_id"]) + self.assertTrue(draft_reversed_move) + self.assertEqual(draft_reversed_move.state, "draft") + self.assertEqual(draft_reversed_move.move_type, "in_refund") + def test_in_refund(self): in_refund_invoice = self.env["account.move"].create( {