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.

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