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.

368 lines
14 KiB

  1. # Copyright 2018 Forest and Biomass Romania
  2. # Copyright 2020 ForgeFlow S.L. (https://www.forgeflow.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import time
  5. from datetime import date
  6. from odoo.tests import common
  7. class TestVATReport(common.TransactionCase):
  8. def setUp(self):
  9. super(TestVATReport, self).setUp()
  10. self.date_from = time.strftime("%Y-%m-01")
  11. self.date_to = time.strftime("%Y-%m-28")
  12. self.company = self.env.ref("base.main_company")
  13. self.receivable_account = self.env["account.account"].search(
  14. [
  15. ("company_id", "=", self.company.id),
  16. ("user_type_id.name", "=", "Receivable"),
  17. ],
  18. limit=1,
  19. )
  20. self.income_account = self.env["account.account"].search(
  21. [
  22. ("company_id", "=", self.company.id),
  23. ("user_type_id.name", "=", "Income"),
  24. ],
  25. limit=1,
  26. )
  27. self.tax_account = self.env["account.account"].search(
  28. [
  29. ("company_id", "=", self.company.id),
  30. (
  31. "user_type_id",
  32. "=",
  33. self.env.ref(
  34. "account.data_account_type_non_current_liabilities"
  35. ).id,
  36. ),
  37. ],
  38. limit=1,
  39. )
  40. self.bank_journal = self.env["account.journal"].search(
  41. [("type", "=", "bank"), ("company_id", "=", self.company.id)], limit=1
  42. )
  43. self.tax_tag_01 = self.env["account.account.tag"].create(
  44. {
  45. "name": "Tag 01",
  46. "applicability": "taxes",
  47. "country_id": self.company.country_id.id,
  48. }
  49. )
  50. self.tax_tag_02 = self.env["account.account.tag"].create(
  51. {
  52. "name": "Tag 02",
  53. "applicability": "taxes",
  54. "country_id": self.company.country_id.id,
  55. }
  56. )
  57. self.tax_tag_03 = self.env["account.account.tag"].create(
  58. {
  59. "name": "Tag 03",
  60. "applicability": "taxes",
  61. "country_id": self.company.country_id.id,
  62. }
  63. )
  64. self.tax_group_10 = self.env["account.tax.group"].create(
  65. {"name": "Tax 10%", "sequence": 1}
  66. )
  67. self.tax_group_20 = self.env["account.tax.group"].create(
  68. {"name": "Tax 20%", "sequence": 2}
  69. )
  70. self.tax_10 = self.env["account.tax"].create(
  71. {
  72. "name": "Tax 10.0%",
  73. "amount": 10.0,
  74. "amount_type": "percent",
  75. "type_tax_use": "sale",
  76. "company_id": self.company.id,
  77. "tax_group_id": self.tax_group_10.id,
  78. "invoice_repartition_line_ids": [
  79. (0, 0, {"factor_percent": 100, "repartition_type": "base"}),
  80. (
  81. 0,
  82. 0,
  83. {
  84. "factor_percent": 100,
  85. "repartition_type": "tax",
  86. "account_id": self.tax_account.id,
  87. "tag_ids": [
  88. (6, 0, [self.tax_tag_01.id, self.tax_tag_02.id])
  89. ],
  90. },
  91. ),
  92. ],
  93. "refund_repartition_line_ids": [
  94. (0, 0, {"factor_percent": 100, "repartition_type": "base"}),
  95. (
  96. 0,
  97. 0,
  98. {
  99. "factor_percent": 100,
  100. "repartition_type": "tax",
  101. "account_id": self.tax_account.id,
  102. },
  103. ),
  104. ],
  105. }
  106. )
  107. self.tax_20 = self.env["account.tax"].create(
  108. {
  109. "sequence": 30,
  110. "name": "Tax 20.0%",
  111. "amount": 20.0,
  112. "amount_type": "percent",
  113. "type_tax_use": "sale",
  114. "company_id": self.company.id,
  115. "cash_basis_transition_account_id": self.tax_account.id,
  116. "tax_group_id": self.tax_group_20.id,
  117. "invoice_repartition_line_ids": [
  118. (0, 0, {"factor_percent": 100, "repartition_type": "base"}),
  119. (
  120. 0,
  121. 0,
  122. {
  123. "factor_percent": 100,
  124. "repartition_type": "tax",
  125. "account_id": self.tax_account.id,
  126. "tag_ids": [
  127. (6, 0, [self.tax_tag_02.id, self.tax_tag_03.id])
  128. ],
  129. },
  130. ),
  131. ],
  132. "refund_repartition_line_ids": [
  133. (0, 0, {"factor_percent": 100, "repartition_type": "base"}),
  134. (
  135. 0,
  136. 0,
  137. {
  138. "factor_percent": 100,
  139. "repartition_type": "tax",
  140. "account_id": self.tax_account.id,
  141. },
  142. ),
  143. ],
  144. }
  145. )
  146. move_form = common.Form(
  147. self.env["account.move"].with_context(default_type="out_invoice")
  148. )
  149. move_form.partner_id = self.env.ref("base.res_partner_2")
  150. move_form.invoice_date = time.strftime("%Y-%m-03")
  151. with move_form.invoice_line_ids.new() as line_form:
  152. line_form.product_id = self.env.ref("product.product_product_4")
  153. line_form.quantity = 1.0
  154. line_form.price_unit = 100.0
  155. line_form.account_id = self.income_account
  156. line_form.tax_ids.add(self.tax_10)
  157. invoice = move_form.save()
  158. invoice.post()
  159. move_form = common.Form(
  160. self.env["account.move"].with_context(default_type="out_invoice")
  161. )
  162. move_form.partner_id = self.env.ref("base.res_partner_2")
  163. move_form.invoice_date = time.strftime("%Y-%m-04")
  164. with move_form.invoice_line_ids.new() as line_form:
  165. line_form.product_id = self.env.ref("product.product_product_4")
  166. line_form.quantity = 1.0
  167. line_form.price_unit = 250.0
  168. line_form.account_id = self.income_account
  169. line_form.tax_ids.add(self.tax_20)
  170. invoice = move_form.save()
  171. invoice.post()
  172. def _get_report_lines(self, taxgroups=False):
  173. based_on = "taxtags"
  174. if taxgroups:
  175. based_on = "taxgroups"
  176. vat_report = self.env["vat.report.wizard"].create(
  177. {
  178. "date_from": self.date_from,
  179. "date_to": self.date_to,
  180. "company_id": self.company.id,
  181. "based_on": based_on,
  182. "tax_detail": True,
  183. }
  184. )
  185. data = vat_report._prepare_vat_report()
  186. res_data = self.env[
  187. "report.account_financial_report.vat_report"
  188. ]._get_report_values(vat_report, data)
  189. return res_data
  190. def check_tag_or_group_in_report(self, tag_or_group_name, vat_report):
  191. tag_or_group_in_report = False
  192. for tag_or_group in vat_report:
  193. if tag_or_group["name"] == tag_or_group_name:
  194. tag_or_group_in_report = True
  195. break
  196. return tag_or_group_in_report
  197. def check_tax_in_report(self, tax_name, vat_report):
  198. tax_in_report = False
  199. for tag_or_group in vat_report:
  200. if tag_or_group["taxes"]:
  201. for tax in tag_or_group["taxes"]:
  202. if tax["name"] == tax_name:
  203. tax_in_report = True
  204. return tax_in_report
  205. def _get_tag_or_group_line(self, tag_or_group_name, vat_report):
  206. tag_or_group_net = False
  207. tag_or_group_tax = False
  208. for tag_or_group in vat_report:
  209. if tag_or_group["name"] == tag_or_group_name:
  210. tag_or_group_net = tag_or_group["net"]
  211. tag_or_group_tax = tag_or_group["tax"]
  212. return tag_or_group_net, tag_or_group_tax
  213. def _get_tax_line(self, tax_name, vat_report):
  214. tax_net = False
  215. tax_tax = False
  216. for tag_or_group in vat_report:
  217. if tag_or_group["taxes"]:
  218. for tax in tag_or_group["taxes"]:
  219. if tax["name"] == tax_name:
  220. tax_net = tax["net"]
  221. tax_tax = tax["tax"]
  222. return tax_net, tax_tax
  223. def test_01_compute(self):
  224. # Generate the vat lines
  225. res_data = self._get_report_lines()
  226. vat_report = res_data["vat_report"]
  227. # Check report based on taxtags
  228. check_tax_tag_01 = self.check_tag_or_group_in_report(
  229. self.tax_tag_01.name, vat_report
  230. )
  231. self.assertTrue(check_tax_tag_01)
  232. check_tax_tag_02 = self.check_tag_or_group_in_report(
  233. self.tax_tag_02.name, vat_report
  234. )
  235. self.assertTrue(check_tax_tag_02)
  236. check_tax_tag_03 = self.check_tag_or_group_in_report(
  237. self.tax_tag_03.name, vat_report
  238. )
  239. self.assertTrue(check_tax_tag_03)
  240. check_tax_10 = self.check_tax_in_report(self.tax_10.name, vat_report)
  241. self.assertTrue(check_tax_10)
  242. check_tax_20 = self.check_tax_in_report(self.tax_20.name, vat_report)
  243. self.assertTrue(check_tax_20)
  244. tag_01_net, tag_01_tax = self._get_tag_or_group_line(
  245. self.tax_tag_01.name, vat_report
  246. )
  247. tag_02_net, tag_02_tax = self._get_tag_or_group_line(
  248. self.tax_tag_02.name, vat_report
  249. )
  250. tag_03_net, tag_03_tax = self._get_tag_or_group_line(
  251. self.tax_tag_03.name, vat_report
  252. )
  253. tax_10_net, tax_10_tax = self._get_tax_line(self.tax_10.name, vat_report)
  254. tax_20_net, tax_20_tax = self._get_tax_line(self.tax_20.name, vat_report)
  255. self.assertEqual(tag_01_net, -100)
  256. self.assertEqual(tag_01_tax, -10)
  257. self.assertEqual(tag_02_net, -350)
  258. self.assertEqual(tag_02_tax, -60)
  259. self.assertEqual(tag_03_net, -250)
  260. self.assertEqual(tag_03_tax, -50)
  261. self.assertEqual(tax_10_net, -100)
  262. self.assertEqual(tax_10_tax, -10)
  263. self.assertEqual(tax_20_net, -250)
  264. self.assertEqual(tax_20_tax, -50)
  265. # Check report based on taxgroups
  266. res_data = self._get_report_lines(taxgroups=True)
  267. vat_report = res_data["vat_report"]
  268. check_group_10 = self.check_tag_or_group_in_report(
  269. self.tax_group_10.name, vat_report
  270. )
  271. self.assertTrue(check_group_10)
  272. check_group_20 = self.check_tag_or_group_in_report(
  273. self.tax_group_20.name, vat_report
  274. )
  275. self.assertTrue(check_group_20)
  276. check_tax_10 = self.check_tax_in_report(self.tax_10.name, vat_report)
  277. self.assertTrue(check_tax_10)
  278. check_tax_20 = self.check_tax_in_report(self.tax_20.name, vat_report)
  279. self.assertTrue(check_tax_20)
  280. group_10_net, group_10_tax = self._get_tag_or_group_line(
  281. self.tax_group_10.name, vat_report
  282. )
  283. group_20_net, group_20_tax = self._get_tag_or_group_line(
  284. self.tax_group_20.name, vat_report
  285. )
  286. tax_10_net, tax_10_tax = self._get_tax_line(self.tax_10.name, vat_report)
  287. tax_20_net, tax_20_tax = self._get_tax_line(self.tax_20.name, vat_report)
  288. self.assertEqual(group_10_net, -100)
  289. self.assertEqual(group_10_tax, -10)
  290. self.assertEqual(group_20_net, -250)
  291. self.assertEqual(group_20_tax, -50)
  292. self.assertEqual(tax_10_net, -100)
  293. self.assertEqual(tax_10_tax, -10)
  294. self.assertEqual(tax_20_net, -250)
  295. self.assertEqual(tax_20_tax, -50)
  296. def test_wizard_date_range(self):
  297. vat_wizard = self.env["vat.report.wizard"]
  298. date_range = self.env["date.range"]
  299. self.type = self.env["date.range.type"].create(
  300. {"name": "Month", "company_id": False, "allow_overlap": False}
  301. )
  302. dt = date_range.create(
  303. {
  304. "name": "FS2016",
  305. "date_start": time.strftime("%Y-%m-01"),
  306. "date_end": time.strftime("%Y-%m-28"),
  307. "type_id": self.type.id,
  308. }
  309. )
  310. wizard = vat_wizard.create(
  311. {
  312. "date_range_id": dt.id,
  313. "date_from": time.strftime("%Y-%m-28"),
  314. "date_to": time.strftime("%Y-%m-01"),
  315. "tax_detail": True,
  316. }
  317. )
  318. wizard.onchange_date_range_id()
  319. self.assertEqual(
  320. wizard.date_from, date(date.today().year, date.today().month, 1)
  321. )
  322. self.assertEqual(
  323. wizard.date_to, date(date.today().year, date.today().month, 28)
  324. )
  325. wizard._export("qweb-pdf")
  326. wizard.button_export_html()
  327. wizard.button_export_pdf()
  328. wizard.button_export_xlsx()
  329. wizard = vat_wizard.create(
  330. {
  331. "date_range_id": dt.id,
  332. "date_from": time.strftime("%Y-%m-28"),
  333. "date_to": time.strftime("%Y-%m-01"),
  334. "based_on": "taxgroups",
  335. "tax_detail": True,
  336. }
  337. )
  338. wizard.onchange_date_range_id()
  339. self.assertEqual(
  340. wizard.date_from, date(date.today().year, date.today().month, 1)
  341. )
  342. self.assertEqual(
  343. wizard.date_to, date(date.today().year, date.today().month, 28)
  344. )
  345. wizard._export("qweb-pdf")
  346. wizard.button_export_html()
  347. wizard.button_export_pdf()
  348. wizard.button_export_xlsx()