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.

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