diff --git a/account_invoice_import/wizard/account_invoice_import.py b/account_invoice_import/wizard/account_invoice_import.py index bd9be65a9f..623f1663ea 100644 --- a/account_invoice_import/wizard/account_invoice_import.py +++ b/account_invoice_import/wizard/account_invoice_import.py @@ -117,7 +117,6 @@ def fallback_parse_pdf_invoice(self, file_data): @api.model def _prepare_create_invoice_vals(self, parsed_inv): aio = self.env['account.invoice'] - ailo = self.env['account.invoice.line'] bdio = self.env['business.document.import'] rpo = self.env['res.partner'] company = self.env.user.company_id @@ -132,16 +131,19 @@ def _prepare_create_invoice_vals(self, parsed_inv): 'currency_id': currency.id, 'type': parsed_inv['type'], 'company_id': company.id, - 'supplier_invoice_number': - parsed_inv.get('invoice_number'), + 'reference': parsed_inv.get('invoice_number'), 'date_invoice': parsed_inv.get('date'), 'journal_id': aio.with_context(type='in_invoice')._default_journal().id, 'invoice_line': [], 'check_total': parsed_inv.get('amount_total'), - } - vals.update(aio.onchange_partner_id( - 'in_invoice', partner.id, company_id=company.id)['value']) + } + new_invoice = aio.new(vals) + new_invoice._onchange_partner_id() + vals.update({ + f: aio._fields[f].convert_to_write(new_invoice) + for f in new_invoice._cache + }) # Force due date of the invoice if parsed_inv.get('date_due'): vals['date_due'] = parsed_inv.get('date_due') @@ -163,11 +165,10 @@ def _prepare_create_invoice_vals(self, parsed_inv): } elif config.invoice_line_method == '1line_static_product': product = config.static_product_id - il_vals = ailo.product_id_change( - product.id, product.uom_id.id, type='in_invoice', - partner_id=partner.id, - fposition_id=partner.property_account_position.id, - company_id=company.id)['value'] + il_vals = self._amend_create_invoice_line_vals( + product, 'in_invoice', partner, + partner.property_account_position_id, company + ) il_vals.update({ 'product_id': product.id, 'price_unit': parsed_inv.get('amount_untaxed'), @@ -193,11 +194,10 @@ def _prepare_create_invoice_vals(self, parsed_inv): } elif config.invoice_line_method == 'nline_static_product': sproduct = config.static_product_id - static_vals = ailo.product_id_change( - sproduct.id, sproduct.uom_id.id, type='in_invoice', - partner_id=partner.id, - fposition_id=partner.property_account_position.id, - company_id=company.id)['value'] + static_vals = self._amend_create_invoice_line_vals( + sproduct, 'in_invoice', partner, + partner.property_account_position_id, company + ) static_vals['product_id'] = sproduct.id else: static_vals = {} @@ -207,13 +207,12 @@ def _prepare_create_invoice_vals(self, parsed_inv): product = bdio._match_product( line['product'], parsed_inv['chatter_msg'], seller=partner) - fposition_id = partner.property_account_position.id il_vals.update( - ailo.product_id_change( - product.id, product.uom_id.id, type='in_invoice', - partner_id=partner.id, - fposition_id=fposition_id, - company_id=company.id)['value']) + self._amend_create_invoice_line_vals( + product, 'in_invoice', partner, + partner.property_account_position_id, company + ) + ) il_vals['product_id'] = product.id elif config.invoice_line_method == 'nline_no_product': taxes = bdio._match_taxes( @@ -242,6 +241,26 @@ def _prepare_create_invoice_vals(self, parsed_inv): line_dict['account_analytic_id'] = aacount_id return vals + @api.model + def _amend_create_invoice_line_vals( + self, product, invoice_type, partner, fposition, company + ): + new_invoice = self.env['account.invoice'].new({ + 'type': invoice_type, + 'partner_id': partner, + 'fiscal_position_id': fposition, + 'company_id': company, + }) + new_invoice_line = self.env['account.invoice.line'].new({ + 'invoice_id': new_invoice, + 'product_id': product, + 'uom_id': product.uom_id + }) + return { + f: new_invoice_line._fields[f].convert_to_write(new_invoice_line) + for f in new_invoice_line._cache + } + @api.model def set_1line_price_unit_and_quantity(self, il_vals, parsed_inv): """For the moment, we only take into account the 'price_include' @@ -367,9 +386,8 @@ def import_invoice(self): existing_invs = aio.search( domain + [( - 'supplier_invoice_number', - '=ilike', - parsed_inv.get('invoice_number'))]) + 'reference', '=ilike', parsed_inv.get('invoice_number') + )]) if existing_invs: raise UserError(_( "This invoice already exists in Odoo. It's " @@ -518,13 +536,16 @@ def update_invoice_lines(self, parsed_inv, invoice, seller): @api.model def _prepare_create_invoice_line(self, product, uom, import_line, invoice): - ailo = self.env['account.invoice.line'] - vals = ailo.product_id_change( - product.id, uom.id, qty=import_line['qty'], type='in_invoice', - partner_id=invoice.partner_id.id, - fposition_id=invoice.fiscal_position.id or False, - currency_id=invoice.currency_id.id, - company_id=invoice.company_id.id)['value'] + new_line = self.env['account.invoice.line'].new({ + 'invoice_id': invoice, + 'qty': import_line['qty'], + 'product_id': product, + }) + new_line._onchange_product_id() + vals = { + f: new_line._fields[f].convert_to_write(new_line) + for f in new_line._cache + } vals.update({ 'product_id': product.id, 'price_unit': import_line.get('price_unit'), @@ -537,8 +558,7 @@ def _prepare_create_invoice_line(self, product, uom, import_line, invoice): def _prepare_update_invoice_vals(self, parsed_inv, partner): bdio = self.env['business.document.import'] vals = { - 'supplier_invoice_number': - parsed_inv.get('invoice_number'), + 'reference': parsed_inv.get('invoice_number'), 'date_invoice': parsed_inv.get('date'), 'check_total': parsed_inv.get('amount_total'), } diff --git a/account_invoice_import_invoice2data/tests/test_invoice_import.py b/account_invoice_import_invoice2data/tests/test_invoice_import.py index 1b42f9a908..57f76542a8 100644 --- a/account_invoice_import_invoice2data/tests/test_invoice_import.py +++ b/account_invoice_import_invoice2data/tests/test_invoice_import.py @@ -17,8 +17,14 @@ def setUp(self): 'description': 'FR-VAT-buy-20.0', 'amount': 0.2, 'type': 'percent', - 'account_collected_id': self.env.ref('account.a_expense').id, - 'account_paid_id': self.env.ref('account.a_expense').id, + 'account_collected_id': self.env['account.account'].search([ + ('user_type_id', '=', + self.env.ref('account.data_account_type_expenses').id) + ], limit=1).id, + 'account_paid_id': self.env['account.account'].search([ + ('user_type_id', '=', + self.env.ref('account.data_account_type_expenses').id) + ], limit=1).id, 'base_sign': -1, 'tax_sign': -1, 'type_tax_use': 'purchase', @@ -43,7 +49,7 @@ def test_import_free_invoice(self): invoices = self.env['account.invoice'].search([ ('state', '=', 'draft'), ('type', '=', 'in_invoice'), - ('supplier_invoice_number', '=', '562044387') + ('reference', '=', '562044387') ]) self.assertEquals(len(invoices), 1) inv = invoices[0] @@ -76,7 +82,7 @@ def test_import_free_invoice(self): # (we re-use the invoice created by the first import !) inv.write({ 'date_invoice': False, - 'supplier_invoice_number': False, + 'reference': False, 'check_total': False, }) @@ -99,7 +105,7 @@ def test_import_free_invoice(self): invoices = self.env['account.invoice'].search([ ('state', '=', 'draft'), ('type', '=', 'in_invoice'), - ('supplier_invoice_number', '=', '562044387') + ('reference', '=', '562044387') ]) self.assertEquals(len(invoices), 1) inv = invoices[0] diff --git a/base_business_document_import/models/business_document_import.py b/base_business_document_import/models/business_document_import.py index 2aa1e91068..ee86b11cf2 100644 --- a/base_business_document_import/models/business_document_import.py +++ b/base_business_document_import/models/business_document_import.py @@ -465,7 +465,7 @@ def _match_tax( type_tax_use='purchase', price_include=False): """Example: tax_dict = { - 'type': 'percent', # required param, 'fixed' or 'percent' + 'amount_type': 'percent', # required param, 'fixed' or 'percent' 'amount': 20.0, # required 'unece_type_code': 'VAT', 'unece_categ_code': 'S', @@ -491,9 +491,10 @@ def _match_tax( domain.append(('price_include', '=', True)) # with the code abose, if you set price_include=None, it will # won't depend on the value of the price_include parameter - assert tax_dict.get('type') in ['fixed', 'percent'], 'bad tax type' + assert tax_dict.get('amount_type') in ['fixed', 'percent'],\ + 'bad tax type' assert 'amount' in tax_dict, 'Missing amount key in tax_dict' - domain.append(('type', '=', tax_dict['type'])) + domain.append(('amount_type', '=', tax_dict['amount_type'])) if tax_dict.get('unece_type_code'): domain.append( ('unece_type_code', '=', tax_dict['unece_type_code'])) @@ -503,7 +504,7 @@ def _match_tax( taxes = ato.search(domain) for tax in taxes: tax_amount = tax.amount - if tax_dict['type'] == 'percent': + if tax_dict['amount_type'] == 'percent': tax_amount *= 100 if not float_compare( tax_dict['amount'], tax_amount, precision_digits=prec): @@ -520,7 +521,7 @@ def _match_tax( tax_dict.get('unece_type_code'), tax_dict.get('unece_categ_code'), tax_dict['amount'], - tax_dict['type'] == 'percent' and '%' or _('(fixed)'))) + tax_dict['amount_type'] == 'percent' and '%' or _('(fixed)'))) def compare_lines( self, existing_lines, import_lines, chatter_msg, diff --git a/base_business_document_import/tests/test_business_document_import.py b/base_business_document_import/tests/test_business_document_import.py index a8f846fe77..4a087b43f2 100644 --- a/base_business_document_import/tests/test_business_document_import.py +++ b/base_business_document_import/tests/test_business_document_import.py @@ -56,7 +56,7 @@ def test_match_shipping_partner(self): 'name': u'Sébastien BEAU', 'email': 'sebastien.beau@akretion.com', 'use_parent_address': True, - 'type': 'default', + 'type': 'contact', }) cpartner3 = rpo.create({ 'parent_id': partner1.id, @@ -169,7 +169,7 @@ def test_match_tax(self): 'type_tax_use': 'purchase', 'price_include': False, 'amount': 0.18, - 'type': 'percent', + 'amount_type': 'percent', 'unece_type_id': self.env.ref('account_tax_unece.tax_type_vat').id, 'unece_categ_id': self.env.ref('account_tax_unece.tax_categ_s').id, }) @@ -179,13 +179,13 @@ def test_match_tax(self): 'type_tax_use': 'purchase', 'price_include': True, 'amount': 0.18, - 'type': 'percent', + 'amount_type': 'percent', 'unece_type_id': self.env.ref('account_tax_unece.tax_type_vat').id, 'unece_categ_id': self.env.ref('account_tax_unece.tax_categ_s').id, }) bdio = self.env['business.document.import'] tax_dict = { - 'type': 'percent', + 'amount_type': 'percent', 'amount': 18, 'unece_type_code': 'VAT', 'unece_categ_code': 'S',