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.

184 lines
6.9 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. date_at = fields.Date(required=True, default=fields.Date.context_today)
  12. date_from = fields.Date(string="Date From")
  13. target_move = fields.Selection(
  14. [("posted", "All Posted Entries"), ("all", "All Entries")],
  15. string="Target Moves",
  16. required=True,
  17. default="posted",
  18. )
  19. account_ids = fields.Many2many(
  20. comodel_name="account.account",
  21. string="Filter accounts",
  22. domain=[("reconcile", "=", True)],
  23. required=True,
  24. )
  25. hide_account_at_0 = fields.Boolean(
  26. string="Hide account ending balance at 0",
  27. default=True,
  28. help="Use this filter to hide an account or a partner "
  29. "with an ending balance at 0. "
  30. "If partners are filtered, "
  31. "debits and credits totals will not match the trial balance.",
  32. )
  33. receivable_accounts_only = fields.Boolean()
  34. payable_accounts_only = fields.Boolean()
  35. partner_ids = fields.Many2many(
  36. comodel_name="res.partner",
  37. string="Filter partners",
  38. default=lambda self: self._default_partners(),
  39. )
  40. foreign_currency = fields.Boolean(
  41. string="Show foreign currency",
  42. help="Display foreign currency for move lines, unless "
  43. "account currency is not setup through chart of accounts "
  44. "will display initial and final balance in that currency.",
  45. default=lambda self: self._default_foreign_currency(),
  46. )
  47. show_partner_details = fields.Boolean(string="Show Partner Details", default=True,)
  48. account_code_from = fields.Many2one(
  49. comodel_name="account.account",
  50. string="Account Code From",
  51. help="Starting account in a range",
  52. )
  53. account_code_to = fields.Many2one(
  54. comodel_name="account.account",
  55. string="Account Code To",
  56. help="Ending account in a range",
  57. )
  58. @api.onchange("account_code_from", "account_code_to")
  59. def on_change_account_range(self):
  60. if (
  61. self.account_code_from
  62. and self.account_code_from.code.isdigit()
  63. and self.account_code_to
  64. and self.account_code_to.code.isdigit()
  65. ):
  66. start_range = int(self.account_code_from.code)
  67. end_range = int(self.account_code_to.code)
  68. self.account_ids = self.env["account.account"].search(
  69. [
  70. ("code", "in", [x for x in range(start_range, end_range + 1)]),
  71. ("reconcile", "=", True),
  72. ]
  73. )
  74. if self.company_id:
  75. self.account_ids = self.account_ids.filtered(
  76. lambda a: a.company_id == self.company_id
  77. )
  78. return {
  79. "domain": {
  80. "account_code_from": [("reconcile", "=", True)],
  81. "account_code_to": [("reconcile", "=", True)],
  82. }
  83. }
  84. def _default_foreign_currency(self):
  85. return self.env.user.has_group("base.group_multi_currency")
  86. @api.onchange("company_id")
  87. def onchange_company_id(self):
  88. """Handle company change."""
  89. if self.company_id and self.partner_ids:
  90. self.partner_ids = self.partner_ids.filtered(
  91. lambda p: p.company_id == self.company_id or not p.company_id
  92. )
  93. if self.company_id and self.account_ids:
  94. if self.receivable_accounts_only or self.payable_accounts_only:
  95. self.onchange_type_accounts_only()
  96. else:
  97. self.account_ids = self.account_ids.filtered(
  98. lambda a: a.company_id == self.company_id
  99. )
  100. res = {"domain": {"account_ids": [], "partner_ids": []}}
  101. if not self.company_id:
  102. return res
  103. else:
  104. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  105. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  106. return res
  107. @api.onchange("account_ids")
  108. def onchange_account_ids(self):
  109. return {"domain": {"account_ids": [("reconcile", "=", True)]}}
  110. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  111. def onchange_type_accounts_only(self):
  112. """Handle receivable/payable accounts only change."""
  113. domain = [("company_id", "=", self.company_id.id)]
  114. if self.receivable_accounts_only or self.payable_accounts_only:
  115. if self.receivable_accounts_only and self.payable_accounts_only:
  116. domain += [("internal_type", "in", ("receivable", "payable"))]
  117. elif self.receivable_accounts_only:
  118. domain += [("internal_type", "=", "receivable")]
  119. elif self.payable_accounts_only:
  120. domain += [("internal_type", "=", "payable")]
  121. self.account_ids = self.env["account.account"].search(domain)
  122. else:
  123. self.account_ids = None
  124. def _print_report(self, report_type):
  125. self.ensure_one()
  126. data = self._prepare_report_open_items()
  127. if report_type == "xlsx":
  128. report_name = "a_f_r.report_open_items_xlsx"
  129. else:
  130. report_name = "account_financial_report.open_items"
  131. return (
  132. self.env["ir.actions.report"]
  133. .search(
  134. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  135. limit=1,
  136. )
  137. .report_action(self, data=data)
  138. )
  139. def button_export_html(self):
  140. self.ensure_one()
  141. report_type = "qweb-html"
  142. return self._export(report_type)
  143. def button_export_pdf(self):
  144. self.ensure_one()
  145. report_type = "qweb-pdf"
  146. return self._export(report_type)
  147. def button_export_xlsx(self):
  148. self.ensure_one()
  149. report_type = "xlsx"
  150. return self._export(report_type)
  151. def _prepare_report_open_items(self):
  152. self.ensure_one()
  153. return {
  154. "wizard_id": self.id,
  155. "date_at": fields.Date.to_string(self.date_at),
  156. "date_from": self.date_from or False,
  157. "only_posted_moves": self.target_move == "posted",
  158. "hide_account_at_0": self.hide_account_at_0,
  159. "foreign_currency": self.foreign_currency,
  160. "show_partner_details": self.show_partner_details,
  161. "company_id": self.company_id.id,
  162. "target_move": self.target_move,
  163. "account_ids": self.account_ids.ids,
  164. "partner_ids": self.partner_ids.ids or [],
  165. "account_financial_report_lang": self.env.lang,
  166. }
  167. def _export(self, report_type):
  168. return self._print_report(report_type)