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.

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