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.

494 lines
19 KiB

  1. # Author: Julien Coux
  2. # Copyright 2016 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import time
  5. from odoo.tests import common
  6. from . import abstract_test_foreign_currency as a_t_f_c
  7. class TestTrialBalance(a_t_f_c.AbstractTestForeignCurrency):
  8. """
  9. Technical tests for Trial Balance Report.
  10. """
  11. def _getReportModel(self):
  12. return self.env['report_trial_balance']
  13. def _getQwebReportName(self):
  14. return 'account_financial_report.report_trial_balance_qweb'
  15. def _getXlsxReportName(self):
  16. return 'a_f_r.report_trial_balance_xlsx'
  17. def _getXlsxReportActionName(self):
  18. return 'account_financial_report.action_report_trial_balance_xlsx'
  19. def _getReportTitle(self):
  20. return 'Odoo'
  21. def _getBaseFilters(self):
  22. return {
  23. 'date_from': time.strftime('%Y-01-01'),
  24. 'date_to': time.strftime('%Y-12-31'),
  25. 'company_id': self.company.id,
  26. 'fy_start_date': time.strftime('%Y-01-01'),
  27. 'foreign_currency': True,
  28. 'show_partner_details': True,
  29. }
  30. def _getAdditionalFiltersToBeTested(self):
  31. return [
  32. {'only_posted_moves': True},
  33. {'hide_account_balance_at_0': True},
  34. {'show_partner_details': True},
  35. {'hierarchy_on': 'computed'},
  36. {'hierarchy_on': 'relation'},
  37. {'only_posted_moves': True, 'hide_account_balance_at_0': True,
  38. 'hierarchy_on': 'computed'},
  39. {'only_posted_moves': True, 'hide_account_balance_at_0': True,
  40. 'hierarchy_on': 'relation'},
  41. {'only_posted_moves': True, 'hide_account_balance_at_0': True},
  42. {'only_posted_moves': True, 'show_partner_details': True},
  43. {'hide_account_balance_at_0': True, 'show_partner_details': True},
  44. {
  45. 'only_posted_moves': True,
  46. 'hide_account_balance_at_0': True,
  47. 'show_partner_details': True
  48. },
  49. ]
  50. def _partner_test_is_possible(self, filters):
  51. return 'show_partner_details' in filters
  52. @common.at_install(False)
  53. @common.post_install(True)
  54. class TestTrialBalanceReport(common.TransactionCase):
  55. def setUp(self):
  56. super(TestTrialBalanceReport, self).setUp()
  57. group_obj = self.env['account.group']
  58. acc_obj = self.env['account.account']
  59. self.group1 = group_obj.create(
  60. {'code_prefix': '1',
  61. 'name': 'Group 1'})
  62. self.group11 = group_obj.create(
  63. {'code_prefix': '11',
  64. 'name': 'Group 11',
  65. 'parent_id': self.group1.id})
  66. self.group2 = group_obj.create(
  67. {'code_prefix': '2',
  68. 'name': 'Group 2'})
  69. self.account100 = acc_obj.create(
  70. {'code': '100',
  71. 'name': 'Account 100',
  72. 'group_id': self.group1.id,
  73. 'user_type_id': self.env.ref(
  74. 'account.data_account_type_receivable').id,
  75. 'reconcile': True})
  76. self.account110 = self.env['account.account'].search([
  77. (
  78. 'user_type_id',
  79. '=',
  80. self.env.ref('account.data_unaffected_earnings').id
  81. )], limit=1)
  82. self.account200 = acc_obj.create(
  83. {'code': '200',
  84. 'name': 'Account 200',
  85. 'group_id': self.group2.id,
  86. 'user_type_id': self.env.ref(
  87. 'account.data_account_type_other_income').id})
  88. self.account300 = acc_obj.create(
  89. {'code': '300',
  90. 'name': 'Account 300',
  91. 'user_type_id': self.env.ref(
  92. 'account.data_account_type_other_income').id})
  93. self.account301 = acc_obj.create(
  94. {'code': '301',
  95. 'name': 'Account 301',
  96. 'group_id': self.group2.id,
  97. 'user_type_id': self.env.ref(
  98. 'account.data_account_type_other_income').id})
  99. self.previous_fy_date_start = '2015-01-01'
  100. self.previous_fy_date_end = '2015-12-31'
  101. self.fy_date_start = '2016-01-01'
  102. self.fy_date_end = '2016-12-31'
  103. self.date_start = '2016-01-01'
  104. self.date_end = '2016-12-31'
  105. def _add_move(
  106. self,
  107. date,
  108. receivable_debit,
  109. receivable_credit,
  110. income_debit,
  111. income_credit,
  112. unaffected_debit=0,
  113. unaffected_credit=0
  114. ):
  115. move_name = 'expense accrual'
  116. journal = self.env['account.journal'].search([
  117. ('code', '=', 'MISC')])
  118. partner = self.env.ref('base.res_partner_12')
  119. move_vals = {
  120. 'journal_id': journal.id,
  121. 'partner_id': partner.id,
  122. 'name': move_name,
  123. 'date': date,
  124. 'line_ids': [
  125. (0, 0, {
  126. 'name': move_name,
  127. 'debit': receivable_debit,
  128. 'credit': receivable_credit,
  129. 'account_id': self.account100.id}),
  130. (0, 0, {
  131. 'name': move_name,
  132. 'debit': income_debit,
  133. 'credit': income_credit,
  134. 'account_id': self.account200.id}),
  135. (0, 0, {
  136. 'name': move_name,
  137. 'debit': unaffected_debit,
  138. 'credit': unaffected_credit,
  139. 'account_id': self.account110.id}),
  140. (0, 0, {
  141. 'name': move_name,
  142. 'debit': receivable_debit,
  143. 'credit': receivable_credit,
  144. 'account_id': self.account300.id}),
  145. (0, 0, {
  146. 'name': move_name,
  147. 'debit': receivable_credit,
  148. 'credit': receivable_debit,
  149. 'account_id': self.account301.id})
  150. ]}
  151. move = self.env['account.move'].create(move_vals)
  152. move.post()
  153. def _get_report_lines(self, with_partners=False, hierarchy_on='computed'):
  154. company = self.env.ref('base.main_company')
  155. trial_balance = self.env['report_trial_balance'].create({
  156. 'date_from': self.date_start,
  157. 'date_to': self.date_end,
  158. 'only_posted_moves': True,
  159. 'hide_account_balance_at_0': False,
  160. 'hierarchy_on': hierarchy_on,
  161. 'company_id': company.id,
  162. 'fy_start_date': self.fy_date_start,
  163. 'show_partner_details': with_partners,
  164. })
  165. trial_balance.compute_data_for_report()
  166. lines = {}
  167. report_account_model = self.env['report_trial_balance_account']
  168. lines['receivable'] = report_account_model.search([
  169. ('report_id', '=', trial_balance.id),
  170. ('account_id', '=', self.account100.id),
  171. ])
  172. lines['income'] = report_account_model.search([
  173. ('report_id', '=', trial_balance.id),
  174. ('account_id', '=', self.account200.id),
  175. ])
  176. lines['unaffected'] = report_account_model.search([
  177. ('report_id', '=', trial_balance.id),
  178. ('account_id', '=', self.account110.id),
  179. ])
  180. lines['group1'] = report_account_model.search([
  181. ('report_id', '=', trial_balance.id),
  182. ('account_group_id', '=', self.group1.id),
  183. ])
  184. lines['group2'] = report_account_model.search([
  185. ('report_id', '=', trial_balance.id),
  186. ('account_group_id', '=', self.group2.id),
  187. ])
  188. if with_partners:
  189. report_partner_model = self.env[
  190. 'report_trial_balance_partner'
  191. ]
  192. lines['partner_receivable'] = report_partner_model.search([
  193. ('report_account_id', '=', lines['receivable'].id),
  194. ('partner_id', '=', self.env.ref('base.res_partner_12').id),
  195. ])
  196. return lines
  197. def test_00_account_group(self):
  198. self.assertEqual(len(self.group1.compute_account_ids.ids), 19)
  199. self.assertEqual(len(self.group2.compute_account_ids.ids), 9)
  200. def test_01_account_balance_computed(self):
  201. # Generate the general ledger line
  202. lines = self._get_report_lines()
  203. self.assertEqual(len(lines['receivable']), 1)
  204. self.assertEqual(len(lines['income']), 1)
  205. # Add a move at the previous day of the first day of fiscal year
  206. # to check the initial balance
  207. self._add_move(
  208. date=self.previous_fy_date_end,
  209. receivable_debit=1000,
  210. receivable_credit=0,
  211. income_debit=0,
  212. income_credit=1000
  213. )
  214. # Re Generate the trial balance line
  215. lines = self._get_report_lines()
  216. self.assertEqual(len(lines['receivable']), 1)
  217. self.assertEqual(len(lines['income']), 1)
  218. # Check the initial and final balance
  219. self.assertEqual(lines['receivable'].initial_balance, 1000)
  220. self.assertEqual(lines['receivable'].debit, 0)
  221. self.assertEqual(lines['receivable'].credit, 0)
  222. self.assertEqual(lines['receivable'].final_balance, 1000)
  223. self.assertEqual(lines['group1'].initial_balance, 1000)
  224. self.assertEqual(lines['group1'].debit, 0)
  225. self.assertEqual(lines['group1'].credit, 0)
  226. self.assertEqual(lines['group1'].final_balance, 1000)
  227. # Add reversale move of the initial move the first day of fiscal year
  228. # to check the first day of fiscal year is not used
  229. # to compute the initial balance
  230. self._add_move(
  231. date=self.fy_date_start,
  232. receivable_debit=0,
  233. receivable_credit=1000,
  234. income_debit=1000,
  235. income_credit=0
  236. )
  237. # Re Generate the trial balance line
  238. lines = self._get_report_lines()
  239. self.assertEqual(len(lines['receivable']), 1)
  240. self.assertEqual(len(lines['income']), 1)
  241. # Check the initial and final balance
  242. self.assertEqual(lines['receivable'].initial_balance, 1000)
  243. self.assertEqual(lines['receivable'].debit, 0)
  244. self.assertEqual(lines['receivable'].credit, 1000)
  245. self.assertEqual(lines['receivable'].final_balance, 0)
  246. self.assertEqual(lines['income'].initial_balance, 0)
  247. self.assertEqual(lines['income'].debit, 1000)
  248. self.assertEqual(lines['income'].credit, 0)
  249. self.assertEqual(lines['income'].final_balance, 1000)
  250. self.assertEqual(lines['group1'].initial_balance, 1000)
  251. self.assertEqual(lines['group1'].debit, 0)
  252. self.assertEqual(lines['group1'].credit, 1000)
  253. self.assertEqual(lines['group1'].final_balance, 0)
  254. self.assertEqual(lines['group2'].initial_balance, 0)
  255. self.assertEqual(lines['group2'].debit, 1000)
  256. self.assertEqual(lines['group2'].credit, 0)
  257. self.assertEqual(lines['group2'].final_balance, 1000)
  258. # Add another move at the end day of fiscal year
  259. # to check that it correctly used on report
  260. self._add_move(
  261. date=self.fy_date_end,
  262. receivable_debit=0,
  263. receivable_credit=1000,
  264. income_debit=1000,
  265. income_credit=0
  266. )
  267. # Re Generate the trial balance line
  268. lines = self._get_report_lines()
  269. self.assertEqual(len(lines['receivable']), 1)
  270. self.assertEqual(len(lines['income']), 1)
  271. # Check the initial and final balance
  272. self.assertEqual(lines['receivable'].initial_balance, 1000)
  273. self.assertEqual(lines['receivable'].debit, 0)
  274. self.assertEqual(lines['receivable'].credit, 2000)
  275. self.assertEqual(lines['receivable'].final_balance, -1000)
  276. self.assertEqual(lines['income'].initial_balance, 0)
  277. self.assertEqual(lines['income'].debit, 2000)
  278. self.assertEqual(lines['income'].credit, 0)
  279. self.assertEqual(lines['income'].final_balance, 2000)
  280. self.assertEqual(lines['group1'].initial_balance, 1000)
  281. self.assertEqual(lines['group1'].debit, 0)
  282. self.assertEqual(lines['group1'].credit, 2000)
  283. self.assertEqual(lines['group1'].final_balance, -1000)
  284. self.assertEqual(lines['group2'].initial_balance, 0)
  285. self.assertEqual(lines['group2'].debit, 2000)
  286. self.assertEqual(lines['group2'].credit, 0)
  287. self.assertEqual(lines['group2'].final_balance, 2000)
  288. self.assertEqual(len(lines['group2'].compute_account_ids), 2)
  289. def test_02_account_balance_hierarchy(self):
  290. # Generate the general ledger line
  291. lines = self._get_report_lines(hierarchy_on='relation')
  292. self.assertEqual(len(lines['receivable']), 1)
  293. self.assertEqual(len(lines['income']), 1)
  294. # Add a move at the previous day of the first day of fiscal year
  295. # to check the initial balance
  296. self._add_move(
  297. date=self.previous_fy_date_end,
  298. receivable_debit=1000,
  299. receivable_credit=0,
  300. income_debit=0,
  301. income_credit=1000
  302. )
  303. # Re Generate the trial balance line
  304. lines = self._get_report_lines(hierarchy_on='relation')
  305. self.assertEqual(len(lines['receivable']), 1)
  306. self.assertEqual(len(lines['income']), 1)
  307. # Check the initial and final balance
  308. self.assertEqual(lines['receivable'].initial_balance, 1000)
  309. self.assertEqual(lines['receivable'].debit, 0)
  310. self.assertEqual(lines['receivable'].credit, 0)
  311. self.assertEqual(lines['receivable'].final_balance, 1000)
  312. self.assertEqual(lines['group1'].initial_balance, 1000)
  313. self.assertEqual(lines['group1'].debit, 0)
  314. self.assertEqual(lines['group1'].credit, 0)
  315. self.assertEqual(lines['group1'].final_balance, 1000)
  316. # Add reversale move of the initial move the first day of fiscal year
  317. # to check the first day of fiscal year is not used
  318. # to compute the initial balance
  319. self._add_move(
  320. date=self.fy_date_start,
  321. receivable_debit=0,
  322. receivable_credit=1000,
  323. income_debit=1000,
  324. income_credit=0
  325. )
  326. # Re Generate the trial balance line
  327. lines = self._get_report_lines(hierarchy_on='relation')
  328. self.assertEqual(len(lines['receivable']), 1)
  329. self.assertEqual(len(lines['income']), 1)
  330. # Check the initial and final balance
  331. self.assertEqual(lines['receivable'].initial_balance, 1000)
  332. self.assertEqual(lines['receivable'].debit, 0)
  333. self.assertEqual(lines['receivable'].credit, 1000)
  334. self.assertEqual(lines['receivable'].final_balance, 0)
  335. self.assertEqual(lines['income'].initial_balance, 0)
  336. self.assertEqual(lines['income'].debit, 1000)
  337. self.assertEqual(lines['income'].credit, 0)
  338. self.assertEqual(lines['income'].final_balance, 1000)
  339. self.assertEqual(lines['group1'].initial_balance, 1000)
  340. self.assertEqual(lines['group1'].debit, 0)
  341. self.assertEqual(lines['group1'].credit, 1000)
  342. self.assertEqual(lines['group1'].final_balance, 0)
  343. self.assertEqual(lines['group2'].initial_balance, 0)
  344. self.assertEqual(lines['group2'].debit, 2000)
  345. self.assertEqual(lines['group2'].credit, 0)
  346. self.assertEqual(lines['group2'].final_balance, 2000)
  347. self.assertEqual(len(lines['group2'].compute_account_ids), 2)
  348. # Add another move at the end day of fiscal year
  349. # to check that it correctly used on report
  350. self._add_move(
  351. date=self.fy_date_end,
  352. receivable_debit=0,
  353. receivable_credit=1000,
  354. income_debit=1000,
  355. income_credit=0
  356. )
  357. # Re Generate the trial balance line
  358. lines = self._get_report_lines(hierarchy_on='relation')
  359. self.assertEqual(len(lines['receivable']), 1)
  360. self.assertEqual(len(lines['income']), 1)
  361. # Check the initial and final balance
  362. self.assertEqual(lines['receivable'].initial_balance, 1000)
  363. self.assertEqual(lines['receivable'].debit, 0)
  364. self.assertEqual(lines['receivable'].credit, 2000)
  365. self.assertEqual(lines['receivable'].final_balance, -1000)
  366. self.assertEqual(lines['income'].initial_balance, 0)
  367. self.assertEqual(lines['income'].debit, 2000)
  368. self.assertEqual(lines['income'].credit, 0)
  369. self.assertEqual(lines['income'].final_balance, 2000)
  370. self.assertEqual(lines['group1'].initial_balance, 1000)
  371. self.assertEqual(lines['group1'].debit, 0)
  372. self.assertEqual(lines['group1'].credit, 2000)
  373. self.assertEqual(lines['group1'].final_balance, -1000)
  374. self.assertEqual(lines['group2'].initial_balance, 0)
  375. self.assertEqual(lines['group2'].debit, 4000)
  376. self.assertEqual(lines['group2'].credit, 0)
  377. self.assertEqual(lines['group2'].final_balance, 4000)
  378. def test_03_partner_balance(self):
  379. # Generate the trial balance line
  380. lines = self._get_report_lines(with_partners=True)
  381. self.assertEqual(len(lines['partner_receivable']), 0)
  382. # Add a move at the previous day of the first day of fiscal year
  383. # to check the initial balance
  384. self._add_move(
  385. date=self.previous_fy_date_end,
  386. receivable_debit=1000,
  387. receivable_credit=0,
  388. income_debit=0,
  389. income_credit=1000
  390. )
  391. # Re Generate the trial balance line
  392. lines = self._get_report_lines(with_partners=True)
  393. self.assertEqual(len(lines['partner_receivable']), 1)
  394. # Check the initial and final balance
  395. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  396. self.assertEqual(lines['partner_receivable'].debit, 0)
  397. self.assertEqual(lines['partner_receivable'].credit, 0)
  398. self.assertEqual(lines['partner_receivable'].final_balance, 1000)
  399. # Add reversale move of the initial move the first day of fiscal year
  400. # to check the first day of fiscal year is not used
  401. # to compute the initial balance
  402. self._add_move(
  403. date=self.fy_date_start,
  404. receivable_debit=0,
  405. receivable_credit=1000,
  406. income_debit=1000,
  407. income_credit=0
  408. )
  409. # Re Generate the trial balance line
  410. lines = self._get_report_lines(with_partners=True)
  411. self.assertEqual(len(lines['partner_receivable']), 1)
  412. # Check the initial and final balance
  413. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  414. self.assertEqual(lines['partner_receivable'].debit, 0)
  415. self.assertEqual(lines['partner_receivable'].credit, 1000)
  416. self.assertEqual(lines['partner_receivable'].final_balance, 0)
  417. # Add another move at the end day of fiscal year
  418. # to check that it correctly used on report
  419. self._add_move(
  420. date=self.fy_date_end,
  421. receivable_debit=0,
  422. receivable_credit=1000,
  423. income_debit=1000,
  424. income_credit=0
  425. )
  426. # Re Generate the trial balance line
  427. lines = self._get_report_lines(with_partners=True)
  428. self.assertEqual(len(lines['partner_receivable']), 1)
  429. # Check the initial and final balance
  430. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  431. self.assertEqual(lines['partner_receivable'].debit, 0)
  432. self.assertEqual(lines['partner_receivable'].credit, 2000)
  433. self.assertEqual(lines['partner_receivable'].final_balance, -1000)