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.

246 lines
8.9 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. # Copyright 2016 Lorenzo Battistini - Agile Business Group
  2. # Copyright 2016 Giovanni Capalbo <giovanni@therp.nl>
  3. # Copyright 2019 Andrea Stirpe <a.stirpe@onestein.nl>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from datetime import datetime
  6. from dateutil.rrule import MONTHLY
  7. import odoo
  8. from odoo.fields import Date
  9. from odoo.tests.common import HttpCase
  10. @odoo.tests.tagged("post_install", "-at_install")
  11. class TestAccountTaxBalance(HttpCase):
  12. def setUp(self):
  13. super().setUp()
  14. self.range_type = self.env["date.range.type"].create(
  15. {"name": "Fiscal year", "company_id": False, "allow_overlap": False}
  16. )
  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. {
  22. "date_start": "%s-01-01" % self.current_year,
  23. "name_prefix": "%s-" % self.current_year,
  24. "type_id": self.range_type.id,
  25. "duration_count": 1,
  26. "unit_of_time": str(MONTHLY),
  27. "count": 12,
  28. }
  29. )
  30. range_generator.action_apply()
  31. self.range = self.env["date.range"]
  32. def test_tax_balance(self):
  33. tax_account_id = (
  34. self.env["account.account"]
  35. .create(
  36. {
  37. "name": "Tax Paid",
  38. "code": "TAXTEST",
  39. "user_type_id": self.env.ref(
  40. "account.data_account_type_current_liabilities"
  41. ).id,
  42. }
  43. )
  44. .id
  45. )
  46. tax = self.env["account.tax"].create(
  47. {"name": "Tax 10.0%", "amount": 10.0, "amount_type": "percent"}
  48. )
  49. invoice_line_account_id = (
  50. self.env["account.account"]
  51. .create(
  52. {
  53. "user_type_id": self.env.ref(
  54. "account.data_account_type_expenses"
  55. ).id,
  56. "code": "EXPTEST",
  57. "name": "Test expense account",
  58. }
  59. )
  60. .id
  61. )
  62. product = self.env.ref("product.product_product_4")
  63. invoice = self.env["account.move"].create(
  64. {
  65. "partner_id": self.env.ref("base.res_partner_2").id,
  66. "type": "out_invoice",
  67. "invoice_line_ids": [
  68. (
  69. 0,
  70. None,
  71. {
  72. "product_id": product.id,
  73. "quantity": 1.0,
  74. "price_unit": 100.0,
  75. "name": "product that cost 100",
  76. "account_id": invoice_line_account_id,
  77. "tax_ids": [(6, 0, [tax.id])],
  78. },
  79. )
  80. ],
  81. }
  82. )
  83. invoice._onchange_invoice_line_ids()
  84. invoice._convert_to_write(invoice._cache)
  85. self.assertEqual(invoice.state, "draft")
  86. # change the state of invoice to open by clicking Validate button
  87. invoice.action_post()
  88. self.assertEqual(tax.base_balance, 100.0)
  89. self.assertEqual(tax.balance, 10.0)
  90. self.assertEqual(tax.base_balance_regular, 100.0)
  91. self.assertEqual(tax.balance_regular, 10.0)
  92. self.assertEqual(tax.base_balance_refund, 0.0)
  93. self.assertEqual(tax.balance_refund, 0.0)
  94. # testing wizard
  95. current_range = self.range.search(
  96. [
  97. (
  98. "date_start",
  99. "=",
  100. "{}-{}-01".format(self.current_year, self.current_month),
  101. )
  102. ]
  103. )
  104. wizard = self.env["wizard.open.tax.balances"].new({})
  105. self.assertFalse(wizard.from_date)
  106. self.assertFalse(wizard.to_date)
  107. wizard = self.env["wizard.open.tax.balances"].new(
  108. {"date_range_id": current_range[0].id}
  109. )
  110. self.assertEqual(wizard.from_date, current_range[0].date_start)
  111. self.assertEqual(wizard.to_date, current_range[0].date_end)
  112. action = wizard.open_taxes()
  113. self.assertEqual(action["context"]["from_date"], current_range[0].date_start)
  114. self.assertEqual(action["context"]["to_date"], current_range[0].date_end)
  115. # exercise search has_moves = True
  116. taxes = self.env["account.tax"].search([("has_moves", "=", True)])
  117. self.assertEqual(len(taxes), 1)
  118. self.assertEqual(taxes[0].name, "Tax 10.0%")
  119. # testing buttons
  120. tax_action = tax.view_tax_lines()
  121. base_action = tax.view_base_lines()
  122. tax_action_move_lines = self.env["account.move.line"].search(
  123. tax_action["domain"]
  124. )
  125. self.assertTrue(invoice.line_ids & tax_action_move_lines)
  126. self.assertEqual(tax_action["xml_id"], "account.action_account_moves_all_tree")
  127. base_action_move_lines = self.env["account.move.line"].search(
  128. base_action["domain"]
  129. )
  130. self.assertTrue(invoice.line_ids & base_action_move_lines)
  131. self.assertEqual(base_action["xml_id"], "account.action_account_moves_all_tree")
  132. # test specific method
  133. state_list = tax.get_target_state_list(target_move="all")
  134. self.assertEqual(state_list, ["posted", "draft"])
  135. state_list = tax.get_target_state_list(target_move="whatever")
  136. self.assertEqual(state_list, [])
  137. product = self.env.ref("product.product_product_2")
  138. refund = self.env["account.move"].create(
  139. {
  140. "partner_id": self.env.ref("base.res_partner_2").id,
  141. "type": "out_refund",
  142. "invoice_line_ids": [
  143. (
  144. 0,
  145. None,
  146. {
  147. "product_id": product.id,
  148. "quantity": 1.0,
  149. "price_unit": 25.0,
  150. "name": "returned product that cost 25",
  151. "account_id": invoice_line_account_id,
  152. "tax_ids": [(6, 0, [tax.id])],
  153. },
  154. )
  155. ],
  156. }
  157. )
  158. refund._onchange_invoice_line_ids()
  159. refund._convert_to_write(invoice._cache)
  160. self.assertEqual(refund.state, "draft")
  161. # change the state of refund to open by clicking Validate button
  162. refund.action_post()
  163. # force the _compute_balance() to be triggered
  164. tax._compute_balance()
  165. self.assertEqual(tax.base_balance, 75.0)
  166. self.assertEqual(tax.balance, 7.5)
  167. self.assertEqual(tax.base_balance_regular, 100.0)
  168. self.assertEqual(tax.balance_regular, 10.0)
  169. self.assertEqual(tax.base_balance_refund, -25.0)
  170. self.assertEqual(tax.balance_refund, -2.5)
  171. # Taxes on liquidity type moves are included
  172. tax_repartition_line = tax.invoice_repartition_line_ids.filtered(
  173. lambda line: line.repartition_type == "tax"
  174. )
  175. liquidity_account_id = (
  176. self.env["account.account"]
  177. .search([("internal_type", "=", "liquidity")], limit=1)
  178. .id
  179. )
  180. move = self.env["account.move"].create(
  181. {
  182. "type": "entry",
  183. "date": Date.context_today(self.env.user),
  184. "journal_id": self.env["account.journal"]
  185. .search([("type", "=", "bank")], limit=1)
  186. .id,
  187. "name": "Test move",
  188. "line_ids": [
  189. (
  190. 0,
  191. 0,
  192. {
  193. "account_id": liquidity_account_id,
  194. "debit": 110,
  195. "credit": 0,
  196. "name": "Bank Fees",
  197. },
  198. ),
  199. (
  200. 0,
  201. 0,
  202. {
  203. "account_id": invoice_line_account_id,
  204. "debit": 0,
  205. "credit": 100,
  206. "name": "Bank Fees",
  207. "tax_ids": [(4, tax.id)],
  208. },
  209. ),
  210. (
  211. 0,
  212. 0,
  213. {
  214. "account_id": tax_account_id,
  215. "debit": 0,
  216. "credit": 10,
  217. "name": "Bank Fees",
  218. "tax_repartition_line_id": tax_repartition_line.id,
  219. },
  220. ),
  221. ],
  222. }
  223. )
  224. move.action_post()
  225. tax.refresh()
  226. self.assertEqual(tax.base_balance, 175.0)
  227. self.assertEqual(tax.balance, 17.5)