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.

164 lines
6.0 KiB

  1. # Author: Damien Crier, Andrea Stirpe, Kevin Graveman, Dennis Sluijk
  2. # Author: Julien Coux
  3. # Copyright 2016 Camptocamp SA, Onestein B.V.
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import api, fields, models
  6. class AgedPartnerBalanceWizard(models.TransientModel):
  7. """Aged partner balance report wizard."""
  8. _name = "aged.partner.balance.report.wizard"
  9. _description = "Aged Partner Balance 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. target_move = fields.Selection(
  19. [("posted", "All Posted Entries"), ("all", "All Entries")],
  20. string="Target Moves",
  21. required=True,
  22. default="posted",
  23. )
  24. account_ids = fields.Many2many(
  25. comodel_name="account.account",
  26. string="Filter accounts",
  27. domain=[("reconcile", "=", True)],
  28. required=True,
  29. )
  30. receivable_accounts_only = fields.Boolean()
  31. payable_accounts_only = fields.Boolean()
  32. partner_ids = fields.Many2many(comodel_name="res.partner", string="Filter partners")
  33. show_move_line_details = fields.Boolean()
  34. account_code_from = fields.Many2one(
  35. comodel_name="account.account",
  36. string="Account Code From",
  37. help="Starting account in a range",
  38. )
  39. account_code_to = fields.Many2one(
  40. comodel_name="account.account",
  41. string="Account Code To",
  42. help="Ending account in a range",
  43. )
  44. @api.onchange("account_code_from", "account_code_to")
  45. def on_change_account_range(self):
  46. if (
  47. self.account_code_from
  48. and self.account_code_from.code.isdigit()
  49. and self.account_code_to
  50. and self.account_code_to.code.isdigit()
  51. ):
  52. start_range = int(self.account_code_from.code)
  53. end_range = int(self.account_code_to.code)
  54. self.account_ids = self.env["account.account"].search(
  55. [
  56. ("code", "in", [x for x in range(start_range, end_range + 1)]),
  57. ("reconcile", "=", True),
  58. ]
  59. )
  60. if self.company_id:
  61. self.account_ids = self.account_ids.filtered(
  62. lambda a: a.company_id == self.company_id
  63. )
  64. return {
  65. "domain": {
  66. "account_code_from": [("reconcile", "=", True)],
  67. "account_code_to": [("reconcile", "=", True)],
  68. }
  69. }
  70. @api.onchange("company_id")
  71. def onchange_company_id(self):
  72. """Handle company change."""
  73. if self.company_id and self.partner_ids:
  74. self.partner_ids = self.partner_ids.filtered(
  75. lambda p: p.company_id == self.company_id or not p.company_id
  76. )
  77. if self.company_id and self.account_ids:
  78. if self.receivable_accounts_only or self.payable_accounts_only:
  79. self.onchange_type_accounts_only()
  80. else:
  81. self.account_ids = self.account_ids.filtered(
  82. lambda a: a.company_id == self.company_id
  83. )
  84. res = {"domain": {"account_ids": [], "partner_ids": []}}
  85. if not self.company_id:
  86. return res
  87. else:
  88. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  89. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  90. return res
  91. @api.onchange("account_ids")
  92. def onchange_account_ids(self):
  93. return {"domain": {"account_ids": [("reconcile", "=", True)]}}
  94. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  95. def onchange_type_accounts_only(self):
  96. """Handle receivable/payable accounts only change."""
  97. domain = [("company_id", "=", self.company_id.id)]
  98. if self.receivable_accounts_only or self.payable_accounts_only:
  99. if self.receivable_accounts_only and self.payable_accounts_only:
  100. domain += [("internal_type", "in", ("receivable", "payable"))]
  101. elif self.receivable_accounts_only:
  102. domain += [("internal_type", "=", "receivable")]
  103. elif self.payable_accounts_only:
  104. domain += [("internal_type", "=", "payable")]
  105. self.account_ids = self.env["account.account"].search(domain)
  106. else:
  107. self.account_ids = None
  108. def _print_report(self, report_type):
  109. self.ensure_one()
  110. data = self._prepare_report_aged_partner_balance()
  111. if report_type == "xlsx":
  112. report_name = "a_f_r.report_aged_partner_balance_xlsx"
  113. else:
  114. report_name = "account_financial_report.aged_partner_balance"
  115. return (
  116. self.env["ir.actions.report"]
  117. .search(
  118. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  119. limit=1,
  120. )
  121. .report_action(self, data=data)
  122. )
  123. def button_export_html(self):
  124. self.ensure_one()
  125. report_type = "qweb-html"
  126. return self._export(report_type)
  127. def button_export_pdf(self):
  128. self.ensure_one()
  129. report_type = "qweb-pdf"
  130. return self._export(report_type)
  131. def button_export_xlsx(self):
  132. self.ensure_one()
  133. report_type = "xlsx"
  134. return self._export(report_type)
  135. def _prepare_report_aged_partner_balance(self):
  136. self.ensure_one()
  137. return {
  138. "wizard_id": self.id,
  139. "date_at": self.date_at,
  140. "only_posted_moves": self.target_move == "posted",
  141. "company_id": self.company_id.id,
  142. "account_ids": self.account_ids.ids,
  143. "partner_ids": self.partner_ids.ids,
  144. "show_move_line_details": self.show_move_line_details,
  145. }
  146. def _export(self, report_type):
  147. """Default export is PDF."""
  148. return self._print_report(report_type)