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.

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