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.

171 lines
6.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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.signal_workflow('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. # testing buttons
  90. tax_action = tax.view_tax_lines()
  91. base_action = tax.view_base_lines()
  92. self.assertTrue(
  93. tax_action['domain'][0][2][0] in
  94. [l.id for l in invoice.move_id.line_ids])
  95. self.assertEqual(
  96. tax_action['xml_id'], 'account.action_account_moves_all_tree')
  97. self.assertTrue(
  98. base_action['domain'][0][2][0] in
  99. [l.id for l in invoice.move_id.line_ids])
  100. self.assertEqual(
  101. base_action['xml_id'], 'account.action_account_moves_all_tree')
  102. # test specific method
  103. state_list = tax.get_target_state_list(target_move='all')
  104. self.assertEqual(state_list, ['posted', 'draft'])
  105. state_list = tax.get_target_state_list(target_move='whatever')
  106. self.assertEqual(state_list, [])
  107. refund = self.env['account.invoice'].create({
  108. 'partner_id': self.env.ref('base.res_partner_2').id,
  109. 'account_id': invoice_account_id,
  110. 'type': 'out_refund',
  111. })
  112. self.env['account.invoice.line'].create({
  113. 'product_id': self.env.ref('product.product_product_2').id,
  114. 'quantity': 1.0,
  115. 'price_unit': 25.0,
  116. 'invoice_id': refund.id,
  117. 'name': 'returned product that cost 25',
  118. 'account_id': invoice_line_account_id,
  119. 'invoice_line_tax_ids': [(6, 0, [tax.id])],
  120. })
  121. refund._onchange_invoice_line_ids()
  122. refund._convert_to_write(invoice._cache)
  123. self.assertEqual(refund.state, 'draft')
  124. # change the state of refund to open by clicking Validate button
  125. refund.signal_workflow('invoice_open')
  126. self.assertEquals(tax.base_balance, 75.)
  127. self.assertEquals(tax.balance, 7.5)
  128. self.assertEquals(tax.base_balance_regular, 100.)
  129. self.assertEquals(tax.balance_regular, 10.)
  130. self.assertEquals(tax.base_balance_refund, -25.)
  131. self.assertEquals(tax.balance_refund, -2.5)
  132. # Taxes on liquidity type moves are included
  133. liquidity_account_id = self.env['account.account'].search(
  134. [('internal_type', '=', 'liquidity')], limit=1).id
  135. self.env['account.move'].create({
  136. 'date': Date.context_today(self.env.user),
  137. 'journal_id': self.env['account.journal'].search(
  138. [('type', '=', 'bank')], limit=1).id,
  139. 'name': 'Test move',
  140. 'line_ids': [(0, 0, {
  141. 'account_id': liquidity_account_id,
  142. 'debit': 110,
  143. 'credit': 0,
  144. 'name': 'Bank Fees',
  145. }), (0, 0, {
  146. 'account_id': invoice_line_account_id,
  147. 'debit': 0,
  148. 'credit': 100,
  149. 'name': 'Bank Fees',
  150. 'tax_ids': [(4, tax.id)]
  151. })],
  152. }).post()
  153. tax.refresh()
  154. self.assertEquals(tax.base_balance, 175.)
  155. self.assertEquals(tax.balance, 17.5)