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.

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