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.

193 lines
7.1 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.company,
  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(
  54. string="Show Partner Details",
  55. default=True,
  56. )
  57. account_code_from = fields.Many2one(
  58. comodel_name="account.account",
  59. string="Account Code From",
  60. help="Starting account in a range",
  61. )
  62. account_code_to = fields.Many2one(
  63. comodel_name="account.account",
  64. string="Account Code To",
  65. help="Ending account in a range",
  66. )
  67. @api.onchange("account_code_from", "account_code_to")
  68. def on_change_account_range(self):
  69. if (
  70. self.account_code_from
  71. and self.account_code_from.code.isdigit()
  72. and self.account_code_to
  73. and self.account_code_to.code.isdigit()
  74. ):
  75. start_range = int(self.account_code_from.code)
  76. end_range = int(self.account_code_to.code)
  77. self.account_ids = self.env["account.account"].search(
  78. [
  79. ("code", "in", [x for x in range(start_range, end_range + 1)]),
  80. ("reconcile", "=", True),
  81. ]
  82. )
  83. if self.company_id:
  84. self.account_ids = self.account_ids.filtered(
  85. lambda a: a.company_id == self.company_id
  86. )
  87. return {
  88. "domain": {
  89. "account_code_from": [("reconcile", "=", True)],
  90. "account_code_to": [("reconcile", "=", True)],
  91. }
  92. }
  93. def _default_foreign_currency(self):
  94. return self.env.user.has_group("base.group_multi_currency")
  95. @api.onchange("company_id")
  96. def onchange_company_id(self):
  97. """Handle company change."""
  98. if self.company_id and self.partner_ids:
  99. self.partner_ids = self.partner_ids.filtered(
  100. lambda p: p.company_id == self.company_id or not p.company_id
  101. )
  102. if self.company_id and self.account_ids:
  103. if self.receivable_accounts_only or self.payable_accounts_only:
  104. self.onchange_type_accounts_only()
  105. else:
  106. self.account_ids = self.account_ids.filtered(
  107. lambda a: a.company_id == self.company_id
  108. )
  109. res = {"domain": {"account_ids": [], "partner_ids": []}}
  110. if not self.company_id:
  111. return res
  112. else:
  113. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  114. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  115. return res
  116. @api.onchange("account_ids")
  117. def onchange_account_ids(self):
  118. return {"domain": {"account_ids": [("reconcile", "=", True)]}}
  119. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  120. def onchange_type_accounts_only(self):
  121. """Handle receivable/payable accounts only change."""
  122. domain = [("company_id", "=", self.company_id.id)]
  123. if self.receivable_accounts_only or self.payable_accounts_only:
  124. if self.receivable_accounts_only and self.payable_accounts_only:
  125. domain += [("internal_type", "in", ("receivable", "payable"))]
  126. elif self.receivable_accounts_only:
  127. domain += [("internal_type", "=", "receivable")]
  128. elif self.payable_accounts_only:
  129. domain += [("internal_type", "=", "payable")]
  130. self.account_ids = self.env["account.account"].search(domain)
  131. else:
  132. self.account_ids = None
  133. def _print_report(self, report_type):
  134. self.ensure_one()
  135. data = self._prepare_report_open_items()
  136. if report_type == "xlsx":
  137. report_name = "a_f_r.report_open_items_xlsx"
  138. else:
  139. report_name = "account_financial_report.open_items"
  140. return (
  141. self.env["ir.actions.report"]
  142. .search(
  143. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  144. limit=1,
  145. )
  146. .report_action(self, data=data)
  147. )
  148. def button_export_html(self):
  149. self.ensure_one()
  150. report_type = "qweb-html"
  151. return self._export(report_type)
  152. def button_export_pdf(self):
  153. self.ensure_one()
  154. report_type = "qweb-pdf"
  155. return self._export(report_type)
  156. def button_export_xlsx(self):
  157. self.ensure_one()
  158. report_type = "xlsx"
  159. return self._export(report_type)
  160. def _prepare_report_open_items(self):
  161. self.ensure_one()
  162. return {
  163. "wizard_id": self.id,
  164. "date_at": fields.Date.to_string(self.date_at),
  165. "date_from": self.date_from or False,
  166. "only_posted_moves": self.target_move == "posted",
  167. "hide_account_at_0": self.hide_account_at_0,
  168. "foreign_currency": self.foreign_currency,
  169. "show_partner_details": self.show_partner_details,
  170. "company_id": self.company_id.id,
  171. "target_move": self.target_move,
  172. "account_ids": self.account_ids.ids,
  173. "partner_ids": self.partner_ids.ids or [],
  174. "account_financial_report_lang": self.env.lang,
  175. }
  176. def _export(self, report_type):
  177. return self._print_report(report_type)