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.

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