[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
This commit is contained in:
Denis Ledoux
2016-05-23 17:11:17 +02:00
parent 68c6a51bfe
commit 0d929dc8fa
+18 -1
View File
@@ -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):