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.

174 lines
5.7 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.user.company_id,
  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. }
  116. def _export(self, report_type):
  117. """Default export is PDF."""
  118. self.ensure_one()
  119. return self._print_report(report_type)
  120. @api.model
  121. def _get_ml_tax_description(
  122. self, move_line_data, tax_line_data, move_line_taxes_data
  123. ):
  124. taxes_description = ""
  125. if move_line_data["tax_line_id"]:
  126. taxes_description = tax_line_data["description"] or tax_line_data["name"]
  127. elif move_line_taxes_data:
  128. tax_names = []
  129. for tax_key in move_line_taxes_data:
  130. tax = move_line_taxes_data[tax_key]
  131. tax_names.append(tax["description"] or tax["name"])
  132. taxes_description = ",".join(tax_names)
  133. return taxes_description
  134. @api.model
  135. def _get_partner_name(self, partner_id, partner_data):
  136. if str(partner_id) in partner_data.keys():
  137. return partner_data[str(partner_id)]["name"]
  138. else:
  139. return ""
  140. @api.model
  141. def _get_atr_from_dict(self, obj_id, data, key):
  142. try:
  143. return data[obj_id][key]
  144. except KeyError:
  145. return data[str(obj_id)][key]
  146. @api.model
  147. def _get_data_from_dict(self, obj_id, data):
  148. if data:
  149. if isinstance(list(data.keys())[0], int):
  150. return data.get(obj_id, False)
  151. else:
  152. return data.get(obj_id(obj_id), False)
  153. else:
  154. return False