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.

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