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.

167 lines
6.2 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.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. receivable_accounts_only = fields.Boolean()
  32. payable_accounts_only = fields.Boolean()
  33. partner_ids = fields.Many2many(comodel_name="res.partner", string="Filter partners")
  34. show_move_line_details = fields.Boolean()
  35. account_code_from = fields.Many2one(
  36. comodel_name="account.account",
  37. string="Account Code From",
  38. help="Starting account in a range",
  39. )
  40. account_code_to = fields.Many2one(
  41. comodel_name="account.account",
  42. string="Account Code To",
  43. help="Ending account in a range",
  44. )
  45. @api.onchange("account_code_from", "account_code_to")
  46. def on_change_account_range(self):
  47. if (
  48. self.account_code_from
  49. and self.account_code_from.code.isdigit()
  50. and self.account_code_to
  51. and self.account_code_to.code.isdigit()
  52. ):
  53. start_range = int(self.account_code_from.code)
  54. end_range = int(self.account_code_to.code)
  55. self.account_ids = self.env["account.account"].search(
  56. [
  57. ("code", "in", [x for x in range(start_range, end_range + 1)]),
  58. ("reconcile", "=", True),
  59. ]
  60. )
  61. if self.company_id:
  62. self.account_ids = self.account_ids.filtered(
  63. lambda a: a.company_id == self.company_id
  64. )
  65. return {
  66. "domain": {
  67. "account_code_from": [("reconcile", "=", True)],
  68. "account_code_to": [("reconcile", "=", True)],
  69. }
  70. }
  71. @api.onchange("company_id")
  72. def onchange_company_id(self):
  73. """Handle company change."""
  74. if self.company_id and self.partner_ids:
  75. self.partner_ids = self.partner_ids.filtered(
  76. lambda p: p.company_id == self.company_id or not p.company_id
  77. )
  78. if self.company_id and self.account_ids:
  79. if self.receivable_accounts_only or self.payable_accounts_only:
  80. self.onchange_type_accounts_only()
  81. else:
  82. self.account_ids = self.account_ids.filtered(
  83. lambda a: a.company_id == self.company_id
  84. )
  85. res = {"domain": {"account_ids": [], "partner_ids": []}}
  86. if not self.company_id:
  87. return res
  88. else:
  89. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  90. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  91. return res
  92. @api.onchange("account_ids")
  93. def onchange_account_ids(self):
  94. return {"domain": {"account_ids": [("reconcile", "=", True)]}}
  95. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  96. def onchange_type_accounts_only(self):
  97. """Handle receivable/payable accounts only change."""
  98. domain = [("company_id", "=", self.company_id.id)]
  99. if self.receivable_accounts_only or self.payable_accounts_only:
  100. if self.receivable_accounts_only and self.payable_accounts_only:
  101. domain += [("internal_type", "in", ("receivable", "payable"))]
  102. elif self.receivable_accounts_only:
  103. domain += [("internal_type", "=", "receivable")]
  104. elif self.payable_accounts_only:
  105. domain += [("internal_type", "=", "payable")]
  106. self.account_ids = self.env["account.account"].search(domain)
  107. else:
  108. self.account_ids = None
  109. def _print_report(self, report_type):
  110. self.ensure_one()
  111. data = self._prepare_report_aged_partner_balance()
  112. if report_type == "xlsx":
  113. report_name = "a_f_r.report_aged_partner_balance_xlsx"
  114. else:
  115. report_name = "account_financial_report.aged_partner_balance"
  116. return (
  117. self.env["ir.actions.report"]
  118. .search(
  119. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  120. limit=1,
  121. )
  122. .report_action(self, data=data)
  123. )
  124. def button_export_html(self):
  125. self.ensure_one()
  126. report_type = "qweb-html"
  127. return self._export(report_type)
  128. def button_export_pdf(self):
  129. self.ensure_one()
  130. report_type = "qweb-pdf"
  131. return self._export(report_type)
  132. def button_export_xlsx(self):
  133. self.ensure_one()
  134. report_type = "xlsx"
  135. return self._export(report_type)
  136. def _prepare_report_aged_partner_balance(self):
  137. self.ensure_one()
  138. return {
  139. "wizard_id": self.id,
  140. "date_at": self.date_at,
  141. "date_from": self.date_from or False,
  142. "only_posted_moves": self.target_move == "posted",
  143. "company_id": self.company_id.id,
  144. "account_ids": self.account_ids.ids,
  145. "partner_ids": self.partner_ids.ids,
  146. "show_move_line_details": self.show_move_line_details,
  147. "account_financial_report_lang": self.env.lang,
  148. }
  149. def _export(self, report_type):
  150. """Default export is PDF."""
  151. return self._print_report(report_type)