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.

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