Skip to content

Commit

Permalink
Add view and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lasley committed Dec 11, 2017
1 parent bfac98f commit 587b4fb
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 145 deletions.
1 change: 1 addition & 0 deletions base_tax_connector/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"installable": True,
"data": [
'security/ir.model.access.csv',
'views/account_tax_transaction_view.xml',
],
"depends": [
"account",
Expand Down
10 changes: 8 additions & 2 deletions base_tax_connector/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

import logging

from odoo import api, models

_logger = logging.getLogger(__name__)


class AccountInvoice(models.Model):

_inherit = 'account.invoice'

@api.multi
def invoice_validate(self):
def action_move_create(self):
"""Add tax purchase/refund logic into invoice validation."""
res = super(AccountInvoice, self).invoice_validate()
res = super(AccountInvoice, self).action_move_create()
for invoice in self:
if 'refund' in invoice.type:
method = self.env['account.tax.group'].invoice_tax_refund
else:
method = self.env['account.tax.group'].invoice_tax_purchase
_logger.info('Running %s on %s with type %s',
method, invoice, invoice.type)
method(invoice)
return res
13 changes: 6 additions & 7 deletions base_tax_connector/models/account_tax_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

from odoo import api, models, fields, _
from odoo.exceptions import ValidationError
from odoo import api, models, fields


class AccountTaxTransaction(models.Model):

_name = 'account.tax.transaction'

line_ids = fields.One2many(
string='Lines',
string='Transaction Lines',
comodel_name='account.tax.transaction.line',
inverse_name='transaction_id',
)
Expand Down Expand Up @@ -45,7 +44,7 @@ class AccountTaxTransaction(models.Model):
compute='_compute_company_id',
store=True,
)
date = fields.Datetime(
date = fields.Date(
compute='_compute_date',
store=True,
)
Expand Down Expand Up @@ -81,13 +80,13 @@ def _compute_invoice_line_ids(self):
@api.depends('invoice_line_ids.partner_id')
def _compute_partner_id(self):
for record in self:
record.partner_id = self.invoice_line_ids[:1].partner_id.id
record.partner_id = self.invoice_line_ids[:1].invoice_id.partner_id.id

@api.multi
@api.depends('invoice_line_ids.company_id')
@api.depends('invoice_line_ids.invoice_id.company_id')
def _compute_company_id(self):
for record in self:
record.company_id = self.invoice_line_ids[:1].company_id.id
record.company_id = self.invoice_line_ids[:1].invoice_id.company_id.id

@api.multi
@api.depends('invoice_line_ids.invoice_id.date')
Expand Down
14 changes: 7 additions & 7 deletions base_tax_connector/models/account_tax_transaction_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ def _check_line_line_ids(self):

for record in self:

if record._check_values_equal('partner_id'):
if record._check_invoice_values_aligned('partner_id'):
raise ValidationError(base_error % 'partners')

if record._check_values_equal('company_id'):
if record._check_invoice_values_aligned('company_id'):
raise ValidationError(base_error % 'companies')

if record._check_values_equal('date'):
if record._check_invoice_values_aligned('date'):
raise ValidationError(base_error % 'dates')

@api.model
Expand Down Expand Up @@ -129,7 +129,7 @@ def get_values_refund(self, account_invoice_tax):
))

