diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py index 103b736f124..eb6aa6f45c9 100644 --- a/addons/account/models/account_invoice.py +++ b/addons/account/models/account_invoice.py @@ -321,9 +321,26 @@ class AccountInvoice(models.Model): @api.model def create(self, vals): + onchanges = { + '_onchange_partner_id': ['account_id', 'payment_term_id', 'fiscal_position_id', 'partner_bank_id'], + '_onchange_journal_id': ['currency_id'], + } + for onchange_method, changed_fields in onchanges.items(): + if any(f not in vals for f in changed_fields): + invoice = self.new(vals) + getattr(invoice, onchange_method)() + for field in changed_fields: + if field not in vals and invoice[field]: + vals[field] = invoice._fields[field].convert_to_write(invoice[field]) if not vals.get('account_id',False): raise UserError(_('Configuration error!\nCould not find any account to create the invoice, are you sure you have a chart of account installed?')) - return super(AccountInvoice, self.with_context(mail_create_nolog=True)).create(vals) + + invoice = super(AccountInvoice, self.with_context(mail_create_nolog=True)).create(vals) + + if any(line.invoice_line_tax_ids for line in invoice.invoice_line_ids) and not invoice.tax_line_ids: + invoice.compute_taxes() + + return invoice @api.model def fields_view_get(self, view_id=None, view_type=False, toolbar=False, submenu=False):