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.

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