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.

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