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.

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