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.

267 lines
10 KiB

  1. # Author: Julien Coux
  2. # Copyright 2016 Camptocamp SA
  3. # Copyright 2017 Akretion - Alexis de Lattre
  4. # Copyright 2018 Eficent Business and IT Consuting Services, S.L.
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from odoo import _, api, fields, models
  7. from odoo.exceptions import UserError, ValidationError
  8. class TrialBalanceReportWizard(models.TransientModel):
  9. """Trial balance report wizard."""
  10. _name = "trial.balance.report.wizard"
  11. _description = "Trial Balance Report Wizard"
  12. _inherit = "account_financial_report_abstract_wizard"
  13. company_id = fields.Many2one(
  14. comodel_name="res.company",
  15. default=lambda self: self.env.user.company_id,
  16. required=False,
  17. string="Company",
  18. )
  19. date_range_id = fields.Many2one(comodel_name="date.range", string="Date range")
  20. date_from = fields.Date(required=True)
  21. date_to = fields.Date(required=True)
  22. fy_start_date = fields.Date(compute="_compute_fy_start_date")
  23. target_move = fields.Selection(
  24. [("posted", "All Posted Entries"), ("all", "All Entries")],
  25. string="Target Moves",
  26. required=True,
  27. default="posted",
  28. )
  29. hierarchy_on = fields.Selection(
  30. [
  31. ("computed", "Computed Accounts"),
  32. ("relation", "Child Accounts"),
  33. ("none", "No hierarchy"),
  34. ],
  35. string="Hierarchy On",
  36. required=True,
  37. default="none",
  38. help="""Computed Accounts: Use when the account group have codes
  39. that represent prefixes of the actual accounts.\n
  40. Child Accounts: Use when your account groups are hierarchical.\n
  41. No hierarchy: Use to display just the accounts, without any grouping.
  42. """,
  43. )
  44. limit_hierarchy_level = fields.Boolean("Limit hierarchy levels")
  45. show_hierarchy_level = fields.Integer("Hierarchy Levels to display", default=1)
  46. hide_parent_hierarchy_level = fields.Boolean(
  47. "Do not display parent levels", default=False
  48. )
  49. account_ids = fields.Many2many(
  50. comodel_name="account.account", string="Filter accounts"
  51. )
  52. hide_account_at_0 = fields.Boolean(
  53. string="Hide accounts at 0",
  54. default=True,
  55. help="When this option is enabled, the trial balance will "
  56. "not display accounts that have initial balance = "
  57. "debit = credit = end balance = 0",
  58. )
  59. receivable_accounts_only = fields.Boolean()
  60. payable_accounts_only = fields.Boolean()
  61. show_partner_details = fields.Boolean()
  62. partner_ids = fields.Many2many(comodel_name="res.partner", string="Filter partners")
  63. journal_ids = fields.Many2many(comodel_name="account.journal")
  64. not_only_one_unaffected_earnings_account = fields.Boolean(
  65. readonly=True, string="Not only one unaffected earnings account"
  66. )
  67. foreign_currency = fields.Boolean(
  68. string="Show foreign currency",
  69. help="Display foreign currency for move lines, unless "
  70. "account currency is not setup through chart of accounts "
  71. "will display initial and final balance in that currency.",
  72. )
  73. @api.constrains("hierarchy_on", "show_hierarchy_level")
  74. def _check_show_hierarchy_level(self):
  75. for rec in self:
  76. if rec.hierarchy_on != "none" and rec.show_hierarchy_level <= 0:
  77. raise UserError(
  78. _("The hierarchy level to filter on must be " "greater than 0.")
  79. )
  80. @api.depends("date_from")
  81. def _compute_fy_start_date(self):
  82. for wiz in self:
  83. if wiz.date_from:
  84. res = self.company_id.compute_fiscalyear_dates(wiz.date_from)
  85. wiz.fy_start_date = res["date_from"]
  86. else:
  87. wiz.fy_start_date = False
  88. @api.onchange("company_id")
  89. def onchange_company_id(self):
  90. """Handle company change."""
  91. account_type = self.env.ref("account.data_unaffected_earnings")
  92. count = self.env["account.account"].search_count(
  93. [
  94. ("user_type_id", "=", account_type.id),
  95. ("company_id", "=", self.company_id.id),
  96. ]
  97. )
  98. self.not_only_one_unaffected_earnings_account = count != 1
  99. if (
  100. self.company_id
  101. and self.date_range_id.company_id
  102. and self.date_range_id.company_id != self.company_id
  103. ):
  104. self.date_range_id = False
  105. if self.company_id and self.partner_ids:
  106. self.partner_ids = self.partner_ids.filtered(
  107. lambda p: p.company_id == self.company_id or not p.company_id
  108. )
  109. if self.company_id and self.journal_ids:
  110. self.journal_ids = self.journal_ids.filtered(
  111. lambda a: a.company_id == self.company_id
  112. )
  113. if self.company_id and self.account_ids:
  114. if self.receivable_accounts_only or self.payable_accounts_only:
  115. self.onchange_type_accounts_only()
  116. else:
  117. self.account_ids = self.account_ids.filtered(
  118. lambda a: a.company_id == self.company_id
  119. )
  120. res = {
  121. "domain": {
  122. "account_ids": [],
  123. "partner_ids": [],
  124. "date_range_id": [],
  125. "journal_ids": [],
  126. }
  127. }
  128. if not self.company_id:
  129. return res
  130. else:
  131. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  132. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  133. res["domain"]["date_range_id"] += [
  134. "|",
  135. ("company_id", "=", self.company_id.id),
  136. ("company_id", "=", False),
  137. ]
  138. res["domain"]["journal_ids"] += [("company_id", "=", self.company_id.id)]
  139. return res
  140. @api.onchange("date_range_id")
  141. def onchange_date_range_id(self):
  142. """Handle date range change."""
  143. self.date_from = self.date_range_id.date_start
  144. self.date_to = self.date_range_id.date_end
  145. @api.constrains("company_id", "date_range_id")
  146. def _check_company_id_date_range_id(self):
  147. for rec in self.sudo():
  148. if (
  149. rec.company_id
  150. and rec.date_range_id.company_id
  151. and rec.company_id != rec.date_range_id.company_id
  152. ):
  153. raise ValidationError(
  154. _(
  155. "The Company in the Trial Balance Report Wizard and in "
  156. "Date Range must be the same."
  157. )
  158. )
  159. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  160. def onchange_type_accounts_only(self):
  161. """Handle receivable/payable accounts only change."""
  162. if self.receivable_accounts_only or self.payable_accounts_only:
  163. domain = [("company_id", "=", self.company_id.id)]
  164. if self.receivable_accounts_only and self.payable_accounts_only:
  165. domain += [("internal_type", "in", ("receivable", "payable"))]
  166. elif self.receivable_accounts_only:
  167. domain += [("internal_type", "=", "receivable")]
  168. elif self.payable_accounts_only:
  169. domain += [("internal_type", "=", "payable")]
  170. self.account_ids = self.env["account.account"].search(domain)
  171. else:
  172. self.account_ids = None
  173. @api.onchange("show_partner_details")
  174. def onchange_show_partner_details(self):
  175. """Handle partners change."""
  176. if self.show_partner_details:
  177. self.receivable_accounts_only = self.payable_accounts_only = True
  178. else:
  179. self.receivable_accounts_only = self.payable_accounts_only = False
  180. @api.depends("company_id")
  181. def _compute_unaffected_earnings_account(self):
  182. account_type = self.env.ref("account.data_unaffected_earnings")
  183. for record in self:
  184. record.unaffected_earnings_account = self.env["account.account"].search(
  185. [
  186. ("user_type_id", "=", account_type.id),
  187. ("company_id", "=", record.company_id.id),
  188. ]
  189. )
  190. unaffected_earnings_account = fields.Many2one(
  191. comodel_name="account.account",
  192. compute="_compute_unaffected_earnings_account",
  193. store=True,
  194. )
  195. def _print_report(self, report_type):
  196. self.ensure_one()
  197. data = self._prepare_report_trial_balance()
  198. if report_type == "xlsx":
  199. report_name = "a_f_r.report_trial_balance_xlsx"
  200. else:
  201. report_name = "account_financial_report.trial_balance"
  202. return (
  203. self.env["ir.actions.report"]
  204. .search(
  205. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  206. limit=1,
  207. )
  208. .report_action(self, data=data)
  209. )
  210. def button_export_html(self):
  211. self.ensure_one()
  212. report_type = "qweb-html"
  213. return self._export(report_type)
  214. def button_export_pdf(self):
  215. self.ensure_one()
  216. report_type = "qweb-pdf"
  217. return self._export(report_type)
  218. def button_export_xlsx(self):
  219. self.ensure_one()
  220. report_type = "xlsx"
  221. return self._export(report_type)
  222. def _prepare_report_trial_balance(self):
  223. self.ensure_one()
  224. return {
  225. "wizard_id": self.id,
  226. "date_from": self.date_from,
  227. "date_to": self.date_to,
  228. "only_posted_moves": self.target_move == "posted",
  229. "hide_account_at_0": self.hide_account_at_0,
  230. "foreign_currency": self.foreign_currency,
  231. "company_id": self.company_id.id,
  232. "account_ids": self.account_ids.ids or [],
  233. "partner_ids": self.partner_ids.ids or [],
  234. "journal_ids": self.journal_ids.ids or [],
  235. "fy_start_date": self.fy_start_date,
  236. "hierarchy_on": self.hierarchy_on,
  237. "limit_hierarchy_level": self.limit_hierarchy_level,
  238. "show_hierarchy_level": self.show_hierarchy_level,
  239. "hide_parent_hierarchy_level": self.hide_parent_hierarchy_level,
  240. "show_partner_details": self.show_partner_details,
  241. "unaffected_earnings_account": self.unaffected_earnings_account.id,
  242. }
  243. def _export(self, report_type):
  244. """Default export is PDF."""
  245. return self._print_report(report_type)