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.

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