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.

168 lines
6.4 KiB

  1. # -*- coding: utf-8 -*-
  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. from odoo.tools.safe_eval import safe_eval
  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. journal_ids = fields.Many2many(
  48. comodel_name="account.journal",
  49. string="Filter journals",
  50. )
  51. not_only_one_unaffected_earnings_account = fields.Boolean(
  52. readonly=True,
  53. string='Not only one unaffected earnings account'
  54. )
  55. foreign_currency = fields.Boolean(
  56. string='Show foreign currency',
  57. help='Display foreign currency for move lines, unless '
  58. 'account currency is not setup through chart of accounts '
  59. 'will display initial and final balance in that currency.'
  60. )
  61. @api.depends('date_from')
  62. def _compute_fy_start_date(self):
  63. for wiz in self.filtered('date_from'):
  64. date = fields.Datetime.from_string(wiz.date_from)
  65. res = self.company_id.compute_fiscalyear_dates(date)
  66. wiz.fy_start_date = res['date_from']
  67. @api.onchange('company_id')
  68. def onchange_company_id(self):
  69. """Handle company change."""
  70. account_type = self.env.ref('account.data_unaffected_earnings')
  71. count = self.env['account.account'].search_count(
  72. [
  73. ('user_type_id', '=', account_type.id),
  74. ('company_id', '=', self.company_id.id)
  75. ])
  76. self.not_only_one_unaffected_earnings_account = count != 1
  77. @api.onchange('date_range_id')
  78. def onchange_date_range_id(self):
  79. """Handle date range change."""
  80. self.date_from = self.date_range_id.date_start
  81. self.date_to = self.date_range_id.date_end
  82. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  83. def onchange_type_accounts_only(self):
  84. """Handle receivable/payable accounts only change."""
  85. if self.receivable_accounts_only or self.payable_accounts_only:
  86. domain = []
  87. if self.receivable_accounts_only and self.payable_accounts_only:
  88. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  89. elif self.receivable_accounts_only:
  90. domain += [('internal_type', '=', 'receivable')]
  91. elif self.payable_accounts_only:
  92. domain += [('internal_type', '=', 'payable')]
  93. self.account_ids = self.env['account.account'].search(domain)
  94. else:
  95. self.account_ids = None
  96. @api.onchange('show_partner_details')
  97. def onchange_show_partner_details(self):
  98. """Handle partners change."""
  99. if self.show_partner_details:
  100. self.receivable_accounts_only = self.payable_accounts_only = True
  101. self.hide_account_balance_at_0 = True
  102. else:
  103. self.receivable_accounts_only = self.payable_accounts_only = False
  104. self.hide_account_balance_at_0 = False
  105. @api.multi
  106. def button_export_html(self):
  107. self.ensure_one()
  108. action = self.env.ref(
  109. 'account_financial_report_qweb.action_report_trial_balance')
  110. vals = action.read()[0]
  111. context1 = vals.get('context', {})
  112. if isinstance(context1, basestring):
  113. context1 = safe_eval(context1)
  114. model = self.env['report_trial_balance_qweb']
  115. report = model.create(self._prepare_report_trial_balance())
  116. report.compute_data_for_report()
  117. context1['active_id'] = report.id
  118. context1['active_ids'] = report.ids
  119. vals['context'] = context1
  120. return vals
  121. @api.multi
  122. def button_export_pdf(self):
  123. self.ensure_one()
  124. report_type = 'qweb-pdf'
  125. return self._export(report_type)
  126. @api.multi
  127. def button_export_xlsx(self):
  128. self.ensure_one()
  129. report_type = 'xlsx'
  130. return self._export(report_type)
  131. def _prepare_report_trial_balance(self):
  132. self.ensure_one()
  133. return {
  134. 'date_from': self.date_from,
  135. 'date_to': self.date_to,
  136. 'only_posted_moves': self.target_move == 'posted',
  137. 'hide_account_balance_at_0': self.hide_account_balance_at_0,
  138. 'foreign_currency': self.foreign_currency,
  139. 'company_id': self.company_id.id,
  140. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  141. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  142. 'filter_journal_ids': [(6, 0, self.journal_ids.ids)],
  143. 'fy_start_date': self.fy_start_date,
  144. 'show_partner_details': self.show_partner_details,
  145. }
  146. def _export(self, report_type):
  147. """Default export is PDF."""
  148. model = self.env['report_trial_balance_qweb']
  149. report = model.create(self._prepare_report_trial_balance())
  150. report.compute_data_for_report()
  151. return report.print_report(report_type)