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.

155 lines
5.8 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. from odoo.tools.safe_eval import safe_eval
  7. from odoo.tools import pycompat
  8. class TrialBalanceReportWizard(models.TransientModel):
  9. """Trial balance report wizard."""
  10. _name = "trial.balance.report.wizard"
  11. _description = "Trial Balance 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. string='Date range'
  20. )
  21. date_from = fields.Date(required=True)
  22. date_to = fields.Date(required=True)
  23. fy_start_date = fields.Date(compute='_compute_fy_start_date')
  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. hide_account_balance_at_0 = fields.Boolean(
  34. string='Hide account ending balance at 0',
  35. help='Use this filter to hide an account or a partner '
  36. 'with an ending balance at 0. '
  37. 'If partners are filtered, '
  38. 'debits and credits totals will not match the trial balance.'
  39. )
  40. receivable_accounts_only = fields.Boolean()
  41. payable_accounts_only = fields.Boolean()
  42. show_partner_details = fields.Boolean()
  43. partner_ids = fields.Many2many(
  44. comodel_name='res.partner',
  45. string='Filter partners',
  46. )
  47. not_only_one_unaffected_earnings_account = fields.Boolean(
  48. readonly=True,
  49. string='Not only one unaffected earnings account'
  50. )
  51. @api.depends('date_from')
  52. def _compute_fy_start_date(self):
  53. for wiz in self.filtered('date_from'):
  54. date = fields.Datetime.from_string(wiz.date_from)
  55. res = self.company_id.compute_fiscalyear_dates(date)
  56. wiz.fy_start_date = res['date_from']
  57. @api.onchange('company_id')
  58. def onchange_company_id(self):
  59. """Handle company change."""
  60. account_type = self.env.ref('account.data_unaffected_earnings')
  61. count = self.env['account.account'].search_count(
  62. [
  63. ('user_type_id', '=', account_type.id),
  64. ('company_id', '=', self.company_id.id)
  65. ])
  66. self.not_only_one_unaffected_earnings_account = count != 1
  67. @api.onchange('date_range_id')
  68. def onchange_date_range_id(self):
  69. """Handle date range change."""
  70. self.date_from = self.date_range_id.date_start
  71. self.date_to = self.date_range_id.date_end
  72. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  73. def onchange_type_accounts_only(self):
  74. """Handle receivable/payable accounts only change."""
  75. if self.receivable_accounts_only or self.payable_accounts_only:
  76. domain = []
  77. if self.receivable_accounts_only and self.payable_accounts_only:
  78. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  79. elif self.receivable_accounts_only:
  80. domain += [('internal_type', '=', 'receivable')]
  81. elif self.payable_accounts_only:
  82. domain += [('internal_type', '=', 'payable')]
  83. self.account_ids = self.env['account.account'].search(domain)
  84. else:
  85. self.account_ids = None
  86. @api.onchange('show_partner_details')
  87. def onchange_show_partner_details(self):
  88. """Handle partners change."""
  89. if self.show_partner_details:
  90. self.receivable_accounts_only = self.payable_accounts_only = True
  91. else:
  92. self.receivable_accounts_only = self.payable_accounts_only = False
  93. @api.multi
  94. def button_export_html(self):
  95. self.ensure_one()
  96. action = self.env.ref(
  97. 'account_financial_report.action_report_trial_balance')
  98. vals = action.read()[0]
  99. context1 = vals.get('context', {})
  100. if isinstance(context1, pycompat.string_types):
  101. context1 = safe_eval(context1)
  102. model = self.env['report_trial_balance']
  103. report = model.create(self._prepare_report_trial_balance())
  104. report.compute_data_for_report()
  105. context1['active_id'] = report.id
  106. context1['active_ids'] = report.ids
  107. vals['context'] = context1
  108. return vals
  109. @api.multi
  110. def button_export_pdf(self):
  111. self.ensure_one()
  112. report_type = 'qweb-pdf'
  113. return self._export(report_type)
  114. @api.multi
  115. def button_export_xlsx(self):
  116. self.ensure_one()
  117. report_type = 'xlsx'
  118. return self._export(report_type)
  119. def _prepare_report_trial_balance(self):
  120. self.ensure_one()
  121. return {
  122. 'date_from': self.date_from,
  123. 'date_to': self.date_to,
  124. 'only_posted_moves': self.target_move == 'posted',
  125. 'hide_account_balance_at_0': self.hide_account_balance_at_0,
  126. 'company_id': self.company_id.id,
  127. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  128. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  129. 'fy_start_date': self.fy_start_date,
  130. 'show_partner_details': self.show_partner_details,
  131. }
  132. def _export(self, report_type):
  133. """Default export is PDF."""
  134. model = self.env['report_trial_balance']
  135. report = model.create(self._prepare_report_trial_balance())
  136. report.compute_data_for_report()
  137. return report.print_report(report_type)