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.

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