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.

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