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.

175 lines
5.8 KiB

  1. # Copyright 2017 ACSONE SA/NV
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import _, api, fields, models
  4. class JournalLedgerReportWizard(models.TransientModel):
  5. """Journal Ledger report wizard."""
  6. _name = "journal.ledger.report.wizard"
  7. _description = "Journal Ledger Report Wizard"
  8. company_id = fields.Many2one(
  9. comodel_name="res.company",
  10. default=lambda self: self.env.company,
  11. string="Company",
  12. required=False,
  13. ondelete="cascade",
  14. )
  15. date_range_id = fields.Many2one(comodel_name="date.range", string="Date range")
  16. date_from = fields.Date(string="Start date", required=True)
  17. date_to = fields.Date(string="End date", required=True)
  18. journal_ids = fields.Many2many(
  19. comodel_name="account.journal", string="Journals", required=False
  20. )
  21. move_target = fields.Selection(
  22. selection="_get_move_targets", default="posted", required=True
  23. )
  24. foreign_currency = fields.Boolean()
  25. sort_option = fields.Selection(
  26. selection="_get_sort_options",
  27. string="Sort entries by",
  28. default="move_name",
  29. required=True,
  30. )
  31. group_option = fields.Selection(
  32. selection="_get_group_options",
  33. string="Group entries by",
  34. default="journal",
  35. required=True,
  36. )
  37. with_account_name = fields.Boolean(default=False)
  38. @api.model
  39. def _get_move_targets(self):
  40. return [("all", _("All")), ("posted", _("Posted")), ("draft", _("Not Posted"))]
  41. @api.model
  42. def _get_sort_options(self):
  43. return [("move_name", _("Entry number")), ("date", _("Date"))]
  44. @api.model
  45. def _get_group_options(self):
  46. return [("journal", _("Journal")), ("none", _("No group"))]
  47. @api.onchange("date_range_id")
  48. def onchange_date_range_id(self):
  49. self.date_from = self.date_range_id.date_start
  50. self.date_to = self.date_range_id.date_end
  51. @api.onchange("company_id")
  52. def onchange_company_id(self):
  53. """Handle company change."""
  54. if (
  55. self.company_id
  56. and self.date_range_id.company_id
  57. and self.date_range_id.company_id != self.company_id
  58. ):
  59. self.date_range_id = False
  60. if self.company_id and self.journal_ids:
  61. self.journal_ids = self.journal_ids.filtered(
  62. lambda p: p.company_id == self.company_id or not p.company_id
  63. )
  64. res = {"domain": {"journal_ids": []}}
  65. if not self.company_id:
  66. return res
  67. else:
  68. res["domain"]["journal_ids"] += [("company_id", "=", self.company_id.id)]
  69. return res
  70. def _print_report(self, report_type):
  71. self.ensure_one()
  72. data = self._prepare_report_journal_ledger()
  73. if report_type == "xlsx":
  74. report_name = "a_f_r.report_journal_ledger_xlsx"
  75. else:
  76. report_name = "account_financial_report.journal_ledger"
  77. return (
  78. self.env["ir.actions.report"]
  79. .search(
  80. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  81. limit=1,
  82. )
  83. .report_action(self, data=data)
  84. )
  85. def button_export_html(self):
  86. self.ensure_one()
  87. report_type = "qweb-html"
  88. return self._export(report_type)
  89. def button_export_pdf(self):
  90. report_type = "qweb-pdf"
  91. return self._export(report_type)
  92. def button_export_xlsx(self):
  93. self.ensure_one()
  94. report_type = "xlsx"
  95. return self._export(report_type)
  96. def _prepare_report_journal_ledger(self):
  97. self.ensure_one()
  98. journals = self.journal_ids
  99. if not journals:
  100. # Not selecting a journal means that we'll display all journals
  101. journals = self.env["account.journal"].search(
  102. [("company_id", "=", self.company_id.id)]
  103. )
  104. return {
  105. "wizard_id": self.id,
  106. "date_from": self.date_from,
  107. "date_to": self.date_to,
  108. "move_target": self.move_target,
  109. "foreign_currency": self.foreign_currency,
  110. "company_id": self.company_id.id,
  111. "journal_ids": journals.ids,
  112. "sort_option": self.sort_option,
  113. "group_option": self.group_option,
  114. "with_account_name": self.with_account_name,
  115. "account_financial_report_lang": self.env.lang,
  116. }
  117. def _export(self, report_type):
  118. """Default export is PDF."""
  119. self.ensure_one()
  120. return self._print_report(report_type)
  121. @api.model
  122. def _get_ml_tax_description(
  123. self, move_line_data, tax_line_data, move_line_taxes_data
  124. ):
  125. taxes_description = ""
  126. if move_line_data["tax_line_id"]:
  127. taxes_description = tax_line_data["description"] or tax_line_data["name"]
  128. elif move_line_taxes_data:
  129. tax_names = []
  130. for tax_key in move_line_taxes_data:
  131. tax = move_line_taxes_data[tax_key]
  132. tax_names.append(tax["description"] or tax["name"])
  133. taxes_description = ",".join(tax_names)
  134. return taxes_description
  135. @api.model
  136. def _get_partner_name(self, partner_id, partner_data):
  137. if str(partner_id) in partner_data.keys():
  138. return partner_data[str(partner_id)]["name"]
  139. else:
  140. return ""
  141. @api.model
  142. def _get_atr_from_dict(self, obj_id, data, key):
  143. try:
  144. return data[obj_id][key]
  145. except KeyError:
  146. return data[str(obj_id)][key]
  147. @api.model
  148. def _get_data_from_dict(self, obj_id, data):
  149. if data:
  150. if isinstance(list(data.keys())[0], int):
  151. return data.get(obj_id, False)
  152. else:
  153. return data.get(obj_id(obj_id), False)
  154. else:
  155. return False