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.

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