From 0d929dc8fab7e3fdcdbcfa50aae5ad3276bef440 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Mon, 23 May 2016 17:08:27 +0200 Subject: [PATCH] [FIX] account: invoice import with only partner and lines When creating a new invoice through the web interface, a bunch of "default" values are loaded once the partner chosen, thanks to an onchange on this field, such as the `account_id`, the fiscal position, etc. It was not when importing an invoice, making it more difficult to import an invoice. In addition, the taxes lines were not computed at all when importing an invoice with taxes within its invoice lines, while it should be the case if the tax lines are not forced during the import. opw-678155 --- addons/account/models/account_invoice.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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):