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.

145 lines
5.5 KiB

  1. # Author: Damien Crier
  2. # Author: Julien Coux
  3. # Copyright 2016 Camptocamp SA
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import api, fields, models
  6. class OpenItemsReportWizard(models.TransientModel):
  7. """Open items report wizard."""
  8. _name = "open.items.report.wizard"
  9. _description = "Open Items Report Wizard"
  10. _inherit = "account_financial_report_abstract_wizard"
  11. company_id = fields.Many2one(
  12. comodel_name="res.company",
  13. default=lambda self: self.env.user.company_id,
  14. required=False,
  15. string="Company",
  16. )
  17. date_at = fields.Date(required=True, default=fields.Date.context_today)
  18. date_from = fields.Date(string="Date From")
  19. target_move = fields.Selection(
  20. [("posted", "All Posted Entries"), ("all", "All Entries")],
  21. string="Target Moves",
  22. required=True,
  23. default="posted",
  24. )
  25. account_ids = fields.Many2many(
  26. comodel_name="account.account",
  27. string="Filter accounts",
  28. domain=[("reconcile", "=", True)],
  29. )
  30. hide_account_at_0 = fields.Boolean(
  31. string="Hide account ending balance at 0",
  32. default=True,
  33. help="Use this filter to hide an account or a partner "
  34. "with an ending balance at 0. "
  35. "If partners are filtered, "
  36. "debits and credits totals will not match the trial balance.",
  37. )
  38. receivable_accounts_only = fields.Boolean()
  39. payable_accounts_only = fields.Boolean()
  40. partner_ids = fields.Many2many(
  41. comodel_name="res.partner",
  42. string="Filter partners",
  43. default=lambda self: self._default_partners(),
  44. )
  45. foreign_currency = fields.Boolean(
  46. string="Show foreign currency",
  47. help="Display foreign currency for move lines, unless "
  48. "account currency is not setup through chart of accounts "
  49. "will display initial and final balance in that currency.",
  50. default=lambda self: self._default_foreign_currency(),
  51. )
  52. def _default_foreign_currency(self):
  53. return self.env.user.has_group("base.group_multi_currency")
  54. @api.onchange("company_id")
  55. def onchange_company_id(self):
  56. """Handle company change."""
  57. if self.company_id and self.partner_ids:
  58. self.partner_ids = self.partner_ids.filtered(
  59. lambda p: p.company_id == self.company_id or not p.company_id
  60. )
  61. if self.company_id and self.account_ids:
  62. if self.receivable_accounts_only or self.payable_accounts_only:
  63. self.onchange_type_accounts_only()
  64. else:
  65. self.account_ids = self.account_ids.filtered(
  66. lambda a: a.company_id == self.company_id
  67. )
  68. res = {"domain": {"account_ids": [], "partner_ids": []}}
  69. if not self.company_id:
  70. return res
  71. else:
  72. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  73. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  74. return res
  75. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  76. def onchange_type_accounts_only(self):
  77. """Handle receivable/payable accounts only change."""
  78. domain = [("company_id", "=", self.company_id.id)]
  79. if self.receivable_accounts_only or self.payable_accounts_only:
  80. if self.receivable_accounts_only and self.payable_accounts_only:
  81. domain += [("internal_type", "in", ("receivable", "payable"))]
  82. elif self.receivable_accounts_only:
  83. domain += [("internal_type", "=", "receivable")]
  84. elif self.payable_accounts_only:
  85. domain += [("internal_type", "=", "payable")]
  86. elif not self.receivable_accounts_only and not self.payable_accounts_only:
  87. domain += [("reconcile", "=", True)]
  88. self.account_ids = self.env["account.account"].search(domain)
  89. def _print_report(self, report_type):
  90. self.ensure_one()
  91. data = self._prepare_report_open_items()
  92. if report_type == "xlsx":
  93. report_name = "a_f_r.report_open_items_xlsx"
  94. else:
  95. report_name = "account_financial_report.open_items"
  96. return (
  97. self.env["ir.actions.report"]
  98. .search(
  99. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  100. limit=1,
  101. )
  102. .report_action(self, data=data)
  103. )
  104. def button_export_html(self):
  105. self.ensure_one()
  106. report_type = "qweb-html"
  107. return self._export(report_type)
  108. def button_export_pdf(self):
  109. self.ensure_one()
  110. report_type = "qweb-pdf"
  111. return self._export(report_type)
  112. def button_export_xlsx(self):
  113. self.ensure_one()
  114. report_type = "xlsx"
  115. return self._export(report_type)
  116. def _prepare_report_open_items(self):
  117. self.ensure_one()
  118. return {
  119. "wizard_id": self.id,
  120. "date_at": fields.Date.to_string(self.date_at),
  121. "date_from": self.date_from or False,
  122. "only_posted_moves": self.target_move == "posted",
  123. "hide_account_at_0": self.hide_account_at_0,
  124. "foreign_currency": self.foreign_currency,
  125. "company_id": self.company_id.id,
  126. "target_move": self.target_move,
  127. "account_ids": self.account_ids.ids,
  128. "partner_ids": self.partner_ids.ids or [],
  129. }
  130. def _export(self, report_type):
  131. return self._print_report(report_type)