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.

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