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.

175 lines
6.7 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_balance_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. receivable_accounts_only = fields.Boolean()
  46. payable_accounts_only = fields.Boolean()
  47. partner_ids = fields.Many2many(
  48. comodel_name='res.partner',
  49. string='Filter partners',
  50. )
  51. account_journal_ids = fields.Many2many(
  52. comodel_name='account.journal',
  53. string='Filter journals',
  54. )
  55. cost_center_ids = fields.Many2many(
  56. comodel_name='account.analytic.account',
  57. string='Filter cost centers',
  58. )
  59. not_only_one_unaffected_earnings_account = fields.Boolean(
  60. readonly=True,
  61. string='Not only one unaffected earnings account'
  62. )
  63. foreign_currency = fields.Boolean(
  64. string='Show foreign currency',
  65. help='Display foreign currency for move lines, unless '
  66. 'account currency is not setup through chart of accounts '
  67. 'will display initial and final balance in that currency.'
  68. )
  69. @api.depends('date_from')
  70. def _compute_fy_start_date(self):
  71. for wiz in self.filtered('date_from'):
  72. date = fields.Datetime.from_string(wiz.date_from)
  73. res = self.company_id.compute_fiscalyear_dates(date)
  74. wiz.fy_start_date = res['date_from']
  75. @api.onchange('company_id')
  76. def onchange_company_id(self):
  77. """Handle company change."""
  78. account_type = self.env.ref('account.data_unaffected_earnings')
  79. count = self.env['account.account'].search_count(
  80. [
  81. ('user_type_id', '=', account_type.id),
  82. ('company_id', '=', self.company_id.id)
  83. ])
  84. self.not_only_one_unaffected_earnings_account = count != 1
  85. @api.onchange('date_range_id')
  86. def onchange_date_range_id(self):
  87. """Handle date range change."""
  88. self.date_from = self.date_range_id.date_start
  89. self.date_to = self.date_range_id.date_end
  90. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  91. def onchange_type_accounts_only(self):
  92. """Handle receivable/payable accounts only change."""
  93. if self.receivable_accounts_only or self.payable_accounts_only:
  94. domain = []
  95. if self.receivable_accounts_only and self.payable_accounts_only:
  96. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  97. elif self.receivable_accounts_only:
  98. domain += [('internal_type', '=', 'receivable')]
  99. elif self.payable_accounts_only:
  100. domain += [('internal_type', '=', 'payable')]
  101. self.account_ids = self.env['account.account'].search(domain)
  102. else:
  103. self.account_ids = None
  104. @api.onchange('partner_ids')
  105. def onchange_partner_ids(self):
  106. """Handle partners change."""
  107. if self.partner_ids:
  108. self.receivable_accounts_only = self.payable_accounts_only = True
  109. else:
  110. self.receivable_accounts_only = self.payable_accounts_only = False
  111. @api.multi
  112. def button_export_html(self):
  113. self.ensure_one()
  114. action = self.env.ref(
  115. 'account_financial_report.action_report_general_ledger')
  116. action_data = action.read()[0]
  117. context1 = action_data.get('context', {})
  118. if isinstance(context1, pycompat.string_types):
  119. context1 = safe_eval(context1)
  120. model = self.env['report_general_ledger']
  121. report = model.create(self._prepare_report_general_ledger())
  122. report.compute_data_for_report()
  123. context1['active_id'] = report.id
  124. context1['active_ids'] = report.ids
  125. action_data['context'] = context1
  126. return action_data
  127. @api.multi
  128. def button_export_pdf(self):
  129. self.ensure_one()
  130. report_type = 'qweb-pdf'
  131. return self._export(report_type)
  132. @api.multi
  133. def button_export_xlsx(self):
  134. self.ensure_one()
  135. report_type = 'xlsx'
  136. return self._export(report_type)
  137. def _prepare_report_general_ledger(self):
  138. self.ensure_one()
  139. return {
  140. 'date_from': self.date_from,
  141. 'date_to': self.date_to,
  142. 'only_posted_moves': self.target_move == 'posted',
  143. 'hide_account_balance_at_0': self.hide_account_balance_at_0,
  144. 'foreign_currency': self.foreign_currency,
  145. 'company_id': self.company_id.id,
  146. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  147. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  148. 'filter_cost_center_ids': [(6, 0, self.cost_center_ids.ids)],
  149. 'filter_journal_ids': [(6, 0, self.account_journal_ids.ids)],
  150. 'centralize': self.centralize,
  151. 'fy_start_date': self.fy_start_date,
  152. }
  153. def _export(self, report_type):
  154. """Default export is PDF."""
  155. model = self.env['report_general_ledger']
  156. report = model.create(self._prepare_report_general_ledger())
  157. report.compute_data_for_report()
  158. return report.print_report(report_type)