Skip to content

Commit

Permalink
[FIX] tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Jan 9, 2017
1 parent 4621566 commit d03bf73
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 48 deletions.
88 changes: 54 additions & 34 deletions account_invoice_import/wizard/account_invoice_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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'),
Expand All @@ -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 = {}
Expand All @@ -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(
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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'),
Expand All @@ -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'),
}
Expand Down
16 changes: 11 additions & 5 deletions account_invoice_import_invoice2data/tests/test_invoice_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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]
Expand Down Expand Up @@ -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,
})

Expand All @@ -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]
Expand Down
11 changes: 6 additions & 5 deletions base_business_document_import/models/business_document_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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']))
Expand All @@ -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):
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
})
Expand All @@ -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',
Expand Down

0 comments on commit d03bf73

Please sign in to comment.