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.

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