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.

274 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="all",
  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(
  63. comodel_name="res.partner", string="Filter partners",
  64. )
  65. journal_ids = fields.Many2many(comodel_name="account.journal",)
  66. not_only_one_unaffected_earnings_account = fields.Boolean(
  67. readonly=True, string="Not only one unaffected earnings account"
  68. )
  69. foreign_currency = fields.Boolean(
  70. string="Show foreign currency",
  71. help="Display foreign currency for move lines, unless "
  72. "account currency is not setup through chart of accounts "
  73. "will display initial and final balance in that currency.",
  74. )
  75. @api.multi
  76. @api.constrains("hierarchy_on", "show_hierarchy_level")
  77. def _check_show_hierarchy_level(self):
  78. for rec in self:
  79. if rec.hierarchy_on != "none" and rec.show_hierarchy_level <= 0:
  80. raise UserError(
  81. _("The hierarchy level to filter on must be " "greater than 0.")
  82. )
  83. @api.depends("date_from")
  84. def _compute_fy_start_date(self):
  85. for wiz in self.filtered("date_from"):
  86. date = fields.Datetime.from_string(wiz.date_from)
  87. res = self.company_id.compute_fiscalyear_dates(date)
  88. wiz.fy_start_date = fields.Date.to_string(res["date_from"])
  89. @api.onchange("company_id")
  90. def onchange_company_id(self):
  91. """Handle company change."""
  92. account_type = self.env.ref("account.data_unaffected_earnings")
  93. count = self.env["account.account"].search_count(
  94. [
  95. ("user_type_id", "=", account_type.id),
  96. ("company_id", "=", self.company_id.id),
  97. ]
  98. )
  99. self.not_only_one_unaffected_earnings_account = count != 1
  100. if (
  101. self.company_id
  102. and self.date_range_id.company_id
  103. and self.date_range_id.company_id != self.company_id
  104. ):
  105. self.date_range_id = False
  106. if self.company_id and self.partner_ids:
  107. self.partner_ids = self.partner_ids.filtered(
  108. lambda p: p.company_id == self.company_id or not p.company_id
  109. )
  110. if self.company_id and self.journal_ids:
  111. self.journal_ids = self.journal_ids.filtered(
  112. lambda a: a.company_id == self.company_id
  113. )
  114. if self.company_id and self.account_ids:
  115. if self.receivable_accounts_only or self.payable_accounts_only:
  116. self.onchange_type_accounts_only()
  117. else:
  118. self.account_ids = self.account_ids.filtered(
  119. lambda a: a.company_id == self.company_id
  120. )
  121. res = {
  122. "domain": {
  123. "account_ids": [],
  124. "partner_ids": [],
  125. "date_range_id": [],
  126. "journal_ids": [],
  127. }
  128. }
  129. if not self.company_id:
  130. return res
  131. else:
  132. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  133. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  134. res["domain"]["date_range_id"] += [
  135. "|",
  136. ("company_id", "=", self.company_id.id),
  137. ("company_id", "=", False),
  138. ]
  139. res["domain"]["journal_ids"] += [("company_id", "=", self.company_id.id)]
  140. return res
  141. @api.onchange("date_range_id")
  142. def onchange_date_range_id(self):
  143. """Handle date range change."""
  144. self.date_from = self.date_range_id.date_start
  145. self.date_to = self.date_range_id.date_end
  146. @api.multi
  147. @api.constrains("company_id", "date_range_id")
  148. def _check_company_id_date_range_id(self):
  149. for rec in self.sudo():
  150. if (
  151. rec.company_id
  152. and rec.date_range_id.company_id
  153. and rec.company_id != rec.date_range_id.company_id
  154. ):
  155. raise ValidationError(
  156. _(
  157. "The Company in the Trial Balance Report Wizard and in "
  158. "Date Range must be the same."
  159. )
  160. )
  161. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  162. def onchange_type_accounts_only(self):
  163. """Handle receivable/payable accounts only change."""
  164. if self.receivable_accounts_only or self.payable_accounts_only:
  165. domain = [("company_id", "=", self.company_id.id)]
  166. if self.receivable_accounts_only and self.payable_accounts_only:
  167. domain += [("internal_type", "in", ("receivable", "payable"))]
  168. elif self.receivable_accounts_only:
  169. domain += [("internal_type", "=", "receivable")]
  170. elif self.payable_accounts_only:
  171. domain += [("internal_type", "=", "payable")]
  172. self.account_ids = self.env["account.account"].search(domain)
  173. else:
  174. self.account_ids = None
  175. @api.onchange("show_partner_details")
  176. def onchange_show_partner_details(self):
  177. """Handle partners change."""
  178. if self.show_partner_details:
  179. self.receivable_accounts_only = self.payable_accounts_only = True
  180. else:
  181. self.receivable_accounts_only = self.payable_accounts_only = False
  182. @api.multi
  183. @api.depends("company_id")
  184. def _compute_unaffected_earnings_account(self):
  185. account_type = self.env.ref("account.data_unaffected_earnings")
  186. for record in self:
  187. record.unaffected_earnings_account = self.env["account.account"].search(
  188. [
  189. ("user_type_id", "=", account_type.id),
  190. ("company_id", "=", record.company_id.id),
  191. ]
  192. )
  193. unaffected_earnings_account = fields.Many2one(
  194. comodel_name="account.account",
  195. compute="_compute_unaffected_earnings_account",
  196. store=True,
  197. )
  198. @api.multi
  199. def _print_report(self, report_type):
  200. self.ensure_one()
  201. data = self._prepare_report_trial_balance()
  202. if report_type == "xlsx":
  203. report_name = "a_f_r.report_trial_balance_xlsx"
  204. else:
  205. report_name = "account_financial_report.trial_balance"
  206. return (
  207. self.env["ir.actions.report"]
  208. .search(
  209. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  210. limit=1,
  211. )
  212. .report_action(self, data=data)
  213. )
  214. @api.multi
  215. def button_export_html(self):
  216. self.ensure_one()
  217. report_type = "qweb-html"
  218. return self._export(report_type)
  219. @api.multi
  220. def button_export_pdf(self):
  221. self.ensure_one()
  222. report_type = "qweb-pdf"
  223. return self._export(report_type)
  224. @api.multi
  225. def button_export_xlsx(self):
  226. self.ensure_one()
  227. report_type = "xlsx"
  228. return self._export(report_type)
  229. def _prepare_report_trial_balance(self):
  230. self.ensure_one()
  231. return {
  232. "wizard_id": self.id,
  233. "date_from": self.date_from,
  234. "date_to": self.date_to,
  235. "only_posted_moves": self.target_move == "posted",
  236. "hide_account_at_0": self.hide_account_at_0,
  237. "foreign_currency": self.foreign_currency,
  238. "company_id": self.company_id.id,
  239. "account_ids": self.account_ids.ids or [],
  240. "partner_ids": self.partner_ids.ids or [],
  241. "journal_ids": self.journal_ids.ids or [],
  242. "fy_start_date": self.fy_start_date,
  243. "hierarchy_on": self.hierarchy_on,
  244. "limit_hierarchy_level": self.limit_hierarchy_level,
  245. "show_hierarchy_level": self.show_hierarchy_level,
  246. "hide_parent_hierarchy_level": self.hide_parent_hierarchy_level,
  247. "show_partner_details": self.show_partner_details,
  248. "unaffected_earnings_account": self.unaffected_earnings_account.id,
  249. }
  250. def _export(self, report_type):
  251. """Default export is PDF."""
  252. return self._print_report(report_type)