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.

194 lines
7.5 KiB

  1. # Author: Julien Coux
  2. # Copyright 2016 Camptocamp SA
  3. # Copyright 2017 Akretion - Alexis de Lattre
  4. # Copyright 2018 Eficent Business and IT Consuting Services, S.L.
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from odoo import api, fields, models, _
  7. from odoo.tools.safe_eval import safe_eval
  8. from odoo.tools import pycompat
  9. from odoo.exceptions import UserError
  10. class TrialBalanceReportWizard(models.TransientModel):
  11. """Trial balance report wizard."""
  12. _name = "trial.balance.report.wizard"
  13. _description = "Trial Balance Report Wizard"
  14. company_id = fields.Many2one(
  15. comodel_name='res.company',
  16. default=lambda self: self.env.user.company_id,
  17. string='Company'
  18. )
  19. date_range_id = fields.Many2one(
  20. comodel_name='date.range',
  21. string='Date range'
  22. )
  23. date_from = fields.Date(required=True)
  24. date_to = fields.Date(required=True)
  25. fy_start_date = fields.Date(compute='_compute_fy_start_date')
  26. target_move = fields.Selection([('posted', 'All Posted Entries'),
  27. ('all', 'All Entries')],
  28. string='Target Moves',
  29. required=True,
  30. default='all')
  31. hierarchy_on = fields.Selection(
  32. [('computed', 'Computed Accounts'),
  33. ('relation', 'Child Accounts'),
  34. ('none', 'No hierarchy')],
  35. string='Hierarchy On',
  36. required=True,
  37. default='computed',
  38. help="""Computed Accounts: Use when the account group have codes
  39. that represent prefixes of the actual accounts.\n
  40. Child Accounts: Use when your account groups are hierarchical.\n
  41. No hierarchy: Use to display just the accounts, without any grouping.
  42. """,
  43. )
  44. limit_hierarchy_level = fields.Boolean('Limit hierarchy levels')
  45. show_hierarchy_level = fields.Integer('Hierarchy Levels to display',
  46. default=1)
  47. account_ids = fields.Many2many(
  48. comodel_name='account.account',
  49. string='Filter accounts',
  50. )
  51. hide_account_at_0 = fields.Boolean(
  52. string='Hide accounts at 0', default=True,
  53. help='When this option is enabled, the trial balance will '
  54. 'not display accounts that have initial balance = '
  55. 'debit = credit = end balance = 0',
  56. )
  57. receivable_accounts_only = fields.Boolean()
  58. payable_accounts_only = fields.Boolean()
  59. show_partner_details = fields.Boolean()
  60. partner_ids = fields.Many2many(
  61. comodel_name='res.partner',
  62. string='Filter partners',
  63. )
  64. journal_ids = fields.Many2many(
  65. comodel_name="account.journal",
  66. )
  67. not_only_one_unaffected_earnings_account = fields.Boolean(
  68. readonly=True,
  69. string='Not only one unaffected earnings account'
  70. )
  71. foreign_currency = fields.Boolean(
  72. string='Show foreign currency',
  73. help='Display foreign currency for move lines, unless '
  74. 'account currency is not setup through chart of accounts '
  75. 'will display initial and final balance in that currency.'
  76. )
  77. @api.multi
  78. @api.constrains('hierarchy_on', 'show_hierarchy_level')
  79. def _check_show_hierarchy_level(self):
  80. for rec in self:
  81. if rec.hierarchy_on != 'none' and rec.show_hierarchy_level <= 0:
  82. raise UserError(_('The hierarchy level to filter on must be '
  83. 'greater than 0.'))
  84. @api.depends('date_from')
  85. def _compute_fy_start_date(self):
  86. for wiz in self.filtered('date_from'):
  87. date = fields.Datetime.from_string(wiz.date_from)
  88. res = self.company_id.compute_fiscalyear_dates(date)
  89. wiz.fy_start_date = res['date_from']
  90. @api.onchange('company_id')
  91. def onchange_company_id(self):
  92. """Handle company change."""
  93. account_type = self.env.ref('account.data_unaffected_earnings')
  94. count = self.env['account.account'].search_count(
  95. [
  96. ('user_type_id', '=', account_type.id),
  97. ('company_id', '=', self.company_id.id)
  98. ])
  99. self.not_only_one_unaffected_earnings_account = count != 1
  100. @api.onchange('date_range_id')
  101. def onchange_date_range_id(self):
  102. """Handle date range change."""
  103. self.date_from = self.date_range_id.date_start
  104. self.date_to = self.date_range_id.date_end
  105. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  106. def onchange_type_accounts_only(self):
  107. """Handle receivable/payable accounts only change."""
  108. if self.receivable_accounts_only or self.payable_accounts_only:
  109. domain = []
  110. if self.receivable_accounts_only and self.payable_accounts_only:
  111. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  112. elif self.receivable_accounts_only:
  113. domain += [('internal_type', '=', 'receivable')]
  114. elif self.payable_accounts_only:
  115. domain += [('internal_type', '=', 'payable')]
  116. self.account_ids = self.env['account.account'].search(domain)
  117. else:
  118. self.account_ids = None
  119. @api.onchange('show_partner_details')
  120. def onchange_show_partner_details(self):
  121. """Handle partners change."""
  122. if self.show_partner_details:
  123. self.receivable_accounts_only = self.payable_accounts_only = True
  124. else:
  125. self.receivable_accounts_only = self.payable_accounts_only = False
  126. @api.multi
  127. def button_export_html(self):
  128. self.ensure_one()
  129. action = self.env.ref(
  130. 'account_financial_report.action_report_trial_balance')
  131. vals = action.read()[0]
  132. context1 = vals.get('context', {})
  133. if isinstance(context1, pycompat.string_types):
  134. context1 = safe_eval(context1)
  135. model = self.env['report_trial_balance']
  136. report = model.create(self._prepare_report_trial_balance())
  137. report.compute_data_for_report()
  138. context1['active_id'] = report.id
  139. context1['active_ids'] = report.ids
  140. vals['context'] = context1
  141. return vals
  142. @api.multi
  143. def button_export_pdf(self):
  144. self.ensure_one()
  145. report_type = 'qweb-pdf'
  146. return self._export(report_type)
  147. @api.multi
  148. def button_export_xlsx(self):
  149. self.ensure_one()
  150. report_type = 'xlsx'
  151. return self._export(report_type)
  152. def _prepare_report_trial_balance(self):
  153. self.ensure_one()
  154. return {
  155. 'date_from': self.date_from,
  156. 'date_to': self.date_to,
  157. 'only_posted_moves': self.target_move == 'posted',
  158. 'hide_account_at_0': self.hide_account_at_0,
  159. 'foreign_currency': self.foreign_currency,
  160. 'company_id': self.company_id.id,
  161. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  162. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  163. 'filter_journal_ids': [(6, 0, self.journal_ids.ids)],
  164. 'fy_start_date': self.fy_start_date,
  165. 'hierarchy_on': self.hierarchy_on,
  166. 'limit_hierarchy_level': self.limit_hierarchy_level,
  167. 'show_hierarchy_level': self.show_hierarchy_level,
  168. 'show_partner_details': self.show_partner_details,
  169. }
  170. def _export(self, report_type):
  171. """Default export is PDF."""
  172. model = self.env['report_trial_balance']
  173. report = model.create(self._prepare_report_trial_balance())
  174. report.compute_data_for_report()
  175. return report.print_report(report_type)