From 624b2feef11e242d55b91930feb616bae974517b Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Tue, 21 Feb 2023 08:08:37 +0000 Subject: [PATCH] [FIX] sale: correctly send auto-generated invoices Since 4d2b04a843c27981e44d6f932d7cf7e6838b4b9d, invoices auto-generated on Sales Order payment were not sent to customers. Invoices are only marked as 'posted' in the override of `_reconcile_after_done` in account_payment which means `_send_invoice` must be called after the super call s.t. the invoices are correctly posted before being sent. opw-3158620 closes odoo/odoo#113229 X-original-commit: 82c53dd3404992b14d1db331c37b6d03c9f0ace1 Signed-off-by: Victor Feyens (vfe) --- addons/sale/models/payment_transaction.py | 9 ++++++--- addons/sale/tests/test_payment_flow.py | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/addons/sale/models/payment_transaction.py b/addons/sale/models/payment_transaction.py index 4ad6a75cb54..8ff6a297d15 100644 --- a/addons/sale/models/payment_transaction.py +++ b/addons/sale/models/payment_transaction.py @@ -116,13 +116,16 @@ class PaymentTransaction(models.Model): confirmed_orders = self._check_amount_and_confirm_order() confirmed_orders._send_order_confirmation_mail() - # invoice the sale orders if needed and send it - if str2bool(self.env['ir.config_parameter'].sudo().get_param('sale.automatic_invoice')): + auto_invoice = str2bool( + self.env['ir.config_parameter'].sudo().get_param('sale.automatic_invoice')) + if auto_invoice: # Invoice the sale orders in self instead of in confirmed_orders to create the invoice # even if only a partial payment was made. self._invoice_sale_orders() + super()._reconcile_after_done() + if auto_invoice: + # Must be called after the super() call to make sure the invoice are correctly posted. self._send_invoice() - return super()._reconcile_after_done() def _send_invoice(self): template_id = int(self.env['ir.config_parameter'].sudo().get_param( diff --git a/addons/sale/tests/test_payment_flow.py b/addons/sale/tests/test_payment_flow.py index c36a1043cac..bfcc4ea106d 100644 --- a/addons/sale/tests/test_payment_flow.py +++ b/addons/sale/tests/test_payment_flow.py @@ -230,6 +230,7 @@ class TestSalePayment(AccountPaymentCommon, SaleCommon, PaymentHttpCommon): self.assertEqual(self.sale_order.state, 'done') self.assertTrue(tx.invoice_ids) self.assertTrue(self.sale_order.invoice_ids) + self.assertTrue(tx.invoice_ids.is_move_sent) def test_so_partial_payment_no_invoice(self): # Set automatic invoice