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.

234 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. "tag_ids",
  61. ]
  62. tax_move_lines = self.env["account.move.line"].search_read(
  63. domain=tax_domain, 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, fields=ml_fields,
  70. )
  71. taxed_move_lines = list(filter(lambda d: d["tax_ids"], taxed_move_lines))
  72. vat_data = []
  73. for tax_move_line in tax_move_lines:
  74. vat_data.append(
  75. {
  76. "net": 0.0,
  77. "tax": tax_move_line["balance"],
  78. "tax_line_id": tax_move_line["tax_line_id"][0],
  79. }
  80. )
  81. for taxed_move_line in taxed_move_lines:
  82. for tax_id in taxed_move_line["tax_ids"]:
  83. vat_data.append(
  84. {
  85. "net": taxed_move_line["balance"],
  86. "tax": 0.0,
  87. "tax_line_id": tax_id,
  88. }
  89. )
  90. tax_ids = list(map(operator.itemgetter("tax_line_id"), vat_data))
  91. tax_ids = list(set(tax_ids))
  92. tax_data = self._get_tax_data(tax_ids)
  93. return vat_data, tax_data
  94. def _get_tax_group_data(self, tax_group_ids):
  95. tax_groups = self.env["account.tax.group"].browse(tax_group_ids)
  96. tax_group_data = {}
  97. for tax_group in tax_groups:
  98. tax_group_data.update(
  99. {
  100. tax_group.id: {
  101. "id": tax_group.id,
  102. "name": tax_group.name,
  103. "code": str(tax_group.sequence),
  104. }
  105. }
  106. )
  107. return tax_group_data
  108. def _get_vat_report_group_data(self, vat_report_data, tax_data, tax_detail):
  109. vat_report = {}
  110. for tax_move_line in vat_report_data:
  111. tax_id = tax_move_line["tax_line_id"]
  112. if tax_data[tax_id]["amount_type"] == "group":
  113. pass
  114. else:
  115. tax_group_id = tax_data[tax_id]["tax_group_id"]
  116. if tax_group_id not in vat_report.keys():
  117. vat_report[tax_group_id] = {}
  118. vat_report[tax_group_id]["net"] = 0.0
  119. vat_report[tax_group_id]["tax"] = 0.0
  120. vat_report[tax_group_id][tax_id] = dict(tax_data[tax_id])
  121. vat_report[tax_group_id][tax_id].update({"net": 0.0, "tax": 0.0})
  122. else:
  123. if tax_id not in vat_report[tax_group_id].keys():
  124. vat_report[tax_group_id][tax_id] = dict(tax_data[tax_id])
  125. vat_report[tax_group_id][tax_id].update(
  126. {"net": 0.0, "tax": 0.0}
  127. )
  128. vat_report[tax_group_id]["net"] += tax_move_line["net"]
  129. vat_report[tax_group_id]["tax"] += tax_move_line["tax"]
  130. vat_report[tax_group_id][tax_id]["net"] += tax_move_line["net"]
  131. vat_report[tax_group_id][tax_id]["tax"] += tax_move_line["tax"]
  132. tax_group_data = self._get_tax_group_data(vat_report.keys())
  133. vat_report_list = []
  134. for tax_group_id in vat_report.keys():
  135. vat_report[tax_group_id]["name"] = tax_group_data[tax_group_id]["name"]
  136. vat_report[tax_group_id]["code"] = tax_group_data[tax_group_id]["code"]
  137. if tax_detail:
  138. vat_report[tax_group_id]["taxes"] = []
  139. for tax_id in vat_report[tax_group_id]:
  140. if isinstance(tax_id, int):
  141. vat_report[tax_group_id]["taxes"].append(
  142. vat_report[tax_group_id][tax_id]
  143. )
  144. vat_report_list.append(vat_report[tax_group_id])
  145. return vat_report_list
  146. def _get_tags_data(self, tags_ids):
  147. tags = self.env["account.account.tag"].browse(tags_ids)
  148. tags_data = {}
  149. for tag in tags:
  150. tags_data.update({tag.id: {"code": "", "name": tag.name}})
  151. return tags_data
  152. def _get_vat_report_tag_data(self, vat_report_data, tax_data, tax_detail):
  153. vat_report = {}
  154. for tax_move_line in vat_report_data:
  155. tax_id = tax_move_line["tax_line_id"]
  156. tags_ids = tax_data[tax_id]["tags_ids"]
  157. if tax_data[tax_id]["amount_type"] == "group":
  158. continue
  159. else:
  160. if tags_ids:
  161. for tag_id in tags_ids:
  162. if tag_id not in vat_report.keys():
  163. vat_report[tag_id] = {}
  164. vat_report[tag_id]["net"] = 0.0
  165. vat_report[tag_id]["tax"] = 0.0
  166. vat_report[tag_id][tax_id] = dict(tax_data[tax_id])
  167. vat_report[tag_id][tax_id].update({"net": 0.0, "tax": 0.0})
  168. else:
  169. if tax_id not in vat_report[tag_id].keys():
  170. vat_report[tag_id][tax_id] = dict(tax_data[tax_id])
  171. vat_report[tag_id][tax_id].update(
  172. {"net": 0.0, "tax": 0.0}
  173. )
  174. vat_report[tag_id][tax_id]["net"] += tax_move_line["net"]
  175. vat_report[tag_id][tax_id]["tax"] += tax_move_line["tax"]
  176. vat_report[tag_id]["net"] += tax_move_line["net"]
  177. vat_report[tag_id]["tax"] += tax_move_line["tax"]
  178. tags_data = self._get_tags_data(vat_report.keys())
  179. vat_report_list = []
  180. for tag_id in vat_report.keys():
  181. vat_report[tag_id]["name"] = tags_data[tag_id]["name"]
  182. vat_report[tag_id]["code"] = tags_data[tag_id]["code"]
  183. if tax_detail:
  184. vat_report[tag_id]["taxes"] = []
  185. for tax_id in vat_report[tag_id]:
  186. if isinstance(tax_id, int):
  187. vat_report[tag_id]["taxes"].append(vat_report[tag_id][tax_id])
  188. vat_report_list.append(vat_report[tag_id])
  189. return vat_report_list
  190. def _get_report_values(self, docids, data):
  191. wizard_id = data["wizard_id"]
  192. company = self.env["res.company"].browse(data["company_id"])
  193. company_id = data["company_id"]
  194. date_from = data["date_from"]
  195. date_to = data["date_to"]
  196. based_on = data["based_on"]
  197. tax_detail = data["tax_detail"]
  198. only_posted_moves = data["only_posted_moves"]
  199. vat_report_data, tax_data = self._get_vat_report_data(
  200. company_id, date_from, date_to, only_posted_moves
  201. )
  202. if based_on == "taxgroups":
  203. vat_report = self._get_vat_report_group_data(
  204. vat_report_data, tax_data, tax_detail
  205. )
  206. else:
  207. vat_report = self._get_vat_report_tag_data(
  208. vat_report_data, tax_data, tax_detail
  209. )
  210. return {
  211. "doc_ids": [wizard_id],
  212. "doc_model": "open.items.report.wizard",
  213. "docs": self.env["open.items.report.wizard"].browse(wizard_id),
  214. "company_name": company.display_name,
  215. "currency_name": company.currency_id.name,
  216. "date_to": data["date_to"],
  217. "date_from": data["date_from"],
  218. "based_on": data["based_on"],
  219. "tax_detail": data["tax_detail"],
  220. "vat_report": vat_report,
  221. }