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.

246 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. '&',
  132. '|', ('company_id', '=', self.company_id.id),
  133. ('company_id', '=', False),
  134. ('parent_id', '=', False)]
  135. res['domain']['date_range_id'] += [
  136. '|', ('company_id', '=', self.company_id.id),
  137. ('company_id', '=', False)]
  138. res['domain']['journal_ids'] += [
  139. ('company_id', '=', self.company_id.id)]
  140. return res
  141. @api.onchange('date_range_id')
  142. def onchange_date_range_id(self):
  143. """Handle date range change."""
  144. self.date_from = self.date_range_id.date_start
  145. self.date_to = self.date_range_id.date_end
  146. @api.multi
  147. @api.constrains('company_id', 'date_range_id')
  148. def _check_company_id_date_range_id(self):
  149. for rec in self.sudo():
  150. if rec.company_id and rec.date_range_id.company_id and\
  151. rec.company_id != rec.date_range_id.company_id:
  152. raise ValidationError(
  153. _('The Company in the Trial Balance Report Wizard and in '
  154. 'Date Range must be the same.'))
  155. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  156. def onchange_type_accounts_only(self):
  157. """Handle receivable/payable accounts only change."""
  158. if self.receivable_accounts_only or self.payable_accounts_only:
  159. domain = [('company_id', '=', self.company_id.id)]
  160. if self.receivable_accounts_only and self.payable_accounts_only:
  161. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  162. elif self.receivable_accounts_only:
  163. domain += [('internal_type', '=', 'receivable')]
  164. elif self.payable_accounts_only:
  165. domain += [('internal_type', '=', 'payable')]
  166. self.account_ids = self.env['account.account'].search(domain)
  167. else:
  168. self.account_ids = None
  169. @api.onchange('show_partner_details')
  170. def onchange_show_partner_details(self):
  171. """Handle partners change."""
  172. if self.show_partner_details:
  173. self.receivable_accounts_only = self.payable_accounts_only = True
  174. else:
  175. self.receivable_accounts_only = self.payable_accounts_only = False
  176. @api.multi
  177. def button_export_html(self):
  178. self.ensure_one()
  179. action = self.env.ref(
  180. 'account_financial_report.action_report_trial_balance')
  181. vals = action.read()[0]
  182. context1 = vals.get('context', {})
  183. if isinstance(context1, pycompat.string_types):
  184. context1 = safe_eval(context1)
  185. model = self.env['report_trial_balance']
  186. report = model.create(self._prepare_report_trial_balance())
  187. report.compute_data_for_report()
  188. context1['active_id'] = report.id
  189. context1['active_ids'] = report.ids
  190. vals['context'] = context1
  191. return vals
  192. @api.multi
  193. def button_export_pdf(self):
  194. self.ensure_one()
  195. report_type = 'qweb-pdf'
  196. return self._export(report_type)
  197. @api.multi
  198. def button_export_xlsx(self):
  199. self.ensure_one()
  200. report_type = 'xlsx'
  201. return self._export(report_type)
  202. def _prepare_report_trial_balance(self):
  203. self.ensure_one()
  204. return {
  205. 'date_from': self.date_from,
  206. 'date_to': self.date_to,
  207. 'only_posted_moves': self.target_move == 'posted',
  208. 'hide_account_at_0': self.hide_account_at_0,
  209. 'foreign_currency': self.foreign_currency,
  210. 'company_id': self.company_id.id,
  211. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  212. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  213. 'filter_journal_ids': [(6, 0, self.journal_ids.ids)],
  214. 'fy_start_date': self.fy_start_date,
  215. 'hierarchy_on': self.hierarchy_on,
  216. 'limit_hierarchy_level': self.limit_hierarchy_level,
  217. 'show_hierarchy_level': self.show_hierarchy_level,
  218. 'hide_parent_hierarchy_level': self.hide_parent_hierarchy_level,
  219. 'show_partner_details': self.show_partner_details,
  220. }
  221. def _export(self, report_type):
  222. """Default export is PDF."""
  223. model = self.env['report_trial_balance']
  224. report = model.create(self._prepare_report_trial_balance())
  225. report.compute_data_for_report()
  226. return report.print_report(report_type)