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.

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