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.

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