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.

837 lines
32 KiB

  1. # Author: Julien Coux
  2. # Copyright 2016 Camptocamp 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.tests import common
  6. class TestTrialBalanceReport(common.TransactionCase):
  7. def setUp(self):
  8. super(TestTrialBalanceReport, self).setUp()
  9. group_obj = self.env["account.group"]
  10. acc_obj = self.env["account.account"]
  11. self.group1 = group_obj.create({"code_prefix": "1", "name": "Group 1"})
  12. self.group11 = group_obj.create(
  13. {"code_prefix": "11", "name": "Group 11", "parent_id": self.group1.id}
  14. )
  15. self.group2 = group_obj.create({"code_prefix": "2", "name": "Group 2"})
  16. self.account100 = acc_obj.create(
  17. {
  18. "code": "100",
  19. "name": "Account 100",
  20. "group_id": self.group1.id,
  21. "user_type_id": self.env.ref("account.data_account_type_receivable").id,
  22. "reconcile": True,
  23. }
  24. )
  25. self.account110 = self.env["account.account"].search(
  26. [
  27. (
  28. "user_type_id",
  29. "=",
  30. self.env.ref("account.data_unaffected_earnings").id,
  31. )
  32. ],
  33. limit=1,
  34. )
  35. self.account200 = acc_obj.create(
  36. {
  37. "code": "200",
  38. "name": "Account 200",
  39. "group_id": self.group2.id,
  40. "user_type_id": self.env.ref(
  41. "account.data_account_type_other_income"
  42. ).id,
  43. }
  44. )
  45. self.account300 = acc_obj.create(
  46. {
  47. "code": "300",
  48. "name": "Account 300",
  49. "user_type_id": self.env.ref(
  50. "account.data_account_type_other_income"
  51. ).id,
  52. }
  53. )
  54. self.account301 = acc_obj.create(
  55. {
  56. "code": "301",
  57. "name": "Account 301",
  58. "group_id": self.group2.id,
  59. "user_type_id": self.env.ref(
  60. "account.data_account_type_other_income"
  61. ).id,
  62. }
  63. )
  64. self.previous_fy_date_start = "2015-01-01"
  65. self.previous_fy_date_end = "2015-12-31"
  66. self.fy_date_start = "2016-01-01"
  67. self.fy_date_end = "2016-12-31"
  68. self.date_start = "2016-01-01"
  69. self.date_end = "2016-12-31"
  70. self.partner = self.env.ref("base.res_partner_12")
  71. self.unaffected_account = self.env["account.account"].search(
  72. [
  73. (
  74. "user_type_id",
  75. "=",
  76. self.env.ref("account.data_unaffected_earnings").id,
  77. )
  78. ],
  79. limit=1,
  80. )
  81. def _add_move(
  82. self,
  83. date,
  84. receivable_debit,
  85. receivable_credit,
  86. income_debit,
  87. income_credit,
  88. unaffected_debit=0,
  89. unaffected_credit=0,
  90. ):
  91. journal = self.env["account.journal"].search([], limit=1)
  92. partner = self.env.ref("base.res_partner_12")
  93. move_vals = {
  94. "journal_id": journal.id,
  95. "date": date,
  96. "line_ids": [
  97. (
  98. 0,
  99. 0,
  100. {
  101. "debit": receivable_debit,
  102. "credit": receivable_credit,
  103. "partner_id": partner.id,
  104. "account_id": self.account100.id,
  105. },
  106. ),
  107. (
  108. 0,
  109. 0,
  110. {
  111. "debit": income_debit,
  112. "credit": income_credit,
  113. "partner_id": partner.id,
  114. "account_id": self.account200.id,
  115. },
  116. ),
  117. (
  118. 0,
  119. 0,
  120. {
  121. "debit": unaffected_debit,
  122. "credit": unaffected_credit,
  123. "partner_id": partner.id,
  124. "account_id": self.account110.id,
  125. },
  126. ),
  127. (
  128. 0,
  129. 0,
  130. {
  131. "debit": receivable_debit,
  132. "credit": receivable_credit,
  133. "partner_id": partner.id,
  134. "account_id": self.account300.id,
  135. },
  136. ),
  137. (
  138. 0,
  139. 0,
  140. {
  141. "debit": receivable_credit,
  142. "credit": receivable_debit,
  143. "partner_id": partner.id,
  144. "account_id": self.account301.id,
  145. },
  146. ),
  147. ],
  148. }
  149. move = self.env["account.move"].create(move_vals)
  150. move.post()
  151. def _get_report_lines(
  152. self, with_partners=False, account_ids=False, hierarchy_on="computed"
  153. ):
  154. company = self.env.ref("base.main_company")
  155. trial_balance = self.env["trial.balance.report.wizard"].create(
  156. {
  157. "date_from": self.date_start,
  158. "date_to": self.date_end,
  159. "target_move": "posted",
  160. "hide_account_at_0": True,
  161. "hierarchy_on": hierarchy_on,
  162. "company_id": company.id,
  163. "account_ids": account_ids,
  164. "fy_start_date": self.fy_date_start,
  165. "show_partner_details": with_partners,
  166. }
  167. )
  168. data = trial_balance._prepare_report_trial_balance()
  169. res_data = self.env[
  170. "report.account_financial_report.trial_balance"
  171. ]._get_report_values(trial_balance, data)
  172. return res_data
  173. def check_account_in_report(self, account_id, trial_balance):
  174. account_in_report = False
  175. for account in trial_balance:
  176. if account["id"] == account_id and account["type"] == "account_type":
  177. account_in_report = True
  178. break
  179. return account_in_report
  180. def _get_account_lines(self, account_id, trial_balance):
  181. lines = False
  182. for account in trial_balance:
  183. if account["id"] == account_id and account["type"] == "account_type":
  184. lines = {
  185. "initial_balance": account["initial_balance"],
  186. "debit": account["debit"],
  187. "credit": account["credit"],
  188. "final_balance": account["ending_balance"],
  189. }
  190. return lines
  191. def _get_group_lines(self, group_id, trial_balance):
  192. lines = False
  193. for group in trial_balance:
  194. if group["id"] == group_id and group["type"] == "group_type":
  195. lines = {
  196. "initial_balance": group["initial_balance"],
  197. "debit": group["debit"],
  198. "credit": group["credit"],
  199. "final_balance": group["ending_balance"],
  200. }
  201. return lines
  202. def check_partner_in_report(self, account_id, partner_id, total_amount):
  203. partner_in_report = False
  204. if account_id in total_amount.keys():
  205. if partner_id in total_amount[account_id]:
  206. partner_in_report = True
  207. return partner_in_report
  208. def _get_partner_lines(self, account_id, partner_id, total_amount):
  209. acc_id = account_id
  210. prt_id = partner_id
  211. lines = {
  212. "initial_balance": total_amount[acc_id][prt_id]["initial_balance"],
  213. "debit": total_amount[acc_id][prt_id]["debit"],
  214. "credit": total_amount[acc_id][prt_id]["credit"],
  215. "final_balance": total_amount[acc_id][prt_id]["ending_balance"],
  216. }
  217. return lines
  218. def _sum_all_accounts(self, trial_balance, feature):
  219. total = 0.0
  220. for account in trial_balance:
  221. if account["type"] == "account_type":
  222. for key in account.keys():
  223. if key == feature:
  224. total += account[key]
  225. return total
  226. def test_00_account_group(self):
  227. self.assertTrue(self.account100 in self.group1.compute_account_ids)
  228. self.assertTrue(self.account200 in self.group2.compute_account_ids)
  229. def test_01_account_balance_computed(self):
  230. # Change code of the P&L for not being automatically included
  231. # in group 1 balances
  232. earning_accs = self.env["account.account"].search(
  233. [("user_type_id", "=", self.env.ref("account.data_unaffected_earnings").id)]
  234. )
  235. for acc in earning_accs:
  236. acc.code = "999" + acc.code
  237. # Generate the general ledger line
  238. res_data = self._get_report_lines()
  239. trial_balance = res_data["trial_balance"]
  240. check_receivable_account = self.check_account_in_report(
  241. self.account100.id, trial_balance
  242. )
  243. self.assertFalse(check_receivable_account)
  244. check_income_account = self.check_account_in_report(
  245. self.account200.id, trial_balance
  246. )
  247. self.assertFalse(check_income_account)
  248. self.assertTrue(
  249. self.check_account_in_report(self.unaffected_account.id, trial_balance)
  250. )
  251. # Add a move at the previous day of the first day of fiscal year
  252. # to check the initial balance
  253. self._add_move(
  254. date=self.previous_fy_date_end,
  255. receivable_debit=1000,
  256. receivable_credit=0,
  257. income_debit=0,
  258. income_credit=1000,
  259. )
  260. # Re Generate the trial balance line
  261. res_data = self._get_report_lines()
  262. trial_balance = res_data["trial_balance"]
  263. check_receivable_account = self.check_account_in_report(
  264. self.account100.id, trial_balance
  265. )
  266. self.assertTrue(check_receivable_account)
  267. check_income_account = self.check_account_in_report(
  268. self.account200.id, trial_balance
  269. )
  270. self.assertFalse(check_income_account)
  271. # Check the initial and final balance
  272. account_receivable_lines = self._get_account_lines(
  273. self.account100.id, trial_balance
  274. )
  275. group1_lines = self._get_group_lines(self.group1.id, trial_balance)
  276. self.assertEqual(account_receivable_lines["initial_balance"], 1000)
  277. self.assertEqual(account_receivable_lines["debit"], 0)
  278. self.assertEqual(account_receivable_lines["credit"], 0)
  279. self.assertEqual(account_receivable_lines["final_balance"], 1000)
  280. self.assertEqual(group1_lines["initial_balance"], 1000)
  281. self.assertEqual(group1_lines["debit"], 0)
  282. self.assertEqual(group1_lines["credit"], 0)
  283. self.assertEqual(group1_lines["final_balance"], 1000)
  284. # Add reversed move of the initial move the first day of fiscal year
  285. # to check the first day of fiscal year is not used
  286. # to compute the initial balance
  287. self._add_move(
  288. date=self.fy_date_start,
  289. receivable_debit=0,
  290. receivable_credit=1000,
  291. income_debit=1000,
  292. income_credit=0,
  293. )
  294. # Re Generate the trial balance line
  295. res_data = self._get_report_lines()
  296. trial_balance = res_data["trial_balance"]
  297. check_receivable_account = self.check_account_in_report(
  298. self.account100.id, trial_balance
  299. )
  300. self.assertTrue(check_receivable_account)
  301. check_income_account = self.check_account_in_report(
  302. self.account200.id, trial_balance
  303. )
  304. self.assertTrue(check_income_account)
  305. # Re Generate the trial balance line with an account filter
  306. res_data = self._get_report_lines(
  307. account_ids=(self.account100 + self.account200).ids
  308. )
  309. trial_balance = res_data["trial_balance"]
  310. self.assertTrue(self.check_account_in_report(self.account100.id, trial_balance))
  311. self.assertTrue(self.check_account_in_report(self.account200.id, trial_balance))
  312. # Unaffected account should not be present
  313. self.assertFalse(
  314. self.check_account_in_report(self.unaffected_account.id, trial_balance)
  315. )
  316. # Check the initial and final balance
  317. account_receivable_lines = self._get_account_lines(
  318. self.account100.id, trial_balance
  319. )
  320. account_income_lines = self._get_account_lines(
  321. self.account200.id, trial_balance
  322. )
  323. group1_lines = self._get_group_lines(self.group1.id, trial_balance)
  324. group2_lines = self._get_group_lines(self.group2.id, trial_balance)
  325. self.assertEqual(account_receivable_lines["initial_balance"], 1000)
  326. self.assertEqual(account_receivable_lines["debit"], 0)
  327. self.assertEqual(account_receivable_lines["credit"], 1000)
  328. self.assertEqual(account_receivable_lines["final_balance"], 0)
  329. self.assertEqual(account_income_lines["initial_balance"], 0)
  330. self.assertEqual(account_income_lines["debit"], 1000)
  331. self.assertEqual(account_income_lines["credit"], 0)
  332. self.assertEqual(account_income_lines["final_balance"], 1000)
  333. self.assertEqual(group1_lines["initial_balance"], 1000)
  334. self.assertEqual(group1_lines["debit"], 0)
  335. self.assertEqual(group1_lines["credit"], 1000)
  336. self.assertEqual(group1_lines["final_balance"], 0)
  337. self.assertEqual(group2_lines["initial_balance"], 0)
  338. self.assertEqual(group2_lines["debit"], 1000)
  339. self.assertEqual(group2_lines["credit"], 0)
  340. self.assertEqual(group2_lines["final_balance"], 1000)
  341. # Add another move at the end day of fiscal year
  342. # to check that it correctly used on report
  343. self._add_move(
  344. date=self.fy_date_end,
  345. receivable_debit=0,
  346. receivable_credit=1000,
  347. income_debit=1000,
  348. income_credit=0,
  349. )
  350. # Re Generate the trial balance line
  351. res_data = self._get_report_lines()
  352. trial_balance = res_data["trial_balance"]
  353. check_receivable_account = self.check_account_in_report(
  354. self.account100.id, trial_balance
  355. )
  356. self.assertTrue(check_receivable_account)
  357. check_income_account = self.check_account_in_report(
  358. self.account200.id, trial_balance
  359. )
  360. self.assertTrue(check_income_account)
  361. # Check the initial and final balance
  362. account_receivable_lines = self._get_account_lines(
  363. self.account100.id, trial_balance
  364. )
  365. account_income_lines = self._get_account_lines(
  366. self.account200.id, trial_balance
  367. )
  368. group1_lines = self._get_group_lines(self.group1.id, trial_balance)
  369. group2_lines = self._get_group_lines(self.group2.id, trial_balance)
  370. self.assertEqual(account_receivable_lines["initial_balance"], 1000)
  371. self.assertEqual(account_receivable_lines["debit"], 0)
  372. self.assertEqual(account_receivable_lines["credit"], 2000)
  373. self.assertEqual(account_receivable_lines["final_balance"], -1000)
  374. self.assertEqual(account_income_lines["initial_balance"], 0)
  375. self.assertEqual(account_income_lines["debit"], 2000)
  376. self.assertEqual(account_income_lines["credit"], 0)
  377. self.assertEqual(account_income_lines["final_balance"], 2000)
  378. self.assertEqual(group1_lines["initial_balance"], 1000)
  379. self.assertEqual(group1_lines["debit"], 0)
  380. self.assertEqual(group1_lines["credit"], 2000)
  381. self.assertEqual(group1_lines["final_balance"], -1000)
  382. self.assertEqual(group2_lines["initial_balance"], 0)
  383. self.assertEqual(group2_lines["debit"], 2000)
  384. self.assertEqual(group2_lines["credit"], 0)
  385. self.assertEqual(group2_lines["final_balance"], 2000)
  386. def test_02_account_balance_hierarchy(self):
  387. # Generate the general ledger line
  388. res_data = self._get_report_lines(hierarchy_on="relation")
  389. trial_balance = res_data["trial_balance"]
  390. check_receivable_account = self.check_account_in_report(
  391. self.account100.id, trial_balance
  392. )
  393. self.assertFalse(check_receivable_account)
  394. check_income_account = self.check_account_in_report(
  395. self.account200.id, trial_balance
  396. )
  397. self.assertFalse(check_income_account)
  398. # Add a move at the previous day of the first day of fiscal year
  399. # to check the initial balance
  400. self._add_move(
  401. date=self.previous_fy_date_end,
  402. receivable_debit=1000,
  403. receivable_credit=0,
  404. income_debit=0,
  405. income_credit=1000,
  406. )
  407. # Re Generate the trial balance line
  408. res_data = self._get_report_lines(hierarchy_on="relation")
  409. trial_balance = res_data["trial_balance"]
  410. check_receivable_account = self.check_account_in_report(
  411. self.account100.id, trial_balance
  412. )
  413. self.assertTrue(check_receivable_account)
  414. check_income_account = self.check_account_in_report(
  415. self.account200.id, trial_balance
  416. )
  417. self.assertFalse(check_income_account)
  418. # Check the initial and final balance
  419. account_receivable_lines = self._get_account_lines(
  420. self.account100.id, trial_balance
  421. )
  422. group1_lines = self._get_group_lines(self.group1.id, trial_balance)
  423. self.assertEqual(account_receivable_lines["initial_balance"], 1000)
  424. self.assertEqual(account_receivable_lines["debit"], 0)
  425. self.assertEqual(account_receivable_lines["credit"], 0)
  426. self.assertEqual(account_receivable_lines["final_balance"], 1000)
  427. self.assertEqual(group1_lines["initial_balance"], 1000)
  428. self.assertEqual(group1_lines["debit"], 0)
  429. self.assertEqual(group1_lines["credit"], 0)
  430. self.assertEqual(group1_lines["final_balance"], 1000)
  431. # Add reversale move of the initial move the first day of fiscal year
  432. # to check the first day of fiscal year is not used
  433. # to compute the initial balance
  434. self._add_move(
  435. date=self.fy_date_start,
  436. receivable_debit=0,
  437. receivable_credit=1000,
  438. income_debit=1000,
  439. income_credit=0,
  440. )
  441. # Re Generate the trial balance line
  442. res_data = self._get_report_lines(hierarchy_on="relation")
  443. trial_balance = res_data["trial_balance"]
  444. check_receivable_account = self.check_account_in_report(
  445. self.account100.id, trial_balance
  446. )
  447. self.assertTrue(check_receivable_account)
  448. check_income_account = self.check_account_in_report(
  449. self.account200.id, trial_balance
  450. )
  451. self.assertTrue(check_income_account)
  452. # Check the initial and final balance
  453. account_receivable_lines = self._get_account_lines(
  454. self.account100.id, trial_balance
  455. )
  456. account_income_lines = self._get_account_lines(
  457. self.account200.id, trial_balance
  458. )
  459. group1_lines = self._get_group_lines(self.group1.id, trial_balance)
  460. group2_lines = self._get_group_lines(self.group2.id, trial_balance)
  461. self.assertEqual(account_receivable_lines["initial_balance"], 1000)
  462. self.assertEqual(account_receivable_lines["debit"], 0)
  463. self.assertEqual(account_receivable_lines["credit"], 1000)
  464. self.assertEqual(account_receivable_lines["final_balance"], 0)
  465. self.assertEqual(account_income_lines["initial_balance"], 0)
  466. self.assertEqual(account_income_lines["debit"], 1000)
  467. self.assertEqual(account_income_lines["credit"], 0)
  468. self.assertEqual(account_income_lines["final_balance"], 1000)
  469. self.assertEqual(group1_lines["initial_balance"], 1000)
  470. self.assertEqual(group1_lines["debit"], 0)
  471. self.assertEqual(group1_lines["credit"], 1000)
  472. self.assertEqual(group1_lines["final_balance"], 0)
  473. self.assertEqual(group2_lines["initial_balance"], 0)
  474. self.assertEqual(group2_lines["debit"], 2000)
  475. self.assertEqual(group2_lines["credit"], 0)
  476. self.assertEqual(group2_lines["final_balance"], 2000)
  477. # Add another move at the end day of fiscal year
  478. # to check that it correctly used on report
  479. self._add_move(
  480. date=self.fy_date_end,
  481. receivable_debit=0,
  482. receivable_credit=1000,
  483. income_debit=1000,
  484. income_credit=0,
  485. )
  486. # Re Generate the trial balance line
  487. res_data = self._get_report_lines(hierarchy_on="relation")
  488. trial_balance = res_data["trial_balance"]
  489. check_receivable_account = self.check_account_in_report(
  490. self.account100.id, trial_balance
  491. )
  492. self.assertTrue(check_receivable_account)
  493. check_income_account = self.check_account_in_report(
  494. self.account200.id, trial_balance
  495. )
  496. self.assertTrue(check_income_account)
  497. # Check the initial and final balance
  498. account_receivable_lines = self._get_account_lines(
  499. self.account100.id, trial_balance
  500. )
  501. account_income_lines = self._get_account_lines(
  502. self.account200.id, trial_balance
  503. )
  504. group1_lines = self._get_group_lines(self.group1.id, trial_balance)
  505. group2_lines = self._get_group_lines(self.group2.id, trial_balance)
  506. self.assertEqual(account_receivable_lines["initial_balance"], 1000)
  507. self.assertEqual(account_receivable_lines["debit"], 0)
  508. self.assertEqual(account_receivable_lines["credit"], 2000)
  509. self.assertEqual(account_receivable_lines["final_balance"], -1000)
  510. self.assertEqual(account_income_lines["initial_balance"], 0)
  511. self.assertEqual(account_income_lines["debit"], 2000)
  512. self.assertEqual(account_income_lines["credit"], 0)
  513. self.assertEqual(account_income_lines["final_balance"], 2000)
  514. self.assertEqual(group1_lines["initial_balance"], 1000)
  515. self.assertEqual(group1_lines["debit"], 0)
  516. self.assertEqual(group1_lines["credit"], 2000)
  517. self.assertEqual(group1_lines["final_balance"], -1000)
  518. self.assertEqual(group2_lines["initial_balance"], 0)
  519. self.assertEqual(group2_lines["debit"], 4000)
  520. self.assertEqual(group2_lines["credit"], 0)
  521. self.assertEqual(group2_lines["final_balance"], 4000)
  522. def test_03_partner_balance(self):
  523. # Generate the trial balance line
  524. res_data = self._get_report_lines(with_partners=True)
  525. total_amount = res_data["total_amount"]
  526. check_partner_receivable = self.check_partner_in_report(
  527. self.account100.id, self.partner.id, total_amount
  528. )
  529. self.assertFalse(check_partner_receivable)
  530. # Add a move at the previous day of the first day of fiscal year
  531. # to check the initial balance
  532. self._add_move(
  533. date=self.previous_fy_date_end,
  534. receivable_debit=1000,
  535. receivable_credit=0,
  536. income_debit=0,
  537. income_credit=1000,
  538. )
  539. # Re Generate the trial balance line
  540. res_data = self._get_report_lines(with_partners=True)
  541. total_amount = res_data["total_amount"]
  542. check_partner_receivable = self.check_partner_in_report(
  543. self.account100.id, self.partner.id, total_amount
  544. )
  545. self.assertTrue(check_partner_receivable)
  546. # Check the initial and final balance
  547. partner_lines = self._get_partner_lines(
  548. self.account100.id, self.partner.id, total_amount
  549. )
  550. self.assertEqual(partner_lines["initial_balance"], 1000)
  551. self.assertEqual(partner_lines["debit"], 0)
  552. self.assertEqual(partner_lines["credit"], 0)
  553. self.assertEqual(partner_lines["final_balance"], 1000)
  554. # Add reversale move of the initial move the first day of fiscal year
  555. # to check the first day of fiscal year is not used
  556. # to compute the initial balance
  557. self._add_move(
  558. date=self.fy_date_start,
  559. receivable_debit=0,
  560. receivable_credit=1000,
  561. income_debit=1000,
  562. income_credit=0,
  563. )
  564. # Re Generate the trial balance line
  565. res_data = self._get_report_lines(with_partners=True)
  566. total_amount = res_data["total_amount"]
  567. check_partner_receivable = self.check_partner_in_report(
  568. self.account100.id, self.partner.id, total_amount
  569. )
  570. self.assertTrue(check_partner_receivable)
  571. # Check the initial and final balance
  572. partner_lines = self._get_partner_lines(
  573. self.account100.id, self.partner.id, total_amount
  574. )
  575. self.assertEqual(partner_lines["initial_balance"], 1000)
  576. self.assertEqual(partner_lines["debit"], 0)
  577. self.assertEqual(partner_lines["credit"], 1000)
  578. self.assertEqual(partner_lines["final_balance"], 0)
  579. # Add another move at the end day of fiscal year
  580. # to check that it correctly used on report
  581. self._add_move(
  582. date=self.fy_date_end,
  583. receivable_debit=0,
  584. receivable_credit=1000,
  585. income_debit=1000,
  586. income_credit=0,
  587. )
  588. # Re Generate the trial balance line
  589. res_data = self._get_report_lines(with_partners=True)
  590. total_amount = res_data["total_amount"]
  591. check_partner_receivable = self.check_partner_in_report(
  592. self.account100.id, self.partner.id, total_amount
  593. )
  594. self.assertTrue(check_partner_receivable)
  595. # Check the initial and final balance
  596. partner_lines = self._get_partner_lines(
  597. self.account100.id, self.partner.id, total_amount
  598. )
  599. self.assertEqual(partner_lines["initial_balance"], 1000)
  600. self.assertEqual(partner_lines["debit"], 0)
  601. self.assertEqual(partner_lines["credit"], 2000)
  602. self.assertEqual(partner_lines["final_balance"], -1000)
  603. def test_04_undistributed_pl(self):
  604. # Add a P&L Move in the previous FY
  605. journal = self.env["account.journal"].search([], limit=1)
  606. move_vals = {
  607. "journal_id": journal.id,
  608. "date": self.previous_fy_date_end,
  609. "line_ids": [
  610. (
  611. 0,
  612. 0,
  613. {"debit": 0.0, "credit": 1000.0, "account_id": self.account300.id},
  614. ),
  615. (
  616. 0,
  617. 0,
  618. {"debit": 1000.0, "credit": 0.0, "account_id": self.account100.id},
  619. ),
  620. ],
  621. }
  622. move = self.env["account.move"].create(move_vals)
  623. move.post()
  624. # Generate the trial balance line
  625. company = self.env.ref("base.main_company")
  626. trial_balance = self.env["trial.balance.report.wizard"].create(
  627. {
  628. "date_from": self.date_start,
  629. "date_to": self.date_end,
  630. "target_move": "posted",
  631. "hide_account_at_0": False,
  632. "hierarchy_on": "none",
  633. "company_id": company.id,
  634. "fy_start_date": self.fy_date_start,
  635. }
  636. )
  637. data = trial_balance._prepare_report_trial_balance()
  638. res_data = self.env[
  639. "report.account_financial_report.trial_balance"
  640. ]._get_report_values(trial_balance, data)
  641. trial_balance = res_data["trial_balance"]
  642. check_unaffected_account = self.check_account_in_report(
  643. self.unaffected_account.id, trial_balance
  644. )
  645. self.assertTrue(check_unaffected_account)
  646. unaffected_lines = self._get_account_lines(
  647. self.unaffected_account.id, trial_balance
  648. )
  649. self.assertEqual(unaffected_lines["initial_balance"], -1000)
  650. self.assertEqual(unaffected_lines["debit"], 0)
  651. self.assertEqual(unaffected_lines["credit"], 0)
  652. self.assertEqual(unaffected_lines["final_balance"], -1000)
  653. # Add a P&L Move to the current FY
  654. journal = self.env["account.journal"].search([], limit=1)
  655. move_vals = {
  656. "journal_id": journal.id,
  657. "date": self.date_start,
  658. "line_ids": [
  659. (
  660. 0,
  661. 0,
  662. {"debit": 0.0, "credit": 1000.0, "account_id": self.account300.id},
  663. ),
  664. (
  665. 0,
  666. 0,
  667. {"debit": 1000.0, "credit": 0.0, "account_id": self.account100.id},
  668. ),
  669. ],
  670. }
  671. move = self.env["account.move"].create(move_vals)
  672. move.post()
  673. # Re Generate the trial balance line
  674. trial_balance = self.env["trial.balance.report.wizard"].create(
  675. {
  676. "date_from": self.date_start,
  677. "date_to": self.date_end,
  678. "target_move": "posted",
  679. "hide_account_at_0": False,
  680. "hierarchy_on": "none",
  681. "company_id": company.id,
  682. "fy_start_date": self.fy_date_start,
  683. }
  684. )
  685. data = trial_balance._prepare_report_trial_balance()
  686. res_data = self.env[
  687. "report.account_financial_report.trial_balance"
  688. ]._get_report_values(trial_balance, data)
  689. trial_balance = res_data["trial_balance"]
  690. # The unaffected earnings account is not affected by a journal entry
  691. # made to the P&L in the current fiscal year.
  692. check_unaffected_account = self.check_account_in_report(
  693. self.unaffected_account.id, trial_balance
  694. )
  695. self.assertTrue(check_unaffected_account)
  696. unaffected_lines = self._get_account_lines(
  697. self.unaffected_account.id, trial_balance
  698. )
  699. self.assertEqual(unaffected_lines["initial_balance"], -1000)
  700. self.assertEqual(unaffected_lines["debit"], 0)
  701. self.assertEqual(unaffected_lines["credit"], 0)
  702. self.assertEqual(unaffected_lines["final_balance"], -1000)
  703. # Add a Move including Unaffected Earnings to the current FY
  704. journal = self.env["account.journal"].search([], limit=1)
  705. move_vals = {
  706. "journal_id": journal.id,
  707. "date": self.date_start,
  708. "line_ids": [
  709. (
  710. 0,
  711. 0,
  712. {"debit": 0.0, "credit": 1000.0, "account_id": self.account110.id},
  713. ),
  714. (
  715. 0,
  716. 0,
  717. {"debit": 1000.0, "credit": 0.0, "account_id": self.account100.id},
  718. ),
  719. ],
  720. }
  721. move = self.env["account.move"].create(move_vals)
  722. move.post()
  723. # Re Generate the trial balance line
  724. trial_balance = self.env["trial.balance.report.wizard"].create(
  725. {
  726. "date_from": self.date_start,
  727. "date_to": self.date_end,
  728. "target_move": "posted",
  729. "hide_account_at_0": False,
  730. "hierarchy_on": "none",
  731. "company_id": company.id,
  732. "fy_start_date": self.fy_date_start,
  733. }
  734. )
  735. data = trial_balance._prepare_report_trial_balance()
  736. res_data = self.env[
  737. "report.account_financial_report.trial_balance"
  738. ]._get_report_values(trial_balance, data)
  739. trial_balance = res_data["trial_balance"]
  740. # The unaffected earnings account affected by a journal entry
  741. # made to the unaffected earnings in the current fiscal year.
  742. check_unaffected_account = self.check_account_in_report(
  743. self.unaffected_account.id, trial_balance
  744. )
  745. self.assertTrue(check_unaffected_account)
  746. unaffected_lines = self._get_account_lines(
  747. self.unaffected_account.id, trial_balance
  748. )
  749. self.assertEqual(unaffected_lines["initial_balance"], -1000)
  750. self.assertEqual(unaffected_lines["debit"], 0)
  751. self.assertEqual(unaffected_lines["credit"], 1000)
  752. self.assertEqual(unaffected_lines["final_balance"], -2000)
  753. # The totals for the Trial Balance are zero
  754. total_initial_balance = self._sum_all_accounts(trial_balance, "initial_balance")
  755. total_final_balance = self._sum_all_accounts(trial_balance, "ending_balance")
  756. total_debit = self._sum_all_accounts(trial_balance, "debit")
  757. total_credit = self._sum_all_accounts(trial_balance, "credit")
  758. self.assertEqual(total_initial_balance, 0)
  759. self.assertEqual(total_final_balance, 0)
  760. self.assertEqual(total_debit, total_credit)