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.

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