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.

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