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.

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