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.

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