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.

323 lines
12 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. account_code_from = fields.Many2one(
  71. comodel_name="account.account",
  72. string="Account Code From",
  73. help="Starting account in a range",
  74. )
  75. account_code_to = fields.Many2one(
  76. comodel_name="account.account",
  77. string="Account Code To",
  78. help="Ending account in a range",
  79. )
  80. show_partner_details = fields.Boolean(string="Show Partner Details", default=True,)
  81. show_cost_center = fields.Boolean(string="Show Analytic Account", default=True,)
  82. @api.onchange("account_code_from", "account_code_to")
  83. def on_change_account_range(self):
  84. if (
  85. self.account_code_from
  86. and self.account_code_from.code.isdigit()
  87. and self.account_code_to
  88. and self.account_code_to.code.isdigit()
  89. ):
  90. start_range = int(self.account_code_from.code)
  91. end_range = int(self.account_code_to.code)
  92. self.account_ids = self.env["account.account"].search(
  93. [("code", "in", [x for x in range(start_range, end_range + 1)])]
  94. )
  95. if self.company_id:
  96. self.account_ids = self.account_ids.filtered(
  97. lambda a: a.company_id == self.company_id
  98. )
  99. def _init_date_from(self):
  100. """set start date to begin of current year if fiscal year running"""
  101. today = fields.Date.context_today(self)
  102. last_fsc_month = self.env.user.company_id.fiscalyear_last_month
  103. last_fsc_day = self.env.user.company_id.fiscalyear_last_day
  104. if (
  105. today.month < int(last_fsc_month)
  106. or today.month == int(last_fsc_month)
  107. and today.day <= last_fsc_day
  108. ):
  109. return time.strftime("%Y-01-01")
  110. else:
  111. return False
  112. def _default_foreign_currency(self):
  113. return self.env.user.has_group("base.group_multi_currency")
  114. @api.depends("date_from")
  115. def _compute_fy_start_date(self):
  116. for wiz in self:
  117. if wiz.date_from:
  118. res = self.company_id.compute_fiscalyear_dates(wiz.date_from)
  119. wiz.fy_start_date = res["date_from"]
  120. else:
  121. wiz.fy_start_date = False
  122. @api.onchange("company_id")
  123. def onchange_company_id(self):
  124. """Handle company change."""
  125. account_type = self.env.ref("account.data_unaffected_earnings")
  126. count = self.env["account.account"].search_count(
  127. [
  128. ("user_type_id", "=", account_type.id),
  129. ("company_id", "=", self.company_id.id),
  130. ]
  131. )
  132. self.not_only_one_unaffected_earnings_account = count != 1
  133. if (
  134. self.company_id
  135. and self.date_range_id.company_id
  136. and self.date_range_id.company_id != self.company_id
  137. ):
  138. self.date_range_id = False
  139. if self.company_id and self.account_journal_ids:
  140. self.account_journal_ids = self.account_journal_ids.filtered(
  141. lambda p: p.company_id == self.company_id or not p.company_id
  142. )
  143. if self.company_id and self.partner_ids:
  144. self.partner_ids = self.partner_ids.filtered(
  145. lambda p: p.company_id == self.company_id or not p.company_id
  146. )
  147. if self.company_id and self.account_ids:
  148. if self.receivable_accounts_only or self.payable_accounts_only:
  149. self.onchange_type_accounts_only()
  150. else:
  151. self.account_ids = self.account_ids.filtered(
  152. lambda a: a.company_id == self.company_id
  153. )
  154. if self.company_id and self.cost_center_ids:
  155. self.cost_center_ids = self.cost_center_ids.filtered(
  156. lambda c: c.company_id == self.company_id
  157. )
  158. res = {
  159. "domain": {
  160. "account_ids": [],
  161. "partner_ids": [],
  162. "account_journal_ids": [],
  163. "cost_center_ids": [],
  164. "date_range_id": [],
  165. }
  166. }
  167. if not self.company_id:
  168. return res
  169. else:
  170. res["domain"]["account_ids"] += [("company_id", "=", self.company_id.id)]
  171. res["domain"]["account_journal_ids"] += [
  172. ("company_id", "=", self.company_id.id)
  173. ]
  174. res["domain"]["partner_ids"] += self._get_partner_ids_domain()
  175. res["domain"]["cost_center_ids"] += [
  176. ("company_id", "=", self.company_id.id)
  177. ]
  178. res["domain"]["date_range_id"] += [
  179. "|",
  180. ("company_id", "=", self.company_id.id),
  181. ("company_id", "=", False),
  182. ]
  183. return res
  184. @api.onchange("date_range_id")
  185. def onchange_date_range_id(self):
  186. """Handle date range change."""
  187. if self.date_range_id:
  188. self.date_from = self.date_range_id.date_start
  189. self.date_to = self.date_range_id.date_end
  190. @api.constrains("company_id", "date_range_id")
  191. def _check_company_id_date_range_id(self):
  192. for rec in self.sudo():
  193. if (
  194. rec.company_id
  195. and rec.date_range_id.company_id
  196. and rec.company_id != rec.date_range_id.company_id
  197. ):
  198. raise ValidationError(
  199. _(
  200. "The Company in the General Ledger Report Wizard and in "
  201. "Date Range must be the same."
  202. )
  203. )
  204. @api.onchange("receivable_accounts_only", "payable_accounts_only")
  205. def onchange_type_accounts_only(self):
  206. """Handle receivable/payable accounts only change."""
  207. if self.receivable_accounts_only or self.payable_accounts_only:
  208. domain = [("company_id", "=", self.company_id.id)]
  209. if self.receivable_accounts_only and self.payable_accounts_only:
  210. domain += [("internal_type", "in", ("receivable", "payable"))]
  211. elif self.receivable_accounts_only:
  212. domain += [("internal_type", "=", "receivable")]
  213. elif self.payable_accounts_only:
  214. domain += [("internal_type", "=", "payable")]
  215. self.account_ids = self.env["account.account"].search(domain)
  216. else:
  217. self.account_ids = None
  218. @api.onchange("partner_ids")
  219. def onchange_partner_ids(self):
  220. """Handle partners change."""
  221. if self.partner_ids:
  222. self.receivable_accounts_only = self.payable_accounts_only = True
  223. else:
  224. self.receivable_accounts_only = self.payable_accounts_only = False
  225. @api.depends("company_id")
  226. def _compute_unaffected_earnings_account(self):
  227. account_type = self.env.ref("account.data_unaffected_earnings")
  228. for record in self:
  229. record.unaffected_earnings_account = self.env["account.account"].search(
  230. [
  231. ("user_type_id", "=", account_type.id),
  232. ("company_id", "=", record.company_id.id),
  233. ]
  234. )
  235. unaffected_earnings_account = fields.Many2one(
  236. comodel_name="account.account",
  237. compute="_compute_unaffected_earnings_account",
  238. store=True,
  239. )
  240. def _print_report(self, report_type):
  241. self.ensure_one()
  242. data = self._prepare_report_general_ledger()
  243. if report_type == "xlsx":
  244. report_name = "a_f_r.report_general_ledger_xlsx"
  245. else:
  246. report_name = "account_financial_report.general_ledger"
  247. return (
  248. self.env["ir.actions.report"]
  249. .search(
  250. [("report_name", "=", report_name), ("report_type", "=", report_type)],
  251. limit=1,
  252. )
  253. .report_action(self, data=data)
  254. )
  255. def button_export_html(self):
  256. self.ensure_one()
  257. report_type = "qweb-html"
  258. return self._export(report_type)
  259. def button_export_pdf(self):
  260. self.ensure_one()
  261. report_type = "qweb-pdf"
  262. return self._export(report_type)
  263. def button_export_xlsx(self):
  264. self.ensure_one()
  265. report_type = "xlsx"
  266. return self._export(report_type)
  267. def _prepare_report_general_ledger(self):
  268. self.ensure_one()
  269. return {
  270. "wizard_id": self.id,
  271. "date_from": self.date_from,
  272. "date_to": self.date_to,
  273. "only_posted_moves": self.target_move == "posted",
  274. "hide_account_at_0": self.hide_account_at_0,
  275. "foreign_currency": self.foreign_currency,
  276. "show_analytic_tags": self.show_analytic_tags,
  277. "company_id": self.company_id.id,
  278. "account_ids": self.account_ids.ids,
  279. "partner_ids": self.partner_ids.ids,
  280. "show_partner_details": self.show_partner_details,
  281. "cost_center_ids": self.cost_center_ids.ids,
  282. "show_cost_center": self.show_cost_center,
  283. "analytic_tag_ids": self.analytic_tag_ids.ids,
  284. "journal_ids": self.account_journal_ids.ids,
  285. "centralize": self.centralize,
  286. "fy_start_date": self.fy_start_date,
  287. "unaffected_earnings_account": self.unaffected_earnings_account.id,
  288. }
  289. def _export(self, report_type):
  290. """Default export is PDF."""
  291. return self._print_report(report_type)
  292. def _get_atr_from_dict(self, obj_id, data, key):
  293. try:
  294. return data[obj_id][key]
  295. except KeyError:
  296. return data[str(obj_id)][key]