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.

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