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.

296 lines
11 KiB

  1. # Author: Julien Coux
  2. # Copyright 2016 Camptocamp SA
  3. # Copyright 2017 Akretion - Alexis de Lattre
  4. # Copyright 2018 ForgeFlow, 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. account_code_from = fields.Many2one(
  74. comodel_name="account.account",
  75. string="Account Code From",
  76. help="Starting account in a range",
  77. )
  78. account_code_to = fields.Many2one(
  79. comodel_name="account.account",
  80. string="Account Code To",
  81. help="Ending account in a range",
  82. )
  83. @api.onchange("account_code_from", "account_code_to")
  84. def on_change_account_range(self):
  85. if (
  86. self.account_code_from
  87. and self.account_code_from.code.isdigit()
  88. and self.account_code_to
  89. and self.account_code_to.code.isdigit()
  90. ):
  91. start_range = int(self.account_code_from.code)
  92. end_range = int(self.account_code_to.code)
  93. self.account_ids = self.env["account.account"].search(
  94. [("code", "in", [x for x in range(start_range, end_range + 1)])]
  95. )
  96. if self.company_id:
  97. self.account_ids = self.account_ids.filtered(
  98. lambda a: a.company_id == self.company_id
  99. )
  100. @api.constrains("hierarchy_on", "show_hierarchy_level")
  101. def _check_show_hierarchy_level(self):
  102. for rec in self:
  103. if rec.hierarchy_on != "none" and rec.show_hierarchy_level <= 0:
  104. raise UserError(
  105. _("The hierarchy level to filter on must be " "greater than 0.")
  106. )
  107. @api.depends("date_from")
  108. def _compute_fy_start_date(self):
  109. for wiz in self:
  110. if wiz.date_from:
  111. res = self.company_id.compute_fiscalyear_dates(wiz.date_from)
  112. wiz.fy_start_date = res["date_from"]
  113. else:
  114. wiz.fy_start_date = False
  115. @api.onchange("company_id")
  116. def onchange_company_id(self):
  117. """Handle company change."""
  118. account_type = self.env.ref("account.data_unaffected_earnings")
  119. count = self.env["account.account"].search_count(
  120. [
  121. ("user_type_id", "=", account_type.id),
  122. ("company_id", "=", self.company_id.id),
  123. ]
  124. )
  125. self.not_only_one_unaffected_earnings_account = count != 1
  126. if (
  127. self.company_id
  128. and self.date_range_id.company_id
  129. and self.date_range_id.company_id != self.company_id
  130. ):
  131. self.date_range_id = False
  132. if self.company_id and self.partner_ids:
  133. self.partner_ids = self.partner_ids.filtered(
  134. lambda p: p.company_id == self.company_id or not p.company_id
  135. )
  136. if self.company_id and self.journal_ids:
  137. self.journal_ids = self.journal_ids.filtered(
  138. lambda a: a.company_id == self.company_id
  139. )
  140. if self.company_id and self.account_ids:
  141. if self.receivable_accounts_only or self.payable_accounts_only:
  142. self.onchange_type_accounts_only()
  143. else:
  144. self.account_ids = self.account_ids.filtered(
  145. lambda a: a.company_id == self.company_id
  146. )
  147. res = {
  148. "domain": {
  149. "account_ids": [],
  150. "partner_ids": [],
  151. "date_range_id": [],
  152. "journal_ids": [],
  153. }
  154. }
  155. if not self.company_id:
  156. return res
  157. else:
  158. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  159. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  160. res["domain"]["date_range_id"] += [
  161. "|",
  162. ("company_id", "=", self.company_id.id),
  163. ("company_id", "=", False),
  164. ]
  165. res["domain"]["journal_ids"] += [("company_id", "=", self.company_id.id)]
  166. return res
  167. @api.onchange("date_range_id")
  168. def onchange_date_range_id(self):
  169. """Handle date range change."""
  170. self.date_from = self.date_range_id.date_start
  171. self.date_to = self.date_range_id.date_end
  172. @api.constrains("company_id", "date_range_id")
  173. def _check_company_id_date_range_id(self):
  174. for rec in self.sudo():
  175. if (
  176. rec.company_id
  177. and rec.date_range_id.company_id
  178. and rec.company_id != rec.date_range_id.company_id
  179. ):
  180. raise ValidationError(
  181. _(
  182. "The Company in the Trial Balance Report Wizard and in "
  183. "Date Range must be the same."
  184. )
  185. )
  186. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  187. def onchange_type_accounts_only(self):
  188. """Handle receivable/payable accounts only change."""
  189. if self.receivable_accounts_only or self.payable_accounts_only:
  190. domain = [("company_id", "=", self.company_id.id)]
  191. if self.receivable_accounts_only and self.payable_accounts_only:
  192. domain += [("internal_type", "in", ("receivable", "payable"))]
  193. elif self.receivable_accounts_only:
  194. domain += [("internal_type", "=", "receivable")]
  195. elif self.payable_accounts_only:
  196. domain += [("internal_type", "=", "payable")]
  197. self.account_ids = self.env["account.account"].search(domain)
  198. else:
  199. self.account_ids = None
  200. @api.onchange("show_partner_details")
  201. def onchange_show_partner_details(self):
  202. """Handle partners change."""
  203. if self.show_partner_details:
  204. self.receivable_accounts_only = self.payable_accounts_only = True
  205. else:
  206. self.receivable_accounts_only = self.payable_accounts_only = False
  207. @api.depends("company_id")
  208. def _compute_unaffected_earnings_account(self):
  209. account_type = self.env.ref("account.data_unaffected_earnings")
  210. for record in self:
  211. record.unaffected_earnings_account = self.env["account.account"].search(
  212. [
  213. ("user_type_id", "=", account_type.id),
  214. ("company_id", "=", record.company_id.id),
  215. ]
  216. )
  217. unaffected_earnings_account = fields.Many2one(
  218. comodel_name="account.account",
  219. compute="_compute_unaffected_earnings_account",
  220. store=True,
  221. )
  222. def _print_report(self, report_type):
  223. self.ensure_one()
  224. data = self._prepare_report_trial_balance()
  225. if report_type == "xlsx":
  226. report_name = "a_f_r.report_trial_balance_xlsx"
  227. else:
  228. report_name = "account_financial_report.trial_balance"
  229. return (
  230. self.env["ir.actions.report"]
  231. .search(
  232. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  233. limit=1,
  234. )
  235. .report_action(self, data=data)
  236. )
  237. def button_export_html(self):
  238. self.ensure_one()
  239. report_type = "qweb-html"
  240. return self._export(report_type)
  241. def button_export_pdf(self):
  242. self.ensure_one()
  243. report_type = "qweb-pdf"
  244. return self._export(report_type)
  245. def button_export_xlsx(self):
  246. self.ensure_one()
  247. report_type = "xlsx"
  248. return self._export(report_type)
  249. def _prepare_report_trial_balance(self):
  250. self.ensure_one()
  251. return {
  252. "wizard_id": self.id,
  253. "date_from": self.date_from,
  254. "date_to": self.date_to,
  255. "only_posted_moves": self.target_move == "posted",
  256. "hide_account_at_0": self.hide_account_at_0,
  257. "foreign_currency": self.foreign_currency,
  258. "company_id": self.company_id.id,
  259. "account_ids": self.account_ids.ids or [],
  260. "partner_ids": self.partner_ids.ids or [],
  261. "journal_ids": self.journal_ids.ids or [],
  262. "fy_start_date": self.fy_start_date,
  263. "hierarchy_on": self.hierarchy_on,
  264. "limit_hierarchy_level": self.limit_hierarchy_level,
  265. "show_hierarchy_level": self.show_hierarchy_level,
  266. "hide_parent_hierarchy_level": self.hide_parent_hierarchy_level,
  267. "show_partner_details": self.show_partner_details,
  268. "unaffected_earnings_account": self.unaffected_earnings_account.id,
  269. "account_financial_report_lang": self.env.lang,
  270. }
  271. def _export(self, report_type):
  272. """Default export is PDF."""
  273. return self._print_report(report_type)