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.

244 lines
9.9 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=False,
  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. hide_parent_hierarchy_level = fields.Boolean(
  49. 'Do not display parent levels', default=False)
  50. account_ids = fields.Many2many(
  51. comodel_name='account.account',
  52. string='Filter accounts',
  53. )
  54. hide_account_at_0 = fields.Boolean(
  55. string='Hide accounts at 0', default=True,
  56. help='When this option is enabled, the trial balance will '
  57. 'not display accounts that have initial balance = '
  58. 'debit = credit = end balance = 0',
  59. )
  60. receivable_accounts_only = fields.Boolean()
  61. payable_accounts_only = fields.Boolean()
  62. show_partner_details = fields.Boolean()
  63. partner_ids = fields.Many2many(
  64. comodel_name='res.partner',
  65. string='Filter partners',
  66. )
  67. journal_ids = fields.Many2many(
  68. comodel_name="account.journal",
  69. )
  70. not_only_one_unaffected_earnings_account = fields.Boolean(
  71. readonly=True,
  72. string='Not only one unaffected earnings account'
  73. )
  74. foreign_currency = fields.Boolean(
  75. string='Show foreign currency',
  76. help='Display foreign currency for move lines, unless '
  77. 'account currency is not setup through chart of accounts '
  78. 'will display initial and final balance in that currency.'
  79. )
  80. @api.multi
  81. @api.constrains('hierarchy_on', 'show_hierarchy_level')
  82. def _check_show_hierarchy_level(self):
  83. for rec in self:
  84. if rec.hierarchy_on != 'none' and rec.show_hierarchy_level <= 0:
  85. raise UserError(_('The hierarchy level to filter on must be '
  86. 'greater than 0.'))
  87. @api.depends('date_from')
  88. def _compute_fy_start_date(self):
  89. for wiz in self.filtered('date_from'):
  90. date = fields.Datetime.from_string(wiz.date_from)
  91. res = self.company_id.compute_fiscalyear_dates(date)
  92. wiz.fy_start_date = res['date_from']
  93. @api.onchange('company_id')
  94. def onchange_company_id(self):
  95. """Handle company change."""
  96. account_type = self.env.ref('account.data_unaffected_earnings')
  97. count = self.env['account.account'].search_count(
  98. [
  99. ('user_type_id', '=', account_type.id),
  100. ('company_id', '=', self.company_id.id)
  101. ])
  102. self.not_only_one_unaffected_earnings_account = count != 1
  103. if self.company_id and self.date_range_id.company_id and \
  104. self.date_range_id.company_id != self.company_id:
  105. self.date_range_id = False
  106. if self.company_id and self.partner_ids:
  107. self.partner_ids = self.partner_ids.filtered(
  108. lambda p: p.company_id == self.company_id or
  109. not p.company_id)
  110. if self.company_id and self.journal_ids:
  111. self.journal_ids = self.journal_ids.filtered(
  112. lambda a: a.company_id == self.company_id)
  113. if self.company_id and self.account_ids:
  114. if self.receivable_accounts_only or self.payable_accounts_only:
  115. self.onchange_type_accounts_only()
  116. else:
  117. self.account_ids = self.account_ids.filtered(
  118. lambda a: a.company_id == self.company_id)
  119. res = {'domain': {'account_ids': [],
  120. 'partner_ids': [],
  121. 'date_range_id': [],
  122. 'journal_ids': [],
  123. }
  124. }
  125. if not self.company_id:
  126. return res
  127. else:
  128. res['domain']['account_ids'] += [
  129. ('company_id', '=', self.company_id.id)]
  130. res['domain']['partner_ids'] += [
  131. '|', ('company_id', '=', self.company_id.id),
  132. ('company_id', '=', False)]
  133. res['domain']['date_range_id'] += [
  134. '|', ('company_id', '=', self.company_id.id),
  135. ('company_id', '=', False)]
  136. res['domain']['journal_ids'] += [
  137. ('company_id', '=', self.company_id.id)]
  138. return res
  139. @api.onchange('date_range_id')
  140. def onchange_date_range_id(self):
  141. """Handle date range change."""
  142. self.date_from = self.date_range_id.date_start
  143. self.date_to = self.date_range_id.date_end
  144. @api.multi
  145. @api.constrains('company_id', 'date_range_id')
  146. def _check_company_id_date_range_id(self):
  147. for rec in self.sudo():
  148. if rec.company_id and rec.date_range_id.company_id and\
  149. rec.company_id != rec.date_range_id.company_id:
  150. raise ValidationError(
  151. _('The Company in the Trial Balance Report Wizard and in '
  152. 'Date Range must be the same.'))
  153. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  154. def onchange_type_accounts_only(self):
  155. """Handle receivable/payable accounts only change."""
  156. if self.receivable_accounts_only or self.payable_accounts_only:
  157. domain = [('company_id', '=', self.company_id.id)]
  158. if self.receivable_accounts_only and self.payable_accounts_only:
  159. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  160. elif self.receivable_accounts_only:
  161. domain += [('internal_type', '=', 'receivable')]
  162. elif self.payable_accounts_only:
  163. domain += [('internal_type', '=', 'payable')]
  164. self.account_ids = self.env['account.account'].search(domain)
  165. else:
  166. self.account_ids = None
  167. @api.onchange('show_partner_details')
  168. def onchange_show_partner_details(self):
  169. """Handle partners change."""
  170. if self.show_partner_details:
  171. self.receivable_accounts_only = self.payable_accounts_only = True
  172. else:
  173. self.receivable_accounts_only = self.payable_accounts_only = False
  174. @api.multi
  175. def button_export_html(self):
  176. self.ensure_one()
  177. action = self.env.ref(
  178. 'account_financial_report.action_report_trial_balance')
  179. vals = action.read()[0]
  180. context1 = vals.get('context', {})
  181. if isinstance(context1, pycompat.string_types):
  182. context1 = safe_eval(context1)
  183. model = self.env['report_trial_balance']
  184. report = model.create(self._prepare_report_trial_balance())
  185. report.compute_data_for_report()
  186. context1['active_id'] = report.id
  187. context1['active_ids'] = report.ids
  188. vals['context'] = context1
  189. return vals
  190. @api.multi
  191. def button_export_pdf(self):
  192. self.ensure_one()
  193. report_type = 'qweb-pdf'
  194. return self._export(report_type)
  195. @api.multi
  196. def button_export_xlsx(self):
  197. self.ensure_one()
  198. report_type = 'xlsx'
  199. return self._export(report_type)
  200. def _prepare_report_trial_balance(self):
  201. self.ensure_one()
  202. return {
  203. 'date_from': self.date_from,
  204. 'date_to': self.date_to,
  205. 'only_posted_moves': self.target_move == 'posted',
  206. 'hide_account_at_0': self.hide_account_at_0,
  207. 'foreign_currency': self.foreign_currency,
  208. 'company_id': self.company_id.id,
  209. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  210. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  211. 'filter_journal_ids': [(6, 0, self.journal_ids.ids)],
  212. 'fy_start_date': self.fy_start_date,
  213. 'hierarchy_on': self.hierarchy_on,
  214. 'limit_hierarchy_level': self.limit_hierarchy_level,
  215. 'show_hierarchy_level': self.show_hierarchy_level,
  216. 'hide_parent_hierarchy_level': self.hide_parent_hierarchy_level,
  217. 'show_partner_details': self.show_partner_details,
  218. }
  219. def _export(self, report_type):
  220. """Default export is PDF."""
  221. model = self.env['report_trial_balance']
  222. report = model.create(self._prepare_report_trial_balance())
  223. report.compute_data_for_report()
  224. return report.print_report(report_type)