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.

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