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.2 KiB

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