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.

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