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.

269 lines
9.8 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 JournalLedgerXslx(models.AbstractModel):
  8. _name = "report.a_f_r.report_journal_ledger_xlsx"
  9. _description = "Journal Ledger XLSX 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 = _("Journal 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. columns = [
  21. {"header": _("Entry"), "field": "entry", "width": 18},
  22. {"header": _("Date"), "field": "date", "width": 11},
  23. {"header": _("Account"), "field": "account_code", "width": 9},
  24. ]
  25. if report.with_auto_sequence:
  26. columns.insert(
  27. 0, {"header": _("Sequence"), "field": "auto_sequence", "width": 10}
  28. )
  29. if report.with_account_name:
  30. columns.append(
  31. {"header": _("Account Name"), "field": "account_name", "width": 15}
  32. )
  33. columns += [
  34. {"header": _("Partner"), "field": "partner", "width": 25},
  35. {"header": _("Ref - Label"), "field": "label", "width": 40},
  36. {"header": _("Taxes"), "field": "taxes_description", "width": 11},
  37. {"header": _("Debit"), "field": "debit", "type": "amount", "width": 14},
  38. {"header": _("Credit"), "field": "credit", "type": "amount", "width": 14},
  39. ]
  40. if report.foreign_currency:
  41. columns += [
  42. {
  43. "header": _("Currency"),
  44. "field": "currency_name",
  45. "width": 14,
  46. "type": "currency_name",
  47. },
  48. {
  49. "header": _("Amount Currency"),
  50. "field": "amount_currency",
  51. "type": "amount",
  52. "width": 18,
  53. },
  54. ]
  55. columns_as_dict = {}
  56. for i, column in enumerate(columns):
  57. columns_as_dict[i] = column
  58. return columns_as_dict
  59. def _get_journal_tax_columns(self, report):
  60. return {
  61. 0: {"header": _("Name"), "field": "tax_name", "width": 35},
  62. 1: {"header": _("Description"), "field": "tax_code", "width": 18},
  63. 2: {
  64. "header": _("Base Debit"),
  65. "field": "base_debit",
  66. "type": "amount",
  67. "width": 14,
  68. },
  69. 3: {
  70. "header": _("Base Credit"),
  71. "field": "base_credit",
  72. "type": "amount",
  73. "width": 14,
  74. },
  75. 4: {
  76. "header": _("Base Balance"),
  77. "field": "base_balance",
  78. "type": "amount",
  79. "width": 14,
  80. },
  81. 5: {
  82. "header": _("Tax Debit"),
  83. "field": "tax_debit",
  84. "type": "amount",
  85. "width": 14,
  86. },
  87. 6: {
  88. "header": _("Tax Credit"),
  89. "field": "tax_credit",
  90. "type": "amount",
  91. "width": 14,
  92. },
  93. 7: {
  94. "header": _("Tax Balance"),
  95. "field": "tax_balance",
  96. "type": "amount",
  97. "width": 14,
  98. },
  99. }
  100. def _get_col_count_filter_name(self):
  101. return 2
  102. def _get_col_count_filter_value(self):
  103. return 3
  104. def _get_report_filters(self, report):
  105. target_label_by_value = {
  106. value: label
  107. for value, label in self.env[
  108. "journal.ledger.report.wizard"
  109. ]._get_move_targets()
  110. }
  111. sort_option_label_by_value = {
  112. value: label
  113. for value, label in self.env[
  114. "journal.ledger.report.wizard"
  115. ]._get_sort_options()
  116. }
  117. return [
  118. [_("Company"), report.company_id.name],
  119. [
  120. _("Date range filter"),
  121. _("From: %s To: %s") % (report.date_from, report.date_to),
  122. ],
  123. [
  124. _("Target moves filter"),
  125. _("%s") % target_label_by_value[report.move_target],
  126. ],
  127. [
  128. _("Entries sorted by"),
  129. _("%s") % sort_option_label_by_value[report.sort_option],
  130. ],
  131. [
  132. _("Journals"),
  133. ", ".join(
  134. [
  135. "{} - {}".format(report_journal.code, report_journal.name)
  136. for report_journal in report.journal_ids
  137. ]
  138. ),
  139. ],
  140. ]
  141. def _generate_report_content(self, workbook, report, data, report_data):
  142. res_data = self.env[
  143. "report.account_financial_report.journal_ledger"
  144. ]._get_report_values(report, data)
  145. group_option = report.group_option
  146. if group_option == "journal":
  147. for ledger in res_data["Journal_Ledgers"]:
  148. self._generate_journal_content(
  149. workbook, report, res_data, ledger, report_data
  150. )
  151. elif group_option == "none":
  152. self._generate_no_group_content(workbook, report, res_data, report_data)
  153. def _generate_no_group_content(self, workbook, report, res_data, report_data):
  154. self._generate_moves_content(
  155. workbook, "Report", report, res_data, res_data["Moves"], report_data
  156. )
  157. self._generate_no_group_taxes_summary(workbook, report, res_data, report_data)
  158. def _generate_journal_content(
  159. self, workbook, report, res_data, ledger, report_data
  160. ):
  161. journal = self.env["account.journal"].browse(ledger["id"])
  162. currency_name = (
  163. journal.currency_id
  164. and journal.currency_id.name
  165. or journal.company_id.currency_id.name
  166. )
  167. sheet_name = "{} ({}) - {}".format(journal.code, currency_name, journal.name)
  168. self._generate_moves_content(
  169. workbook, sheet_name, report, res_data, ledger["report_moves"], report_data
  170. )
  171. self._generate_journal_taxes_summary(workbook, ledger, report_data)
  172. def _generate_no_group_taxes_summary(self, workbook, report, res_data, report_data):
  173. self._generate_taxes_summary(
  174. workbook, "Tax Report", res_data["tax_line_data"], report_data
  175. )
  176. def _generate_journal_taxes_summary(self, workbook, ledger, report_data):
  177. journal = self.env["account.journal"].browse(ledger["id"])
  178. currency_name = (
  179. journal.currency_id
  180. and journal.currency_id.name
  181. or journal.company_id.currency_id.name
  182. )
  183. sheet_name = "Tax - {} ({}) - {}".format(
  184. journal.code, currency_name, journal.name
  185. )
  186. self._generate_taxes_summary(
  187. workbook, sheet_name, ledger["tax_lines"], report_data
  188. )
  189. def _generate_moves_content(
  190. self, workbook, sheet_name, report, res_data, moves, report_data
  191. ):
  192. report_data["workbook"] = workbook
  193. report_data["sheet"] = workbook.add_worksheet(sheet_name)
  194. self._set_column_width(report_data)
  195. report_data["row_pos"] = 1
  196. self.write_array_title(sheet_name, report_data)
  197. report_data["row_pos"] += 2
  198. self.write_array_header(report_data)
  199. account_ids_data = res_data["account_ids_data"]
  200. partner_ids_data = res_data["partner_ids_data"]
  201. currency_ids_data = res_data["currency_ids_data"]
  202. move_ids_data = res_data["move_ids_data"]
  203. for move in moves:
  204. for line in move["report_move_lines"]:
  205. currency_data = currency_ids_data.get(line["currency_id"], False)
  206. currency_name = currency_data and currency_data["name"] or ""
  207. account_data = account_ids_data.get(line["account_id"], False)
  208. account_name = account_data and account_data["name"] or ""
  209. account_code = account_data and account_data["code"] or ""
  210. move_data = move_ids_data.get(line["move_id"], False)
  211. move_entry = move_data and move_data["entry"] or ""
  212. line["partner"] = self._get_partner_name(
  213. line["partner_id"], partner_ids_data
  214. )
  215. line["auto_sequence"] = line["auto_sequence"]
  216. line["account_code"] = account_code
  217. line["account_name"] = account_name
  218. line["currency_name"] = currency_name
  219. line["entry"] = move_entry
  220. line["taxes_description"] = report._get_ml_tax_description(
  221. line,
  222. res_data["tax_line_data"].get(line["tax_line_id"]),
  223. res_data["move_line_ids_taxes_data"].get(
  224. line["move_line_id"], False
  225. ),
  226. )
  227. self.write_line_from_dict(line, report_data)
  228. report_data["row_pos"] += 1
  229. def _generate_taxes_summary(
  230. self, workbook, sheet_name, tax_lines_dict, report_data
  231. ):
  232. report_data["workbook"] = workbook
  233. report_data["sheet"] = workbook.add_worksheet(sheet_name)
  234. report_data["row_pos"] = 1
  235. self.write_array_title(sheet_name, report_data)
  236. report_data["row_pos"] += 2
  237. def _get_partner_name(self, partner_id, partner_data):
  238. if partner_id in partner_data.keys():
  239. return partner_data[partner_id]["name"]
  240. else:
  241. return ""