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.

136 lines
5.1 KiB

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