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.

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