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.

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