From 344a28a524fe1112f4da3448ce4dd39174a62ddc Mon Sep 17 00:00:00 2001 From: eLBati Date: Fri, 29 Apr 2016 12:23:48 +0200 Subject: [PATCH] [FIX+IMP] account_tax_balance: * Tests * PEP8 * Use invoice._convert_to_write(invoice._cache). This way, the onchange will be inheritable and will add here also the added values * better get_context_values * unify method compute_balance * open move lines linked to balance --- account_tax_balance/README.rst | 3 +- account_tax_balance/__openerp__.py | 3 +- account_tax_balance/models/account_tax.py | 105 ++++++++------- account_tax_balance/tests/__init__.py | 7 +- .../tests/test_account_tax_balance.py | 126 ++++++++++++------ .../views/account_tax_view.xml | 6 +- .../wizard/open_tax_balances_view.xml | 6 +- 7 files changed, 157 insertions(+), 99 deletions(-) diff --git a/account_tax_balance/README.rst b/account_tax_balance/README.rst index 8b9b2905..ba3acddb 100644 --- a/account_tax_balance/README.rst +++ b/account_tax_balance/README.rst @@ -12,7 +12,7 @@ It depends on date_range module and exposes 'compute' methods that can be called Usage ===== -Accounting --> Reporting --> Open Tax Balances +Accounting --> Reporting --> Taxes Balance Select the company, the date range, the target moves and 'open taxes' @@ -42,6 +42,7 @@ Contributors ------------ * Lorenzo Battistini +* Giovanni Capalbo Maintainer ---------- diff --git a/account_tax_balance/__openerp__.py b/account_tax_balance/__openerp__.py index 14f7df88..c1077042 100644 --- a/account_tax_balance/__openerp__.py +++ b/account_tax_balance/__openerp__.py @@ -7,7 +7,8 @@ "version": "9.0.1.0.0", "category": "Accounting & Finance", "website": "https://www.agilebg.com/", - "author": "Agile Business Group, Odoo Community Association (OCA)", + "author": "Agile Business Group, Therp BV, " + "Odoo Community Association (OCA)", "license": "AGPL-3", "application": False, "installable": True, diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index fbcfbce2..2a0c149e 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -2,7 +2,7 @@ # © 2016 Lorenzo Battistini - Agile Business Group # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp import models, fields +from openerp import models, fields, api class AccountTax(models.Model): @@ -13,31 +13,18 @@ class AccountTax(models.Model): string="Base Balance", compute="_compute_balance") def get_context_values(self): - if not self.env.context.get('from_date'): - from_date = fields.Date.context_today(self) - else: - from_date = self.env.context['from_date'] - if not self.env.context.get('to_date'): - to_date = fields.Date.context_today(self) - else: - to_date = self.env.context['to_date'] - if not self.env.context.get('target_move'): - target_move = 'posted' - else: - target_move = self.env.context['target_move'] - if not self.env.context.get('company_id'): - company_id = self.env.user.company_id.id - else: - company_id = self.env.context['company_id'] - return from_date, to_date, company_id, target_move + context = self.env.context + return ( + context.get('from_date', fields.Date.context_today(self)), + context.get('to_date', fields.Date.context_today(self)), + context.get('company_id', self.env.user.company_id.id), + context.get('target_move', 'posted') + ) def _compute_balance(self): - from_date, to_date, company_id, target_move = self.get_context_values() for tax in self: - tax.balance = tax.compute_balance( - from_date, to_date, company_id, target_move) - tax.base_balance = tax.compute_base_balance( - from_date, to_date, company_id, target_move) + tax.balance = tax.compute_balance(tax_or_base='tax') + tax.base_balance = tax.compute_balance(tax_or_base='base') def get_target_state_list(self, target_move="posted"): if target_move == 'posted': @@ -48,39 +35,63 @@ class AccountTax(models.Model): state = [] return state - def get_move_line_domain(self, from_date, to_date, company_id): + def get_move_line_partial_domain(self, from_date, to_date, company_id): return [ ('date', '<=', to_date), ('date', '>=', from_date), ('company_id', '=', company_id), ] - def compute_balance( - self, from_date, to_date, company_id, target_move="posted" - ): + def compute_balance(self, tax_or_base='tax'): self.ensure_one() - move_line_model = self.env['account.move.line'] - state_list = self.get_target_state_list(target_move) - domain = self.get_move_line_domain(from_date, to_date, company_id) - domain.extend([ + move_lines = self.get_move_lines_domain(tax_or_base=tax_or_base) + # balance is debit - credit whereas on tax return you want to see what + # vat has to be paid so: + # VAT on sales (credit) - VAT on purchases (debit). + total = -sum([l.balance for l in move_lines]) + return total + + def get_balance_domain(self, state_list): + return [ ('move_id.state', 'in', state_list), ('tax_line_id', '=', self.id), - ]) - move_lines = move_line_model.search(domain) - total = sum([l.balance for l in move_lines]) - return total + ] - def compute_base_balance( - self, from_date, to_date, company_id, target_move="posted" - ): - self.ensure_one() - move_line_model = self.env['account.move.line'] - state_list = self.get_target_state_list(target_move) - domain = self.get_move_line_domain(from_date, to_date, company_id) - domain.extend([ + def get_base_balance_domain(self, state_list): + return [ ('move_id.state', 'in', state_list), ('tax_ids', 'in', self.id), - ]) - move_lines = move_line_model.search(domain) - total = sum([l.balance for l in move_lines]) - return total + ] + + def get_move_lines_domain(self, tax_or_base='tax'): + move_line_model = self.env['account.move.line'] + from_date, to_date, company_id, target_move = self.get_context_values() + state_list = self.get_target_state_list(target_move) + domain = self.get_move_line_partial_domain( + from_date, to_date, company_id) + balance_domain = [] + if tax_or_base == 'tax': + balance_domain = self.get_balance_domain(state_list) + elif tax_or_base == 'base': + balance_domain = self.get_base_balance_domain(state_list) + domain.extend(balance_domain) + return move_line_model.search(domain) + + def get_lines_action(self, tax_or_base='tax'): + move_lines = self.get_move_lines_domain(tax_or_base=tax_or_base) + move_line_ids = [l.id for l in move_lines] + action = self.env.ref('account.action_account_moves_all_tree') + vals = action.read()[0] + vals['context'] = {} + vals['domain'] = [('id', 'in', move_line_ids)] + return vals + + @api.multi + def view_tax_lines(self): + self.ensure_one() + return self.get_lines_action(tax_or_base='tax') + + @api.multi + def view_base_lines(self): + self.ensure_one() + return self.get_lines_action(tax_or_base='base') diff --git a/account_tax_balance/tests/__init__.py b/account_tax_balance/tests/__init__.py index 7094e61d..b1bcc511 100644 --- a/account_tax_balance/tests/__init__.py +++ b/account_tax_balance/tests/__init__.py @@ -1,3 +1,6 @@ -#Accounting tests extending AccountingTestCase +# -*- coding: utf-8 -*- +# © 2016 Lorenzo Battistini - Agile Business Group +# © 2016 Giovanni Capalbo +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from . import test_account_tax_balance +from . import test_account_tax_balance diff --git a/account_tax_balance/tests/test_account_tax_balance.py b/account_tax_balance/tests/test_account_tax_balance.py index 9c7cd74a..15d0382d 100644 --- a/account_tax_balance/tests/test_account_tax_balance.py +++ b/account_tax_balance/tests/test_account_tax_balance.py @@ -1,51 +1,54 @@ -#from openerp.addons.account.tests.account_test_users import AccountTestUsers +# -*- coding: utf-8 -*- +# © 2016 Lorenzo Battistini - Agile Business Group +# © 2016 Giovanni Capalbo +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + from openerp.tests.common import TransactionCase -from openerp.tools import float_compare - +from datetime import datetime +from dateutil.rrule import MONTHLY + class TestAccountTaxBalance(TransactionCase): - - def setUp(self): - super(TestAccountTaxBalance, self).setUp() - self.fixed_tax = self.tax_model.create({ - 'name': "Fixed tax", - 'amount_type': 'fixed', - 'amount': 10.0, - 'sequence': 1, - }) - self.fixed_tax_bis = self.tax_model.create({ - 'name': "Fixed tax bis", - 'amount_type': 'fixed', - 'amount': 15, - 'sequence': 2, - }) - self.percent_tax = self.tax_model.create({ - 'name': "Percent tax", - 'amount_type': 'percent', - 'amount': 10.0, - 'sequence': 3, - }) - self.bank_journal = self.env['account.journal'].search([('type', '=', 'bank'), ('company_id', '=', self.account_manager.company_id.id)])[0] - self.bank_account = self.bank_journal.default_debit_account_id - self.expense_account = self.env['account.account'].search([('user_type_id.type', '=', 'payable')], limit=1) #Should be done by onchange later + def setUp(self): + super(TestAccountTaxBalance, self).setUp() + self.range_type = self.env['date.range.type'].create( + {'name': 'Fiscal year', + 'company_id': False, + 'allow_overlap': False}) + self.range_generator = self.env['date.range.generator'] + self.current_year = datetime.now().year + self.current_month = datetime.now().month + range_generator = self.range_generator.create({ + 'date_start': '%s-01-01' % self.current_year, + 'name_prefix': '%s-' % self.current_year, + 'type_id': self.range_type.id, + 'duration_count': 1, + 'unit_of_time': MONTHLY, + 'count': 12}) + range_generator.action_apply() + self.range = self.env['date.range'] def test_tax_balance(self): - company_id = self.env['res.users'].browse(self.env.uid).company_id.id + tax_account_id = self.env['account.account'].search( + [('name', '=', 'Tax Paid')], limit=1).id tax = self.env['account.tax'].create({ 'name': 'Tax 10.0', 'amount': 10.0, 'amount_type': 'fixed', + 'account_id': tax_account_id, }) - analytic_account = self.env['account.analytic.account'].create({ - 'name': 'test account', - }) - invoice_account = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_receivable').id)], limit=1).id - invoice_line_account = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_expenses').id)], limit=1).id + invoice_account_id = self.env['account.account'].search( + [('user_type_id', '=', self.env.ref( + 'account.data_account_type_receivable' + ).id)], limit=1).id + invoice_line_account_id = self.env['account.account'].search( + [('user_type_id', '=', self.env.ref( + 'account.data_account_type_expenses').id)], limit=1).id invoice = self.env['account.invoice'].create({ 'partner_id': self.env.ref('base.res_partner_2').id, - 'account_id': invoice_account, - 'type': 'in_invoice', + 'account_id': invoice_account_id, + 'type': 'out_invoice', }) self.env['account.invoice.line'].create({ @@ -54,19 +57,56 @@ class TestAccountTaxBalance(TransactionCase): 'price_unit': 100.0, 'invoice_id': invoice.id, 'name': 'product that cost 100', - 'account_id': invoice_line_account, + 'account_id': invoice_line_account_id, 'invoice_line_tax_ids': [(6, 0, [tax.id])], - 'account_analytic_id': analytic_account.id, }) + invoice._onchange_invoice_line_ids() + invoice._convert_to_write(invoice._cache) + self.assertEqual(invoice.state, 'draft') - # : check that Initially supplier bill state is "Draft" - self.assertTrue((invoice.state == 'draft'), "Initially vendor bill state is Draft") - - #change the state of invoice to open by clicking Validate button + # change the state of invoice to open by clicking Validate button invoice.signal_workflow('invoice_open') - + self.assertEquals(tax.base_balance, 100) self.assertEquals(tax.balance, 10) - + # testing wizard + current_range = self.range.search([ + ('date_start', '=', '%s-%s-01' % ( + self.current_year, self.current_month)) + ]) + wizard = self.env['wizard.open.tax.balances'].new({}) + self.assertFalse(wizard.from_date) + self.assertFalse(wizard.to_date) + wizard = self.env['wizard.open.tax.balances'].new({ + 'date_range_id': current_range[0].id, + }) + wizard.onchange_date_range_id() + wizard._convert_to_write(wizard._cache) + action = wizard.open_taxes() + self.assertEqual( + action['context']['from_date'], current_range[0].date_start) + self.assertEqual( + action['context']['to_date'], current_range[0].date_end) + self.assertEqual( + action['xml_id'], 'account_tax_balance.action_tax_balances_tree') + + # testing buttons + tax_action = tax.view_tax_lines() + base_action = tax.view_base_lines() + self.assertTrue( + tax_action['domain'][0][2][0] in + [l.id for l in invoice.move_id.line_ids]) + self.assertEqual( + tax_action['xml_id'], 'account.action_account_moves_all_tree') + self.assertTrue( + base_action['domain'][0][2][0] in + [l.id for l in invoice.move_id.line_ids]) + self.assertEqual( + base_action['xml_id'], 'account.action_account_moves_all_tree') + # test specific method + state_list = tax.get_target_state_list(target_move='all') + self.assertEqual(state_list, ['posted', 'draft']) + state_list = tax.get_target_state_list(target_move='whatever') + self.assertEqual(state_list, []) diff --git a/account_tax_balance/views/account_tax_view.xml b/account_tax_balance/views/account_tax_view.xml index a204056d..e3f097cf 100644 --- a/account_tax_balance/views/account_tax_view.xml +++ b/account_tax_balance/views/account_tax_view.xml @@ -13,6 +13,8 @@ + + @@ -36,7 +38,7 @@ - Tax Balances + Taxes Balance account.tax form tree @@ -44,4 +46,4 @@ - \ No newline at end of file + diff --git a/account_tax_balance/wizard/open_tax_balances_view.xml b/account_tax_balance/wizard/open_tax_balances_view.xml index fed17d33..39446320 100644 --- a/account_tax_balance/wizard/open_tax_balances_view.xml +++ b/account_tax_balance/wizard/open_tax_balances_view.xml @@ -5,7 +5,7 @@ wizard_open_tax_balances wizard.open.tax.balances -
+ @@ -23,7 +23,7 @@ - Open Tax Balances + Taxes Balance wizard.open.tax.balances form form @@ -37,4 +37,4 @@ parent="account.menu_finance_reports" groups="account.group_account_user,account.group_account_manager"> - \ No newline at end of file +