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.

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