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.

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