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.

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