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.

277 lines
11 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 operator
  5. from odoo import api, models
  6. class VATReport(models.AbstractModel):
  7. _name = "report.account_financial_report.vat_report"
  8. _description = "Vat Report Report"
  9. def _get_tax_data(self, tax_ids, hide_tax_at_zero):
  10. domain = []
  11. if hide_tax_at_zero:
  12. domain += [("id", "in", tax_ids)]
  13. taxes = self.env["account.tax"].search(domain)
  14. tax_data = {}
  15. for tax in taxes:
  16. tax_data.update(
  17. {
  18. tax.id: {
  19. "id": tax.id,
  20. "name": tax.name,
  21. "tax_group_id": tax.tax_group_id.id,
  22. "type_tax_use": tax.type_tax_use,
  23. "amount_type": tax.amount_type,
  24. "tags_ids": tax.invoice_repartition_line_ids.tag_ids.ids,
  25. }
  26. }
  27. )
  28. return tax_data
  29. @api.model
  30. def _get_tax_report_domain(self, company_id, date_from, date_to, only_posted_moves):
  31. domain = [
  32. ("company_id", "=", company_id),
  33. ("date", ">=", date_from),
  34. ("date", "<=", date_to),
  35. ("tax_line_id", "!=", False),
  36. ("tax_exigible", "=", True),
  37. ]
  38. if only_posted_moves:
  39. domain += [("move_id.state", "=", "posted")]
  40. return domain
  41. @api.model
  42. def _get_net_report_domain(self, company_id, date_from, date_to, only_posted_moves):
  43. domain = [
  44. ("company_id", "=", company_id),
  45. ("date", ">=", date_from),
  46. ("date", "<=", date_to),
  47. ("tax_exigible", "=", True),
  48. ]
  49. if only_posted_moves:
  50. domain += [("move_id.state", "=", "posted")]
  51. return domain
  52. def _get_vat_report_data(
  53. self, company_id, date_from, date_to, only_posted_moves, hide_tax_at_zero
  54. ):
  55. tax_domain = self._get_tax_report_domain(
  56. company_id, date_from, date_to, only_posted_moves
  57. )
  58. ml_fields = [
  59. "id",
  60. "tax_base_amount",
  61. "balance",
  62. "tax_line_id",
  63. "tax_ids",
  64. "analytic_tag_ids",
  65. "tag_ids",
  66. ]
  67. tax_move_lines = self.env["account.move.line"].search_read(
  68. domain=tax_domain, fields=ml_fields,
  69. )
  70. net_domain = self._get_net_report_domain(
  71. company_id, date_from, date_to, only_posted_moves
  72. )
  73. taxed_move_lines = self.env["account.move.line"].search_read(
  74. domain=net_domain, fields=ml_fields,
  75. )
  76. taxed_move_lines = list(filter(lambda d: d["tax_ids"], taxed_move_lines))
  77. vat_data = []
  78. for tax_move_line in tax_move_lines:
  79. vat_data.append(
  80. {
  81. "net": 0.0,
  82. "tax": tax_move_line["balance"],
  83. "tax_line_id": tax_move_line["tax_line_id"][0],
  84. }
  85. )
  86. for taxed_move_line in taxed_move_lines:
  87. for tax_id in taxed_move_line["tax_ids"]:
  88. vat_data.append(
  89. {
  90. "net": taxed_move_line["balance"],
  91. "tax": 0.0,
  92. "tax_line_id": tax_id,
  93. }
  94. )
  95. tax_ids = list(map(operator.itemgetter("tax_line_id"), vat_data))
  96. tax_ids = list(set(tax_ids))
  97. tax_data = self._get_tax_data(tax_ids, hide_tax_at_zero)
  98. return vat_data, tax_data
  99. def _get_tax_group_data(self, tax_group_ids):
  100. tax_groups = self.env["account.tax.group"].browse(tax_group_ids)
  101. tax_group_data = {}
  102. for tax_group in tax_groups:
  103. tax_group_data.update(
  104. {
  105. tax_group.id: {
  106. "id": tax_group.id,
  107. "name": tax_group.name,
  108. "code": str(tax_group.sequence),
  109. }
  110. }
  111. )
  112. return tax_group_data
  113. @api.model
  114. def _initialize_vat_report(self, tax_id, tag_or_group_id, vat_report, tax_data):
  115. if tag_or_group_id not in vat_report.keys():
  116. vat_report[tag_or_group_id] = {}
  117. vat_report[tag_or_group_id]["net"] = 0.0
  118. vat_report[tag_or_group_id]["tax"] = 0.0
  119. vat_report[tag_or_group_id][tax_id] = dict(tax_data[tax_id])
  120. vat_report[tag_or_group_id][tax_id].update({"net": 0.0, "tax": 0.0})
  121. else:
  122. if tax_id not in vat_report[tag_or_group_id].keys():
  123. vat_report[tag_or_group_id][tax_id] = dict(tax_data[tax_id])
  124. vat_report[tag_or_group_id][tax_id].update({"net": 0.0, "tax": 0.0})
  125. return vat_report
  126. def _get_vat_report_group_data(
  127. self, vat_report_data, tax_data, tax_detail, hide_tax_at_zero
  128. ):
  129. vat_report = {}
  130. if not hide_tax_at_zero:
  131. for tax_id in tax_data.keys():
  132. if tax_data[tax_id]["amount_type"] == "group":
  133. continue
  134. tax_group_id = tax_data[tax_id]["tax_group_id"]
  135. vat_report = self._initialize_vat_report(
  136. tax_id, tax_group_id, vat_report, tax_data
  137. )
  138. for tax_move_line in vat_report_data:
  139. tax_id = tax_move_line["tax_line_id"]
  140. if tax_data[tax_id]["amount_type"] == "group":
  141. continue
  142. tax_group_id = tax_data[tax_id]["tax_group_id"]
  143. vat_report = self._initialize_vat_report(
  144. tax_id, tax_group_id, vat_report, tax_data
  145. )
  146. vat_report[tax_group_id]["net"] += tax_move_line["net"]
  147. vat_report[tax_group_id]["tax"] += tax_move_line["tax"]
  148. vat_report[tax_group_id][tax_id]["net"] += tax_move_line["net"]
  149. vat_report[tax_group_id][tax_id]["tax"] += tax_move_line["tax"]
  150. tax_group_data = self._get_tax_group_data(vat_report.keys())
  151. vat_report_list = []
  152. for tax_group_id in vat_report.keys():
  153. vat_report_group = {}
  154. vat_report_group.update(
  155. {
  156. tax_group_id: {
  157. "name": tax_group_data[tax_group_id]["name"],
  158. "code": tax_group_data[tax_group_id]["code"],
  159. "net": vat_report[tax_group_id]["net"],
  160. "tax": vat_report[tax_group_id]["tax"],
  161. }
  162. }
  163. )
  164. if tax_detail:
  165. vat_report_group[tax_group_id]["taxes"] = []
  166. for tax_id in vat_report[tax_group_id]:
  167. if isinstance(tax_id, int):
  168. vat_report_group[tax_group_id]["taxes"].append(
  169. vat_report[tax_group_id][tax_id]
  170. )
  171. vat_report_list.append(vat_report_group[tax_group_id])
  172. return vat_report_list
  173. def _get_tags_data(self, tags_ids):
  174. tags = self.env["account.account.tag"].browse(tags_ids)
  175. tags_data = {}
  176. for tag in tags:
  177. tags_data.update({tag.id: {"code": "", "name": tag.name}})
  178. return tags_data
  179. def _get_vat_report_tag_data(
  180. self, vat_report_data, tax_data, tax_detail, hide_tax_at_zero
  181. ):
  182. vat_report = {}
  183. if not hide_tax_at_zero:
  184. for tax_id in tax_data.keys():
  185. if tax_data[tax_id]["amount_type"] == "group":
  186. continue
  187. tags_ids = tax_data[tax_id]["tags_ids"]
  188. if tags_ids:
  189. for tag_id in tags_ids:
  190. vat_report = self._initialize_vat_report(
  191. tax_id, tag_id, vat_report, tax_data
  192. )
  193. for tax_move_line in vat_report_data:
  194. tax_id = tax_move_line["tax_line_id"]
  195. tags_ids = tax_data[tax_id]["tags_ids"]
  196. if tax_data[tax_id]["amount_type"] == "group":
  197. continue
  198. else:
  199. if tags_ids:
  200. for tag_id in tags_ids:
  201. vat_report = self._initialize_vat_report(
  202. tax_id, tag_id, vat_report, tax_data
  203. )
  204. vat_report[tag_id][tax_id]["net"] += tax_move_line["net"]
  205. vat_report[tag_id][tax_id]["tax"] += tax_move_line["tax"]
  206. vat_report[tag_id]["net"] += tax_move_line["net"]
  207. vat_report[tag_id]["tax"] += tax_move_line["tax"]
  208. tags_data = self._get_tags_data(vat_report.keys())
  209. vat_report_list = []
  210. for tag_id in vat_report.keys():
  211. vat_report_tag = {}
  212. vat_report_tag.update(
  213. {
  214. tag_id: {
  215. "name": tags_data[tag_id]["name"],
  216. "code": tags_data[tag_id]["code"],
  217. "net": vat_report[tag_id]["net"],
  218. "tax": vat_report[tag_id]["tax"],
  219. }
  220. }
  221. )
  222. if tax_detail:
  223. vat_report_tag[tag_id]["taxes"] = []
  224. for tax_id in vat_report[tag_id]:
  225. if isinstance(tax_id, int):
  226. vat_report_tag[tag_id]["taxes"].append(
  227. vat_report[tag_id][tax_id]
  228. )
  229. vat_report_list.append(vat_report_tag[tag_id])
  230. return vat_report_list
  231. def _get_report_values(self, docids, data):
  232. wizard_id = data["wizard_id"]
  233. company = self.env["res.company"].browse(data["company_id"])
  234. company_id = data["company_id"]
  235. date_from = data["date_from"]
  236. date_to = data["date_to"]
  237. based_on = data["based_on"]
  238. tax_detail = data["tax_detail"]
  239. only_posted_moves = data["only_posted_moves"]
  240. hide_tax_at_zero = data["hide_tax_at_zero"]
  241. vat_report_data, tax_data = self._get_vat_report_data(
  242. company_id, date_from, date_to, only_posted_moves, hide_tax_at_zero
  243. )
  244. if based_on == "taxgroups":
  245. vat_report = self._get_vat_report_group_data(
  246. vat_report_data, tax_data, tax_detail, hide_tax_at_zero
  247. )
  248. else:
  249. vat_report = self._get_vat_report_tag_data(
  250. vat_report_data, tax_data, tax_detail, hide_tax_at_zero
  251. )
  252. return {
  253. "doc_ids": [wizard_id],
  254. "doc_model": "open.items.report.wizard",
  255. "docs": self.env["open.items.report.wizard"].browse(wizard_id),
  256. "company_name": company.display_name,
  257. "currency_name": company.currency_id.name,
  258. "date_to": data["date_to"],
  259. "date_from": data["date_from"],
  260. "based_on": data["based_on"],
  261. "tax_detail": data["tax_detail"],
  262. "vat_report": vat_report,
  263. }