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.

333 lines
12 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. {"name": "Tag 01", "applicability": "taxes"}
  45. )
  46. self.tax_tag_02 = self.env["account.account.tag"].create(
  47. {"name": "Tag 02", "applicability": "taxes"}
  48. )
  49. self.tax_tag_03 = self.env["account.account.tag"].create(
  50. {"name": "Tag 03", "applicability": "taxes"}
  51. )
  52. self.tax_group_10 = self.env["account.tax.group"].create(
  53. {"name": "Tax 10%", "sequence": 1}
  54. )
  55. self.tax_group_20 = self.env["account.tax.group"].create(
  56. {"name": "Tax 20%", "sequence": 2}
  57. )
  58. self.tax_10 = self.env["account.tax"].create(
  59. {
  60. "name": "Tax 10.0%",
  61. "amount": 10.0,
  62. "amount_type": "percent",
  63. "type_tax_use": "sale",
  64. "account_id": self.tax_account.id,
  65. "company_id": self.company.id,
  66. "refund_account_id": self.tax_account.id,
  67. "tax_group_id": self.tax_group_10.id,
  68. "tag_ids": [(6, 0, [self.tax_tag_01.id, self.tax_tag_02.id])],
  69. }
  70. )
  71. self.tax_20 = self.env["account.tax"].create(
  72. {
  73. "sequence": 30,
  74. "name": "Tax 20.0%",
  75. "amount": 20.0,
  76. "amount_type": "percent",
  77. "type_tax_use": "sale",
  78. "tax_exigibility": "on_payment",
  79. "account_id": self.tax_account.id,
  80. "company_id": self.company.id,
  81. "refund_account_id": self.tax_account.id,
  82. "cash_basis_account_id": self.tax_account.id,
  83. "tax_group_id": self.tax_group_20.id,
  84. "tag_ids": [(6, 0, [self.tax_tag_02.id, self.tax_tag_03.id])],
  85. }
  86. )
  87. invoice = self.env["account.invoice"].create(
  88. {
  89. "partner_id": self.env.ref("base.res_partner_2").id,
  90. "account_id": self.receivable_account.id,
  91. "company_id": self.company.id,
  92. "date_invoice": time.strftime("%Y-%m-03"),
  93. "type": "out_invoice",
  94. }
  95. )
  96. self.env["account.invoice.line"].create(
  97. {
  98. "product_id": self.env.ref("product.product_product_4").id,
  99. "quantity": 1.0,
  100. "price_unit": 100.0,
  101. "invoice_id": invoice.id,
  102. "name": "product",
  103. "account_id": self.income_account.id,
  104. "invoice_line_tax_ids": [(6, 0, [self.tax_10.id])],
  105. }
  106. )
  107. invoice.compute_taxes()
  108. invoice.action_invoice_open()
  109. self.cbinvoice = self.env["account.invoice"].create(
  110. {
  111. "partner_id": self.env.ref("base.res_partner_2").id,
  112. "account_id": self.receivable_account.id,
  113. "company_id": self.company.id,
  114. "date_invoice": time.strftime("%Y-%m-05"),
  115. "type": "out_invoice",
  116. }
  117. )
  118. self.env["account.invoice.line"].create(
  119. {
  120. "product_id": self.env.ref("product.product_product_4").id,
  121. "quantity": 1.0,
  122. "price_unit": 500.0,
  123. "invoice_id": self.cbinvoice.id,
  124. "name": "product",
  125. "account_id": self.income_account.id,
  126. "invoice_line_tax_ids": [(6, 0, [self.tax_20.id])],
  127. }
  128. )
  129. self.cbinvoice.compute_taxes()
  130. self.cbinvoice.action_invoice_open()
  131. self.cbinvoice.pay_and_reconcile(
  132. self.bank_journal.id, 300, date(date.today().year, date.today().month, 10)
  133. )
  134. def _get_report_lines(self, taxgroups=False):
  135. based_on = "taxtags"
  136. if taxgroups:
  137. based_on = "taxgroups"
  138. vat_report = self.env["vat.report.wizard"].create(
  139. {
  140. "date_from": self.date_from,
  141. "date_to": self.date_to,
  142. "company_id": self.company.id,
  143. "based_on": based_on,
  144. "tax_detail": True,
  145. }
  146. )
  147. data = vat_report._prepare_vat_report()
  148. res_data = self.env[
  149. "report.account_financial_report.vat_report"
  150. ]._get_report_values(vat_report, data)
  151. return res_data
  152. def check_tag_or_group_in_report(self, tag_or_group_name, vat_report):
  153. tag_or_group_in_report = False
  154. for tag_or_group in vat_report:
  155. if tag_or_group["name"] == tag_or_group_name:
  156. tag_or_group_in_report = True
  157. break
  158. return tag_or_group_in_report
  159. def check_tax_in_report(self, tax_name, vat_report):
  160. tax_in_report = False
  161. for tag_or_group in vat_report:
  162. if tag_or_group["taxes"]:
  163. for tax in tag_or_group["taxes"]:
  164. if tax["name"] == tax_name:
  165. tax_in_report = True
  166. return tax_in_report
  167. def _get_tag_or_group_line(self, tag_or_group_name, vat_report):
  168. tag_or_group_net = False
  169. tag_or_group_tax = False
  170. for tag_or_group in vat_report:
  171. if tag_or_group["name"] == tag_or_group_name:
  172. tag_or_group_net = tag_or_group["net"]
  173. tag_or_group_tax = tag_or_group["tax"]
  174. return tag_or_group_net, tag_or_group_tax
  175. def _get_tax_line(self, tax_name, vat_report):
  176. tax_net = False
  177. tax_tax = False
  178. for tag_or_group in vat_report:
  179. if tag_or_group["taxes"]:
  180. for tax in tag_or_group["taxes"]:
  181. if tax["name"] == tax_name:
  182. tax_net = tax["net"]
  183. tax_tax = tax["tax"]
  184. return tax_net, tax_tax
  185. def test_01_compute(self):
  186. # Generate the vat lines
  187. res_data = self._get_report_lines()
  188. vat_report = res_data["vat_report"]
  189. # Check report based on taxtags
  190. check_tax_tag_01 = self.check_tag_or_group_in_report(
  191. self.tax_tag_01.name, vat_report
  192. )
  193. self.assertTrue(check_tax_tag_01)
  194. check_tax_tag_02 = self.check_tag_or_group_in_report(
  195. self.tax_tag_02.name, vat_report
  196. )
  197. self.assertTrue(check_tax_tag_02)
  198. check_tax_tag_03 = self.check_tag_or_group_in_report(
  199. self.tax_tag_03.name, vat_report
  200. )
  201. self.assertTrue(check_tax_tag_03)
  202. check_tax_10 = self.check_tax_in_report(self.tax_10.name, vat_report)
  203. self.assertTrue(check_tax_10)
  204. check_tax_20 = self.check_tax_in_report(self.tax_20.name, vat_report)
  205. self.assertTrue(check_tax_20)
  206. tag_01_net, tag_01_tax = self._get_tag_or_group_line(
  207. self.tax_tag_01.name, vat_report
  208. )
  209. tag_02_net, tag_02_tax = self._get_tag_or_group_line(
  210. self.tax_tag_02.name, vat_report
  211. )
  212. tag_03_net, tag_03_tax = self._get_tag_or_group_line(
  213. self.tax_tag_03.name, vat_report
  214. )
  215. tax_10_net, tax_10_tax = self._get_tax_line(self.tax_10.name, vat_report)
  216. tax_20_net, tax_20_tax = self._get_tax_line(self.tax_20.name, vat_report)
  217. self.assertEqual(tag_01_net, 100)
  218. self.assertEqual(tag_01_tax, 10)
  219. self.assertEqual(tag_02_net, 350)
  220. self.assertEqual(tag_02_tax, 60)
  221. self.assertEqual(tag_03_net, 250)
  222. self.assertEqual(tag_03_tax, 50)
  223. self.assertEqual(tax_10_net, 100)
  224. self.assertEqual(tax_10_tax, 10)
  225. self.assertEqual(tax_20_net, 250)
  226. self.assertEqual(tax_20_tax, 50)
  227. # Check report based on taxgroups
  228. res_data = self._get_report_lines(taxgroups=True)
  229. vat_report = res_data["vat_report"]
  230. check_group_10 = self.check_tag_or_group_in_report(
  231. self.tax_group_10.name, vat_report
  232. )
  233. self.assertTrue(check_group_10)
  234. check_group_20 = self.check_tag_or_group_in_report(
  235. self.tax_group_20.name, vat_report
  236. )
  237. self.assertTrue(check_group_20)
  238. check_tax_10 = self.check_tax_in_report(self.tax_10.name, vat_report)
  239. self.assertTrue(check_tax_10)
  240. check_tax_20 = self.check_tax_in_report(self.tax_20.name, vat_report)
  241. self.assertTrue(check_tax_20)
  242. group_10_net, group_10_tax = self._get_tag_or_group_line(
  243. self.tax_group_10.name, vat_report
  244. )
  245. group_20_net, group_20_tax = self._get_tag_or_group_line(
  246. self.tax_group_20.name, vat_report
  247. )
  248. tax_10_net, tax_10_tax = self._get_tax_line(self.tax_10.name, vat_report)
  249. tax_20_net, tax_20_tax = self._get_tax_line(self.tax_20.name, vat_report)
  250. self.assertEqual(group_10_net, 100)
  251. self.assertEqual(group_10_tax, 10)
  252. self.assertEqual(group_20_net, 250)
  253. self.assertEqual(group_20_tax, 50)
  254. self.assertEqual(tax_10_net, 100)
  255. self.assertEqual(tax_10_tax, 10)
  256. self.assertEqual(tax_20_net, 250)
  257. self.assertEqual(tax_20_tax, 50)
  258. def test_wizard_date_range(self):
  259. vat_wizard = self.env["vat.report.wizard"]
  260. date_range = self.env["date.range"]
  261. self.type = self.env["date.range.type"].create(
  262. {"name": "Month", "company_id": False, "allow_overlap": False}
  263. )
  264. dt = date_range.create(
  265. {
  266. "name": "FS2016",
  267. "date_start": time.strftime("%Y-%m-01"),
  268. "date_end": time.strftime("%Y-%m-28"),
  269. "type_id": self.type.id,
  270. }
  271. )
  272. wizard = vat_wizard.create(
  273. {
  274. "date_range_id": dt.id,
  275. "date_from": time.strftime("%Y-%m-28"),
  276. "date_to": time.strftime("%Y-%m-01"),
  277. "tax_detail": True,
  278. }
  279. )
  280. wizard.onchange_date_range_id()
  281. self.assertEqual(
  282. wizard.date_from, date(date.today().year, date.today().month, 1)
  283. )
  284. self.assertEqual(
  285. wizard.date_to, date(date.today().year, date.today().month, 28)
  286. )
  287. wizard._export("qweb-pdf")
  288. wizard.button_export_html()
  289. wizard.button_export_pdf()
  290. wizard.button_export_xlsx()
  291. wizard = vat_wizard.create(
  292. {
  293. "date_range_id": dt.id,
  294. "date_from": time.strftime("%Y-%m-28"),
  295. "date_to": time.strftime("%Y-%m-01"),
  296. "based_on": "taxgroups",
  297. "tax_detail": True,
  298. }
  299. )
  300. wizard.onchange_date_range_id()
  301. self.assertEqual(
  302. wizard.date_from, date(date.today().year, date.today().month, 1)
  303. )
  304. self.assertEqual(
  305. wizard.date_to, date(date.today().year, date.today().month, 28)
  306. )
  307. wizard._export("qweb-pdf")
  308. wizard.button_export_html()
  309. wizard.button_export_pdf()
  310. wizard.button_export_xlsx()