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.

291 lines
11 KiB

  1. # Author: Damien Crier
  2. # Author: Julien Coux
  3. # Author: Jordi Ballester
  4. # Copyright 2016 Camptocamp SA
  5. # Copyright 2017 Akretion - Alexis de Lattre
  6. # Copyright 2017 Eficent Business and IT Consulting Services, S.L.
  7. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  8. import time
  9. from odoo import _, api, fields, models
  10. from odoo.exceptions import ValidationError
  11. class GeneralLedgerReportWizard(models.TransientModel):
  12. """General ledger report wizard."""
  13. _name = "general.ledger.report.wizard"
  14. _description = "General Ledger Report Wizard"
  15. _inherit = "account_financial_report_abstract_wizard"
  16. company_id = fields.Many2one(
  17. comodel_name="res.company",
  18. default=lambda self: self.env.user.company_id,
  19. required=False,
  20. string="Company",
  21. )
  22. date_range_id = fields.Many2one(comodel_name="date.range", string="Date range")
  23. date_from = fields.Date(required=True, default=lambda self: self._init_date_from())
  24. date_to = fields.Date(required=True, default=fields.Date.context_today)
  25. fy_start_date = fields.Date(compute="_compute_fy_start_date")
  26. target_move = fields.Selection(
  27. [("posted", "All Posted Entries"), ("all", "All Entries")],
  28. string="Target Moves",
  29. required=True,
  30. default="posted",
  31. )
  32. account_ids = fields.Many2many(
  33. comodel_name="account.account", string="Filter accounts"
  34. )
  35. centralize = fields.Boolean(string="Activate centralization", default=True)
  36. hide_account_at_0 = fields.Boolean(
  37. string="Hide account ending balance at 0",
  38. help="Use this filter to hide an account or a partner "
  39. "with an ending balance at 0. "
  40. "If partners are filtered, "
  41. "debits and credits totals will not match the trial balance.",
  42. )
  43. show_analytic_tags = fields.Boolean(string="Show analytic tags")
  44. receivable_accounts_only = fields.Boolean()
  45. payable_accounts_only = fields.Boolean()
  46. partner_ids = fields.Many2many(
  47. comodel_name="res.partner",
  48. string="Filter partners",
  49. default=lambda self: self._default_partners(),
  50. )
  51. analytic_tag_ids = fields.Many2many(
  52. comodel_name="account.analytic.tag", string="Filter analytic tags"
  53. )
  54. account_journal_ids = fields.Many2many(
  55. comodel_name="account.journal", string="Filter journals"
  56. )
  57. cost_center_ids = fields.Many2many(
  58. comodel_name="account.analytic.account", string="Filter cost centers"
  59. )
  60. not_only_one_unaffected_earnings_account = fields.Boolean(
  61. readonly=True, string="Not only one unaffected earnings account"
  62. )
  63. foreign_currency = fields.Boolean(
  64. string="Show foreign currency",
  65. help="Display foreign currency for move lines, unless "
  66. "account currency is not setup through chart of accounts "
  67. "will display initial and final balance in that currency.",
  68. default=lambda self: self._default_foreign_currency(),
  69. )
  70. def _init_date_from(self):
  71. """set start date to begin of current year if fiscal year running"""
  72. today = fields.Date.context_today(self)
  73. last_fsc_month = self.env.user.company_id.fiscalyear_last_month
  74. last_fsc_day = self.env.user.company_id.fiscalyear_last_day
  75. if (
  76. today.month < int(last_fsc_month)
  77. or today.month == int(last_fsc_month)
  78. and today.day <= last_fsc_day
  79. ):
  80. return time.strftime("%Y-01-01")
  81. else:
  82. return False
  83. def _default_foreign_currency(self):
  84. return self.env.user.has_group("base.group_multi_currency")
  85. @api.depends("date_from")
  86. def _compute_fy_start_date(self):
  87. for wiz in self:
  88. if wiz.date_from:
  89. res = self.company_id.compute_fiscalyear_dates(wiz.date_from)
  90. wiz.fy_start_date = res["date_from"]
  91. else:
  92. wiz.fy_start_date = False
  93. @api.onchange("company_id")
  94. def onchange_company_id(self):
  95. """Handle company change."""
  96. account_type = self.env.ref("account.data_unaffected_earnings")
  97. count = self.env["account.account"].search_count(
  98. [
  99. ("user_type_id", "=", account_type.id),
  100. ("company_id", "=", self.company_id.id),
  101. ]
  102. )
  103. self.not_only_one_unaffected_earnings_account = count != 1
  104. if (
  105. self.company_id
  106. and self.date_range_id.company_id
  107. and self.date_range_id.company_id != self.company_id
  108. ):
  109. self.date_range_id = False
  110. if self.company_id and self.account_journal_ids:
  111. self.account_journal_ids = self.account_journal_ids.filtered(
  112. lambda p: p.company_id == self.company_id or not p.company_id
  113. )
  114. if self.company_id and self.partner_ids:
  115. self.partner_ids = self.partner_ids.filtered(
  116. lambda p: p.company_id == self.company_id or not p.company_id
  117. )
  118. if self.company_id and self.account_ids:
  119. if self.receivable_accounts_only or self.payable_accounts_only:
  120. self.onchange_type_accounts_only()
  121. else:
  122. self.account_ids = self.account_ids.filtered(
  123. lambda a: a.company_id == self.company_id
  124. )
  125. if self.company_id and self.cost_center_ids:
  126. self.cost_center_ids = self.cost_center_ids.filtered(
  127. lambda c: c.company_id == self.company_id
  128. )
  129. res = {
  130. "domain": {
  131. "account_ids": [],
  132. "partner_ids": [],
  133. "account_journal_ids": [],
  134. "cost_center_ids": [],
  135. "date_range_id": [],
  136. }
  137. }
  138. if not self.company_id:
  139. return res
  140. else:
  141. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  142. res["domain"]["account_journal_ids"] += [
  143. ("company_id", "=", self.company_id.id)
  144. ]
  145. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  146. res["domain"]["cost_center_ids"] += [
  147. ("company_id", "=", self.company_id.id)
  148. ]
  149. res["domain"]["date_range_id"] += [
  150. "|",
  151. ("company_id", "=", self.company_id.id),
  152. ("company_id", "=", False),
  153. ]
  154. return res
  155. @api.onchange("date_range_id")
  156. def onchange_date_range_id(self):
  157. """Handle date range change."""
  158. if self.date_range_id:
  159. self.date_from = self.date_range_id.date_start
  160. self.date_to = self.date_range_id.date_end
  161. @api.constrains("company_id", "date_range_id")
  162. def _check_company_id_date_range_id(self):
  163. for rec in self.sudo():
  164. if (
  165. rec.company_id
  166. and rec.date_range_id.company_id
  167. and rec.company_id != rec.date_range_id.company_id
  168. ):
  169. raise ValidationError(
  170. _(
  171. "The Company in the General Ledger Report Wizard and in "
  172. "Date Range must be the same."
  173. )
  174. )
  175. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  176. def onchange_type_accounts_only(self):
  177. """Handle receivable/payable accounts only change."""
  178. if self.receivable_accounts_only or self.payable_accounts_only:
  179. domain = [("company_id", "=", self.company_id.id)]
  180. if self.receivable_accounts_only and self.payable_accounts_only:
  181. domain += [("internal_type", "in", ("receivable", "payable"))]
  182. elif self.receivable_accounts_only:
  183. domain += [("internal_type", "=", "receivable")]
  184. elif self.payable_accounts_only:
  185. domain += [("internal_type", "=", "payable")]
  186. self.account_ids = self.env["account.account"].search(domain)
  187. else:
  188. self.account_ids = None
  189. @api.onchange("partner_ids")
  190. def onchange_partner_ids(self):
  191. """Handle partners change."""
  192. if self.partner_ids:
  193. self.receivable_accounts_only = self.payable_accounts_only = True
  194. else:
  195. self.receivable_accounts_only = self.payable_accounts_only = False
  196. @api.depends("company_id")
  197. def _compute_unaffected_earnings_account(self):
  198. account_type = self.env.ref("account.data_unaffected_earnings")
  199. for record in self:
  200. record.unaffected_earnings_account = self.env["account.account"].search(
  201. [
  202. ("user_type_id", "=", account_type.id),
  203. ("company_id", "=", record.company_id.id),
  204. ]
  205. )
  206. unaffected_earnings_account = fields.Many2one(
  207. comodel_name="account.account",
  208. compute="_compute_unaffected_earnings_account",
  209. store=True,
  210. )
  211. def _print_report(self, report_type):
  212. self.ensure_one()
  213. data = self._prepare_report_general_ledger()
  214. if report_type == "xlsx":
  215. report_name = "a_f_r.report_general_ledger_xlsx"
  216. else:
  217. report_name = "account_financial_report.general_ledger"
  218. return (
  219. self.env["ir.actions.report"]
  220. .search(
  221. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  222. limit=1,
  223. )
  224. .report_action(self, data=data)
  225. )
  226. def button_export_html(self):
  227. self.ensure_one()
  228. report_type = "qweb-html"
  229. return self._export(report_type)
  230. def button_export_pdf(self):
  231. self.ensure_one()
  232. report_type = "qweb-pdf"
  233. return self._export(report_type)
  234. def button_export_xlsx(self):
  235. self.ensure_one()
  236. report_type = "xlsx"
  237. return self._export(report_type)
  238. def _prepare_report_general_ledger(self):
  239. self.ensure_one()
  240. return {
  241. "wizard_id": self.id,
  242. "date_from": self.date_from,
  243. "date_to": self.date_to,
  244. "only_posted_moves": self.target_move == "posted",
  245. "hide_account_at_0": self.hide_account_at_0,
  246. "foreign_currency": self.foreign_currency,
  247. "show_analytic_tags": self.show_analytic_tags,
  248. "company_id": self.company_id.id,
  249. "account_ids": self.account_ids.ids,
  250. "partner_ids": self.partner_ids.ids,
  251. "cost_center_ids": self.cost_center_ids.ids,
  252. "analytic_tag_ids": self.analytic_tag_ids.ids,
  253. "journal_ids": self.account_journal_ids.ids,
  254. "centralize": self.centralize,
  255. "fy_start_date": self.fy_start_date,
  256. "unaffected_earnings_account": self.unaffected_earnings_account.id,
  257. }
  258. def _export(self, report_type):
  259. """Default export is PDF."""
  260. return self._print_report(report_type)
  261. def _get_atr_from_dict(self, obj_id, data, key):
  262. try:
  263. return data[obj_id][key]
  264. except KeyError:
  265. return data[str(obj_id)][key]