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.

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