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.

772 lines
32 KiB

  1. # © 2016 Julien Coux (Camptocamp)
  2. # © 2018 Forest and Biomass Romania SA
  3. # Copyright 2020 ForgeFlow S.L. (https://www.forgeflow.com)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import 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. accounts = self.env["account.account"].search(accounts_domain)
  336. tb_initial_acc = []
  337. for account in accounts:
  338. tb_initial_acc.append(
  339. {"account_id": account.id, "balance": 0.0, "amount_currency": 0.0}
  340. )
  341. initial_domain_bs = self._get_initial_balances_bs_ml_domain(
  342. account_ids,
  343. journal_ids,
  344. partner_ids,
  345. company_id,
  346. date_from,
  347. only_posted_moves,
  348. show_partner_details,
  349. )
  350. tb_initial_acc_bs = self.env["account.move.line"].read_group(
  351. domain=initial_domain_bs,
  352. fields=["account_id", "balance", "amount_currency"],
  353. groupby=["account_id"],
  354. )
  355. initial_domain_pl = self._get_initial_balances_pl_ml_domain(
  356. account_ids,
  357. journal_ids,
  358. partner_ids,
  359. company_id,
  360. date_from,
  361. only_posted_moves,
  362. show_partner_details,
  363. fy_start_date,
  364. )
  365. tb_initial_acc_pl = self.env["account.move.line"].read_group(
  366. domain=initial_domain_pl,
  367. fields=["account_id", "balance", "amount_currency"],
  368. groupby=["account_id"],
  369. )
  370. tb_initial_acc_rg = tb_initial_acc_bs + tb_initial_acc_pl
  371. for account_rg in tb_initial_acc_rg:
  372. element = list(
  373. filter(
  374. lambda acc_dict: acc_dict["account_id"]
  375. == account_rg["account_id"][0],
  376. tb_initial_acc,
  377. )
  378. )
  379. if element:
  380. element[0]["balance"] += account_rg["balance"]
  381. element[0]["amount_currency"] += account_rg["amount_currency"]
  382. if hide_account_at_0:
  383. tb_initial_acc = [p for p in tb_initial_acc if p["balance"] != 0]
  384. period_domain = self._get_period_ml_domain(
  385. account_ids,
  386. journal_ids,
  387. partner_ids,
  388. company_id,
  389. date_to,
  390. date_from,
  391. only_posted_moves,
  392. show_partner_details,
  393. )
  394. tb_period_acc = self.env["account.move.line"].read_group(
  395. domain=period_domain,
  396. fields=["account_id", "debit", "credit", "balance", "amount_currency"],
  397. groupby=["account_id"],
  398. )
  399. if show_partner_details:
  400. tb_initial_prt_bs = self.env["account.move.line"].read_group(
  401. domain=initial_domain_bs,
  402. fields=["account_id", "partner_id", "balance", "amount_currency"],
  403. groupby=["account_id", "partner_id"],
  404. lazy=False,
  405. )
  406. tb_initial_prt_pl = self.env["account.move.line"].read_group(
  407. domain=initial_domain_pl,
  408. fields=["account_id", "partner_id", "balance", "amount_currency"],
  409. groupby=["account_id", "partner_id"],
  410. )
  411. tb_initial_prt = tb_initial_prt_bs + tb_initial_prt_pl
  412. if hide_account_at_0:
  413. tb_initial_prt = [p for p in tb_initial_prt if p["balance"] != 0]
  414. tb_period_prt = self.env["account.move.line"].read_group(
  415. domain=period_domain,
  416. fields=[
  417. "account_id",
  418. "partner_id",
  419. "debit",
  420. "credit",
  421. "balance",
  422. "amount_currency",
  423. ],
  424. groupby=["account_id", "partner_id"],
  425. lazy=False,
  426. )
  427. total_amount = {}
  428. partners_data = []
  429. total_amount = self._compute_account_amount(
  430. total_amount, tb_initial_acc, tb_period_acc, foreign_currency
  431. )
  432. if show_partner_details:
  433. total_amount, partners_data = self._compute_partner_amount(
  434. total_amount, tb_initial_prt, tb_period_prt, foreign_currency
  435. )
  436. accounts_ids = list(total_amount.keys())
  437. unaffected_id = unaffected_earnings_account
  438. if unaffected_id not in accounts_ids:
  439. accounts_ids.append(unaffected_id)
  440. total_amount[unaffected_id] = {}
  441. total_amount[unaffected_id]["initial_balance"] = 0.0
  442. total_amount[unaffected_id]["balance"] = 0.0
  443. total_amount[unaffected_id]["credit"] = 0.0
  444. total_amount[unaffected_id]["debit"] = 0.0
  445. total_amount[unaffected_id]["ending_balance"] = 0.0
  446. if foreign_currency:
  447. total_amount[unaffected_id]["initial_currency_balance"] = 0.0
  448. total_amount[unaffected_id]["ending_currency_balance"] = 0.0
  449. accounts_data = self._get_accounts_data(accounts_ids)
  450. pl_initial_balance, pl_initial_currency_balance = self._get_pl_initial_balance(
  451. account_ids,
  452. journal_ids,
  453. partner_ids,
  454. company_id,
  455. fy_start_date,
  456. only_posted_moves,
  457. show_partner_details,
  458. foreign_currency,
  459. )
  460. total_amount[unaffected_id]["ending_balance"] += pl_initial_balance
  461. total_amount[unaffected_id]["initial_balance"] += pl_initial_balance
  462. if foreign_currency:
  463. total_amount[unaffected_id][
  464. "ending_currency_balance"
  465. ] += pl_initial_currency_balance
  466. total_amount[unaffected_id][
  467. "initial_currency_balance"
  468. ] += pl_initial_currency_balance
  469. return total_amount, accounts_data, partners_data
  470. def _get_hierarchy_groups(
  471. self, group_ids, groups_data, old_groups_ids, foreign_currency
  472. ):
  473. new_parents = False
  474. for group_id in group_ids:
  475. if groups_data[group_id]["parent_id"]:
  476. new_parents = True
  477. nw_id = groups_data[group_id]["parent_id"]
  478. if nw_id in groups_data.keys():
  479. groups_data[nw_id]["initial_balance"] += groups_data[group_id][
  480. "initial_balance"
  481. ]
  482. groups_data[nw_id]["debit"] += groups_data[group_id]["debit"]
  483. groups_data[nw_id]["credit"] += groups_data[group_id]["credit"]
  484. groups_data[nw_id]["balance"] += groups_data[group_id]["balance"]
  485. groups_data[nw_id]["ending_balance"] += groups_data[group_id][
  486. "ending_balance"
  487. ]
  488. if foreign_currency:
  489. groups_data[nw_id]["initial_currency_balance"] += groups_data[
  490. group_id
  491. ]["initial_currency_balance"]
  492. groups_data[nw_id]["ending_currency_balance"] += groups_data[
  493. group_id
  494. ]["ending_currency_balance"]
  495. else:
  496. groups_data[nw_id] = {}
  497. groups_data[nw_id]["initial_balance"] = groups_data[group_id][
  498. "initial_balance"
  499. ]
  500. groups_data[nw_id]["debit"] = groups_data[group_id]["debit"]
  501. groups_data[nw_id]["credit"] = groups_data[group_id]["credit"]
  502. groups_data[nw_id]["balance"] = groups_data[group_id]["balance"]
  503. groups_data[nw_id]["ending_balance"] = groups_data[group_id][
  504. "ending_balance"
  505. ]
  506. if foreign_currency:
  507. groups_data[nw_id]["initial_currency_balance"] = groups_data[
  508. group_id
  509. ]["initial_currency_balance"]
  510. groups_data[nw_id]["ending_currency_balance"] = groups_data[
  511. group_id
  512. ]["ending_currency_balance"]
  513. if new_parents:
  514. nw_groups_ids = []
  515. for group_id in list(groups_data.keys()):
  516. if group_id not in old_groups_ids:
  517. nw_groups_ids.append(group_id)
  518. old_groups_ids.append(group_id)
  519. groups = self.env["account.group"].browse(nw_groups_ids)
  520. for group in groups:
  521. groups_data[group.id].update(
  522. {
  523. "id": group.id,
  524. "code": group.code_prefix,
  525. "name": group.name,
  526. "parent_id": group.parent_id.id,
  527. "parent_path": group.parent_path,
  528. "complete_code": group.complete_code,
  529. "account_ids": group.compute_account_ids.ids,
  530. "type": "group_type",
  531. }
  532. )
  533. groups_data = self._get_hierarchy_groups(
  534. nw_groups_ids, groups_data, old_groups_ids, foreign_currency
  535. )
  536. return groups_data
  537. def _get_groups_data(self, accounts_data, total_amount, foreign_currency):
  538. accounts_ids = list(accounts_data.keys())
  539. accounts = self.env["account.account"].browse(accounts_ids)
  540. account_group_relation = {}
  541. for account in accounts:
  542. accounts_data[account.id]["complete_code"] = (
  543. account.group_id.complete_code if account.group_id.id else ""
  544. )
  545. if account.group_id.id:
  546. if account.group_id.id not in account_group_relation.keys():
  547. account_group_relation.update({account.group_id.id: [account.id]})
  548. else:
  549. account_group_relation[account.group_id.id].append(account.id)
  550. groups = self.env["account.group"].browse(account_group_relation.keys())
  551. groups_data = {}
  552. for group in groups:
  553. groups_data.update(
  554. {
  555. group.id: {
  556. "id": group.id,
  557. "code": group.code_prefix,
  558. "name": group.name,
  559. "parent_id": group.parent_id.id,
  560. "parent_path": group.parent_path,
  561. "type": "group_type",
  562. "complete_code": group.complete_code,
  563. "account_ids": group.compute_account_ids.ids,
  564. "initial_balance": 0.0,
  565. "credit": 0.0,
  566. "debit": 0.0,
  567. "balance": 0.0,
  568. "ending_balance": 0.0,
  569. }
  570. }
  571. )
  572. if foreign_currency:
  573. groups_data[group.id]["initial_currency_balance"] = 0.0
  574. groups_data[group.id]["ending_currency_balance"] = 0.0
  575. for group_id in account_group_relation.keys():
  576. for account_id in account_group_relation[group_id]:
  577. groups_data[group_id]["initial_balance"] += total_amount[account_id][
  578. "initial_balance"
  579. ]
  580. groups_data[group_id]["debit"] += total_amount[account_id]["debit"]
  581. groups_data[group_id]["credit"] += total_amount[account_id]["credit"]
  582. groups_data[group_id]["balance"] += total_amount[account_id]["balance"]
  583. groups_data[group_id]["ending_balance"] += total_amount[account_id][
  584. "ending_balance"
  585. ]
  586. if foreign_currency:
  587. groups_data[group_id]["initial_currency_balance"] += total_amount[
  588. account_id
  589. ]["initial_currency_balance"]
  590. groups_data[group_id]["ending_currency_balance"] += total_amount[
  591. account_id
  592. ]["ending_currency_balance"]
  593. group_ids = list(groups_data.keys())
  594. old_group_ids = list(groups_data.keys())
  595. groups_data = self._get_hierarchy_groups(
  596. group_ids, groups_data, old_group_ids, foreign_currency
  597. )
  598. return groups_data
  599. def _get_computed_groups_data(self, accounts_data, total_amount, foreign_currency):
  600. groups = self.env["account.group"].search([("id", "!=", False)])
  601. groups_data = {}
  602. for group in groups:
  603. len_group_code = len(group.code_prefix)
  604. groups_data.update(
  605. {
  606. group.id: {
  607. "id": group.id,
  608. "code": group.code_prefix,
  609. "name": group.name,
  610. "parent_id": group.parent_id.id,
  611. "parent_path": group.parent_path,
  612. "type": "group_type",
  613. "complete_code": group.complete_code,
  614. "account_ids": group.compute_account_ids.ids,
  615. "initial_balance": 0.0,
  616. "credit": 0.0,
  617. "debit": 0.0,
  618. "balance": 0.0,
  619. "ending_balance": 0.0,
  620. }
  621. }
  622. )
  623. if foreign_currency:
  624. groups_data[group.id]["initial_currency_balance"] = 0.0
  625. groups_data[group.id]["ending_currency_balance"] = 0.0
  626. for account in accounts_data.values():
  627. if group.code_prefix == account["code"][:len_group_code]:
  628. acc_id = account["id"]
  629. group_id = group.id
  630. groups_data[group_id]["initial_balance"] += total_amount[acc_id][
  631. "initial_balance"
  632. ]
  633. groups_data[group_id]["debit"] += total_amount[acc_id]["debit"]
  634. groups_data[group_id]["credit"] += total_amount[acc_id]["credit"]
  635. groups_data[group_id]["balance"] += total_amount[acc_id]["balance"]
  636. groups_data[group_id]["ending_balance"] += total_amount[acc_id][
  637. "ending_balance"
  638. ]
  639. if foreign_currency:
  640. groups_data[group_id][
  641. "initial_currency_balance"
  642. ] += total_amount[acc_id]["initial_currency_balance"]
  643. groups_data[group_id][
  644. "ending_currency_balance"
  645. ] += total_amount[acc_id]["ending_currency_balance"]
  646. return groups_data
  647. def _get_report_values(self, docids, data):
  648. show_partner_details = data["show_partner_details"]
  649. wizard_id = data["wizard_id"]
  650. company = self.env["res.company"].browse(data["company_id"])
  651. company_id = data["company_id"]
  652. partner_ids = data["partner_ids"]
  653. journal_ids = data["journal_ids"]
  654. account_ids = data["account_ids"]
  655. date_to = data["date_to"]
  656. date_from = data["date_from"]
  657. hide_account_at_0 = data["hide_account_at_0"]
  658. hierarchy_on = data["hierarchy_on"]
  659. show_hierarchy_level = data["show_hierarchy_level"]
  660. foreign_currency = data["foreign_currency"]
  661. only_posted_moves = data["only_posted_moves"]
  662. unaffected_earnings_account = data["unaffected_earnings_account"]
  663. fy_start_date = data["fy_start_date"]
  664. total_amount, accounts_data, partners_data = self._get_data(
  665. account_ids,
  666. journal_ids,
  667. partner_ids,
  668. company_id,
  669. date_to,
  670. date_from,
  671. foreign_currency,
  672. only_posted_moves,
  673. show_partner_details,
  674. hide_account_at_0,
  675. unaffected_earnings_account,
  676. fy_start_date,
  677. )
  678. trial_balance = []
  679. if not show_partner_details:
  680. for account_id in accounts_data.keys():
  681. accounts_data[account_id].update(
  682. {
  683. "initial_balance": total_amount[account_id]["initial_balance"],
  684. "credit": total_amount[account_id]["credit"],
  685. "debit": total_amount[account_id]["debit"],
  686. "balance": total_amount[account_id]["balance"],
  687. "ending_balance": total_amount[account_id]["ending_balance"],
  688. "type": "account_type",
  689. }
  690. )
  691. if foreign_currency:
  692. accounts_data[account_id].update(
  693. {
  694. "ending_currency_balance": total_amount[account_id][
  695. "ending_currency_balance"
  696. ],
  697. "initial_currency_balance": total_amount[account_id][
  698. "initial_currency_balance"
  699. ],
  700. }
  701. )
  702. if hierarchy_on == "relation":
  703. groups_data = self._get_groups_data(
  704. accounts_data, total_amount, foreign_currency
  705. )
  706. trial_balance = list(groups_data.values())
  707. trial_balance += list(accounts_data.values())
  708. trial_balance = sorted(trial_balance, key=lambda k: k["complete_code"])
  709. for trial in trial_balance:
  710. counter = trial["complete_code"].count("/")
  711. trial["level"] = counter
  712. if hierarchy_on == "computed":
  713. groups_data = self._get_computed_groups_data(
  714. accounts_data, total_amount, foreign_currency
  715. )
  716. trial_balance = list(groups_data.values())
  717. trial_balance += list(accounts_data.values())
  718. trial_balance = sorted(trial_balance, key=lambda k: k["code"])
  719. if hierarchy_on == "none":
  720. trial_balance = list(accounts_data.values())
  721. trial_balance = sorted(trial_balance, key=lambda k: k["code"])
  722. else:
  723. if foreign_currency:
  724. for account_id in accounts_data.keys():
  725. total_amount[account_id]["currency_id"] = accounts_data[account_id][
  726. "currency_id"
  727. ]
  728. total_amount[account_id]["currency_name"] = accounts_data[
  729. account_id
  730. ]["currency_name"]
  731. return {
  732. "doc_ids": [wizard_id],
  733. "doc_model": "trial.balance.report.wizard",
  734. "docs": self.env["trial.balance.report.wizard"].browse(wizard_id),
  735. "foreign_currency": data["foreign_currency"],
  736. "company_name": company.display_name,
  737. "company_currency": company.currency_id,
  738. "currency_name": company.currency_id.name,
  739. "date_from": data["date_from"],
  740. "date_to": data["date_to"],
  741. "only_posted_moves": data["only_posted_moves"],
  742. "hide_account_at_0": data["hide_account_at_0"],
  743. "show_partner_details": data["show_partner_details"],
  744. "limit_hierarchy_level": data["limit_hierarchy_level"],
  745. "hierarchy_on": hierarchy_on,
  746. "trial_balance": trial_balance,
  747. "total_amount": total_amount,
  748. "accounts_data": accounts_data,
  749. "partners_data": partners_data,
  750. "show_hierarchy_level": show_hierarchy_level,
  751. }