diff --git a/account_commission/models/account_move.py b/account_commission/models/account_move.py index 665f0a535..1769d4633 100644 --- a/account_commission/models/account_move.py +++ b/account_commission/models/account_move.py @@ -114,6 +114,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/account_commission/tests/test_account_commission.py b/account_commission/tests/test_account_commission.py index 94652c8f3..13e80609e 100644 --- a/account_commission/tests/test_account_commission.py +++ b/account_commission/tests/test_account_commission.py @@ -454,3 +454,14 @@ def test_account_commission_multiple_settlement_ids(self): settlements.make_invoices(self.journal, self.commission_product, grouped=True) invoices = settlements.mapped("invoice_id") self.assertEqual(2, invoices.settlement_count) + + def test_unlink_settlement_invoice(self): + settlements = self._create_multi_settlements() + 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")) + )