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.

281 lines
11 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='posted')
  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. account_code_from = fields.Many2one(
  80. comodel_name='account.account',
  81. string='Account Code From',
  82. help='Starting account in a range')
  83. account_code_to = fields.Many2one(
  84. comodel_name='account.account',
  85. string='Account Code To',
  86. help='Ending account in a range')
  87. @api.onchange('account_code_from', 'account_code_to')
  88. def on_change_account_range(self):
  89. if self.account_code_from and self.account_code_from.code.isdigit() and \
  90. self.account_code_to and self.account_code_to.code.isdigit():
  91. start_range = int(self.account_code_from.code)
  92. end_range = int(self.account_code_to.code)
  93. self.account_ids = self.env['account.account'].search(
  94. [('code', 'in', [
  95. x for x in range(start_range, end_range + 1)])])
  96. if self.company_id:
  97. self.account_ids = self.account_ids.filtered(
  98. lambda a: a.company_id == self.company_id)
  99. @api.multi
  100. @api.constrains('hierarchy_on', 'show_hierarchy_level')
  101. def _check_show_hierarchy_level(self):
  102. for rec in self:
  103. if rec.hierarchy_on != 'none' and rec.show_hierarchy_level <= 0:
  104. raise UserError(_('The hierarchy level to filter on must be '
  105. 'greater than 0.'))
  106. @api.depends('date_from')
  107. def _compute_fy_start_date(self):
  108. for wiz in self.filtered('date_from'):
  109. date = fields.Datetime.from_string(wiz.date_from)
  110. res = self.company_id.compute_fiscalyear_dates(date)
  111. wiz.fy_start_date = fields.Date.to_string(res['date_from'])
  112. @api.onchange('company_id')
  113. def onchange_company_id(self):
  114. """Handle company change."""
  115. account_type = self.env.ref('account.data_unaffected_earnings')
  116. count = self.env['account.account'].search_count(
  117. [
  118. ('user_type_id', '=', account_type.id),
  119. ('company_id', '=', self.company_id.id)
  120. ])
  121. self.not_only_one_unaffected_earnings_account = count != 1
  122. if self.company_id and self.date_range_id.company_id and \
  123. self.date_range_id.company_id != self.company_id:
  124. self.date_range_id = False
  125. if self.company_id and self.partner_ids:
  126. self.partner_ids = self.partner_ids.filtered(
  127. lambda p: p.company_id == self.company_id or
  128. not p.company_id)
  129. if self.company_id and self.journal_ids:
  130. self.journal_ids = self.journal_ids.filtered(
  131. lambda a: a.company_id == self.company_id)
  132. if self.company_id and self.account_ids:
  133. if self.receivable_accounts_only or self.payable_accounts_only:
  134. self.onchange_type_accounts_only()
  135. else:
  136. self.account_ids = self.account_ids.filtered(
  137. lambda a: a.company_id == self.company_id)
  138. res = {'domain': {'account_ids': [],
  139. 'partner_ids': [],
  140. 'date_range_id': [],
  141. 'journal_ids': [],
  142. }
  143. }
  144. if not self.company_id:
  145. return res
  146. else:
  147. res['domain']['account_ids'] += [
  148. ('company_id', '=', self.company_id.id)]
  149. res['domain']['partner_ids'] += self._get_partner_ids_domain()
  150. res['domain']['date_range_id'] += [
  151. '|', ('company_id', '=', self.company_id.id),
  152. ('company_id', '=', False)]
  153. res['domain']['journal_ids'] += [
  154. ('company_id', '=', self.company_id.id)]
  155. return res
  156. @api.onchange('date_range_id')
  157. def onchange_date_range_id(self):
  158. """Handle date range change."""
  159. self.date_from = self.date_range_id.date_start
  160. self.date_to = self.date_range_id.date_end
  161. @api.multi
  162. @api.constrains('company_id', 'date_range_id')
  163. def _check_company_id_date_range_id(self):
  164. for rec in self.sudo():
  165. if rec.company_id and rec.date_range_id.company_id and\
  166. rec.company_id != rec.date_range_id.company_id:
  167. raise ValidationError(
  168. _('The Company in the Trial Balance Report Wizard and in '
  169. 'Date Range must be the same.'))
  170. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  171. def onchange_type_accounts_only(self):
  172. """Handle receivable/payable accounts only change."""
  173. if self.receivable_accounts_only or self.payable_accounts_only:
  174. domain = [('company_id', '=', self.company_id.id)]
  175. if self.receivable_accounts_only and self.payable_accounts_only:
  176. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  177. elif self.receivable_accounts_only:
  178. domain += [('internal_type', '=', 'receivable')]
  179. elif self.payable_accounts_only:
  180. domain += [('internal_type', '=', 'payable')]
  181. self.account_ids = self.env['account.account'].search(domain)
  182. else:
  183. self.account_ids = None
  184. @api.onchange('show_partner_details')
  185. def onchange_show_partner_details(self):
  186. """Handle partners change."""
  187. if self.show_partner_details:
  188. self.receivable_accounts_only = self.payable_accounts_only = True
  189. else:
  190. self.receivable_accounts_only = self.\
  191. payable_accounts_only = False
  192. @api.multi
  193. @api.depends('company_id')
  194. def _compute_unaffected_earnings_account(self):
  195. account_type = self.env.ref('account.data_unaffected_earnings')
  196. for record in self:
  197. record.unaffected_earnings_account = self.env[
  198. 'account.account'].search(
  199. [
  200. ('user_type_id', '=', account_type.id),
  201. ('company_id', '=', record.company_id.id)
  202. ])
  203. unaffected_earnings_account = fields.Many2one(
  204. comodel_name='account.account',
  205. compute='_compute_unaffected_earnings_account',
  206. store=True
  207. )
  208. @api.multi
  209. def _print_report(self, report_type):
  210. self.ensure_one()
  211. data = self._prepare_report_trial_balance()
  212. if report_type == 'xlsx':
  213. report_name = 'a_f_r.report_trial_balance_xlsx'
  214. else:
  215. report_name = 'account_financial_report.trial_balance'
  216. return self.env['ir.actions.report'].search(
  217. [('report_name', '=', report_name),
  218. ('report_type', '=', report_type)], limit=1).report_action(
  219. self, data=data)
  220. @api.multi
  221. def button_export_html(self):
  222. self.ensure_one()
  223. report_type = 'qweb-html'
  224. return self._export(report_type)
  225. @api.multi
  226. def button_export_pdf(self):
  227. self.ensure_one()
  228. report_type = 'qweb-pdf'
  229. return self._export(report_type)
  230. @api.multi
  231. def button_export_xlsx(self):
  232. self.ensure_one()
  233. report_type = 'xlsx'
  234. return self._export(report_type)
  235. def _prepare_report_trial_balance(self):
  236. self.ensure_one()
  237. return {
  238. 'wizard_id': self.id,
  239. 'date_from': self.date_from,
  240. 'date_to': self.date_to,
  241. 'only_posted_moves': self.target_move == 'posted',
  242. 'hide_account_at_0': self.hide_account_at_0,
  243. 'foreign_currency': self.foreign_currency,
  244. 'company_id': self.company_id.id,
  245. 'account_ids': self.account_ids.ids or [],
  246. 'partner_ids': self.partner_ids.ids or [],
  247. 'journal_ids': self.journal_ids.ids or [],
  248. 'fy_start_date': self.fy_start_date,
  249. 'hierarchy_on': self.hierarchy_on,
  250. 'limit_hierarchy_level': self.limit_hierarchy_level,
  251. 'show_hierarchy_level': self.show_hierarchy_level,
  252. 'hide_parent_hierarchy_level': self.hide_parent_hierarchy_level,
  253. 'show_partner_details': self.show_partner_details,
  254. 'unaffected_earnings_account': self.unaffected_earnings_account.id,
  255. }
  256. def _export(self, report_type):
  257. """Default export is PDF."""
  258. return self._print_report(report_type)