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.

209 lines
8.2 KiB

  1. # Author: Damien Crier
  2. # Author: Julien Coux
  3. # Author: Jordi Ballester
  4. # Copyright 2016 Camptocamp SA
  5. # Copyright 2017 Akretion - Alexis de Lattre
  6. # Copyright 2017 Eficent Business and IT Consulting Services, S.L.
  7. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  8. from odoo import api, fields, models, _
  9. from odoo.tools.safe_eval import safe_eval
  10. from odoo.tools import pycompat
  11. from odoo.exceptions import ValidationError
  12. class GeneralLedgerReportWizard(models.TransientModel):
  13. """General ledger report wizard."""
  14. _name = "general.ledger.report.wizard"
  15. _description = "General Ledger Report Wizard"
  16. company_id = fields.Many2one(
  17. comodel_name='res.company',
  18. default=lambda self: self.env.user.company_id,
  19. required=True,
  20. string='Company'
  21. )
  22. date_range_id = fields.Many2one(
  23. comodel_name='date.range',
  24. string='Date range'
  25. )
  26. date_from = fields.Date(required=True)
  27. date_to = fields.Date(required=True)
  28. fy_start_date = fields.Date(compute='_compute_fy_start_date')
  29. target_move = fields.Selection([('posted', 'All Posted Entries'),
  30. ('all', 'All Entries')],
  31. string='Target Moves',
  32. required=True,
  33. default='all')
  34. account_ids = fields.Many2many(
  35. comodel_name='account.account',
  36. string='Filter accounts',
  37. )
  38. centralize = fields.Boolean(string='Activate centralization',
  39. default=True)
  40. hide_account_at_0 = fields.Boolean(
  41. string='Hide account ending balance at 0',
  42. help='Use this filter to hide an account or a partner '
  43. 'with an ending balance at 0. '
  44. 'If partners are filtered, '
  45. 'debits and credits totals will not match the trial balance.'
  46. )
  47. show_analytic_tags = fields.Boolean(
  48. string='Show analytic tags',
  49. )
  50. receivable_accounts_only = fields.Boolean()
  51. payable_accounts_only = fields.Boolean()
  52. partner_ids = fields.Many2many(
  53. comodel_name='res.partner',
  54. string='Filter partners',
  55. )
  56. analytic_tag_ids = fields.Many2many(
  57. comodel_name='account.analytic.tag',
  58. string='Filter accounts',
  59. )
  60. account_journal_ids = fields.Many2many(
  61. comodel_name='account.journal',
  62. string='Filter journals',
  63. )
  64. cost_center_ids = fields.Many2many(
  65. comodel_name='account.analytic.account',
  66. string='Filter cost centers',
  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.depends('date_from')
  79. def _compute_fy_start_date(self):
  80. for wiz in self.filtered('date_from'):
  81. date = fields.Datetime.from_string(wiz.date_from)
  82. res = self.company_id.compute_fiscalyear_dates(date)
  83. wiz.fy_start_date = res['date_from']
  84. @api.onchange('company_id')
  85. def onchange_company_id(self):
  86. """Handle company change."""
  87. account_type = self.env.ref('account.data_unaffected_earnings')
  88. count = self.env['account.account'].search_count(
  89. [
  90. ('user_type_id', '=', account_type.id),
  91. ('company_id', '=', self.company_id.id)
  92. ])
  93. self.not_only_one_unaffected_earnings_account = count != 1
  94. if self.company_id and self.date_range_id.company_id and \
  95. self.date_range_id.company_id != self.company_id:
  96. self.date_range_id = False
  97. if self.company_id and self.partner_ids:
  98. self.partner_ids = self.partner_ids.filtered(
  99. lambda p: p.company_id == self.company_id or
  100. not p.company_id)
  101. if self.company_id and self.account_ids:
  102. self.account_ids = self.account_ids.filtered(
  103. lambda a: a.company_id == self.company_id)
  104. if self.company_id and self.cost_center_ids:
  105. self.cost_center_ids = self.cost_center_ids.filtered(
  106. lambda c: c.company_id == self.company_id)
  107. @api.onchange('date_range_id')
  108. def onchange_date_range_id(self):
  109. """Handle date range change."""
  110. self.date_from = self.date_range_id.date_start
  111. self.date_to = self.date_range_id.date_end
  112. @api.multi
  113. @api.constrains('company_id', 'date_range_id')
  114. def _check_company_id_date_range_id(self):
  115. for rec in self.sudo():
  116. if rec.company_id and rec.date_range_id.company_id and\
  117. rec.company_id != rec.date_range_id.company_id:
  118. raise ValidationError(
  119. _('The Company in the General Ledger Report Wizard and in '
  120. 'Date Range must be the same.'))
  121. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  122. def onchange_type_accounts_only(self):
  123. """Handle receivable/payable accounts only change."""
  124. if self.receivable_accounts_only or self.payable_accounts_only:
  125. domain = [('company_id', '=', self.company_id.id)]
  126. if self.receivable_accounts_only and self.payable_accounts_only:
  127. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  128. elif self.receivable_accounts_only:
  129. domain += [('internal_type', '=', 'receivable')]
  130. elif self.payable_accounts_only:
  131. domain += [('internal_type', '=', 'payable')]
  132. self.account_ids = self.env['account.account'].search(domain)
  133. else:
  134. self.account_ids = None
  135. @api.onchange('partner_ids')
  136. def onchange_partner_ids(self):
  137. """Handle partners change."""
  138. if self.partner_ids:
  139. self.receivable_accounts_only = self.payable_accounts_only = True
  140. else:
  141. self.receivable_accounts_only = self.payable_accounts_only = False
  142. @api.multi
  143. def button_export_html(self):
  144. self.ensure_one()
  145. action = self.env.ref(
  146. 'account_financial_report.action_report_general_ledger')
  147. action_data = action.read()[0]
  148. context1 = action_data.get('context', {})
  149. if isinstance(context1, pycompat.string_types):
  150. context1 = safe_eval(context1)
  151. model = self.env['report_general_ledger']
  152. report = model.create(self._prepare_report_general_ledger())
  153. report.compute_data_for_report()
  154. context1['active_id'] = report.id
  155. context1['active_ids'] = report.ids
  156. action_data['context'] = context1
  157. return action_data
  158. @api.multi
  159. def button_export_pdf(self):
  160. self.ensure_one()
  161. report_type = 'qweb-pdf'
  162. return self._export(report_type)
  163. @api.multi
  164. def button_export_xlsx(self):
  165. self.ensure_one()
  166. report_type = 'xlsx'
  167. return self._export(report_type)
  168. def _prepare_report_general_ledger(self):
  169. self.ensure_one()
  170. return {
  171. 'date_from': self.date_from,
  172. 'date_to': self.date_to,
  173. 'only_posted_moves': self.target_move == 'posted',
  174. 'hide_account_at_0': self.hide_account_at_0,
  175. 'foreign_currency': self.foreign_currency,
  176. 'show_analytic_tags': self.show_analytic_tags,
  177. 'company_id': self.company_id.id,
  178. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  179. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  180. 'filter_cost_center_ids': [(6, 0, self.cost_center_ids.ids)],
  181. 'filter_analytic_tag_ids': [(6, 0, self.analytic_tag_ids.ids)],
  182. 'filter_journal_ids': [(6, 0, self.account_journal_ids.ids)],
  183. 'centralize': self.centralize,
  184. 'fy_start_date': self.fy_start_date,
  185. }
  186. def _export(self, report_type):
  187. """Default export is PDF."""
  188. model = self.env['report_general_ledger']
  189. report = model.create(self._prepare_report_general_ledger())
  190. report.compute_data_for_report()
  191. return report.print_report(report_type)