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.

637 lines
30 KiB

  1. # © 2016 Julien Coux (Camptocamp)
  2. # © 2018 Forest and Biomass Romania SA
  3. # Copyright 2020 ForgeFlow S.L. (https://www.forgeflow.com)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import models, api
  6. class TrialBalanceReport(models.AbstractModel):
  7. _name = 'report.account_financial_report.trial_balance'
  8. _description = "Trial Balance Report"
  9. @api.model
  10. def get_html(self, given_context=None):
  11. return self._get_html()
  12. def _get_html(self):
  13. result = {}
  14. rcontext = {}
  15. context = dict(self.env.context)
  16. rcontext.update(context.get('data'))
  17. active_id = context.get('active_id')
  18. wiz = self.env['open.items.report.wizard'].browse(active_id)
  19. rcontext['o'] = wiz
  20. result['html'] = self.env.ref(
  21. 'account_financial_report.report_trial_balance').render(rcontext)
  22. return result
  23. def _get_accounts_data(self, account_ids):
  24. accounts = self.env['account.account'].browse(account_ids)
  25. accounts_data = {}
  26. for account in accounts:
  27. accounts_data.update({account.id: {
  28. 'id': account.id,
  29. 'code': account.code,
  30. 'name': account.name,
  31. 'group_id': account.group_id.id,
  32. 'hide_account': False,
  33. 'currency_id': account.currency_id or False,
  34. 'currency_name': account.currency_id.name}
  35. })
  36. return accounts_data
  37. def _get_initial_balances_bs_ml_domain(
  38. self, account_ids, journal_ids, partner_ids, company_id, date_from,
  39. only_posted_moves, show_partner_details):
  40. accounts_domain = [
  41. ('company_id', '=', company_id),
  42. ('user_type_id.include_initial_balance', '=', True)]
  43. if account_ids:
  44. accounts_domain += [('id', 'in', account_ids)]
  45. domain = [('date', '<', date_from)]
  46. accounts = self.env['account.account'].search(accounts_domain)
  47. domain += [('account_id', 'in', accounts.ids)]
  48. if company_id:
  49. domain += [('company_id', '=', company_id)]
  50. if journal_ids:
  51. domain += [('journal_id', 'in', journal_ids)]
  52. if partner_ids:
  53. domain += [('partner_id', 'in', partner_ids)]
  54. if only_posted_moves:
  55. domain += [('move_id.state', '=', 'posted')]
  56. if show_partner_details:
  57. domain += [('account_id.internal_type', 'in',
  58. ['receivable', 'payable'])]
  59. return domain
  60. def _get_initial_balances_pl_ml_domain(
  61. self, account_ids, journal_ids, partner_ids, company_id, date_from,
  62. only_posted_moves, show_partner_details, fy_start_date):
  63. accounts_domain = [
  64. ('company_id', '=', company_id),
  65. ('user_type_id.include_initial_balance', '=', False)]
  66. if account_ids:
  67. accounts_domain += [('id', 'in', account_ids)]
  68. domain = [('date', '<', date_from),
  69. ('date', '>=', fy_start_date)
  70. ]
  71. accounts = self.env['account.account'].search(accounts_domain)
  72. domain += [('account_id', 'in', accounts.ids)]
  73. if company_id:
  74. domain += [('company_id', '=', company_id)]
  75. if journal_ids:
  76. domain += [('journal_id', 'in', journal_ids)]
  77. if partner_ids:
  78. domain += [('partner_id', 'in', partner_ids)]
  79. if only_posted_moves:
  80. domain += [('move_id.state', '=', 'posted')]
  81. if show_partner_details:
  82. domain += [('account_id.internal_type', 'in', [
  83. 'receivable', 'payable'])]
  84. return domain
  85. @api.model
  86. def _get_period_ml_domain(
  87. self, account_ids, journal_ids, partner_ids, company_id, date_to,
  88. date_from, only_posted_moves, show_partner_details):
  89. domain = ['&', ('date', '>=', date_from), ('date', '<=', date_to)]
  90. if company_id:
  91. domain += [('company_id', '=', company_id)]
  92. if account_ids:
  93. domain += [('account_id', 'in', account_ids)]
  94. if journal_ids:
  95. domain += [('journal_id', 'in', journal_ids)]
  96. if partner_ids:
  97. domain += [('partner_id', 'in', partner_ids)]
  98. if only_posted_moves:
  99. domain += [('move_id.state', '=', 'posted')]
  100. if show_partner_details:
  101. domain += [('account_id.internal_type', 'in', [
  102. 'receivable', 'payable'])]
  103. return domain
  104. def _get_initial_balance_fy_pl_ml_domain(
  105. self, account_ids, journal_ids, partner_ids, company_id,
  106. fy_start_date, only_posted_moves, show_partner_details):
  107. accounts_domain = [
  108. ('company_id', '=', company_id),
  109. ('user_type_id.include_initial_balance', '=', False)]
  110. if account_ids:
  111. accounts_domain += [('id', 'in', account_ids)]
  112. domain = [('date', '<', fy_start_date)]
  113. accounts = self.env['account.account'].search(accounts_domain)
  114. domain += [('account_id', 'in', accounts.ids)]
  115. if company_id:
  116. domain += [('company_id', '=', company_id)]
  117. if journal_ids:
  118. domain += [('journal_id', 'in', journal_ids)]
  119. if partner_ids:
  120. domain += [('partner_id', 'in', partner_ids)]
  121. if only_posted_moves:
  122. domain += [('move_id.state', '=', 'posted')]
  123. if show_partner_details:
  124. domain += [('account_id.internal_type', 'in',
  125. ['receivable', 'payable'])]
  126. return domain
  127. def _get_pl_initial_balance(
  128. self, account_ids, journal_ids, partner_ids, company_id,
  129. fy_start_date, only_posted_moves, show_partner_details,
  130. foreign_currency):
  131. domain = self._get_initial_balance_fy_pl_ml_domain(
  132. account_ids, journal_ids, partner_ids, company_id, fy_start_date,
  133. only_posted_moves, show_partner_details)
  134. initial_balances = self.env['account.move.line'].read_group(
  135. domain=domain,
  136. fields=[
  137. 'account_id',
  138. 'balance',
  139. 'amount_currency'],
  140. groupby=['account_id'])
  141. pl_initial_balance = 0.0
  142. pl_initial_currency_balance = 0.0
  143. for initial_balance in initial_balances:
  144. pl_initial_balance += initial_balance['balance']
  145. if foreign_currency:
  146. pl_initial_currency_balance += initial_balance[
  147. 'amount_currency']
  148. return pl_initial_balance, pl_initial_currency_balance
  149. def _get_data(self, account_ids, journal_ids, partner_ids,
  150. company_id, date_to, date_from, foreign_currency,
  151. only_posted_moves, show_partner_details, hide_account_at_0,
  152. unaffected_earnings_account, fy_start_date):
  153. initial_domain_bs = self._get_initial_balances_bs_ml_domain(
  154. account_ids, journal_ids, partner_ids,
  155. company_id, date_from, only_posted_moves, show_partner_details)
  156. tb_initial_acc_bs = self.env['account.move.line'].read_group(
  157. domain=initial_domain_bs,
  158. fields=[
  159. 'account_id',
  160. 'balance',
  161. 'amount_currency'],
  162. groupby=['account_id'])
  163. initial_domain_pl = self._get_initial_balances_pl_ml_domain(
  164. account_ids, journal_ids, partner_ids,
  165. company_id, date_from, only_posted_moves, show_partner_details,
  166. fy_start_date)
  167. tb_initial_acc_pl = self.env['account.move.line'].read_group(
  168. domain=initial_domain_pl,
  169. fields=[
  170. 'account_id',
  171. 'balance',
  172. 'amount_currency'],
  173. groupby=['account_id'])
  174. tb_initial_acc = tb_initial_acc_bs + tb_initial_acc_pl
  175. if hide_account_at_0:
  176. tb_initial_acc = [p for p in tb_initial_acc if p['balance'] != 0]
  177. period_domain = self._get_period_ml_domain(
  178. account_ids, journal_ids, partner_ids, company_id, date_to,
  179. date_from, only_posted_moves, show_partner_details)
  180. tb_period_acc = self.env['account.move.line'].read_group(
  181. domain=period_domain,
  182. fields=[
  183. 'account_id',
  184. 'debit',
  185. 'credit',
  186. 'balance',
  187. 'amount_currency'],
  188. groupby=['account_id'])
  189. if show_partner_details:
  190. tb_initial_prt_bs = self.env['account.move.line'].read_group(
  191. domain=initial_domain_bs,
  192. fields=[
  193. 'account_id',
  194. 'partner_id',
  195. 'balance',
  196. 'amount_currency'],
  197. groupby=['account_id', 'partner_id'], lazy=False)
  198. tb_initial_prt_pl = self.env['account.move.line'].read_group(
  199. domain=initial_domain_pl,
  200. fields=[
  201. 'account_id',
  202. 'partner_id',
  203. 'balance',
  204. 'amount_currency'],
  205. groupby=['account_id', 'partner_id'])
  206. tb_initial_prt = tb_initial_prt_bs + tb_initial_prt_pl
  207. if hide_account_at_0:
  208. tb_initial_prt = \
  209. [p for p in tb_initial_prt if p['balance'] != 0]
  210. tb_period_prt = self.env['account.move.line'].read_group(
  211. domain=period_domain,
  212. fields=[
  213. 'account_id',
  214. 'partner_id',
  215. 'debit',
  216. 'credit',
  217. 'balance',
  218. 'amount_currency'],
  219. groupby=['account_id', 'partner_id'], lazy=False)
  220. total_amount = {}
  221. partners_data = []
  222. for tb in tb_period_acc:
  223. acc_id = tb['account_id'][0]
  224. total_amount[acc_id] = {}
  225. total_amount[acc_id]['credit'] = tb['credit']
  226. total_amount[acc_id]['debit'] = tb['debit']
  227. total_amount[acc_id]['balance'] = tb['balance']
  228. total_amount[acc_id]['initial_balance'] = 0.0
  229. total_amount[acc_id]['ending_balance'] = tb['balance']
  230. if foreign_currency:
  231. total_amount[acc_id]['initial_currency_balance'] =\
  232. 0.0
  233. total_amount[acc_id]['ending_currency_balance'] =\
  234. tb['amount_currency']
  235. for tb in tb_initial_acc:
  236. acc_id = tb['account_id'][0]
  237. if acc_id not in total_amount.keys():
  238. total_amount[acc_id] = {}
  239. total_amount[acc_id]['credit'] = 0.0
  240. total_amount[acc_id]['debit'] = 0.0
  241. total_amount[acc_id]['balance'] = 0.0
  242. total_amount[acc_id]['initial_balance'] = tb['balance']
  243. total_amount[acc_id]['ending_balance'] = tb['balance']
  244. if foreign_currency:
  245. total_amount[acc_id]['initial_currency_balance'] = tb[
  246. 'amount_currency']
  247. total_amount[acc_id]['ending_currency_balance'] = tb[
  248. 'amount_currency']
  249. else:
  250. total_amount[acc_id]['initial_balance'] = tb['balance']
  251. total_amount[acc_id]['ending_balance'] += tb['balance']
  252. if foreign_currency:
  253. total_amount[acc_id]['initial_currency_balance'] = tb[
  254. 'amount_currency']
  255. total_amount[acc_id]['ending_currency_balance'] += tb[
  256. 'amount_currency']
  257. if show_partner_details:
  258. partners_ids = set()
  259. partners_data = {}
  260. for tb in tb_period_prt:
  261. acc_id = tb['account_id'][0]
  262. if tb['partner_id']:
  263. prt_id = tb['partner_id'][0]
  264. if tb['partner_id'] not in partners_ids:
  265. partners_data.update({
  266. prt_id: {
  267. 'id': prt_id,
  268. 'name': tb['partner_id'][1],
  269. }
  270. })
  271. total_amount[acc_id][prt_id] = {}
  272. total_amount[acc_id][prt_id]['credit'] = tb['credit']
  273. total_amount[acc_id][prt_id]['debit'] = tb['debit']
  274. total_amount[acc_id][prt_id]['balance'] = tb['balance']
  275. total_amount[acc_id][prt_id]['initial_balance'] = 0.0
  276. total_amount[acc_id][prt_id]['ending_balance'] = tb[
  277. 'balance']
  278. if foreign_currency:
  279. total_amount[acc_id][prt_id][
  280. 'initial_currency_balance'] = 0.0
  281. total_amount[acc_id][prt_id][
  282. 'ending_currency_balance'] = tb['amount_currency']
  283. partners_ids.add(tb['partner_id'])
  284. for tb in tb_initial_prt:
  285. acc_id = tb['account_id'][0]
  286. if tb['partner_id']:
  287. prt_id = tb['partner_id'][0]
  288. if tb['partner_id'] not in partners_ids:
  289. partners_data.update({
  290. prt_id: {
  291. 'id': prt_id,
  292. 'name': tb['partner_id'][1],
  293. }
  294. })
  295. if acc_id not in total_amount.keys():
  296. total_amount[acc_id][prt_id] = {}
  297. total_amount[acc_id][prt_id]['credit'] = 0.0
  298. total_amount[acc_id][prt_id]['debit'] = 0.0
  299. total_amount[acc_id][prt_id]['balance'] = 0.0
  300. total_amount[acc_id][prt_id]['initial_balance'] = tb[
  301. 'balance']
  302. total_amount[acc_id][prt_id]['ending_balance'] = tb[
  303. 'balance']
  304. if foreign_currency:
  305. total_amount[acc_id][prt_id][
  306. 'initial_currency_balance'] = tb[
  307. 'amount_currency']
  308. total_amount[acc_id][prt_id][
  309. 'ending_currency_balance'] = tb[
  310. 'amount_currency']
  311. partners_ids.add(tb['partner_id'])
  312. elif prt_id not in total_amount[acc_id].keys():
  313. total_amount[acc_id][prt_id] = {}
  314. total_amount[acc_id][prt_id]['credit'] = 0.0
  315. total_amount[acc_id][prt_id]['debit'] = 0.0
  316. total_amount[acc_id][prt_id]['balance'] = 0.0
  317. total_amount[acc_id][prt_id]['initial_balance'] = tb[
  318. 'balance']
  319. total_amount[acc_id][prt_id]['ending_balance'] = tb[
  320. 'balance']
  321. if foreign_currency:
  322. total_amount[acc_id][prt_id][
  323. 'initial_currency_balance'] = tb[
  324. 'amount_currency']
  325. total_amount[acc_id][prt_id][
  326. 'ending_currency_balance'] = tb[
  327. 'amount_currency']
  328. partners_ids.add(tb['partner_id'])
  329. else:
  330. total_amount[acc_id][prt_id]['initial_balance'] += \
  331. tb['balance']
  332. total_amount[acc_id][prt_id]['ending_balance'] += tb[
  333. 'balance']
  334. if foreign_currency:
  335. total_amount[acc_id][prt_id][
  336. 'initial_currency_balance'] += tb[
  337. 'amount_currency']
  338. total_amount[acc_id][prt_id][
  339. 'ending_currency_balance'] += tb[
  340. 'amount_currency']
  341. accounts_ids = list(total_amount.keys())
  342. unaffected_id = unaffected_earnings_account
  343. if unaffected_id not in accounts_ids:
  344. accounts_ids.append(unaffected_id)
  345. total_amount[unaffected_id] = {}
  346. total_amount[unaffected_id]['initial_balance'] = 0.0
  347. total_amount[unaffected_id]['balance'] = 0.0
  348. total_amount[unaffected_id]['credit'] = 0.0
  349. total_amount[unaffected_id]['debit'] = 0.0
  350. total_amount[unaffected_id]['ending_balance'] = 0.0
  351. if foreign_currency:
  352. total_amount[unaffected_id]['initial_currency_balance'] =\
  353. 0.0
  354. total_amount[unaffected_id]['ending_currency_balance'] = 0.0
  355. accounts_data = self._get_accounts_data(accounts_ids)
  356. pl_initial_balance, pl_initial_currency_balance = \
  357. self._get_pl_initial_balance(
  358. account_ids, journal_ids, partner_ids, company_id,
  359. fy_start_date, only_posted_moves, show_partner_details,
  360. foreign_currency)
  361. total_amount[unaffected_id]['ending_balance'] += pl_initial_balance
  362. total_amount[unaffected_id]['initial_balance'] += pl_initial_balance
  363. if foreign_currency:
  364. total_amount[unaffected_id]['ending_currency_balance'] += \
  365. pl_initial_currency_balance
  366. total_amount[unaffected_id]['initial_currency_balance'] += \
  367. pl_initial_currency_balance
  368. return total_amount, accounts_data, partners_data
  369. def _get_hierarchy_groups(self, group_ids, groups_data, old_groups_ids,
  370. foreign_currency):
  371. new_parents = False
  372. for group_id in group_ids:
  373. if groups_data[group_id]['parent_id']:
  374. new_parents = True
  375. nw_id = groups_data[group_id]['parent_id']
  376. if nw_id in groups_data.keys():
  377. groups_data[nw_id]['initial_balance'] += groups_data[
  378. group_id]['initial_balance']
  379. groups_data[nw_id]['debit'] += groups_data[group_id][
  380. 'debit']
  381. groups_data[nw_id]['credit'] += groups_data[group_id][
  382. 'credit']
  383. groups_data[nw_id]['balance'] += groups_data[group_id][
  384. 'balance']
  385. groups_data[nw_id]['ending_balance'] += groups_data[
  386. group_id]['ending_balance']
  387. if foreign_currency:
  388. groups_data[nw_id]['initial_currency_balance'] += \
  389. groups_data[group_id]['initial_currency_balance']
  390. groups_data[nw_id]['ending_currency_balance'] += \
  391. groups_data[group_id]['ending_currency_balance']
  392. else:
  393. groups_data[nw_id] = {}
  394. groups_data[nw_id]['initial_balance'] = groups_data[
  395. group_id]['initial_balance']
  396. groups_data[nw_id]['debit'] = groups_data[group_id]['debit']
  397. groups_data[nw_id]['credit'] = groups_data[group_id][
  398. 'credit']
  399. groups_data[nw_id]['balance'] = groups_data[group_id][
  400. 'balance']
  401. groups_data[nw_id]['ending_balance'] = groups_data[
  402. group_id]['ending_balance']
  403. if foreign_currency:
  404. groups_data[nw_id]['initial_currency_balance'] = \
  405. groups_data[group_id]['initial_currency_balance']
  406. groups_data[nw_id]['ending_currency_balance'] = \
  407. groups_data[group_id]['ending_currency_balance']
  408. if new_parents:
  409. nw_groups_ids = []
  410. for group_id in list(groups_data.keys()):
  411. if group_id not in old_groups_ids:
  412. nw_groups_ids.append(group_id)
  413. old_groups_ids.append(group_id)
  414. groups = self.env['account.group'].browse(nw_groups_ids)
  415. for group in groups:
  416. groups_data[group.id].update({
  417. 'id': group.id,
  418. 'code': group.code_prefix,
  419. 'name': group.name,
  420. 'parent_id': group.parent_id.id,
  421. 'parent_path': group.parent_path,
  422. 'complete_code': group.complete_code,
  423. 'account_ids': group.compute_account_ids.ids,
  424. 'type': 'group_type'})
  425. groups_data = self._get_hierarchy_groups(
  426. nw_groups_ids, groups_data, old_groups_ids, foreign_currency
  427. )
  428. return groups_data
  429. def _get_groups_data(self, accounts_data, total_amount, foreign_currency):
  430. accounts_ids = list(accounts_data.keys())
  431. accounts = self.env['account.account'].browse(accounts_ids)
  432. account_group_relation = {}
  433. for account in accounts:
  434. accounts_data[account.id]['complete_code'] = \
  435. account.group_id.complete_code if account.group_id.id else ""
  436. if account.group_id.id:
  437. if account.group_id.id not in account_group_relation.keys():
  438. account_group_relation.update({account.group_id.id: [
  439. account.id]})
  440. else:
  441. account_group_relation[account.group_id.id].append(
  442. account.id)
  443. groups = self.env['account.group'].browse(account_group_relation.keys())
  444. groups_data = {}
  445. for group in groups:
  446. groups_data.update({group.id: {
  447. 'id': group.id,
  448. 'code': group.code_prefix,
  449. 'name': group.name,
  450. 'parent_id': group.parent_id.id,
  451. 'parent_path': group.parent_path,
  452. 'type': 'group_type',
  453. 'complete_code': group.complete_code,
  454. 'account_ids': group.compute_account_ids.ids,
  455. 'initial_balance': 0.0,
  456. 'credit': 0.0,
  457. 'debit': 0.0,
  458. 'balance': 0.0,
  459. 'ending_balance': 0.0}
  460. })
  461. if foreign_currency:
  462. groups_data[group.id]['initial_currency_balance'] = 0.0
  463. groups_data[group.id]['ending_currency_balance'] = 0.0
  464. for group_id in account_group_relation.keys():
  465. for account_id in account_group_relation[group_id]:
  466. groups_data[group_id]['initial_balance'] += total_amount[
  467. account_id]['initial_balance']
  468. groups_data[group_id]['debit'] += total_amount[
  469. account_id]['debit']
  470. groups_data[group_id]['credit'] += total_amount[
  471. account_id]['credit']
  472. groups_data[group_id]['balance'] += total_amount[
  473. account_id]['balance']
  474. groups_data[group_id]['ending_balance'] += total_amount[
  475. account_id]['ending_balance']
  476. if foreign_currency:
  477. groups_data[group_id]['initial_currency_balance'] += \
  478. total_amount[account_id]['initial_currency_balance']
  479. groups_data[group_id]['ending_currency_balance'] += \
  480. total_amount[account_id]['ending_currency_balance']
  481. group_ids = list(groups_data.keys())
  482. old_group_ids = list(groups_data.keys())
  483. groups_data = self._get_hierarchy_groups(
  484. group_ids, groups_data, old_group_ids, foreign_currency
  485. )
  486. return groups_data
  487. def _get_computed_groups_data(self, accounts_data, total_amount,
  488. foreign_currency):
  489. groups = self.env['account.group'].search([('id', '!=', False)])
  490. groups_data = {}
  491. for group in groups:
  492. len_group_code = len(group.code_prefix)
  493. groups_data.update({group.id: {'id': group.id,
  494. 'code': group.code_prefix,
  495. 'name': group.name,
  496. 'parent_id': group.parent_id.id,
  497. 'parent_path': group.parent_path,
  498. 'type': 'group_type',
  499. 'complete_code': group.complete_code,
  500. 'account_ids':
  501. group.compute_account_ids.ids,
  502. 'initial_balance': 0.0,
  503. 'credit': 0.0,
  504. 'debit': 0.0,
  505. 'balance': 0.0,
  506. 'ending_balance': 0.0}})
  507. if foreign_currency:
  508. groups_data[group.id]['initial_currency_balance'] = 0.0
  509. groups_data[group.id]['ending_currency_balance'] = 0.0
  510. for account in accounts_data.values():
  511. if group.code_prefix == account['code'][:len_group_code]:
  512. acc_id = account['id']
  513. group_id = group.id
  514. groups_data[group_id]['initial_balance'] += total_amount[
  515. acc_id]['initial_balance']
  516. groups_data[group_id]['debit'] += total_amount[
  517. acc_id]['debit']
  518. groups_data[group_id]['credit'] += total_amount[
  519. acc_id]['credit']
  520. groups_data[group_id]['balance'] += total_amount[
  521. acc_id]['balance']
  522. groups_data[group_id]['ending_balance'] += total_amount[
  523. acc_id]['ending_balance']
  524. if foreign_currency:
  525. groups_data[group_id]['initial_currency_balance'] += \
  526. total_amount[acc_id]['initial_currency_balance']
  527. groups_data[group_id]['ending_currency_balance'] += \
  528. total_amount[acc_id]['ending_currency_balance']
  529. return groups_data
  530. @api.multi
  531. def _get_report_values(self, docids, data):
  532. show_partner_details = data['show_partner_details']
  533. wizard_id = data['wizard_id']
  534. company = self.env['res.company'].browse(data['company_id'])
  535. company_id = data['company_id']
  536. partner_ids = data['partner_ids']
  537. journal_ids = data['journal_ids']
  538. account_ids = data['account_ids']
  539. date_to = data['date_to']
  540. date_from = data['date_from']
  541. hide_account_at_0 = data['hide_account_at_0']
  542. hierarchy_on = data['hierarchy_on']
  543. show_hierarchy_level = data['show_hierarchy_level']
  544. foreign_currency = data['foreign_currency']
  545. only_posted_moves = data['only_posted_moves']
  546. unaffected_earnings_account = data['unaffected_earnings_account']
  547. fy_start_date = data['fy_start_date']
  548. total_amount, accounts_data, partners_data = self._get_data(
  549. account_ids, journal_ids, partner_ids, company_id, date_to,
  550. date_from, foreign_currency, only_posted_moves,
  551. show_partner_details, hide_account_at_0,
  552. unaffected_earnings_account, fy_start_date)
  553. trial_balance = []
  554. if not show_partner_details:
  555. for account_id in accounts_data.keys():
  556. accounts_data[account_id].update({
  557. 'initial_balance': total_amount[account_id][
  558. 'initial_balance'],
  559. 'credit': total_amount[account_id]['credit'],
  560. 'debit': total_amount[account_id]['debit'],
  561. 'balance': total_amount[account_id]['balance'],
  562. 'ending_balance': total_amount[account_id][
  563. 'ending_balance'],
  564. 'type': 'account_type',
  565. })
  566. if foreign_currency:
  567. accounts_data[account_id].update({
  568. 'ending_currency_balance': total_amount[account_id][
  569. 'ending_currency_balance'],
  570. 'initial_currency_balance': total_amount[account_id][
  571. 'initial_currency_balance']
  572. })
  573. if hierarchy_on == 'relation':
  574. groups_data = self._get_groups_data(
  575. accounts_data, total_amount, foreign_currency)
  576. trial_balance = list(groups_data.values())
  577. trial_balance += list(accounts_data.values())
  578. trial_balance = sorted(trial_balance, key=lambda k: k[
  579. 'complete_code'])
  580. for trial in trial_balance:
  581. counter = trial['complete_code'].count('/')
  582. trial['level'] = counter
  583. if hierarchy_on == 'computed':
  584. groups_data = self._get_computed_groups_data(
  585. accounts_data, total_amount, foreign_currency)
  586. trial_balance = list(groups_data.values())
  587. trial_balance += list(accounts_data.values())
  588. trial_balance = sorted(trial_balance, key=lambda k: k['code'])
  589. if hierarchy_on == 'none':
  590. trial_balance = list(accounts_data.values())
  591. trial_balance = sorted(trial_balance, key=lambda k: k['code'])
  592. else:
  593. if foreign_currency:
  594. for account_id in accounts_data.keys():
  595. total_amount[account_id]['currency_id'] = accounts_data[
  596. account_id]['currency_id']
  597. total_amount[account_id]['currency_name'] = accounts_data[
  598. account_id]['currency_name']
  599. return {
  600. 'doc_ids': [wizard_id],
  601. 'doc_model': 'trial.balance.report.wizard',
  602. 'docs': self.env['trial.balance.report.wizard'].browse(wizard_id),
  603. 'foreign_currency': data['foreign_currency'],
  604. 'company_name': company.display_name,
  605. 'company_currency': company.currency_id,
  606. 'currency_name': company.currency_id.name,
  607. 'date_from': data['date_from'],
  608. 'date_to': data['date_to'],
  609. 'only_posted_moves': data['only_posted_moves'],
  610. 'hide_account_at_0': data['hide_account_at_0'],
  611. 'show_partner_details': data['show_partner_details'],
  612. 'limit_hierarchy_level': data['limit_hierarchy_level'],
  613. 'hierarchy_on': hierarchy_on,
  614. 'trial_balance': trial_balance,
  615. 'total_amount': total_amount,
  616. 'accounts_data': accounts_data,
  617. 'partners_data': partners_data,
  618. 'show_hierarchy_level': show_hierarchy_level,
  619. }