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.

259 lines
11 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. import time
  13. class GeneralLedgerReportWizard(models.TransientModel):
  14. """General ledger report wizard."""
  15. _name = "general.ledger.report.wizard"
  16. _description = "General Ledger Report Wizard"
  17. _inherit = 'account_financial_report_abstract_wizard'
  18. company_id = fields.Many2one(
  19. comodel_name='res.company',
  20. default=lambda self: self.env.user.company_id,
  21. required=False,
  22. string='Company'
  23. )
  24. date_range_id = fields.Many2one(
  25. comodel_name='date.range',
  26. string='Date range'
  27. )
  28. date_from = fields.Date(required=True,
  29. default=lambda self: self._init_date_from())
  30. date_to = fields.Date(required=True,
  31. default=fields.Date.context_today)
  32. fy_start_date = fields.Date(compute='_compute_fy_start_date')
  33. target_move = fields.Selection([('posted', 'All Posted Entries'),
  34. ('all', 'All Entries')],
  35. string='Target Moves',
  36. required=True,
  37. default='all')
  38. account_ids = fields.Many2many(
  39. comodel_name='account.account',
  40. string='Filter accounts',
  41. )
  42. centralize = fields.Boolean(string='Activate centralization',
  43. default=True)
  44. hide_account_at_0 = fields.Boolean(
  45. string='Hide account ending balance at 0',
  46. help='Use this filter to hide an account or a partner '
  47. 'with an ending balance at 0. '
  48. 'If partners are filtered, '
  49. 'debits and credits totals will not match the trial balance.'
  50. )
  51. show_analytic_tags = fields.Boolean(
  52. string='Show analytic tags',
  53. )
  54. receivable_accounts_only = fields.Boolean()
  55. payable_accounts_only = fields.Boolean()
  56. partner_ids = fields.Many2many(
  57. comodel_name='res.partner',
  58. string='Filter partners',
  59. default=lambda self: self._default_partners(),
  60. )
  61. analytic_tag_ids = fields.Many2many(
  62. comodel_name='account.analytic.tag',
  63. string='Filter analytic tags',
  64. )
  65. account_journal_ids = fields.Many2many(
  66. comodel_name='account.journal',
  67. string='Filter journals',
  68. )
  69. cost_center_ids = fields.Many2many(
  70. comodel_name='account.analytic.account',
  71. string='Filter cost centers',
  72. )
  73. not_only_one_unaffected_earnings_account = fields.Boolean(
  74. readonly=True,
  75. string='Not only one unaffected earnings account'
  76. )
  77. foreign_currency = fields.Boolean(
  78. string='Show foreign currency',
  79. help='Display foreign currency for move lines, unless '
  80. 'account currency is not setup through chart of accounts '
  81. 'will display initial and final balance in that currency.',
  82. default=lambda self: self._default_foreign_currency(),
  83. )
  84. def _init_date_from(self):
  85. """set start date to begin of current year if fiscal year running"""
  86. today = fields.Date.context_today(self)
  87. cur_month = fields.Date.from_string(today).month
  88. cur_day = fields.Date.from_string(today).day
  89. last_fsc_month = self.env.user.company_id.fiscalyear_last_month
  90. last_fsc_day = self.env.user.company_id.fiscalyear_last_day
  91. if cur_month < last_fsc_month \
  92. or cur_month == last_fsc_month and cur_day <= last_fsc_day:
  93. return time.strftime('%Y-01-01')
  94. def _default_foreign_currency(self):
  95. return self.env.user.has_group('base.group_multi_currency')
  96. @api.depends('date_from')
  97. def _compute_fy_start_date(self):
  98. for wiz in self.filtered('date_from'):
  99. date = fields.Datetime.from_string(wiz.date_from)
  100. res = self.company_id.compute_fiscalyear_dates(date)
  101. wiz.fy_start_date = fields.Date.to_string(res['date_from'])
  102. @api.onchange('company_id')
  103. def onchange_company_id(self):
  104. """Handle company change."""
  105. account_type = self.env.ref('account.data_unaffected_earnings')
  106. count = self.env['account.account'].search_count(
  107. [
  108. ('user_type_id', '=', account_type.id),
  109. ('company_id', '=', self.company_id.id)
  110. ])
  111. self.not_only_one_unaffected_earnings_account = count != 1
  112. if self.company_id and self.date_range_id.company_id and \
  113. self.date_range_id.company_id != self.company_id:
  114. self.date_range_id = False
  115. if self.company_id and self.account_journal_ids:
  116. self.account_journal_ids = self.account_journal_ids.filtered(
  117. lambda p: p.company_id == self.company_id or
  118. not p.company_id)
  119. if self.company_id and self.partner_ids:
  120. self.partner_ids = self.partner_ids.filtered(
  121. lambda p: p.company_id == self.company_id or
  122. not p.company_id)
  123. if self.company_id and self.account_ids:
  124. if self.receivable_accounts_only or self.payable_accounts_only:
  125. self.onchange_type_accounts_only()
  126. else:
  127. self.account_ids = self.account_ids.filtered(
  128. lambda a: a.company_id == self.company_id)
  129. if self.company_id and self.cost_center_ids:
  130. self.cost_center_ids = self.cost_center_ids.filtered(
  131. lambda c: c.company_id == self.company_id)
  132. res = {'domain': {'account_ids': [],
  133. 'partner_ids': [],
  134. 'account_journal_ids': [],
  135. 'cost_center_ids': [],
  136. 'date_range_id': []
  137. }
  138. }
  139. if not self.company_id:
  140. return res
  141. else:
  142. res['domain']['account_ids'] += [
  143. ('company_id', '=', self.company_id.id)]
  144. res['domain']['account_journal_ids'] += [
  145. ('company_id', '=', self.company_id.id)]
  146. res['domain']['partner_ids'] += self._get_partner_ids_domain()
  147. res['domain']['cost_center_ids'] += [
  148. ('company_id', '=', self.company_id.id)]
  149. res['domain']['date_range_id'] += [
  150. '|', ('company_id', '=', self.company_id.id),
  151. ('company_id', '=', False)]
  152. return res
  153. @api.onchange('date_range_id')
  154. def onchange_date_range_id(self):
  155. """Handle date range change."""
  156. if self.date_range_id:
  157. self.date_from = self.date_range_id.date_start
  158. self.date_to = self.date_range_id.date_end
  159. @api.multi
  160. @api.constrains('company_id', 'date_range_id')
  161. def _check_company_id_date_range_id(self):
  162. for rec in self.sudo():
  163. if rec.company_id and rec.date_range_id.company_id and\
  164. rec.company_id != rec.date_range_id.company_id:
  165. raise ValidationError(
  166. _('The Company in the General Ledger Report Wizard and in '
  167. 'Date Range must be the same.'))
  168. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  169. def onchange_type_accounts_only(self):
  170. """Handle receivable/payable accounts only change."""
  171. if self.receivable_accounts_only or self.payable_accounts_only:
  172. domain = [('company_id', '=', self.company_id.id)]
  173. if self.receivable_accounts_only and self.payable_accounts_only:
  174. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  175. elif self.receivable_accounts_only:
  176. domain += [('internal_type', '=', 'receivable')]
  177. elif self.payable_accounts_only:
  178. domain += [('internal_type', '=', 'payable')]
  179. self.account_ids = self.env['account.account'].search(domain)
  180. else:
  181. self.account_ids = None
  182. @api.onchange('partner_ids')
  183. def onchange_partner_ids(self):
  184. """Handle partners change."""
  185. if self.partner_ids:
  186. self.receivable_accounts_only = self.payable_accounts_only = True
  187. else:
  188. self.receivable_accounts_only = self.payable_accounts_only = False
  189. @api.multi
  190. def button_export_html(self):
  191. self.ensure_one()
  192. action = self.env.ref(
  193. 'account_financial_report.action_report_general_ledger')
  194. action_data = action.read()[0]
  195. context1 = action_data.get('context', {})
  196. if isinstance(context1, pycompat.string_types):
  197. context1 = safe_eval(context1)
  198. model = self.env['report_general_ledger']
  199. report = model.create(self._prepare_report_general_ledger())
  200. report.compute_data_for_report()
  201. context1['active_id'] = report.id
  202. context1['active_ids'] = report.ids
  203. action_data['context'] = context1
  204. return action_data
  205. @api.multi
  206. def button_export_pdf(self):
  207. self.ensure_one()
  208. report_type = 'qweb-pdf'
  209. return self._export(report_type)
  210. @api.multi
  211. def button_export_xlsx(self):
  212. self.ensure_one()
  213. report_type = 'xlsx'
  214. return self._export(report_type)
  215. def _prepare_report_general_ledger(self):
  216. self.ensure_one()
  217. return {
  218. 'date_from': self.date_from,
  219. 'date_to': self.date_to,
  220. 'only_posted_moves': self.target_move == 'posted',
  221. 'hide_account_at_0': self.hide_account_at_0,
  222. 'foreign_currency': self.foreign_currency,
  223. 'show_analytic_tags': self.show_analytic_tags,
  224. 'company_id': self.company_id.id,
  225. 'filter_account_ids': [(6, 0, self.account_ids.ids)],
  226. 'filter_partner_ids': [(6, 0, self.partner_ids.ids)],
  227. 'filter_cost_center_ids': [(6, 0, self.cost_center_ids.ids)],
  228. 'filter_analytic_tag_ids': [(6, 0, self.analytic_tag_ids.ids)],
  229. 'filter_journal_ids': [(6, 0, self.account_journal_ids.ids)],
  230. 'centralize': self.centralize,
  231. 'fy_start_date': self.fy_start_date,
  232. }
  233. def _export(self, report_type):
  234. """Default export is PDF."""
  235. model = self.env['report_general_ledger']
  236. report = model.create(self._prepare_report_general_ledger())
  237. report.compute_data_for_report()
  238. return report.print_report(report_type)