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.

264 lines
10 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.tools.safe_eval import safe_eval
  11. from odoo.tools import pycompat
  12. from odoo.exceptions import ValidationError
  13. import time
  14. class GeneralLedgerReportWizard(models.TransientModel):
  15. """General ledger report wizard."""
  16. _name = "general.ledger.report.wizard"
  17. _description = "General Ledger Report Wizard"
  18. _inherit = 'account_financial_report_abstract_wizard'
  19. company_id = fields.Many2one(
  20. comodel_name='res.company',
  21. default=lambda self: self.env.user.company_id,
  22. required=False,
  23. string='Company'
  24. )
  25. date_range_id = fields.Many2one(
  26. comodel_name='date.range',
  27. string='Date range'
  28. )
  29. date_from = fields.Date(required=True,
  30. default=lambda self: self._init_date_from())
  31. date_to = fields.Date(required=True,
  32. default=fields.Date.context_today)
  33. fy_start_date = fields.Date(compute='_compute_fy_start_date')
  34. target_move = fields.Selection([('posted', 'All Posted Entries'),
  35. ('all', 'All Entries')],
  36. string='Target Moves',
  37. required=True,
  38. default='all')
  39. account_ids = fields.Many2many(
  40. comodel_name='account.account',
  41. string='Filter accounts',
  42. )
  43. centralize = fields.Boolean(string='Activate centralization',
  44. default=True)
  45. hide_account_at_0 = fields.Boolean(
  46. string='Hide account ending balance at 0',
  47. help='Use this filter to hide an account or a partner '
  48. 'with an ending balance at 0. '
  49. 'If partners are filtered, '
  50. 'debits and credits totals will not match the trial balance.'
  51. )
  52. show_analytic_tags = fields.Boolean(
  53. string='Show analytic tags',
  54. )
  55. account_type_ids = fields.Many2many(
  56. 'account.account.type',
  57. string='Account Types',
  58. )
  59. partner_ids = fields.Many2many(
  60. comodel_name='res.partner',
  61. string='Filter partners',
  62. default=lambda self: self._default_partners(),
  63. )
  64. analytic_tag_ids = fields.Many2many(
  65. comodel_name='account.analytic.tag',
  66. string='Filter analytic tags',
  67. )
  68. account_journal_ids = fields.Many2many(
  69. comodel_name='account.journal',
  70. string='Filter journals',
  71. )
  72. cost_center_ids = fields.Many2many(
  73. comodel_name='account.analytic.account',
  74. string='Filter cost centers',
  75. )
  76. not_only_one_unaffected_earnings_account = fields.Boolean(
  77. readonly=True,
  78. string='Not only one unaffected earnings account'
  79. )
  80. foreign_currency = fields.Boolean(
  81. string='Show foreign currency',
  82. help='Display foreign currency for move lines, unless '
  83. 'account currency is not setup through chart of accounts '
  84. 'will display initial and final balance in that currency.',
  85. default=lambda self: self._default_foreign_currency(),
  86. )
  87. partner_ungrouped = fields.Boolean(
  88. string='Partner ungrouped',
  89. help='If set moves are not grouped by partner in any case'
  90. )
  91. def _init_date_from(self):
  92. """set start date to begin of current year if fiscal year running"""
  93. today = fields.Date.context_today(self)
  94. cur_month = fields.Date.from_string(today).month
  95. cur_day = fields.Date.from_string(today).day
  96. last_fsc_month = self.env.user.company_id.fiscalyear_last_month
  97. last_fsc_day = self.env.user.company_id.fiscalyear_last_day
  98. if cur_month < last_fsc_month \
  99. or cur_month == last_fsc_month and cur_day <= last_fsc_day:
  100. return time.strftime('%Y-01-01')
  101. def _default_foreign_currency(self):
  102. return self.env.user.has_group('base.group_multi_currency')
  103. @api.depends('date_from')
  104. def _compute_fy_start_date(self):
  105. for wiz in self.filtered('date_from'):
  106. date = fields.Datetime.from_string(wiz.date_from)
  107. res = self.company_id.compute_fiscalyear_dates(date)
  108. wiz.fy_start_date = fields.Date.to_string(res['date_from'])
  109. @api.onchange('company_id')
  110. def onchange_company_id(self):
  111. """Handle company change."""
  112. account_type = self.env.ref('account.data_unaffected_earnings')
  113. count = self.env['account.account'].search_count(
  114. [
  115. ('user_type_id', '=', account_type.id),
  116. ('company_id', '=', self.company_id.id)
  117. ])
  118. self.not_only_one_unaffected_earnings_account = count != 1
  119. if self.company_id and self.date_range_id.company_id and \
  120. self.date_range_id.company_id != self.company_id:
  121. self.date_range_id = False
  122. if self.company_id and self.account_journal_ids:
  123. self.account_journal_ids = self.account_journal_ids.filtered(
  124. lambda p: p.company_id == self.company_id or
  125. not p.company_id)
  126. if self.company_id and self.partner_ids:
  127. self.partner_ids = self.partner_ids.filtered(
  128. lambda p: p.company_id == self.company_id or
  129. not p.company_id)
  130. if self.company_id and self.account_ids:
  131. if self.account_type_ids:
  132. self._onchange_account_type_ids()
  133. else:
  134. self.account_ids = self.account_ids.filtered(
  135. lambda a: a.company_id == self.company_id)
  136. if self.company_id and self.cost_center_ids:
  137. self.cost_center_ids = self.cost_center_ids.filtered(
  138. lambda c: c.company_id == self.company_id)
  139. res = {'domain': {'account_ids': [],
  140. 'partner_ids': [],
  141. 'account_journal_ids': [],
  142. 'cost_center_ids': [],
  143. 'date_range_id': []
  144. }
  145. }
  146. if not self.company_id:
  147. return res
  148. else:
  149. res['domain']['account_ids'] += [
  150. ('company_id', '=', self.company_id.id)]
  151. res['domain']['account_journal_ids'] += [
  152. ('company_id', '=', self.company_id.id)]
  153. res['domain']['partner_ids'] += self._get_partner_ids_domain()
  154. res['domain']['cost_center_ids'] += [
  155. ('company_id', '=', self.company_id.id)]
  156. res['domain']['date_range_id'] += [
  157. '|', ('company_id', '=', self.company_id.id),
  158. ('company_id', '=', False)]
  159. return res
  160. @api.onchange('date_range_id')
  161. def onchange_date_range_id(self):
  162. """Handle date range change."""
  163. if self.date_range_id:
  164. self.date_from = self.date_range_id.date_start
  165. self.date_to = self.date_range_id.date_end
  166. @api.multi
  167. @api.constrains('company_id', 'date_range_id')
  168. def _check_company_id_date_range_id(self):
  169. for rec in self.sudo():
  170. if rec.company_id and rec.date_range_id.company_id and\
  171. rec.company_id != rec.date_range_id.company_id:
  172. raise ValidationError(
  173. _('The Company in the General Ledger Report Wizard and in '
  174. 'Date Range must be the same.'))
  175. @api.onchange('account_type_ids')
  176. def _onchange_account_type_ids(self):
  177. if self.account_type_ids:
  178. self.account_ids = self.env['account.account'].search([
  179. ('company_id', '=', self.company_id.id),
  180. ('user_type_id', 'in', self.account_type_ids.ids)])
  181. else:
  182. self.account_ids = None
  183. @api.onchange('partner_ids')
  184. def onchange_partner_ids(self):
  185. """Handle partners change."""
  186. if self.partner_ids:
  187. self.account_type_ids = self.env['account.account.type'].search([
  188. ('type', 'in', ['receivable', 'payable'])])
  189. else:
  190. self.account_type_ids = None
  191. # Somehow this is required to force onchange on _default_partners()
  192. self._onchange_account_type_ids()
  193. @api.multi
  194. def button_export_html(self):
  195. self.ensure_one()
  196. action = self.env.ref(
  197. 'account_financial_report.action_report_general_ledger')
  198. action_data = action.read()[0]
  199. context1 = action_data.get('context', {})
  200. if isinstance(context1, pycompat.string_types):
  201. context1 = safe_eval(context1)
  202. model = self.env['report_general_ledger']
  203. report = model.create(self._prepare_report_general_ledger())
  204. report.compute_data_for_report()
  205. context1['active_id'] = report.id
  206. context1['active_ids'] = report.ids
  207. action_data['context'] = context1
  208. return action_data
  209. @api.multi
  210. def button_export_pdf(self):
  211. self.ensure_one()
  212. report_type = 'qweb-pdf'
  213. return self._export(report_type)
  214. @api.multi
  215. def button_export_xlsx(self):
  216. self.ensure_one()
  217. report_type = 'xlsx'
  218. return self._export(report_type)
  219. def _prepare_report_general_ledger(self):
  220. self.ensure_one()
  221. return {
  222. 'date_from': self.date_from,
  223. 'date_to': self.date_to,
  224. 'only_posted_moves': self.target_move == 'posted',
  225. 'hide_account_at_0': self.hide_account_at_0,
  226. 'foreign_currency': self.foreign_currency,
  227. 'show_analytic_tags': self.show_analytic_tags,
  228. 'company_id': self.company_id.id,
  229. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  230. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  231. 'filter_cost_center_ids': [(6, 0, self.cost_center_ids.ids)],
  232. 'filter_analytic_tag_ids': [(6, 0, self.analytic_tag_ids.ids)],
  233. 'filter_journal_ids': [(6, 0, self.account_journal_ids.ids)],
  234. 'centralize': self.centralize,
  235. 'fy_start_date': self.fy_start_date,
  236. 'partner_ungrouped': self.partner_ungrouped,
  237. }
  238. def _export(self, report_type):
  239. """Default export is PDF."""
  240. model = self.env['report_general_ledger']
  241. report = model.create(self._prepare_report_general_ledger())
  242. report.compute_data_for_report()
  243. return report.print_report(report_type)