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.

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