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.

259 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. def _init_date_from(self):
  88. """set start date to begin of current year if fiscal year running"""
  89. today = fields.Date.context_today(self)
  90. cur_month = fields.Date.from_string(today).month
  91. cur_day = fields.Date.from_string(today).day
  92. last_fsc_month = self.env.user.company_id.fiscalyear_last_month
  93. last_fsc_day = self.env.user.company_id.fiscalyear_last_day
  94. if cur_month < last_fsc_month \
  95. or cur_month == last_fsc_month and cur_day <= last_fsc_day:
  96. return time.strftime('%Y-01-01')
  97. def _default_foreign_currency(self):
  98. return self.env.user.has_group('base.group_multi_currency')
  99. @api.depends('date_from')
  100. def _compute_fy_start_date(self):
  101. for wiz in self.filtered('date_from'):
  102. date = fields.Datetime.from_string(wiz.date_from)
  103. res = self.company_id.compute_fiscalyear_dates(date)
  104. wiz.fy_start_date = fields.Date.to_string(res['date_from'])
  105. @api.onchange('company_id')
  106. def onchange_company_id(self):
  107. """Handle company change."""
  108. account_type = self.env.ref('account.data_unaffected_earnings')
  109. count = self.env['account.account'].search_count(
  110. [
  111. ('user_type_id', '=', account_type.id),
  112. ('company_id', '=', self.company_id.id)
  113. ])
  114. self.not_only_one_unaffected_earnings_account = count != 1
  115. if self.company_id and self.date_range_id.company_id and \
  116. self.date_range_id.company_id != self.company_id:
  117. self.date_range_id = False
  118. if self.company_id and self.account_journal_ids:
  119. self.account_journal_ids = self.account_journal_ids.filtered(
  120. lambda p: p.company_id == self.company_id or
  121. not p.company_id)
  122. if self.company_id and self.partner_ids:
  123. self.partner_ids = self.partner_ids.filtered(
  124. lambda p: p.company_id == self.company_id or
  125. not p.company_id)
  126. if self.company_id and self.account_ids:
  127. if self.account_type_ids:
  128. self._onchange_account_type_ids()
  129. else:
  130. self.account_ids = self.account_ids.filtered(
  131. lambda a: a.company_id == self.company_id)
  132. if self.company_id and self.cost_center_ids:
  133. self.cost_center_ids = self.cost_center_ids.filtered(
  134. lambda c: c.company_id == self.company_id)
  135. res = {'domain': {'account_ids': [],
  136. 'partner_ids': [],
  137. 'account_journal_ids': [],
  138. 'cost_center_ids': [],
  139. 'date_range_id': []
  140. }
  141. }
  142. if not self.company_id:
  143. return res
  144. else:
  145. res['domain']['account_ids'] += [
  146. ('company_id', '=', self.company_id.id)]
  147. res['domain']['account_journal_ids'] += [
  148. ('company_id', '=', self.company_id.id)]
  149. res['domain']['partner_ids'] += self._get_partner_ids_domain()
  150. res['domain']['cost_center_ids'] += [
  151. ('company_id', '=', self.company_id.id)]
  152. res['domain']['date_range_id'] += [
  153. '|', ('company_id', '=', self.company_id.id),
  154. ('company_id', '=', False)]
  155. return res
  156. @api.onchange('date_range_id')
  157. def onchange_date_range_id(self):
  158. """Handle date range change."""
  159. if self.date_range_id:
  160. self.date_from = self.date_range_id.date_start
  161. self.date_to = self.date_range_id.date_end
  162. @api.multi
  163. @api.constrains('company_id', 'date_range_id')
  164. def _check_company_id_date_range_id(self):
  165. for rec in self.sudo():
  166. if rec.company_id and rec.date_range_id.company_id and\
  167. rec.company_id != rec.date_range_id.company_id:
  168. raise ValidationError(
  169. _('The Company in the General Ledger Report Wizard and in '
  170. 'Date Range must be the same.'))
  171. @api.onchange('account_type_ids')
  172. def _onchange_account_type_ids(self):
  173. if self.account_type_ids:
  174. self.account_ids = self.env['account.account'].search([
  175. ('company_id', '=', self.company_id.id),
  176. ('user_type_id', 'in', self.account_type_ids.ids)])
  177. else:
  178. self.account_ids = None
  179. @api.onchange('partner_ids')
  180. def onchange_partner_ids(self):
  181. """Handle partners change."""
  182. if self.partner_ids:
  183. self.account_type_ids = self.env['account.account.type'].search([
  184. ('type', 'in', ['receivable', 'payable'])])
  185. else:
  186. self.account_type_ids = None
  187. # Somehow this is required to force onchange on _default_partners()
  188. self._onchange_account_type_ids()
  189. @api.multi
  190. def button_export_html(self):
  191. self.ensure_one()
  192. action = self.env.ref(
  193. 'account_financial_report.action_report_general_ledger')
  194. action_data = action.read()[0]
  195. context1 = action_data.get('context', {})
  196. if isinstance(context1, pycompat.string_types):
  197. context1 = safe_eval(context1)
  198. model = self.env['report_general_ledger']
  199. report = model.create(self._prepare_report_general_ledger())
  200. report.compute_data_for_report()
  201. context1['active_id'] = report.id
  202. context1['active_ids'] = report.ids
  203. action_data['context'] = context1
  204. return action_data
  205. @api.multi
  206. def button_export_pdf(self):
  207. self.ensure_one()
  208. report_type = 'qweb-pdf'
  209. return self._export(report_type)
  210. @api.multi
  211. def button_export_xlsx(self):
  212. self.ensure_one()
  213. report_type = 'xlsx'
  214. return self._export(report_type)
  215. def _prepare_report_general_ledger(self):
  216. self.ensure_one()
  217. return {
  218. 'date_from': self.date_from,
  219. 'date_to': self.date_to,
  220. 'only_posted_moves': self.target_move == 'posted',
  221. 'hide_account_at_0': self.hide_account_at_0,
  222. 'foreign_currency': self.foreign_currency,
  223. 'show_analytic_tags': self.show_analytic_tags,
  224. 'company_id': self.company_id.id,
  225. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  226. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  227. 'filter_cost_center_ids': [(6, 0, self.cost_center_ids.ids)],
  228. 'filter_analytic_tag_ids': [(6, 0, self.analytic_tag_ids.ids)],
  229. 'filter_journal_ids': [(6, 0, self.account_journal_ids.ids)],
  230. 'centralize': self.centralize,
  231. 'fy_start_date': self.fy_start_date,
  232. }
  233. def _export(self, report_type):
  234. """Default export is PDF."""
  235. model = self.env['report_general_ledger']
  236. report = model.create(self._prepare_report_general_ledger())
  237. report.compute_data_for_report()
  238. return report.print_report(report_type)