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.

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