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.

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