From 9519e9616571ffe345e245f9f41b94d77c391bb2 Mon Sep 17 00:00:00 2001 From: jguenat Date: Mon, 17 Apr 2023 16:02:43 +0200 Subject: [PATCH] [FIX] account_commission: revert settlement in invoiced state to settled when deleting settlement invoice - fix bug when deleting a draft settlement invoice, the settlement is stuck on "invoiced" state --- sale_commission/models/account_move.py | 7 +++++++ sale_commission/tests/test_sale_commission.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/sale_commission/models/account_move.py b/sale_commission/models/account_move.py index 00fc6429b..49884e2d2 100644 --- a/sale_commission/models/account_move.py +++ b/sale_commission/models/account_move.py @@ -89,6 +89,13 @@ def fields_view_get( res["arch"] = etree.tostring(invoice_xml) return res + def unlink(self): + """Put 'invoiced' settlements associated to the invoices back in settled state.""" + self.invoice_line_ids.settlement_id.filtered( + lambda s: s.state == "invoiced" + ).write({"state": "settled"}) + return super().unlink() + class AccountMoveLine(models.Model): _inherit = [ diff --git a/sale_commission/tests/test_sale_commission.py b/sale_commission/tests/test_sale_commission.py index b020bcb98..9ccade4e8 100644 --- a/sale_commission/tests/test_sale_commission.py +++ b/sale_commission/tests/test_sale_commission.py @@ -692,3 +692,19 @@ def test_payment_date_settlement(self): ) self.assertEqual(1, len(settlements)) self.assertEqual(1, len(settlements.line_ids)) + + def test_unlink_settlement_invoice(self): + self._create_order_and_invoice_and_settle( + self.agent_quaterly, + self.env.ref("sale_commission.demo_commission"), + 1, + ) + settlements = self.settle_model.search([("state", "=", "settled")]) + invoices = settlements.make_invoices(self.journal, self.commission_product) + self.assertTrue( + all(state == "invoiced" for state in settlements.mapped("state")) + ) + invoices.unlink() + self.assertTrue( + all(state == "settled" for state in settlements.mapped("state")) + )