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.

172 lines
6.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Author: Damien Crier
  3. # Author: Julien Coux
  4. # Copyright 2016 Camptocamp SA
  5. # Copyright 2017 Akretion - Alexis de Lattre
  6. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  7. from odoo import models, fields, api
  8. from odoo.tools.safe_eval import safe_eval
  9. class GeneralLedgerReportWizard(models.TransientModel):
  10. """General ledger report wizard."""
  11. _name = "general.ledger.report.wizard"
  12. _description = "General Ledger Report Wizard"
  13. company_id = fields.Many2one(
  14. comodel_name='res.company',
  15. default=lambda self: self.env.user.company_id,
  16. string='Company'
  17. )
  18. date_range_id = fields.Many2one(
  19. comodel_name='date.range',
  20. string='Date range'
  21. )
  22. date_from = fields.Date(required=True)
  23. date_to = fields.Date(required=True)
  24. fy_start_date = fields.Date(compute='_compute_fy_start_date')
  25. target_move = fields.Selection([('posted', 'All Posted Entries'),
  26. ('all', 'All Entries')],
  27. string='Target Moves',
  28. required=True,
  29. default='all')
  30. account_ids = fields.Many2many(
  31. comodel_name='account.account',
  32. string='Filter accounts',
  33. )
  34. centralize = fields.Boolean(string='Activate centralization',
  35. default=True)
  36. hide_account_balance_at_0 = fields.Boolean(
  37. string='Hide account ending balance at 0',
  38. help='Use this filter to hide an account or a partner '
  39. 'with an ending balance at 0. '
  40. 'If partners are filtered, '
  41. 'debits and credits totals will not match the trial balance.'
  42. )
  43. receivable_accounts_only = fields.Boolean()
  44. payable_accounts_only = fields.Boolean()
  45. partner_ids = fields.Many2many(
  46. comodel_name='res.partner',
  47. string='Filter partners',
  48. )
  49. journal_ids = fields.Many2many(
  50. comodel_name="account.journal",
  51. string="Filter journals",
  52. )
  53. cost_center_ids = fields.Many2many(
  54. comodel_name='account.analytic.account',
  55. string='Filter cost centers',
  56. )
  57. not_only_one_unaffected_earnings_account = fields.Boolean(
  58. readonly=True,
  59. string='Not only one unaffected earnings account'
  60. )
  61. foreign_currency = fields.Boolean(
  62. string='Show foreign currency',
  63. help='Display foreign currency for move lines, unless '
  64. 'account currency is not setup through chart of accounts '
  65. 'will display initial and final balance in that currency.'
  66. )
  67. @api.depends('date_from')
  68. def _compute_fy_start_date(self):
  69. for wiz in self.filtered('date_from'):
  70. date = fields.Datetime.from_string(wiz.date_from)
  71. res = self.company_id.compute_fiscalyear_dates(date)
  72. wiz.fy_start_date = res['date_from']
  73. @api.onchange('company_id')
  74. def onchange_company_id(self):
  75. """Handle company change."""
  76. account_type = self.env.ref('account.data_unaffected_earnings')
  77. count = self.env['account.account'].search_count(
  78. [
  79. ('user_type_id', '=', account_type.id),
  80. ('company_id', '=', self.company_id.id)
  81. ])
  82. self.not_only_one_unaffected_earnings_account = count != 1
  83. @api.onchange('date_range_id')
  84. def onchange_date_range_id(self):
  85. """Handle date range change."""
  86. self.date_from = self.date_range_id.date_start
  87. self.date_to = self.date_range_id.date_end
  88. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  89. def onchange_type_accounts_only(self):
  90. """Handle receivable/payable accounts only change."""
  91. if self.receivable_accounts_only or self.payable_accounts_only:
  92. domain = []
  93. if self.receivable_accounts_only and self.payable_accounts_only:
  94. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  95. elif self.receivable_accounts_only:
  96. domain += [('internal_type', '=', 'receivable')]
  97. elif self.payable_accounts_only:
  98. domain += [('internal_type', '=', 'payable')]
  99. self.account_ids = self.env['account.account'].search(domain)
  100. else:
  101. self.account_ids = None
  102. @api.onchange('partner_ids')
  103. def onchange_partner_ids(self):
  104. """Handle partners change."""
  105. if self.partner_ids:
  106. self.receivable_accounts_only = self.payable_accounts_only = True
  107. else:
  108. self.receivable_accounts_only = self.payable_accounts_only = False
  109. @api.multi
  110. def button_export_html(self):
  111. self.ensure_one()
  112. action = self.env.ref(
  113. 'account_financial_report_qweb.action_report_general_ledger')
  114. vals = action.read()[0]
  115. context1 = vals.get('context', {})
  116. if isinstance(context1, basestring):
  117. context1 = safe_eval(context1)
  118. model = self.env['report_general_ledger_qweb']
  119. report = model.create(self._prepare_report_general_ledger())
  120. report.compute_data_for_report()
  121. context1['active_id'] = report.id
  122. context1['active_ids'] = report.ids
  123. vals['context'] = context1
  124. return vals
  125. @api.multi
  126. def button_export_pdf(self):
  127. self.ensure_one()
  128. report_type = 'qweb-pdf'
  129. return self._export(report_type)
  130. @api.multi
  131. def button_export_xlsx(self):
  132. self.ensure_one()
  133. report_type = 'xlsx'
  134. return self._export(report_type)
  135. def _prepare_report_general_ledger(self):
  136. self.ensure_one()
  137. return {
  138. 'date_from': self.date_from,
  139. 'date_to': self.date_to,
  140. 'only_posted_moves': self.target_move == 'posted',
  141. 'hide_account_balance_at_0': self.hide_account_balance_at_0,
  142. 'foreign_currency': self.foreign_currency,
  143. 'company_id': self.company_id.id,
  144. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  145. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  146. 'filter_journal_ids': [(6, 0, self.journal_ids.ids)],
  147. 'filter_cost_center_ids': [(6, 0, self.cost_center_ids.ids)],
  148. 'centralize': self.centralize,
  149. 'fy_start_date': self.fy_start_date,
  150. }
  151. def _export(self, report_type):
  152. """Default export is PDF."""
  153. model = self.env['report_general_ledger_qweb']
  154. report = model.create(self._prepare_report_general_ledger())
  155. report.compute_data_for_report()
  156. return report.print_report(report_type)