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.

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