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