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.

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