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.

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