purchase = self.search([
('invoice_id', '=', original_invoice.id),
('invoice_line_ids', 'in', original_invoice.invoice_line_ids.ids),
('tax_id', '=', account_invoice_tax.tax_id.id),
('account_analytic_id', '=',
account_invoice_tax.account_analytic_id.id),
Expand Down Expand Up @@ -159,10 +159,10 @@ def _get_invoice_lines_for_tax(self, tax, invoice):
@api.multi
def _check_invoice_values_aligned(self, attribute_name):
self.ensure_one()
invoices_all = self.transaction_id.mapped(
'line_ids.invoice_line_ids.invoice_id',
invoices_all = self.transaction_id.line_ids.mapped(
'invoice_line_ids.invoice_id',
)
check_value = getattr(self, attribute_name)
check_value = getattr(self.transaction_id, attribute_name)
invoices_non_equal = invoices_all.filtered(
lambda r: getattr(r, attribute_name) != check_value
)
Expand Down
109 changes: 87 additions & 22 deletions base_tax_connector/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,89 @@ class StopTestException(Exception):

class TestCommon(TransactionCase):

def _create_partner(self, create_fiscal_position=True):
def setUp(self):
super(TestCommon, self).setUp()
account_type = self.env['account.account.type'].create({
'name': 'test',
})
self.account = self.env['account.account'].create({
'code': 'TEST',
'company_id': self.env.user.company_id.id,
'name': 'Test',
'user_type_id': account_type.id,
})
self.journal = self.env['account.journal'].create({
'name': 'Journal',
'company_id': self.env.user.company_id.id,
'code': 'journal',
'type': 'sale',
})
self.state = self.env.ref('base.state_us_23')
self.fiscal_position = self.env['account.fiscal.position'].create({
'name': 'Test',
'country_id': self.state.country_id.id,
'state_ids': [(6, 0, self.state.ids)],
})
# This is used to circumvent unique constrains in names.
self.counter = 0

def _increment_counter(self):
self.counter += 1

def _create_partner(self):
self._increment_counter()
partner = self.env['res.partner'].create({
'name': 'City of Henderson',
'name': 'City of Henderson %d' % self.counter,
'street': '240 S Water St.',
'zip': '89015',
'state_id': self.env.ref('base.state_us_23').id,
'state_id': self.state.id,
'country_id': self.state.country_id.id,
'property_account_receivable_id': self.account.id,
'property_account_payable_id': self.account.id,
'company_id': self.env.user.company_id.id,
})
if create_fiscal_position:
self.env['account.fiscal.position'].create({
'name': 'Test',
'country_id': partner.state_id.country_id.id,
'state_ids': [(6, 0, partner.state_id.ids)],
})
return partner

def _create_tax(self, scope='none', tax_group=None):
def _create_tax(self, scope='sale', tax_group=None, make_default=True):
self._increment_counter()
if not tax_group:
tax_group = self._create_tax_group()
return self.env['account.tax'].create({
'name': 'Test Tax',
tax = self.env['account.tax'].create({
'name': 'Test Tax %d' % self.counter,
'type_tax_use': scope,
'amount_type': 'cache',
'amount': 0,
'tax_group_id': tax_group.id,
'company_id': self.env.user.company_id.id,
})
if make_default:
settings = self.env['account.config.settings'].create({
'default_sale_tax_id': tax.id,
})
settings.execute()
return tax

def _create_tax_group(self, name='Test Group'):
return self.env['account.tax.group'].create({
'name': name,
})

def _create_product(self, tax=None):
self._increment_counter()
if tax is None:
tax = self._create_tax()
return self.env['product.product'].create({
'name': 'Test Product',
product = self.env['product.product'].create({
'name': 'Test Product %d' % self.counter,
'invoice_policy': 'order',
'taxes_id': [(6, 0, tax.ids)],
})
return product

def _create_sale(self, taxable=True):
partner = self._create_partner(taxable)
product = self._create_product()
def _create_sale(self, tax=None, confirmed=True):
if tax is None:
tax = self._create_tax()
partner = self._create_partner()
product = self._create_product(tax)
sale = self.env['sale.order'].create({
'partner_id': partner.id,
'order_line': [(0, 0, {
Expand All @@ -65,14 +106,38 @@ def _create_sale(self, taxable=True):
'price_unit': 79.00,
'name': product.display_name,
'customer_lead': 0.00,
'tax_id': [(6, 0, tax.ids)],
})]
})
if confirmed:
sale.action_confirm()
return sale

def _create_invoice(self, taxable=True):
sale = self._create_sale(taxable)
invoice_ids = sale.action_invoice_create()
return self.env['account.invoice'].browse(invoice_ids)
def _create_invoice(self, tax=None):
if tax is None:
tax = self._create_tax()
partner = self._create_partner()
product = self._create_product(tax)
invoice = self.env['account.invoice'].create({
'partner_id': partner.id,
'user_id': self.env.ref('base.user_demo').id,
'reference_type': 'none',
'account_id': self.account.id,
'journal_id': self.journal.id,
'fiscal_position_id': self.fiscal_position.id,
'payment_term_id':
self.env.ref('account.account_payment_term').id,
'type': 'out_invoice',
'invoice_line_ids': [(0, 0, {
'product_id': product.id,
'quantity': 1,
'price_unit': 79.00,
'name': product.display_name,
'account_id': self.account.id,
'invoice_line_tax_ids': [(6, 0, tax.ids)],
})]
})
return invoice

def _get_rate(self, reference=None):
if reference is None:
Expand All @@ -85,7 +150,7 @@ def _get_rate(self, reference=None):

def _get_transaction_for_invoice(self, invoice):
return self.env['account.tax.transaction'].search([
('invoice_id', '=', invoice.id),
('invoice_line_ids', 'in', invoice.invoice_line_ids.ids),
])

def _stop_test_mock(self):
Expand Down
17 changes: 10 additions & 7 deletions base_tax_connector/tests/test_account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@

class TestAccountInvoice(TestCommon):

def test_invoice_validate_purchase(self):
def test_action_invoice_open_purchase(self):
"""Invoice validation should trigger a tax purchase."""
invoice = self._create_invoice()
self.assertFalse(self._get_transaction_for_invoice(invoice))
invoice.invoice_validate()
invoice.action_invoice_open()
transaction = self._get_transaction_for_invoice(invoice)
self.assertTrue(transaction)
self.assertFalse(transaction.parent_id)
self.assertFalse(transaction.mapped('line_ids.parent_id'))
return invoice

def test_invoice_validate_refund(self):
def test_action_invoice_open_refund(self):
"""Refund invoice validation should trigger a tax refund."""
purchase = self._create_invoice()
purchase = self.test_action_invoice_open_purchase()
refund = purchase.refund()
self.assertFalse(self._get_transaction_for_invoice(refund))
refund.invoice_validate()
refund.action_invoice_open()
transaction = self._get_transaction_for_invoice(refund)
self.assertTrue(transaction)
self.assertEqual(transaction.parent_id, purchase)
self.assertEqual(transaction.mapped('line_ids.parent_id'),
self._get_transaction_for_invoice(purchase).line_ids,
)
Loading

0 comments on commit 587b4fb

Please sign in to comment.