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.

184 lines
7.0 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. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  8. from odoo import api, fields, models
  9. from odoo.tools.safe_eval import safe_eval
  10. from odoo.tools import pycompat
  11. class GeneralLedgerReportWizard(models.TransientModel):
  12. """General ledger report wizard."""
  13. _name = "general.ledger.report.wizard"
  14. _description = "General Ledger Report Wizard"
  15. company_id = fields.Many2one(
  16. comodel_name='res.company',
  17. default=lambda self: self.env.user.company_id,
  18. string='Company'
  19. )
  20. date_range_id = fields.Many2one(
  21. comodel_name='date.range',
  22. string='Date range'
  23. )
  24. date_from = fields.Date(required=True)
  25. date_to = fields.Date(required=True)
  26. fy_start_date = fields.Date(compute='_compute_fy_start_date')
  27. target_move = fields.Selection([('posted', 'All Posted Entries'),
  28. ('all', 'All Entries')],
  29. string='Target Moves',
  30. required=True,
  31. default='all')
  32. account_ids = fields.Many2many(
  33. comodel_name='account.account',
  34. string='Filter accounts',
  35. )
  36. centralize = fields.Boolean(string='Activate centralization',
  37. default=True)
  38. hide_account_at_0 = fields.Boolean(
  39. string='Hide account ending balance at 0',
  40. help='Use this filter to hide an account or a partner '
  41. 'with an ending balance at 0. '
  42. 'If partners are filtered, '
  43. 'debits and credits totals will not match the trial balance.'
  44. )
  45. show_analytic_tags = fields.Boolean(
  46. string='Show analytic tags',
  47. )
  48. receivable_accounts_only = fields.Boolean()
  49. payable_accounts_only = fields.Boolean()
  50. partner_ids = fields.Many2many(
  51. comodel_name='res.partner',
  52. string='Filter partners',
  53. )
  54. analytic_tag_ids = fields.Many2many(
  55. comodel_name='account.analytic.tag',
  56. string='Filter accounts',
  57. )
  58. account_journal_ids = fields.Many2many(
  59. comodel_name='account.journal',
  60. string='Filter journals',
  61. )
  62. cost_center_ids = fields.Many2many(
  63. comodel_name='account.analytic.account',
  64. string='Filter cost centers',
  65. )
  66. not_only_one_unaffected_earnings_account = fields.Boolean(
  67. readonly=True,
  68. string='Not only one unaffected earnings account'
  69. )
  70. foreign_currency = fields.Boolean(
  71. string='Show foreign currency',
  72. help='Display foreign currency for move lines, unless '
  73. 'account currency is not setup through chart of accounts '
  74. 'will display initial and final balance in that currency.'
  75. )
  76. @api.depends('date_from')
  77. def _compute_fy_start_date(self):
  78. for wiz in self.filtered('date_from'):
  79. date = fields.Datetime.from_string(wiz.date_from)
  80. res = self.company_id.compute_fiscalyear_dates(date)
  81. wiz.fy_start_date = res['date_from']
  82. @api.onchange('company_id')
  83. def onchange_company_id(self):
  84. """Handle company change."""
  85. account_type = self.env.ref('account.data_unaffected_earnings')
  86. count = self.env['account.account'].search_count(
  87. [
  88. ('user_type_id', '=', account_type.id),
  89. ('company_id', '=', self.company_id.id)
  90. ])
  91. self.not_only_one_unaffected_earnings_account = count != 1
  92. @api.onchange('date_range_id')
  93. def onchange_date_range_id(self):
  94. """Handle date range change."""
  95. self.date_from = self.date_range_id.date_start
  96. self.date_to = self.date_range_id.date_end
  97. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  98. def onchange_type_accounts_only(self):
  99. """Handle receivable/payable accounts only change."""
  100. if self.receivable_accounts_only or self.payable_accounts_only:
  101. domain = []
  102. if self.receivable_accounts_only and self.payable_accounts_only:
  103. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  104. elif self.receivable_accounts_only:
  105. domain += [('internal_type', '=', 'receivable')]
  106. elif self.payable_accounts_only:
  107. domain += [('internal_type', '=', 'payable')]
  108. self.account_ids = self.env['account.account'].search(domain)
  109. else:
  110. self.account_ids = None
  111. @api.onchange('partner_ids')
  112. def onchange_partner_ids(self):
  113. """Handle partners change."""
  114. if self.partner_ids:
  115. self.receivable_accounts_only = self.payable_accounts_only = True
  116. else:
  117. self.receivable_accounts_only = self.payable_accounts_only = False
  118. @api.multi
  119. def button_export_html(self):
  120. self.ensure_one()
  121. action = self.env.ref(
  122. 'account_financial_report.action_report_general_ledger')
  123. action_data = action.read()[0]
  124. context1 = action_data.get('context', {})
  125. if isinstance(context1, pycompat.string_types):
  126. context1 = safe_eval(context1)
  127. model = self.env['report_general_ledger']
  128. report = model.create(self._prepare_report_general_ledger())
  129. report.compute_data_for_report()
  130. context1['active_id'] = report.id
  131. context1['active_ids'] = report.ids
  132. action_data['context'] = context1
  133. return action_data
  134. @api.multi
  135. def button_export_pdf(self):
  136. self.ensure_one()
  137. report_type = 'qweb-pdf'
  138. return self._export(report_type)
  139. @api.multi
  140. def button_export_xlsx(self):
  141. self.ensure_one()
  142. report_type = 'xlsx'
  143. return self._export(report_type)
  144. def _prepare_report_general_ledger(self):
  145. self.ensure_one()
  146. return {
  147. 'date_from': self.date_from,
  148. 'date_to': self.date_to,
  149. 'only_posted_moves': self.target_move == 'posted',
  150. 'hide_account_at_0': self.hide_account_at_0,
  151. 'foreign_currency': self.foreign_currency,
  152. 'show_analytic_tags': self.show_analytic_tags,
  153. 'company_id': self.company_id.id,
  154. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  155. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  156. 'filter_cost_center_ids': [(6, 0, self.cost_center_ids.ids)],
  157. 'filter_analytic_tag_ids': [(6, 0, self.analytic_tag_ids.ids)],
  158. 'filter_journal_ids': [(6, 0, self.account_journal_ids.ids)],
  159. 'centralize': self.centralize,
  160. 'fy_start_date': self.fy_start_date,
  161. }
  162. def _export(self, report_type):
  163. """Default export is PDF."""
  164. model = self.env['report_general_ledger']
  165. report = model.create(self._prepare_report_general_ledger())
  166. report.compute_data_for_report()
  167. return report.print_report(report_type)