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.

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