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.

656 lines
32 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 += round(initial_balance[
  147. 'amount_currency'], 2)
  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. accounts_domain = [('company_id', '=', company_id)]
  154. if account_ids:
  155. accounts_domain += [('id', 'in', account_ids)]
  156. accounts = self.env['account.account'].search(accounts_domain)
  157. tb_initial_acc = []
  158. for account in accounts:
  159. tb_initial_acc.append({
  160. 'account_id': account.id,
  161. 'balance': 0.0,
  162. 'amount_currency': 0.0,
  163. })
  164. initial_domain_bs = self._get_initial_balances_bs_ml_domain(
  165. account_ids, journal_ids, partner_ids,
  166. company_id, date_from, only_posted_moves, show_partner_details)
  167. tb_initial_acc_bs = self.env['account.move.line'].read_group(
  168. domain=initial_domain_bs,
  169. fields=[
  170. 'account_id',
  171. 'balance',
  172. 'amount_currency'],
  173. groupby=['account_id'])
  174. initial_domain_pl = self._get_initial_balances_pl_ml_domain(
  175. account_ids, journal_ids, partner_ids,
  176. company_id, date_from, only_posted_moves, show_partner_details,
  177. fy_start_date)
  178. tb_initial_acc_pl = self.env['account.move.line'].read_group(
  179. domain=initial_domain_pl,
  180. fields=[
  181. 'account_id',
  182. 'balance',
  183. 'amount_currency'],
  184. groupby=['account_id'])
  185. tb_initial_acc_rg = tb_initial_acc_bs + tb_initial_acc_pl
  186. for account_rg in tb_initial_acc_rg:
  187. element = list(filter(
  188. lambda acc_dict: acc_dict['account_id'] == account_rg[
  189. 'account_id'][0], tb_initial_acc))
  190. if element:
  191. element[0]['balance'] += account_rg['balance']
  192. element[0]['amount_currency'] += account_rg['amount_currency']
  193. if hide_account_at_0:
  194. tb_initial_acc = [p for p in tb_initial_acc if p['balance'] != 0]
  195. period_domain = self._get_period_ml_domain(
  196. account_ids, journal_ids, partner_ids, company_id, date_to,
  197. date_from, only_posted_moves, show_partner_details)
  198. tb_period_acc = self.env['account.move.line'].read_group(
  199. domain=period_domain,
  200. fields=[
  201. 'account_id',
  202. 'debit',
  203. 'credit',
  204. 'balance',
  205. 'amount_currency'],
  206. groupby=['account_id'])
  207. if show_partner_details:
  208. tb_initial_prt_bs = self.env['account.move.line'].read_group(
  209. domain=initial_domain_bs,
  210. fields=[
  211. 'account_id',
  212. 'partner_id',
  213. 'balance',
  214. 'amount_currency'],
  215. groupby=['account_id', 'partner_id'], lazy=False)
  216. tb_initial_prt_pl = self.env['account.move.line'].read_group(
  217. domain=initial_domain_pl,
  218. fields=[
  219. 'account_id',
  220. 'partner_id',
  221. 'balance',
  222. 'amount_currency'],
  223. groupby=['account_id', 'partner_id'])
  224. tb_initial_prt = tb_initial_prt_bs + tb_initial_prt_pl
  225. if hide_account_at_0:
  226. tb_initial_prt = \
  227. [p for p in tb_initial_prt if p['balance'] != 0]
  228. tb_period_prt = self.env['account.move.line'].read_group(
  229. domain=period_domain,
  230. fields=[
  231. 'account_id',
  232. 'partner_id',
  233. 'debit',
  234. 'credit',
  235. 'balance',
  236. 'amount_currency'],
  237. groupby=['account_id', 'partner_id'], lazy=False)
  238. total_amount = {}
  239. partners_data = []
  240. for tb in tb_period_acc:
  241. acc_id = tb['account_id'][0]
  242. total_amount[acc_id] = {}
  243. total_amount[acc_id]['credit'] = tb['credit']
  244. total_amount[acc_id]['debit'] = tb['debit']
  245. total_amount[acc_id]['balance'] = tb['balance']
  246. total_amount[acc_id]['initial_balance'] = 0.0
  247. total_amount[acc_id]['ending_balance'] = tb['balance']
  248. if foreign_currency:
  249. total_amount[acc_id]['initial_currency_balance'] =\
  250. 0.0
  251. total_amount[acc_id]['ending_currency_balance'] =\
  252. round(tb['amount_currency'], 2)
  253. for tb in tb_initial_acc:
  254. acc_id = tb['account_id']
  255. if acc_id not in total_amount.keys():
  256. total_amount[acc_id] = {}
  257. total_amount[acc_id]['credit'] = 0.0
  258. total_amount[acc_id]['debit'] = 0.0
  259. total_amount[acc_id]['balance'] = 0.0
  260. total_amount[acc_id]['initial_balance'] = tb['balance']
  261. total_amount[acc_id]['ending_balance'] = tb['balance']
  262. if foreign_currency:
  263. total_amount[acc_id]['initial_currency_balance'] = round(
  264. tb['amount_currency'], 2)
  265. total_amount[acc_id]['ending_currency_balance'] = round(
  266. tb['amount_currency'], 2)
  267. else:
  268. total_amount[acc_id]['initial_balance'] = tb['balance']
  269. total_amount[acc_id]['ending_balance'] += tb['balance']
  270. if foreign_currency:
  271. total_amount[acc_id]['initial_currency_balance'] = round(
  272. tb['amount_currency'], 2)
  273. total_amount[acc_id]['ending_currency_balance'] += round(
  274. tb['amount_currency'], 2)
  275. if show_partner_details:
  276. partners_ids = set()
  277. partners_data = {}
  278. for tb in tb_period_prt:
  279. acc_id = tb['account_id'][0]
  280. if tb['partner_id']:
  281. prt_id = tb['partner_id'][0]
  282. if tb['partner_id'] not in partners_ids:
  283. partners_data.update({
  284. prt_id: {
  285. 'id': prt_id,
  286. 'name': tb['partner_id'][1],
  287. }
  288. })
  289. total_amount[acc_id][prt_id] = {}
  290. total_amount[acc_id][prt_id]['credit'] = tb['credit']
  291. total_amount[acc_id][prt_id]['debit'] = tb['debit']
  292. total_amount[acc_id][prt_id]['balance'] = tb['balance']
  293. total_amount[acc_id][prt_id]['initial_balance'] = 0.0
  294. total_amount[acc_id][prt_id]['ending_balance'] = tb[
  295. 'balance']
  296. if foreign_currency:
  297. total_amount[acc_id][prt_id][
  298. 'initial_currency_balance'] = 0.0
  299. total_amount[acc_id][prt_id][
  300. 'ending_currency_balance'] = round(
  301. tb['amount_currency'], 2)
  302. partners_ids.add(tb['partner_id'])
  303. for tb in tb_initial_prt:
  304. acc_id = tb['account_id'][0]
  305. if tb['partner_id']:
  306. prt_id = tb['partner_id'][0]
  307. if tb['partner_id'] not in partners_ids:
  308. partners_data.update({
  309. prt_id: {
  310. 'id': prt_id,
  311. 'name': tb['partner_id'][1],
  312. }
  313. })
  314. if acc_id not in total_amount.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'] = round(
  326. tb['amount_currency'], 2)
  327. total_amount[acc_id][prt_id][
  328. 'ending_currency_balance'] = round(
  329. tb['amount_currency'], 2)
  330. partners_ids.add(tb['partner_id'])
  331. elif prt_id not in total_amount[acc_id].keys():
  332. total_amount[acc_id][prt_id] = {}
  333. total_amount[acc_id][prt_id]['credit'] = 0.0
  334. total_amount[acc_id][prt_id]['debit'] = 0.0
  335. total_amount[acc_id][prt_id]['balance'] = 0.0
  336. total_amount[acc_id][prt_id]['initial_balance'] = tb[
  337. 'balance']
  338. total_amount[acc_id][prt_id]['ending_balance'] = tb[
  339. 'balance']
  340. if foreign_currency:
  341. total_amount[acc_id][prt_id][
  342. 'initial_currency_balance'] = round(
  343. tb['amount_currency'], 2)
  344. total_amount[acc_id][prt_id][
  345. 'ending_currency_balance'] = round(
  346. tb['amount_currency'], 2)
  347. partners_ids.add(tb['partner_id'])
  348. else:
  349. total_amount[acc_id][prt_id]['initial_balance'] += \
  350. tb['balance']
  351. total_amount[acc_id][prt_id]['ending_balance'] += tb[
  352. 'balance']
  353. if foreign_currency:
  354. total_amount[acc_id][prt_id][
  355. 'initial_currency_balance'] += round(
  356. tb['amount_currency'], 2)
  357. total_amount[acc_id][prt_id][
  358. 'ending_currency_balance'] += round(
  359. tb['amount_currency'], 2)
  360. accounts_ids = list(total_amount.keys())
  361. unaffected_id = unaffected_earnings_account
  362. if unaffected_id not in accounts_ids:
  363. accounts_ids.append(unaffected_id)
  364. total_amount[unaffected_id] = {}
  365. total_amount[unaffected_id]['initial_balance'] = 0.0
  366. total_amount[unaffected_id]['balance'] = 0.0
  367. total_amount[unaffected_id]['credit'] = 0.0
  368. total_amount[unaffected_id]['debit'] = 0.0
  369. total_amount[unaffected_id]['ending_balance'] = 0.0
  370. if foreign_currency:
  371. total_amount[unaffected_id]['initial_currency_balance'] =\
  372. 0.0
  373. total_amount[unaffected_id]['ending_currency_balance'] = 0.0
  374. accounts_data = self._get_accounts_data(accounts_ids)
  375. pl_initial_balance, pl_initial_currency_balance = \
  376. self._get_pl_initial_balance(
  377. account_ids, journal_ids, partner_ids, company_id,
  378. fy_start_date, only_posted_moves, show_partner_details,
  379. foreign_currency)
  380. total_amount[unaffected_id]['ending_balance'] += pl_initial_balance
  381. total_amount[unaffected_id]['initial_balance'] += pl_initial_balance
  382. if foreign_currency:
  383. total_amount[unaffected_id]['ending_currency_balance'] += \
  384. pl_initial_currency_balance
  385. total_amount[unaffected_id]['initial_currency_balance'] += \
  386. pl_initial_currency_balance
  387. return total_amount, accounts_data, partners_data
  388. def _get_hierarchy_groups(self, group_ids, groups_data, old_groups_ids,
  389. foreign_currency):
  390. new_parents = False
  391. for group_id in group_ids:
  392. if groups_data[group_id]['parent_id']:
  393. new_parents = True
  394. nw_id = groups_data[group_id]['parent_id']
  395. if nw_id in groups_data.keys():
  396. groups_data[nw_id]['initial_balance'] += groups_data[
  397. group_id]['initial_balance']
  398. groups_data[nw_id]['debit'] += groups_data[group_id][
  399. 'debit']
  400. groups_data[nw_id]['credit'] += groups_data[group_id][
  401. 'credit']
  402. groups_data[nw_id]['balance'] += groups_data[group_id][
  403. 'balance']
  404. groups_data[nw_id]['ending_balance'] += groups_data[
  405. group_id]['ending_balance']
  406. if foreign_currency:
  407. groups_data[nw_id]['initial_currency_balance'] += \
  408. groups_data[group_id]['initial_currency_balance']
  409. groups_data[nw_id]['ending_currency_balance'] += \
  410. groups_data[group_id]['ending_currency_balance']
  411. else:
  412. groups_data[nw_id] = {}
  413. groups_data[nw_id]['initial_balance'] = groups_data[
  414. group_id]['initial_balance']
  415. groups_data[nw_id]['debit'] = groups_data[group_id]['debit']
  416. groups_data[nw_id]['credit'] = groups_data[group_id][
  417. 'credit']
  418. groups_data[nw_id]['balance'] = groups_data[group_id][
  419. 'balance']
  420. groups_data[nw_id]['ending_balance'] = groups_data[
  421. group_id]['ending_balance']
  422. if foreign_currency:
  423. groups_data[nw_id]['initial_currency_balance'] = \
  424. groups_data[group_id]['initial_currency_balance']
  425. groups_data[nw_id]['ending_currency_balance'] = \
  426. groups_data[group_id]['ending_currency_balance']
  427. if new_parents:
  428. nw_groups_ids = []
  429. for group_id in list(groups_data.keys()):
  430. if group_id not in old_groups_ids:
  431. nw_groups_ids.append(group_id)
  432. old_groups_ids.append(group_id)
  433. groups = self.env['account.group'].browse(nw_groups_ids)
  434. for group in groups:
  435. groups_data[group.id].update({
  436. 'id': group.id,
  437. 'code': group.code_prefix,
  438. 'name': group.name,
  439. 'parent_id': group.parent_id.id,
  440. 'parent_path': group.parent_path,
  441. 'complete_code': group.complete_code,
  442. 'account_ids': group.compute_account_ids.ids,
  443. 'type': 'group_type'})
  444. groups_data = self._get_hierarchy_groups(
  445. nw_groups_ids, groups_data, old_groups_ids, foreign_currency
  446. )
  447. return groups_data
  448. def _get_groups_data(self, accounts_data, total_amount, foreign_currency):
  449. accounts_ids = list(accounts_data.keys())
  450. accounts = self.env['account.account'].browse(accounts_ids)
  451. account_group_relation = {}
  452. for account in accounts:
  453. accounts_data[account.id]['complete_code'] = \
  454. account.group_id.complete_code if account.group_id.id else ""
  455. if account.group_id.id:
  456. if account.group_id.id not in account_group_relation.keys():
  457. account_group_relation.update({account.group_id.id: [
  458. account.id]})
  459. else:
  460. account_group_relation[account.group_id.id].append(
  461. account.id)
  462. groups = self.env['account.group'].browse(account_group_relation.keys())
  463. groups_data = {}
  464. for group in groups:
  465. groups_data.update({group.id: {
  466. 'id': group.id,
  467. 'code': group.code_prefix,
  468. 'name': group.name,
  469. 'parent_id': group.parent_id.id,
  470. 'parent_path': group.parent_path,
  471. 'type': 'group_type',
  472. 'complete_code': group.complete_code,
  473. 'account_ids': group.compute_account_ids.ids,
  474. 'initial_balance': 0.0,
  475. 'credit': 0.0,
  476. 'debit': 0.0,
  477. 'balance': 0.0,
  478. 'ending_balance': 0.0}
  479. })
  480. if foreign_currency:
  481. groups_data[group.id]['initial_currency_balance'] = 0.0
  482. groups_data[group.id]['ending_currency_balance'] = 0.0
  483. for group_id in account_group_relation.keys():
  484. for account_id in account_group_relation[group_id]:
  485. groups_data[group_id]['initial_balance'] += total_amount[
  486. account_id]['initial_balance']
  487. groups_data[group_id]['debit'] += total_amount[
  488. account_id]['debit']
  489. groups_data[group_id]['credit'] += total_amount[
  490. account_id]['credit']
  491. groups_data[group_id]['balance'] += total_amount[
  492. account_id]['balance']
  493. groups_data[group_id]['ending_balance'] += total_amount[
  494. account_id]['ending_balance']
  495. if foreign_currency:
  496. groups_data[group_id]['initial_currency_balance'] += \
  497. total_amount[account_id]['initial_currency_balance']
  498. groups_data[group_id]['ending_currency_balance'] += \
  499. total_amount[account_id]['ending_currency_balance']
  500. group_ids = list(groups_data.keys())
  501. old_group_ids = list(groups_data.keys())
  502. groups_data = self._get_hierarchy_groups(
  503. group_ids, groups_data, old_group_ids, foreign_currency
  504. )
  505. return groups_data
  506. def _get_computed_groups_data(self, accounts_data, total_amount,
  507. foreign_currency):
  508. groups = self.env['account.group'].search([('id', '!=', False)])
  509. groups_data = {}
  510. for group in groups:
  511. len_group_code = len(group.code_prefix)
  512. groups_data.update({group.id: {'id': group.id,
  513. 'code': group.code_prefix,
  514. 'name': group.name,
  515. 'parent_id': group.parent_id.id,
  516. 'parent_path': group.parent_path,
  517. 'type': 'group_type',
  518. 'complete_code': group.complete_code,
  519. 'account_ids':
  520. group.compute_account_ids.ids,
  521. 'initial_balance': 0.0,
  522. 'credit': 0.0,
  523. 'debit': 0.0,
  524. 'balance': 0.0,
  525. 'ending_balance': 0.0}})
  526. if foreign_currency:
  527. groups_data[group.id]['initial_currency_balance'] = 0.0
  528. groups_data[group.id]['ending_currency_balance'] = 0.0
  529. for account in accounts_data.values():
  530. if group.code_prefix == account['code'][:len_group_code]:
  531. acc_id = account['id']
  532. group_id = group.id
  533. groups_data[group_id]['initial_balance'] += total_amount[
  534. acc_id]['initial_balance']
  535. groups_data[group_id]['debit'] += total_amount[
  536. acc_id]['debit']
  537. groups_data[group_id]['credit'] += total_amount[
  538. acc_id]['credit']
  539. groups_data[group_id]['balance'] += total_amount[
  540. acc_id]['balance']
  541. groups_data[group_id]['ending_balance'] += total_amount[
  542. acc_id]['ending_balance']
  543. if foreign_currency:
  544. groups_data[group_id]['initial_currency_balance'] += \
  545. total_amount[acc_id]['initial_currency_balance']
  546. groups_data[group_id]['ending_currency_balance'] += \
  547. total_amount[acc_id]['ending_currency_balance']
  548. return groups_data
  549. @api.multi
  550. def _get_report_values(self, docids, data):
  551. show_partner_details = data['show_partner_details']
  552. wizard_id = data['wizard_id']
  553. company = self.env['res.company'].browse(data['company_id'])
  554. company_id = data['company_id']
  555. partner_ids = data['partner_ids']
  556. journal_ids = data['journal_ids']
  557. account_ids = data['account_ids']
  558. date_to = data['date_to']
  559. date_from = data['date_from']
  560. hide_account_at_0 = data['hide_account_at_0']
  561. hierarchy_on = data['hierarchy_on']
  562. show_hierarchy_level = data['show_hierarchy_level']
  563. foreign_currency = data['foreign_currency']
  564. only_posted_moves = data['only_posted_moves']
  565. unaffected_earnings_account = data['unaffected_earnings_account']
  566. fy_start_date = data['fy_start_date']
  567. total_amount, accounts_data, partners_data = self._get_data(
  568. account_ids, journal_ids, partner_ids, company_id, date_to,
  569. date_from, foreign_currency, only_posted_moves,
  570. show_partner_details, hide_account_at_0,
  571. unaffected_earnings_account, fy_start_date)
  572. trial_balance = []
  573. if not show_partner_details:
  574. for account_id in accounts_data.keys():
  575. accounts_data[account_id].update({
  576. 'initial_balance': total_amount[account_id][
  577. 'initial_balance'],
  578. 'credit': total_amount[account_id]['credit'],
  579. 'debit': total_amount[account_id]['debit'],
  580. 'balance': total_amount[account_id]['balance'],
  581. 'ending_balance': total_amount[account_id][
  582. 'ending_balance'],
  583. 'type': 'account_type',
  584. })
  585. if foreign_currency:
  586. accounts_data[account_id].update({
  587. 'ending_currency_balance': total_amount[account_id][
  588. 'ending_currency_balance'],
  589. 'initial_currency_balance': total_amount[account_id][
  590. 'initial_currency_balance']
  591. })
  592. if hierarchy_on == 'relation':
  593. groups_data = self._get_groups_data(
  594. accounts_data, total_amount, foreign_currency)
  595. trial_balance = list(groups_data.values())
  596. trial_balance += list(accounts_data.values())
  597. trial_balance = sorted(trial_balance, key=lambda k: k[
  598. 'complete_code'])
  599. for trial in trial_balance:
  600. counter = trial['complete_code'].count('/')
  601. trial['level'] = counter
  602. if hierarchy_on == 'computed':
  603. groups_data = self._get_computed_groups_data(
  604. accounts_data, total_amount, foreign_currency)
  605. trial_balance = list(groups_data.values())
  606. trial_balance += list(accounts_data.values())
  607. trial_balance = sorted(trial_balance, key=lambda k: k['code'])
  608. if hierarchy_on == 'none':
  609. trial_balance = list(accounts_data.values())
  610. trial_balance = sorted(trial_balance, key=lambda k: k['code'])
  611. else:
  612. if foreign_currency:
  613. for account_id in accounts_data.keys():
  614. total_amount[account_id]['currency_id'] = accounts_data[
  615. account_id]['currency_id']
  616. total_amount[account_id]['currency_name'] = accounts_data[
  617. account_id]['currency_name']
  618. return {
  619. 'doc_ids': [wizard_id],
  620. 'doc_model': 'trial.balance.report.wizard',
  621. 'docs': self.env['trial.balance.report.wizard'].browse(wizard_id),
  622. 'foreign_currency': data['foreign_currency'],
  623. 'company_name': company.display_name,
  624. 'company_currency': company.currency_id,
  625. 'currency_name': company.currency_id.name,
  626. 'date_from': data['date_from'],
  627. 'date_to': data['date_to'],
  628. 'only_posted_moves': data['only_posted_moves'],
  629. 'hide_account_at_0': data['hide_account_at_0'],
  630. 'show_partner_details': data['show_partner_details'],
  631. 'limit_hierarchy_level': data['limit_hierarchy_level'],
  632. 'hierarchy_on': hierarchy_on,
  633. 'trial_balance': trial_balance,
  634. 'total_amount': total_amount,
  635. 'accounts_data': accounts_data,
  636. 'partners_data': partners_data,
  637. 'show_hierarchy_level': show_hierarchy_level,
  638. }