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.

140 lines
5.3 KiB

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