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.

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