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.

356 lines
11 KiB

  1. # Copyright 2018 Forest and Biomass Romania
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, fields, models
  4. class VATReport(models.TransientModel):
  5. _name = "report_vat_report"
  6. _inherit = 'account_financial_report_abstract'
  7. """ Here, we just define class fields.
  8. For methods, go more bottom at this file.
  9. The class hierarchy is :
  10. * VATReport
  11. ** VATReportTaxTags
  12. *** VATReportTax
  13. """
  14. # Filters fields, used for data computation
  15. company_id = fields.Many2one(comodel_name='res.company')
  16. date_from = fields.Date()
  17. date_to = fields.Date()
  18. based_on = fields.Selection([('taxtags', 'Tax Tags'),
  19. ('taxgroups', 'Tax Groups')],
  20. string='Based On',
  21. required=True,
  22. default='taxtags')
  23. tax_detail = fields.Boolean('Tax Detail')
  24. # Data fields, used to browse report data
  25. taxtags_ids = fields.One2many(
  26. comodel_name='report_vat_report_taxtag',
  27. inverse_name='report_id'
  28. )
  29. class VATReportTaxTags(models.TransientModel):
  30. _name = 'report_vat_report_taxtag'
  31. _inherit = 'account_financial_report_abstract'
  32. _order = 'code ASC'
  33. report_id = fields.Many2one(
  34. comodel_name='report_vat_report',
  35. ondelete='cascade',
  36. index=True
  37. )
  38. # Data fields, used to keep link with real object
  39. taxtag_id = fields.Many2one(
  40. 'account.account.tag',
  41. index=True
  42. )
  43. taxgroup_id = fields.Many2one(
  44. 'account.tax.group',
  45. index=True
  46. )
  47. # Data fields, used for report display
  48. code = fields.Char()
  49. name = fields.Char()
  50. net = fields.Float(digits=(16, 2))
  51. tax = fields.Float(digits=(16, 2))
  52. # Data fields, used to browse report data
  53. tax_ids = fields.One2many(
  54. comodel_name='report_vat_report_tax',
  55. inverse_name='report_tax_id',
  56. string='Taxes'
  57. )
  58. class VATReportTax(models.TransientModel):
  59. _name = 'report_vat_report_tax'
  60. _inherit = 'account_financial_report_abstract'
  61. _order = 'name ASC'
  62. report_tax_id = fields.Many2one(
  63. comodel_name='report_vat_report_taxtag',
  64. ondelete='cascade',
  65. index=True
  66. )
  67. # Data fields, used to keep link with real object
  68. tax_id = fields.Many2one(
  69. 'account.tax',
  70. index=True,
  71. string='Tax ID',
  72. )
  73. # Data fields, used for report display
  74. code = fields.Char()
  75. name = fields.Char()
  76. net = fields.Float(digits=(16, 2))
  77. tax = fields.Float(digits=(16, 2))
  78. class VATReportCompute(models.TransientModel):
  79. """ Here, we just define methods.
  80. For class fields, go more top at this file.
  81. """
  82. _inherit = 'report_vat_report'
  83. @api.multi
  84. def print_report(self, report_type='qweb'):
  85. self.ensure_one()
  86. if report_type == 'xlsx':
  87. report_name = 'a_f_r.report_vat_report_xlsx'
  88. else:
  89. report_name = 'account_financial_report.report_vat_report_qweb'
  90. context = dict(self.env.context)
  91. action = self.env['ir.actions.report'].search(
  92. [('report_name', '=', report_name),
  93. ('report_type', '=', report_type)], limit=1)
  94. return action.with_context(context).report_action(self, config=False)
  95. def _get_html(self):
  96. result = {}
  97. rcontext = {}
  98. context = dict(self.env.context)
  99. report = self.browse(context.get('active_id'))
  100. if report:
  101. rcontext['o'] = report
  102. result['html'] = self.env.ref(
  103. 'account_financial_report.report_vat_report').render(
  104. rcontext)
  105. return result
  106. @api.model
  107. def get_html(self, given_context=None):
  108. return self.with_context(given_context)._get_html()
  109. @api.multi
  110. def compute_data_for_report(self):
  111. self.ensure_one()
  112. # Compute report data
  113. if self.based_on == 'taxtags':
  114. self._inject_taxtags_values()
  115. self._inject_tax_taxtags_values()
  116. elif self.based_on == 'taxgroups':
  117. self._inject_taxgroups_values()
  118. self._inject_tax_taxgroups_values()
  119. # Refresh cache because all data are computed with SQL requests
  120. self.refresh()
  121. def _inject_taxtags_values(self):
  122. """Inject report values for report_vat_report_taxtags."""
  123. query_inject_taxtags = """
  124. WITH
  125. taxtags AS
  126. (SELECT coalesce(regexp_replace(tag.name,
  127. '[^0-9\\.]+', '', 'g'), ' ') AS code,
  128. tag.name, tag.id,
  129. coalesce(sum(movetax.tax_base_amount), 0.00) AS net,
  130. coalesce(sum(movetax.balance), 0.00) AS tax
  131. FROM
  132. account_account_tag AS tag
  133. INNER JOIN account_tax_account_tag AS taxtag
  134. ON tag.id = taxtag.account_account_tag_id
  135. INNER JOIN account_tax AS tax
  136. ON tax.id = taxtag.account_tax_id
  137. INNER JOIN account_move_line AS movetax
  138. ON movetax.tax_line_id = tax.id
  139. INNER JOIN account_move AS move
  140. ON move.id = movetax.move_id
  141. WHERE tag.id is not null AND movetax.tax_exigible
  142. AND move.company_id = %s AND move.date >= %s
  143. AND move.date <= %s AND move.state = 'posted'
  144. GROUP BY tag.id
  145. ORDER BY code, tag.name
  146. )
  147. INSERT INTO
  148. report_vat_report_taxtag
  149. (
  150. report_id,
  151. create_uid,
  152. create_date,
  153. taxtag_id,
  154. code,
  155. name,
  156. net, tax
  157. )
  158. SELECT
  159. %s AS report_id,
  160. %s AS create_uid,
  161. NOW() AS create_date,
  162. tag.id,
  163. tag.code,
  164. tag.name,
  165. abs(tag.net),
  166. abs(tag.tax)
  167. FROM
  168. taxtags tag
  169. """
  170. query_inject_taxtags_params = (self.company_id.id, self.date_from,
  171. self.date_to, self.id, self.env.uid)
  172. self.env.cr.execute(query_inject_taxtags, query_inject_taxtags_params)
  173. def _inject_taxgroups_values(self):
  174. """Inject report values for report_vat_report_taxtags."""
  175. query_inject_taxgroups = """
  176. WITH
  177. taxgroups AS
  178. (SELECT coalesce(taxgroup.sequence, 0) AS code,
  179. taxgroup.name, taxgroup.id,
  180. coalesce(sum(movetax.tax_base_amount), 0.00) AS net,
  181. coalesce(sum(movetax.balance), 0.00) AS tax
  182. FROM
  183. account_tax_group AS taxgroup
  184. INNER JOIN account_tax AS tax
  185. ON tax.tax_group_id = taxgroup.id
  186. INNER JOIN account_move_line AS movetax
  187. ON movetax.tax_line_id = tax.id
  188. INNER JOIN account_move AS move
  189. ON move.id = movetax.move_id
  190. WHERE taxgroup.id is not null AND movetax.tax_exigible
  191. AND move.company_id = %s AND move.date >= %s
  192. AND move.date <= %s AND move.state = 'posted'
  193. GROUP BY taxgroup.id
  194. ORDER BY code, taxgroup.name
  195. )
  196. INSERT INTO
  197. report_vat_report_taxtag
  198. (
  199. report_id,
  200. create_uid,
  201. create_date,
  202. taxgroup_id,
  203. code,
  204. name,
  205. net, tax
  206. )
  207. SELECT
  208. %s AS report_id,
  209. %s AS create_uid,
  210. NOW() AS create_date,
  211. groups.id,
  212. groups.code,
  213. groups.name,
  214. abs(groups.net),
  215. abs(groups.tax)
  216. FROM
  217. taxgroups groups
  218. """
  219. query_inject_taxgroups_params = (self.company_id.id, self.date_from,
  220. self.date_to, self.id, self.env.uid)
  221. self.env.cr.execute(query_inject_taxgroups,
  222. query_inject_taxgroups_params)
  223. def _inject_tax_taxtags_values(self):
  224. """ Inject report values for report_vat_report_tax. """
  225. # pylint: disable=sql-injection
  226. query_inject_tax = """
  227. WITH
  228. taxtags_tax AS
  229. (
  230. SELECT
  231. tag.id AS report_tax_id, ' ' AS code,
  232. tax.name, tax.id,
  233. coalesce(sum(movetax.tax_base_amount), 0.00) AS net,
  234. coalesce(sum(movetax.balance), 0.00) AS tax
  235. FROM
  236. report_vat_report_taxtag AS tag
  237. INNER JOIN account_tax_account_tag AS taxtag
  238. ON tag.taxtag_id = taxtag.account_account_tag_id
  239. INNER JOIN account_tax AS tax
  240. ON tax.id = taxtag.account_tax_id
  241. INNER JOIN account_move_line AS movetax
  242. ON movetax.tax_line_id = tax.id
  243. INNER JOIN account_move AS move
  244. ON move.id = movetax.move_id
  245. WHERE tag.id is not null AND movetax.tax_exigible
  246. AND tag.report_id = %s AND move.company_id = %s
  247. AND move.date >= %s AND move.date <= %s
  248. AND move.state = 'posted'
  249. GROUP BY tag.id, tax.id
  250. ORDER BY tax.name
  251. )
  252. INSERT INTO
  253. report_vat_report_tax
  254. (
  255. report_tax_id,
  256. create_uid,
  257. create_date,
  258. tax_id,
  259. name,
  260. net,
  261. tax
  262. )
  263. SELECT
  264. tt.report_tax_id,
  265. %s AS create_uid,
  266. NOW() AS create_date,
  267. tt.id,
  268. tt.name,
  269. abs(tt.net),
  270. abs(tt.tax)
  271. FROM
  272. taxtags_tax tt
  273. """
  274. query_inject_tax_params = (self.id, self.company_id.id, self.date_from,
  275. self.date_to, self.env.uid)
  276. self.env.cr.execute(query_inject_tax, query_inject_tax_params)
  277. def _inject_tax_taxgroups_values(self):
  278. """ Inject report values for report_vat_report_tax. """
  279. # pylint: disable=sql-injection
  280. query_inject_tax = """
  281. WITH
  282. taxtags_tax AS
  283. (
  284. SELECT
  285. taxtag.id AS report_tax_id, ' ' AS code,
  286. tax.name, tax.id,
  287. coalesce(sum(movetax.tax_base_amount), 0.00) AS net,
  288. coalesce(sum(movetax.balance), 0.00) AS tax
  289. FROM
  290. report_vat_report_taxtag AS taxtag
  291. INNER JOIN account_tax AS tax
  292. ON tax.tax_group_id = taxtag.taxgroup_id
  293. INNER JOIN account_move_line AS movetax
  294. ON movetax.tax_line_id = tax.id
  295. INNER JOIN account_move AS move
  296. ON move.id = movetax.move_id
  297. WHERE taxtag.id is not null AND movetax.tax_exigible
  298. AND taxtag.report_id = %s AND move.company_id = %s
  299. AND move.date >= %s AND move.date <= %s
  300. AND move.state = 'posted'
  301. GROUP BY taxtag.id, tax.id
  302. ORDER BY tax.name
  303. )
  304. INSERT INTO
  305. report_vat_report_tax
  306. (
  307. report_tax_id,
  308. create_uid,
  309. create_date,
  310. tax_id,
  311. name,
  312. net,
  313. tax
  314. )
  315. SELECT
  316. tt.report_tax_id,
  317. %s AS create_uid,
  318. NOW() AS create_date,
  319. tt.id,
  320. tt.name,
  321. abs(tt.net),
  322. abs(tt.tax)
  323. FROM
  324. taxtags_tax tt
  325. """
  326. query_inject_tax_params = (self.id, self.company_id.id, self.date_from,
  327. self.date_to, self.env.uid)
  328. self.env.cr.execute(query_inject_tax, query_inject_tax_params)