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.

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