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.

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