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.

249 lines
10 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.assertIn(tax, taxes)
  92. # testing buttons
  93. tax_action = tax.view_tax_lines()
  94. base_action = tax.view_base_lines()
  95. tax_action_move_lines = self.env['account.move.line'].\
  96. search(tax_action['domain'])
  97. self.assertTrue(invoice.move_id.line_ids & tax_action_move_lines)
  98. self.assertEqual(
  99. tax_action['xml_id'], 'account.action_account_moves_all_tree')
  100. base_action_move_lines = self.env['account.move.line'].\
  101. search(base_action['domain'])
  102. self.assertTrue(invoice.move_id.line_ids & base_action_move_lines)
  103. self.assertEqual(
  104. base_action['xml_id'], 'account.action_account_moves_all_tree')
  105. # test specific method
  106. state_list = tax.get_target_state_list(target_move='all')
  107. self.assertEqual(state_list, ['posted', 'draft'])
  108. state_list = tax.get_target_state_list(target_move='whatever')
  109. self.assertEqual(state_list, [])
  110. refund = self.env['account.invoice'].create({
  111. 'partner_id': self.env.ref('base.res_partner_2').id,
  112. 'account_id': invoice_account_id,
  113. 'type': 'out_refund',
  114. })
  115. self.env['account.invoice.line'].create({
  116. 'product_id': self.env.ref('product.product_product_2').id,
  117. 'quantity': 1.0,
  118. 'price_unit': 25.0,
  119. 'invoice_id': refund.id,
  120. 'name': 'returned product that cost 25',
  121. 'account_id': invoice_line_account_id,
  122. 'invoice_line_tax_ids': [(6, 0, [tax.id])],
  123. })
  124. refund._onchange_invoice_line_ids()
  125. refund._convert_to_write(invoice._cache)
  126. self.assertEqual(refund.state, 'draft')
  127. # change the state of refund to open by clicking Validate button
  128. refund.action_invoice_open()
  129. self.assertEquals(tax.base_balance, 75.)
  130. self.assertEquals(tax.balance, 7.5)
  131. self.assertEquals(tax.base_balance_regular, 100.)
  132. self.assertEquals(tax.balance_regular, 10.)
  133. self.assertEquals(tax.base_balance_refund, -25.)
  134. self.assertEquals(tax.balance_refund, -2.5)
  135. # Taxes on liquidity type moves are included
  136. liquidity_account_id = self.env['account.account'].search(
  137. [('internal_type', '=', 'liquidity')], limit=1).id
  138. self.env['account.move'].create({
  139. 'date': Date.context_today(self.env.user),
  140. 'journal_id': self.env['account.journal'].search(
  141. [('type', '=', 'bank')], limit=1).id,
  142. 'name': 'Test move',
  143. 'line_ids': [(0, 0, {
  144. 'account_id': liquidity_account_id,
  145. 'debit': 110,
  146. 'credit': 0,
  147. 'name': 'Bank Fees',
  148. }), (0, 0, {
  149. 'account_id': invoice_line_account_id,
  150. 'debit': 0,
  151. 'credit': 100,
  152. 'name': 'Bank Fees',
  153. 'tax_ids': [(4, tax.id)]
  154. }), (0, 0, {
  155. 'account_id': tax.account_id.id,
  156. 'debit': 0,
  157. 'credit': 10,
  158. 'name': 'Bank Fees',
  159. 'tax_line_id': tax.id,
  160. })],
  161. }).post()
  162. tax.refresh()
  163. self.assertEquals(tax.base_balance, 175.)
  164. self.assertEquals(tax.balance, 17.5)
  165. def test_receivable_move_type(self):
  166. """
  167. Receivable moves linked to invoices are not considered as refunds.
  168. Also, receivable moves coming from refunds are considered as such.
  169. """
  170. receivable_account = self.env['account.account'].search(
  171. [('user_type_id', '=', self.env.ref(
  172. 'account.data_account_type_receivable'
  173. ).id)], limit=1)
  174. invoice = self.env['account.invoice'].create({
  175. 'partner_id': self.env.ref('base.res_partner_2').id,
  176. 'account_id': receivable_account.id,
  177. 'type': 'out_invoice',
  178. 'invoice_line_ids': [(0, 0, {
  179. 'product_id': self.env.ref('product.product_product_4').id,
  180. 'account_id': receivable_account.id,
  181. 'quantity': 1.0,
  182. 'price_unit': 100.0,
  183. 'name': 'product that costs 100',
  184. }), (0, 0, {
  185. 'product_id': self.env.ref('product.product_product_4').id,
  186. 'account_id': receivable_account.id,
  187. 'quantity': 1.0,
  188. 'price_unit': -100.0,
  189. 'name': 'product that costs -100',
  190. })]
  191. })
  192. invoice.action_invoice_open()
  193. self.assertEqual(invoice.move_id.move_type, 'receivable')
  194. refund = invoice.refund()
  195. refund.action_invoice_open()
  196. self.assertEqual(refund.move_id.move_type, 'receivable_refund')
  197. def test_payable_move_type(self):
  198. """
  199. Payable moves linked to bills are not considered as refunds.
  200. Also, payable moves coming from refunds are considered as such.
  201. """
  202. payable_account = self.env['account.account'].search(
  203. [('user_type_id', '=', self.env.ref(
  204. 'account.data_account_type_payable'
  205. ).id)], limit=1)
  206. bill = self.env['account.invoice'].create({
  207. 'partner_id': self.env.ref('base.res_partner_2').id,
  208. 'account_id': payable_account.id,
  209. 'type': 'in_invoice',
  210. 'invoice_line_ids': [(0, 0, {
  211. 'product_id': self.env.ref('product.product_product_4').id,
  212. 'account_id': payable_account.id,
  213. 'quantity': 1.0,
  214. 'price_unit': 100.0,
  215. 'name': 'product that costs 100',
  216. }), (0, 0, {
  217. 'product_id': self.env.ref('product.product_product_4').id,
  218. 'account_id': payable_account.id,
  219. 'quantity': 1.0,
  220. 'price_unit': -100.0,
  221. 'name': 'product that costs -100',
  222. })]
  223. })
  224. bill.action_invoice_open()
  225. self.assertEqual(bill.move_id.move_type, 'payable')
  226. refund = bill.refund()
  227. refund.action_invoice_open()
  228. self.assertEqual(refund.move_id.move_type, 'payable_refund')