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.

170 lines
6.6 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. hierarchy_on = fields.Selection([('computed', 'Computed Accounts'),
  30. ('relation', 'Child Accounts')],
  31. string='Hierarchy On',
  32. required=True,
  33. default='computed')
  34. account_ids = fields.Many2many(
  35. comodel_name='account.account',
  36. string='Filter accounts',
  37. )
  38. hide_account_balance_at_0 = fields.Boolean(
  39. string='Hide account ending balance at 0',
  40. help='Use this filter to hide an account or a partner '
  41. 'with an ending balance at 0. '
  42. 'If partners are filtered, '
  43. 'debits and credits totals will not match the trial balance.'
  44. )
  45. receivable_accounts_only = fields.Boolean()
  46. payable_accounts_only = fields.Boolean()
  47. show_partner_details = fields.Boolean()
  48. partner_ids = fields.Many2many(
  49. comodel_name='res.partner',
  50. string='Filter partners',
  51. )
  52. not_only_one_unaffected_earnings_account = fields.Boolean(
  53. readonly=True,
  54. string='Not only one unaffected earnings account'
  55. )
  56. foreign_currency = fields.Boolean(
  57. string='Show foreign currency',
  58. help='Display foreign currency for move lines, unless '
  59. 'account currency is not setup through chart of accounts '
  60. 'will display initial and final balance in that currency.'
  61. )
  62. @api.depends('date_from')
  63. def _compute_fy_start_date(self):
  64. for wiz in self.filtered('date_from'):
  65. date = fields.Datetime.from_string(wiz.date_from)
  66. res = self.company_id.compute_fiscalyear_dates(date)
  67. wiz.fy_start_date = res['date_from']
  68. @api.onchange('company_id')
  69. def onchange_company_id(self):
  70. """Handle company change."""
  71. account_type = self.env.ref('account.data_unaffected_earnings')
  72. count = self.env['account.account'].search_count(
  73. [
  74. ('user_type_id', '=', account_type.id),
  75. ('company_id', '=', self.company_id.id)
  76. ])
  77. self.not_only_one_unaffected_earnings_account = count != 1
  78. @api.onchange('date_range_id')
  79. def onchange_date_range_id(self):
  80. """Handle date range change."""
  81. self.date_from = self.date_range_id.date_start
  82. self.date_to = self.date_range_id.date_end
  83. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  84. def onchange_type_accounts_only(self):
  85. """Handle receivable/payable accounts only change."""
  86. if self.receivable_accounts_only or self.payable_accounts_only:
  87. domain = []
  88. if self.receivable_accounts_only and self.payable_accounts_only:
  89. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  90. elif self.receivable_accounts_only:
  91. domain += [('internal_type', '=', 'receivable')]
  92. elif self.payable_accounts_only:
  93. domain += [('internal_type', '=', 'payable')]
  94. self.account_ids = self.env['account.account'].search(domain)
  95. else:
  96. self.account_ids = None
  97. @api.onchange('show_partner_details')
  98. def onchange_show_partner_details(self):
  99. """Handle partners change."""
  100. if self.show_partner_details:
  101. self.receivable_accounts_only = self.payable_accounts_only = True
  102. self.hide_account_balance_at_0 = True
  103. else:
  104. self.receivable_accounts_only = self.payable_accounts_only = False
  105. self.hide_account_balance_at_0 = False
  106. @api.multi
  107. def button_export_html(self):
  108. self.ensure_one()
  109. action = self.env.ref(
  110. 'account_financial_report.action_report_trial_balance')
  111. vals = action.read()[0]
  112. context1 = vals.get('context', {})
  113. if isinstance(context1, pycompat.string_types):
  114. context1 = safe_eval(context1)
  115. model = self.env['report_trial_balance']
  116. report = model.create(self._prepare_report_trial_balance())
  117. report.compute_data_for_report()
  118. context1['active_id'] = report.id
  119. context1['active_ids'] = report.ids
  120. vals['context'] = context1
  121. return vals
  122. @api.multi
  123. def button_export_pdf(self):
  124. self.ensure_one()
  125. report_type = 'qweb-pdf'
  126. return self._export(report_type)
  127. @api.multi
  128. def button_export_xlsx(self):
  129. self.ensure_one()
  130. report_type = 'xlsx'
  131. return self._export(report_type)
  132. def _prepare_report_trial_balance(self):
  133. self.ensure_one()
  134. return {
  135. 'date_from': self.date_from,
  136. 'date_to': self.date_to,
  137. 'only_posted_moves': self.target_move == 'posted',
  138. 'hide_account_balance_at_0': self.hide_account_balance_at_0,
  139. 'foreign_currency': self.foreign_currency,
  140. 'company_id': self.company_id.id,
  141. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  142. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  143. 'fy_start_date': self.fy_start_date,
  144. 'hierarchy_on': self.hierarchy_on,
  145. 'show_partner_details': self.show_partner_details,
  146. }
  147. def _export(self, report_type):
  148. """Default export is PDF."""
  149. model = self.env['report_trial_balance']
  150. report = model.create(self._prepare_report_trial_balance())
  151. report.compute_data_for_report()
  152. return report.print_report(report_type)