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.

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