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.

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