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.

147 lines
5.4 KiB

  1. # Author: Damien Crier
  2. # Author: Julien Coux
  3. # Copyright 2016 Camptocamp SA
  4. # Copyright 2017 Akretion - Alexis de Lattre
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from odoo import models, fields, api
  7. class GeneralLedgerReportWizard(models.TransientModel):
  8. """General ledger report wizard."""
  9. _name = "general.ledger.report.wizard"
  10. _description = "General Ledger Report Wizard"
  11. company_id = fields.Many2one(
  12. comodel_name='res.company',
  13. default=lambda self: self.env.user.company_id,
  14. string='Company'
  15. )
  16. date_range_id = fields.Many2one(
  17. comodel_name='date.range',
  18. string='Date range'
  19. )
  20. date_from = fields.Date(required=True)
  21. date_to = fields.Date(required=True)
  22. fy_start_date = fields.Date(compute='_compute_fy_start_date')
  23. target_move = fields.Selection([('posted', 'All Posted Entries'),
  24. ('all', 'All Entries')],
  25. string='Target Moves',
  26. required=True,
  27. default='all')
  28. account_ids = fields.Many2many(
  29. comodel_name='account.account',
  30. string='Filter accounts',
  31. )
  32. centralize = fields.Boolean(string='Activate centralization',
  33. default=True)
  34. hide_account_balance_at_0 = fields.Boolean(
  35. string='Hide account ending balance at 0',
  36. help='Use this filter to hide an account or a partner '
  37. 'with an ending balance at 0. '
  38. 'If partners are filtered, '
  39. 'debits and credits totals will not match the trial balance.'
  40. )
  41. receivable_accounts_only = fields.Boolean()
  42. payable_accounts_only = fields.Boolean()
  43. partner_ids = fields.Many2many(
  44. comodel_name='res.partner',
  45. string='Filter partners',
  46. )
  47. cost_center_ids = fields.Many2many(
  48. comodel_name='account.analytic.account',
  49. string='Filter cost centers',
  50. )
  51. not_only_one_unaffected_earnings_account = fields.Boolean(
  52. readonly=True,
  53. string='Not only one unaffected earnings account'
  54. )
  55. @api.depends('date_from')
  56. def _compute_fy_start_date(self):
  57. for wiz in self.filtered('date_from'):
  58. date = fields.Datetime.from_string(wiz.date_from)
  59. res = self.company_id.compute_fiscalyear_dates(date)
  60. wiz.fy_start_date = res['date_from']
  61. @api.onchange('company_id')
  62. def onchange_company_id(self):
  63. """Handle company change."""
  64. account_type = self.env.ref('account.data_unaffected_earnings')
  65. count = self.env['account.account'].search_count(
  66. [
  67. ('user_type_id', '=', account_type.id),
  68. ('company_id', '=', self.company_id.id)
  69. ])
  70. self.not_only_one_unaffected_earnings_account = count != 1
  71. @api.onchange('date_range_id')
  72. def onchange_date_range_id(self):
  73. """Handle date range change."""
  74. self.date_from = self.date_range_id.date_start
  75. self.date_to = self.date_range_id.date_end
  76. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  77. def onchange_type_accounts_only(self):
  78. """Handle receivable/payable accounts only change."""
  79. if self.receivable_accounts_only or self.payable_accounts_only:
  80. domain = []
  81. if self.receivable_accounts_only and self.payable_accounts_only:
  82. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  83. elif self.receivable_accounts_only:
  84. domain += [('internal_type', '=', 'receivable')]
  85. elif self.payable_accounts_only:
  86. domain += [('internal_type', '=', 'payable')]
  87. self.account_ids = self.env['account.account'].search(domain)
  88. else:
  89. self.account_ids = None
  90. @api.onchange('partner_ids')
  91. def onchange_partner_ids(self):
  92. """Handle partners change."""
  93. if self.partner_ids:
  94. self.receivable_accounts_only = self.payable_accounts_only = True
  95. else:
  96. self.receivable_accounts_only = self.payable_accounts_only = False
  97. @api.multi
  98. def button_export_html(self):
  99. self.ensure_one()
  100. report_type = 'qweb-html'
  101. return self._export(report_type)
  102. @api.multi
  103. def button_export_pdf(self):
  104. self.ensure_one()
  105. report_type = 'qweb-pdf'
  106. return self._export(report_type)
  107. @api.multi
  108. def button_export_xlsx(self):
  109. self.ensure_one()
  110. report_type = 'xlsx'
  111. return self._export(report_type)
  112. def _prepare_report_general_ledger(self):
  113. self.ensure_one()
  114. return {
  115. 'date_from': self.date_from,
  116. 'date_to': self.date_to,
  117. 'only_posted_moves': self.target_move == 'posted',
  118. 'hide_account_balance_at_0': self.hide_account_balance_at_0,
  119. 'company_id': self.company_id.id,
  120. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  121. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  122. 'filter_cost_center_ids': [(6, 0, self.cost_center_ids.ids)],
  123. 'centralize': self.centralize,
  124. 'fy_start_date': self.fy_start_date,
  125. }
  126. def _export(self, report_type):
  127. """Default export is PDF."""
  128. model = self.env['report_general_ledger']
  129. report = model.create(self._prepare_report_general_ledger())
  130. return report.print_report(report_type)