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.

346 lines
14 KiB

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