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.

257 lines
9.2 KiB

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