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.

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