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.

295 lines
12 KiB

  1. # Author: Damien Crier
  2. # Author: Julien Coux
  3. # Copyright 2016 Camptocamp SA
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import _, models
  6. class GeneralLedgerXslx(models.AbstractModel):
  7. _name = "report.a_f_r.report_general_ledger_xlsx"
  8. _description = "General Ledger XLSL Report"
  9. _inherit = "report.account_financial_report.abstract_report_xlsx"
  10. def _get_report_name(self, report, data=False):
  11. company_id = data.get("company_id", False)
  12. report_name = _("General Ledger")
  13. if company_id:
  14. company = self.env["res.company"].browse(company_id)
  15. suffix = " - {} - {}".format(company.name, company.currency_id.name)
  16. report_name = report_name + suffix
  17. return report_name
  18. def _get_report_columns(self, report):
  19. res = {
  20. 0: {"header": _("Date"), "field": "date", "width": 11},
  21. 1: {"header": _("Entry"), "field": "entry", "width": 18},
  22. 2: {"header": _("Journal"), "field": "journal", "width": 8},
  23. 3: {"header": _("Account"), "field": "account", "width": 9},
  24. 4: {"header": _("Taxes"), "field": "taxes_description", "width": 15},
  25. 5: {"header": _("Partner"), "field": "partner_name", "width": 25},
  26. 6: {"header": _("Ref - Label"), "field": "ref", "width": 40},
  27. 7: {"header": _("Cost center"), "field": "cost_center", "width": 15},
  28. 8: {"header": _("Tags"), "field": "tags", "width": 10},
  29. 9: {"header": _("Rec."), "field": "rec_name", "width": 5},
  30. 10: {
  31. "header": _("Debit"),
  32. "field": "debit",
  33. "field_initial_balance": "initial_debit",
  34. "field_final_balance": "final_debit",
  35. "type": "amount",
  36. "width": 14,
  37. },
  38. 11: {
  39. "header": _("Credit"),
  40. "field": "credit",
  41. "field_initial_balance": "initial_credit",
  42. "field_final_balance": "final_credit",
  43. "type": "amount",
  44. "width": 14,
  45. },
  46. 12: {
  47. "header": _("Cumul. Bal."),
  48. "field": "balance",
  49. "field_initial_balance": "initial_balance",
  50. "field_final_balance": "final_balance",
  51. "type": "amount",
  52. "width": 14,
  53. },
  54. }
  55. if report.foreign_currency:
  56. foreign_currency = {
  57. 13: {
  58. "header": _("Cur."),
  59. "field": "currency_name",
  60. "field_currency_balance": "currency_name",
  61. "type": "currency_name",
  62. "width": 7,
  63. },
  64. 14: {
  65. "header": _("Amount cur."),
  66. "field": "bal_curr",
  67. "field_initial_balance": "initial_bal_curr",
  68. "field_final_balance": "final_bal_curr",
  69. "type": "amount_currency",
  70. "width": 14,
  71. },
  72. }
  73. res = {**res, **foreign_currency}
  74. return res
  75. def _get_report_filters(self, report):
  76. return [
  77. [
  78. _("Date range filter"),
  79. _("From: %s To: %s") % (report.date_from, report.date_to),
  80. ],
  81. [
  82. _("Target moves filter"),
  83. _("All posted entries")
  84. if report.target_move == "all"
  85. else _("All entries"),
  86. ],
  87. [
  88. _("Account balance at 0 filter"),
  89. _("Hide") if report.hide_account_at_0 else _("Show"),
  90. ],
  91. [_("Centralize filter"), _("Yes") if report.centralize else _("No")],
  92. [
  93. _("Show analytic tags"),
  94. _("Yes") if report.show_analytic_tags else _("No"),
  95. ],
  96. [
  97. _("Show foreign currency"),
  98. _("Yes") if report.foreign_currency else _("No"),
  99. ],
  100. ]
  101. def _get_col_count_filter_name(self):
  102. return 2
  103. def _get_col_count_filter_value(self):
  104. return 2
  105. def _get_col_pos_initial_balance_label(self):
  106. return 5
  107. def _get_col_count_final_balance_name(self):
  108. return 5
  109. def _get_col_pos_final_balance_label(self):
  110. return 5
  111. # flake8: noqa: C901
  112. def _generate_report_content(self, workbook, report, data):
  113. res_data = self.env[
  114. "report.account_financial_report.general_ledger"
  115. ]._get_report_values(report, data)
  116. general_ledger = res_data["general_ledger"]
  117. accounts_data = res_data["accounts_data"]
  118. partners_data = res_data["partners_data"]
  119. journals_data = res_data["journals_data"]
  120. taxes_data = res_data["taxes_data"]
  121. tags_data = res_data["tags_data"]
  122. filter_partner_ids = res_data["filter_partner_ids"]
  123. foreign_currency = res_data["foreign_currency"]
  124. # For each account
  125. for account in general_ledger:
  126. # Write account title
  127. self.write_array_title(
  128. account["code"] + " - " + accounts_data[account["id"]]["name"]
  129. )
  130. if not account["partners"]:
  131. # Display array header for move lines
  132. self.write_array_header()
  133. # Display initial balance line for account
  134. account.update(
  135. {
  136. "initial_debit": account["init_bal"]["debit"],
  137. "initial_credit": account["init_bal"]["credit"],
  138. "initial_balance": account["init_bal"]["balance"],
  139. }
  140. )
  141. if foreign_currency:
  142. account.update(
  143. {"initial_bal_curr": account["init_bal"]["bal_curr"]}
  144. )
  145. self.write_initial_balance_from_dict(account)
  146. # Display account move lines
  147. for line in account["move_lines"]:
  148. line.update(
  149. {
  150. "account": account["code"],
  151. "journal": journals_data[line["journal_id"]]["code"],
  152. }
  153. )
  154. if line["currency_id"]:
  155. line.update(
  156. {
  157. "currency_name": line["currency_id"][1],
  158. "currency_id": line["currency_id"][0],
  159. }
  160. )
  161. if line["ref"] != "Centralized entries":
  162. taxes_description = ""
  163. tags = ""
  164. for tax_id in line["tax_ids"]:
  165. taxes_description += (
  166. str(taxes_data[tax_id]["amount"])
  167. + " "
  168. + taxes_data[tax_id]["string"]
  169. + " "
  170. )
  171. for tag_id in line["tag_ids"]:
  172. tags += tags_data[tag_id]["name"] + " "
  173. line.update(
  174. {"taxes_description": taxes_description, "tags": tags}
  175. )
  176. self.write_line_from_dict(line)
  177. else:
  178. # For each partner
  179. for partner in account["list_partner"]:
  180. # Write partner title
  181. self.write_array_title(partners_data[partner["id"]]["name"])
  182. # Display array header for move lines
  183. self.write_array_header()
  184. # Display initial balance line for partner
  185. partner.update(
  186. {
  187. "initial_debit": partner["init_bal"]["debit"],
  188. "initial_credit": partner["init_bal"]["credit"],
  189. "initial_balance": partner["init_bal"]["balance"],
  190. "name": partners_data[partner["id"]]["name"],
  191. "type": "partner",
  192. "currency_id": accounts_data[account["id"]]["currency_id"],
  193. }
  194. )
  195. if foreign_currency:
  196. partner.update(
  197. {"initial_bal_culrr": partner["init_bal"]["bal_curr"]}
  198. )
  199. self.write_initial_balance_from_dict(partner)
  200. # Display account move lines
  201. for line in partner["move_lines"]:
  202. line.update(
  203. {
  204. "account": account["code"],
  205. "journal": journals_data[line["journal_id"]]["code"],
  206. }
  207. )
  208. if line["ref"] != "Centralized entries":
  209. taxes_description = ""
  210. tags = ""
  211. for tax_id in line["tax_ids"]:
  212. taxes_description += (
  213. str(taxes_data[tax_id]["amount"])
  214. + " "
  215. + taxes_data[tax_id]["string"]
  216. + " "
  217. )
  218. for tag_id in line["tag_ids"]:
  219. tags += tags_data[tag_id]["name"] + " "
  220. line.update(
  221. {"taxes_description": taxes_description, "tags": tags}
  222. )
  223. self.write_line_from_dict(line)
  224. # Display ending balance line for partner
  225. partner.update(
  226. {
  227. "final_debit": partner["fin_bal"]["debit"],
  228. "final_credit": partner["fin_bal"]["credit"],
  229. "final_balance": partner["fin_bal"]["balance"],
  230. }
  231. )
  232. if foreign_currency:
  233. partner.update(
  234. {"final_bal_curr": partner["fin_bal"]["bal_curr"]}
  235. )
  236. self.write_ending_balance_from_dict(partner)
  237. # Line break
  238. self.row_pos += 1
  239. # Display ending balance line for account
  240. if not filter_partner_ids:
  241. account.update(
  242. {
  243. "final_debit": account["fin_bal"]["debit"],
  244. "final_credit": account["fin_bal"]["credit"],
  245. "final_balance": account["fin_bal"]["balance"],
  246. }
  247. )
  248. if foreign_currency:
  249. account.update({"final_bal_curr": account["fin_bal"]["bal_curr"]})
  250. self.write_ending_balance_from_dict(account)
  251. # 2 lines break
  252. self.row_pos += 2
  253. def write_initial_balance_from_dict(self, my_object):
  254. """Specific function to write initial balance for General Ledger"""
  255. if "partner" in my_object["type"]:
  256. label = _("Partner Initial balance")
  257. elif "account" in my_object["type"]:
  258. label = _("Initial balance")
  259. super(GeneralLedgerXslx, self).write_initial_balance_from_dict(my_object, label)
  260. def write_ending_balance_from_dict(self, my_object):
  261. """Specific function to write ending balance for General Ledger"""
  262. if "partner" in my_object["type"]:
  263. name = my_object["name"]
  264. label = _("Partner ending balance")
  265. elif "account" in my_object["type"]:
  266. name = my_object["code"] + " - " + my_object["name"]
  267. label = _("Ending balance")
  268. super(GeneralLedgerXslx, self).write_ending_balance_from_dict(
  269. my_object, name, label
  270. )