From 389bd53a0bcde0d5911f15a09cd03a84678b5380 Mon Sep 17 00:00:00 2001 From: Thomas Binsfeld Date: Fri, 1 Mar 2019 11:02:30 +0000 Subject: [PATCH] [IMP] sale: split invoicing method for cleaner overrides closes odoo/odoo#31529 Signed-off-by: Damien Bouvy (dbo) --- addons/sale/models/sale.py | 57 +++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index ac06b1368f6..fb4dcdeecd6 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -506,6 +506,37 @@ class SaleOrder(models.Model): action = {'type': 'ir.actions.act_window_close'} return action + def _finalize_invoices(self, invoices, references): + """ + Invoked after creating invoices at the end of action_invoice_create. + :param invoices: {group_key: invoice} + :param references: {invoice: order} + """ + for invoice in invoices.values(): + invoice.compute_taxes() + if not invoice.invoice_line_ids: + raise UserError(_( + 'There is no invoiceable line. If a product has a Delivered quantities invoicing policy, please make sure that a quantity has been delivered.')) + # If invoice is negative, do a refund invoice instead + if invoice.amount_total < 0: + invoice.type = 'out_refund' + for line in invoice.invoice_line_ids: + line.quantity = -line.quantity + # Use additional field helper function (for account extensions) + for line in invoice.invoice_line_ids: + line._set_additional_fields(invoice) + # Necessary to force computation of taxes. In account_invoice, they are triggered + # by onchanges, which are not triggered when doing a create. + invoice.compute_taxes() + # Idem for partner + so_payment_term_id = invoice.payment_term_id.id + invoice._onchange_partner_id() + # To keep the payment terms set on the SO + invoice.payment_term_id = so_payment_term_id + invoice.message_post_with_view('mail.message_origin_link', + values={'self': invoice, 'origin': references[invoice]}, + subtype_id=self.env.ref('mail.mt_note').id) + @api.multi def action_invoice_create(self, grouped=False, final=False): """ @@ -587,31 +618,7 @@ class SaleOrder(models.Model): if not invoices: raise UserError(_('There is no invoiceable line. If a product has a Delivered quantities invoicing policy, please make sure that a quantity has been delivered.')) - for invoice in invoices.values(): - invoice.compute_taxes() - if not invoice.invoice_line_ids: - raise UserError(_('There is no invoiceable line. If a product has a Delivered quantities invoicing policy, please make sure that a quantity has been delivered.')) - # If invoice is negative, do a refund invoice instead - if invoice.amount_total < 0: - invoice.type = 'out_refund' - for line in invoice.invoice_line_ids: - line.quantity = -line.quantity - # Use additional field helper function (for account extensions) - for line in invoice.invoice_line_ids: - line._set_additional_fields(invoice) - # Necessary to force computation of taxes. In account_invoice, they are triggered - # by onchanges, which are not triggered when doing a create. - invoice.compute_taxes() - # Idem for partner - so_payment_term_id = invoice.payment_term_id.id - fp_invoice = invoice.fiscal_position_id - invoice._onchange_partner_id() - invoice.fiscal_position_id = fp_invoice - # To keep the payment terms set on the SO - invoice.payment_term_id = so_payment_term_id - invoice.message_post_with_view('mail.message_origin_link', - values={'self': invoice, 'origin': references[invoice]}, - subtype_id=self.env.ref('mail.mt_note').id) + self._finalize_invoices(invoices, references) return [inv.id for inv in invoices.values()] @api.multi