You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
7.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2016 Lorenzo Battistini - Agile Business Group
  3. # © 2016 Giovanni Capalbo <giovanni@therp.nl>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from openerp.fields import Date
  6. from openerp.tests.common import HttpCase
  7. from datetime import datetime
  8. from dateutil.rrule import MONTHLY
  9. class TestAccountTaxBalance(HttpCase):
  10. at_install = False
  11. post_install = False
  12. def setUp(self):
  13. super(TestAccountTaxBalance, self).setUp()
  14. self.range_type = self.env['date.range.type'].create(
  15. {'name': 'Fiscal year',
  16. 'company_id': False,
  17. 'allow_overlap': False})
  18. self.range_generator = self.env['date.range.generator']
  19. self.current_year = datetime.now().year
  20. self.current_month = datetime.now().month
  21. range_generator = self.range_generator.create({
  22. 'date_start': '%s-01-01' % self.current_year,
  23. 'name_prefix': '%s-' % self.current_year,
  24. 'type_id': self.range_type.id,
  25. 'duration_count': 1,
  26. 'unit_of_time': MONTHLY,
  27. 'count': 12})
  28. range_generator.action_apply()
  29. self.range = self.env['date.range']
  30. def test_tax_balance(self):
  31. tax_account_id = self.env['account.account'].search(
  32. [('name', '=', 'Tax Paid')], limit=1).id
  33. tax = self.env['account.tax'].create({
  34. 'name': 'Tax 10.0%',
  35. 'amount': 10.0,
  36. 'amount_type': 'percent',
  37. 'account_id': tax_account_id,
  38. })
  39. invoice_account_id = self.env['account.account'].search(
  40. [('user_type_id', '=', self.env.ref(
  41. 'account.data_account_type_receivable'
  42. ).id)], limit=1).id
  43. invoice_line_account_id = self.env['account.account'].search(
  44. [('user_type_id', '=', self.env.ref(
  45. 'account.data_account_type_expenses').id)], limit=1).id
  46. invoice = self.env['account.invoice'].create({
  47. 'partner_id': self.env.ref('base.res_partner_2').id,
  48. 'account_id': invoice_account_id,
  49. 'type': 'out_invoice',
  50. })
  51. self.env['account.invoice.line'].create({
  52. 'product_id': self.env.ref('product.product_product_4').id,
  53. 'quantity': 1.0,
  54. 'price_unit': 100.0,
  55. 'invoice_id': invoice.id,
  56. 'name': 'product that cost 100',
  57. 'account_id': invoice_line_account_id,
  58. 'invoice_line_tax_ids': [(6, 0, [tax.id])],
  59. })
  60. invoice._onchange_invoice_line_ids()
  61. invoice._convert_to_write(invoice._cache)
  62. self.assertEqual(invoice.state, 'draft')
  63. # change the state of invoice to open by clicking Validate button
  64. invoice.action_invoice_open()
  65. self.assertEqual(tax.base_balance, 100.)
  66. self.assertEqual(tax.balance, 10.)
  67. self.assertEqual(tax.base_balance_regular, 100.)
  68. self.assertEqual(tax.balance_regular, 10.)
  69. self.assertEqual(tax.base_balance_refund, 0.)
  70. self.assertEqual(tax.balance_refund, 0.)
  71. # testing wizard
  72. current_range = self.range.search([
  73. ('date_start', '=', '%s-%s-01' % (
  74. self.current_year, self.current_month))
  75. ])
  76. wizard = self.env['wizard.open.tax.balances'].new({})
  77. self.assertFalse(wizard.from_date)
  78. self.assertFalse(wizard.to_date)
  79. wizard = self.env['wizard.open.tax.balances'].new({
  80. 'date_range_id': current_range[0].id,
  81. })
  82. wizard.onchange_date_range_id()
  83. wizard._convert_to_write(wizard._cache)
  84. action = wizard.open_taxes()
  85. self.assertEqual(
  86. action['context']['from_date'], current_range[0].date_start)
  87. self.assertEqual(
  88. action['context']['to_date'], current_range[0].date_end)
  89. self.assertEqual(
  90. action['xml_id'], 'account_tax_balance.action_tax_balances_tree')
  91. # exercise search has_moves = True
  92. taxes = self.env['account.tax'].search([('has_moves', '=', True)])
  93. self.assertEqual(len(taxes), 1)
  94. self.assertEqual(taxes[0].name, "Tax 10.0%")
  95. # testing buttons
  96. tax_action = tax.view_tax_lines()
  97. base_action = tax.view_base_lines()
  98. tax_action_move_lines = self.env['account.move.line'].\
  99. search(tax_action['domain'])
  100. self.assertTrue(invoice.move_id.line_ids & tax_action_move_lines)
  101. self.assertEqual(
  102. tax_action['xml_id'], 'account.action_account_moves_all_tree')
  103. base_action_move_lines = self.env['account.move.line'].\
  104. search(base_action['domain'])
  105. self.assertTrue(invoice.move_id.line_ids & base_action_move_lines)
  106. self.assertEqual(
  107. base_action['xml_id'], 'account.action_account_moves_all_tree')
  108. # test specific method
  109. state_list = tax.get_target_state_list(target_move='all')
  110. self.assertEqual(state_list, ['posted', 'draft'])
  111. state_list = tax.get_target_state_list(target_move='whatever')
  112. self.assertEqual(state_list, [])
  113. refund = self.env['account.invoice'].create({
  114. 'partner_id': self.env.ref('base.res_partner_2').id,
  115. 'account_id': invoice_account_id,
  116. 'type': 'out_refund',
  117. })
  118. self.env['account.invoice.line'].create({
  119. 'product_id': self.env.ref('product.product_product_2').id,
  120. 'quantity': 1.0,
  121. 'price_unit': 25.0,
  122. 'invoice_id': refund.id,
  123. 'name': 'returned product that cost 25',
  124. 'account_id': invoice_line_account_id,
  125. 'invoice_line_tax_ids': [(6, 0, [tax.id])],
  126. })
  127. refund._onchange_invoice_line_ids()
  128. refund._convert_to_write(invoice._cache)
  129. self.assertEqual(refund.state, 'draft')
  130. # change the state of refund to open by clicking Validate button
  131. refund.action_invoice_open()
  132. self.assertEqual(tax.base_balance, 75.)
  133. self.assertEqual(tax.balance, 7.5)
  134. self.assertEqual(tax.base_balance_regular, 100.)
  135. self.assertEqual(tax.balance_regular, 10.)
  136. self.assertEqual(tax.base_balance_refund, -25.)
  137. self.assertEqual(tax.balance_refund, -2.5)
  138. # Taxes on liquidity type moves are included
  139. liquidity_account_id = self.env['account.account'].search(
  140. [('internal_type', '=', 'liquidity')], limit=1).id
  141. self.env['account.move'].create({
  142. 'date': Date.context_today(self.env.user),
  143. 'journal_id': self.env['account.journal'].search(
  144. [('type', '=', 'bank')], limit=1).id,
  145. 'name': 'Test move',
  146. 'line_ids': [(0, 0, {
  147. 'account_id': liquidity_account_id,
  148. 'debit': 110,
  149. 'credit': 0,
  150. 'name': 'Bank Fees',
  151. }), (0, 0, {
  152. 'account_id': invoice_line_account_id,
  153. 'debit': 0,
  154. 'credit': 100,
  155. 'name': 'Bank Fees',
  156. 'tax_ids': [(4, tax.id)]
  157. }), (0, 0, {
  158. 'account_id': tax.account_id.id,
  159. 'debit': 0,
  160. 'credit': 10,
  161. 'name': 'Bank Fees',
  162. 'tax_line_id': tax.id,
  163. })],
  164. }).post()
  165. tax.refresh()
  166. self.assertEqual(tax.base_balance, 175.)
  167. self.assertEqual(tax.balance, 17.5)