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.

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