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.

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