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.

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