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.

309 lines
12 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. # Copyright 2020 Druidoo
  8. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  9. from odoo import api, fields, models, _
  10. from odoo.exceptions import ValidationError
  11. import time
  12. class GeneralLedgerReportWizard(models.TransientModel):
  13. """General ledger report wizard."""
  14. _name = "general.ledger.report.wizard"
  15. _description = "General Ledger Report Wizard"
  16. _inherit = 'account_financial_report_abstract_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='posted')
  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 analytic tags',
  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. account_code_from = fields.Many2one(
  84. comodel_name='account.account',
  85. string='Account Code From',
  86. help='Starting account in a range')
  87. account_code_to = fields.Many2one(
  88. comodel_name='account.account',
  89. string='Account Code To',
  90. help='Ending account in a range')
  91. show_partner_details = fields.Boolean(
  92. string='Show Partner Details',
  93. default=True,
  94. )
  95. @api.onchange('account_code_from', 'account_code_to')
  96. def on_change_account_range(self):
  97. if self.account_code_from and self.account_code_from.code.isdigit() and \
  98. self.account_code_to and self.account_code_to.code.isdigit():
  99. start_range = int(self.account_code_from.code)
  100. end_range = int(self.account_code_to.code)
  101. self.account_ids = self.env['account.account'].search(
  102. [('code', 'in', [
  103. x for x in range(start_range, end_range + 1)])])
  104. if self.company_id:
  105. self.account_ids = self.account_ids.filtered(
  106. lambda a: a.company_id == self.company_id)
  107. def _init_date_from(self):
  108. """set start date to begin of current year if fiscal year running"""
  109. today = fields.Date.context_today(self)
  110. cur_month = fields.Date.from_string(today).month
  111. cur_day = fields.Date.from_string(today).day
  112. last_fsc_month = self.env.user.company_id.fiscalyear_last_month
  113. last_fsc_day = self.env.user.company_id.fiscalyear_last_day
  114. if cur_month < last_fsc_month \
  115. or cur_month == last_fsc_month and cur_day <= last_fsc_day:
  116. return time.strftime('%Y-01-01')
  117. def _default_foreign_currency(self):
  118. return self.env.user.has_group('base.group_multi_currency')
  119. @api.depends('date_from')
  120. def _compute_fy_start_date(self):
  121. for wiz in self.filtered('date_from'):
  122. date = fields.Datetime.from_string(wiz.date_from)
  123. res = self.company_id.compute_fiscalyear_dates(date)
  124. wiz.fy_start_date = fields.Date.to_string(res['date_from'])
  125. @api.onchange('company_id')
  126. def onchange_company_id(self):
  127. """Handle company change."""
  128. account_type = self.env.ref('account.data_unaffected_earnings')
  129. count = self.env['account.account'].search_count(
  130. [
  131. ('user_type_id', '=', account_type.id),
  132. ('company_id', '=', self.company_id.id)
  133. ])
  134. self.not_only_one_unaffected_earnings_account = count != 1
  135. if self.company_id and self.date_range_id.company_id and \
  136. self.date_range_id.company_id != self.company_id:
  137. self.date_range_id = False
  138. if self.company_id and self.account_journal_ids:
  139. self.account_journal_ids = self.account_journal_ids.filtered(
  140. lambda p: p.company_id == self.company_id or
  141. not p.company_id)
  142. if self.company_id and self.partner_ids:
  143. self.partner_ids = self.partner_ids.filtered(
  144. lambda p: p.company_id == self.company_id or
  145. not p.company_id)
  146. if self.company_id and self.account_ids:
  147. if self.receivable_accounts_only or self.payable_accounts_only:
  148. self.onchange_type_accounts_only()
  149. else:
  150. self.account_ids = self.account_ids.filtered(
  151. lambda a: a.company_id == self.company_id)
  152. if self.company_id and self.cost_center_ids:
  153. self.cost_center_ids = self.cost_center_ids.filtered(
  154. lambda c: c.company_id == self.company_id)
  155. res = {'domain': {'account_ids': [],
  156. 'partner_ids': [],
  157. 'account_journal_ids': [],
  158. 'cost_center_ids': [],
  159. 'date_range_id': []
  160. }
  161. }
  162. if not self.company_id:
  163. return res
  164. else:
  165. res['domain']['account_ids'] += [
  166. ('company_id', '=', self.company_id.id)]
  167. res['domain']['account_journal_ids'] += [
  168. ('company_id', '=', self.company_id.id)]
  169. res['domain']['partner_ids'] += self._get_partner_ids_domain()
  170. res['domain']['cost_center_ids'] += [
  171. ('company_id', '=', self.company_id.id)]
  172. res['domain']['date_range_id'] += [
  173. '|', ('company_id', '=', self.company_id.id),
  174. ('company_id', '=', False)]
  175. return res
  176. @api.onchange('date_range_id')
  177. def onchange_date_range_id(self):
  178. """Handle date range change."""
  179. if self.date_range_id:
  180. self.date_from = self.date_range_id.date_start
  181. self.date_to = self.date_range_id.date_end
  182. @api.multi
  183. @api.constrains('company_id', 'date_range_id')
  184. def _check_company_id_date_range_id(self):
  185. for rec in self.sudo():
  186. if rec.company_id and rec.date_range_id.company_id and\
  187. rec.company_id != rec.date_range_id.company_id:
  188. raise ValidationError(
  189. _('The Company in the General Ledger Report Wizard and in '
  190. 'Date Range must be the same.'))
  191. @api.onchange('receivable_accounts_only', 'payable_accounts_only')
  192. def onchange_type_accounts_only(self):
  193. """Handle receivable/payable accounts only change."""
  194. if self.receivable_accounts_only or self.payable_accounts_only:
  195. domain = [('company_id', '=', self.company_id.id)]
  196. if self.receivable_accounts_only and self.payable_accounts_only:
  197. domain += [('internal_type', 'in', ('receivable', 'payable'))]
  198. elif self.receivable_accounts_only:
  199. domain += [('internal_type', '=', 'receivable')]
  200. elif self.payable_accounts_only:
  201. domain += [('internal_type', '=', 'payable')]
  202. self.account_ids = self.env['account.account'].search(domain)
  203. else:
  204. self.account_ids = None
  205. @api.onchange('partner_ids')
  206. def onchange_partner_ids(self):
  207. """Handle partners change."""
  208. if self.partner_ids:
  209. self.receivable_accounts_only = self.payable_accounts_only = True
  210. else:
  211. self.receivable_accounts_only = self.payable_accounts_only = False
  212. @api.multi
  213. @api.depends('company_id')
  214. def _compute_unaffected_earnings_account(self):
  215. account_type = self.env.ref('account.data_unaffected_earnings')
  216. for record in self:
  217. record.unaffected_earnings_account = self.env[
  218. 'account.account'].search(
  219. [
  220. ('user_type_id', '=', account_type.id),
  221. ('company_id', '=', record.company_id.id)
  222. ])
  223. unaffected_earnings_account = fields.Many2one(
  224. comodel_name='account.account',
  225. compute='_compute_unaffected_earnings_account',
  226. store=True
  227. )
  228. @api.multi
  229. def _print_report(self, report_type):
  230. self.ensure_one()
  231. data = self._prepare_report_general_ledger()
  232. if report_type == 'xlsx':
  233. report_name = 'a_f_r.report_general_ledger_xlsx'
  234. else:
  235. report_name = 'account_financial_report.general_ledger'
  236. return self.env['ir.actions.report'].search(
  237. [('report_name', '=', report_name),
  238. ('report_type', '=', report_type)], limit=1).report_action(
  239. self, data=data)
  240. @api.multi
  241. def button_export_html(self):
  242. self.ensure_one()
  243. report_type = 'qweb-html'
  244. return self._export(report_type)
  245. @api.multi
  246. def button_export_pdf(self):
  247. self.ensure_one()
  248. report_type = 'qweb-pdf'
  249. return self._export(report_type)
  250. @api.multi
  251. def button_export_xlsx(self):
  252. self.ensure_one()
  253. report_type = 'xlsx'
  254. return self._export(report_type)
  255. def _prepare_report_general_ledger(self):
  256. self.ensure_one()
  257. return {
  258. 'wizard_id': self.id,
  259. 'date_from': self.date_from,
  260. 'date_to': self.date_to,
  261. 'only_posted_moves': self.target_move == 'posted',
  262. 'hide_account_at_0': self.hide_account_at_0,
  263. 'foreign_currency': self.foreign_currency,
  264. 'show_analytic_tags': self.show_analytic_tags,
  265. 'company_id': self.company_id.id,
  266. 'account_ids': self.account_ids.ids,
  267. 'partner_ids': self.partner_ids.ids,
  268. 'show_partner_details': self.show_partner_details,
  269. 'cost_center_ids': self.cost_center_ids.ids,
  270. 'analytic_tag_ids': self.analytic_tag_ids.ids,
  271. 'journal_ids': self.account_journal_ids.ids,
  272. 'centralize': self.centralize,
  273. 'fy_start_date': self.fy_start_date,
  274. 'unaffected_earnings_account': self.unaffected_earnings_account.id,
  275. }
  276. def _export(self, report_type):
  277. """Default export is PDF."""
  278. return self._print_report(report_type)
  279. def _get_atr_from_dict(self, obj_id, data, key):
  280. try:
  281. return data[obj_id][key]
  282. except KeyError:
  283. return data[str(obj_id)][key]