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.

275 lines
11 KiB

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