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.

502 lines
20 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([], limit=1)
  117. partner = self.env.ref('base.res_partner_12')
  118. move_vals = {
  119. 'journal_id': journal.id,
  120. 'partner_id': partner.id,
  121. 'name': move_name,
  122. 'date': date,
  123. 'line_ids': [
  124. (0, 0, {
  125. 'name': move_name,
  126. 'debit': receivable_debit,
  127. 'credit': receivable_credit,
  128. 'account_id': self.account100.id}),
  129. (0, 0, {
  130. 'name': move_name,
  131. 'debit': income_debit,
  132. 'credit': income_credit,
  133. 'account_id': self.account200.id}),
  134. (0, 0, {
  135. 'name': move_name,
  136. 'debit': unaffected_debit,
  137. 'credit': unaffected_credit,
  138. 'account_id': self.account110.id}),
  139. (0, 0, {
  140. 'name': move_name,
  141. 'debit': receivable_debit,
  142. 'credit': receivable_credit,
  143. 'account_id': self.account300.id}),
  144. (0, 0, {
  145. 'name': move_name,
  146. 'debit': receivable_credit,
  147. 'credit': receivable_debit,
  148. 'account_id': self.account301.id})
  149. ]}
  150. move = self.env['account.move'].create(move_vals)
  151. move.post()
  152. def _get_report_lines(self, with_partners=False, hierarchy_on='computed'):
  153. company = self.env.ref('base.main_company')
  154. trial_balance = self.env['report_trial_balance'].create({
  155. 'date_from': self.date_start,
  156. 'date_to': self.date_end,
  157. 'only_posted_moves': True,
  158. 'hide_account_balance_at_0': False,
  159. 'hierarchy_on': hierarchy_on,
  160. 'company_id': company.id,
  161. 'fy_start_date': self.fy_date_start,
  162. 'show_partner_details': with_partners,
  163. })
  164. trial_balance.compute_data_for_report()
  165. lines = {}
  166. report_account_model = self.env['report_trial_balance_account']
  167. lines['receivable'] = report_account_model.search([
  168. ('report_id', '=', trial_balance.id),
  169. ('account_id', '=', self.account100.id),
  170. ])
  171. lines['income'] = report_account_model.search([
  172. ('report_id', '=', trial_balance.id),
  173. ('account_id', '=', self.account200.id),
  174. ])
  175. lines['unaffected'] = report_account_model.search([
  176. ('report_id', '=', trial_balance.id),
  177. ('account_id', '=', self.account110.id),
  178. ])
  179. lines['group1'] = report_account_model.search([
  180. ('report_id', '=', trial_balance.id),
  181. ('account_group_id', '=', self.group1.id),
  182. ])
  183. lines['group2'] = report_account_model.search([
  184. ('report_id', '=', trial_balance.id),
  185. ('account_group_id', '=', self.group2.id),
  186. ])
  187. if with_partners:
  188. report_partner_model = self.env[
  189. 'report_trial_balance_partner'
  190. ]
  191. lines['partner_receivable'] = report_partner_model.search([
  192. ('report_account_id', '=', lines['receivable'].id),
  193. ('partner_id', '=', self.env.ref('base.res_partner_12').id),
  194. ])
  195. return lines
  196. def test_00_account_group(self):
  197. self.assertGreaterEqual(len(self.group1.compute_account_ids), 19)
  198. self.assertGreaterEqual(len(self.group2.compute_account_ids), 9)
  199. def test_01_account_balance_computed(self):
  200. # Make sure there's no account of type "Current Year Earnings" in the
  201. # groups - We change the code
  202. earning_accs = self.env['account.account'].search([
  203. ('user_type_id', '=',
  204. self.env.ref('account.data_unaffected_earnings').id)
  205. ])
  206. for acc in earning_accs:
  207. if acc.code.startswith('1') or acc.code.startswith('2'):
  208. acc.code = '999' + acc.code
  209. # Generate the general ledger line
  210. lines = self._get_report_lines()
  211. self.assertEqual(len(lines['receivable']), 1)
  212. self.assertEqual(len(lines['income']), 1)
  213. # Add a move at the previous day of the first day of fiscal year
  214. # to check the initial balance
  215. self._add_move(
  216. date=self.previous_fy_date_end,
  217. receivable_debit=1000,
  218. receivable_credit=0,
  219. income_debit=0,
  220. income_credit=1000
  221. )
  222. # Re Generate the trial balance line
  223. lines = self._get_report_lines()
  224. self.assertEqual(len(lines['receivable']), 1)
  225. self.assertEqual(len(lines['income']), 1)
  226. # Check the initial and final balance
  227. self.assertEqual(lines['receivable'].initial_balance, 1000)
  228. self.assertEqual(lines['receivable'].debit, 0)
  229. self.assertEqual(lines['receivable'].credit, 0)
  230. self.assertEqual(lines['receivable'].final_balance, 1000)
  231. self.assertEqual(lines['group1'].initial_balance, 1000)
  232. self.assertEqual(lines['group1'].debit, 0)
  233. self.assertEqual(lines['group1'].credit, 0)
  234. self.assertEqual(lines['group1'].final_balance, 1000)
  235. # Add reversed move of the initial move the first day of fiscal year
  236. # to check the first day of fiscal year is not used
  237. # to compute the initial balance
  238. self._add_move(
  239. date=self.fy_date_start,
  240. receivable_debit=0,
  241. receivable_credit=1000,
  242. income_debit=1000,
  243. income_credit=0
  244. )
  245. # Re Generate the trial balance line
  246. lines = self._get_report_lines()
  247. self.assertEqual(len(lines['receivable']), 1)
  248. self.assertEqual(len(lines['income']), 1)
  249. # Check the initial and final balance
  250. self.assertEqual(lines['receivable'].initial_balance, 1000)
  251. self.assertEqual(lines['receivable'].debit, 0)
  252. self.assertEqual(lines['receivable'].credit, 1000)
  253. self.assertEqual(lines['receivable'].final_balance, 0)
  254. self.assertEqual(lines['income'].initial_balance, 0)
  255. self.assertEqual(lines['income'].debit, 1000)
  256. self.assertEqual(lines['income'].credit, 0)
  257. self.assertEqual(lines['income'].final_balance, 1000)
  258. self.assertEqual(lines['group1'].initial_balance, 1000)
  259. self.assertEqual(lines['group1'].debit, 0)
  260. self.assertEqual(lines['group1'].credit, 1000)
  261. self.assertEqual(lines['group1'].final_balance, 0)
  262. self.assertEqual(lines['group2'].initial_balance, 0)
  263. self.assertEqual(lines['group2'].debit, 1000)
  264. self.assertEqual(lines['group2'].credit, 0)
  265. self.assertEqual(lines['group2'].final_balance, 1000)
  266. # Add another move at the end day of fiscal year
  267. # to check that it correctly used on report
  268. self._add_move(
  269. date=self.fy_date_end,
  270. receivable_debit=0,
  271. receivable_credit=1000,
  272. income_debit=1000,
  273. income_credit=0
  274. )
  275. # Re Generate the trial balance line
  276. lines = self._get_report_lines()
  277. self.assertEqual(len(lines['receivable']), 1)
  278. self.assertEqual(len(lines['income']), 1)
  279. # Check the initial and final balance
  280. self.assertEqual(lines['receivable'].initial_balance, 1000)
  281. self.assertEqual(lines['receivable'].debit, 0)
  282. self.assertEqual(lines['receivable'].credit, 2000)
  283. self.assertEqual(lines['receivable'].final_balance, -1000)
  284. self.assertEqual(lines['income'].initial_balance, 0)
  285. self.assertEqual(lines['income'].debit, 2000)
  286. self.assertEqual(lines['income'].credit, 0)
  287. self.assertEqual(lines['income'].final_balance, 2000)
  288. self.assertEqual(lines['group1'].initial_balance, 1000)
  289. self.assertEqual(lines['group1'].debit, 0)
  290. self.assertEqual(lines['group1'].credit, 2000)
  291. self.assertEqual(lines['group1'].final_balance, -1000)
  292. self.assertEqual(lines['group2'].initial_balance, 0)
  293. self.assertEqual(lines['group2'].debit, 2000)
  294. self.assertEqual(lines['group2'].credit, 0)
  295. self.assertEqual(lines['group2'].final_balance, 2000)
  296. self.assertEqual(len(lines['group2'].compute_account_ids), 2)
  297. def test_02_account_balance_hierarchy(self):
  298. # Generate the general ledger line
  299. lines = self._get_report_lines(hierarchy_on='relation')
  300. self.assertEqual(len(lines['receivable']), 1)
  301. self.assertEqual(len(lines['income']), 1)
  302. # Add a move at the previous day of the first day of fiscal year
  303. # to check the initial balance
  304. self._add_move(
  305. date=self.previous_fy_date_end,
  306. receivable_debit=1000,
  307. receivable_credit=0,
  308. income_debit=0,
  309. income_credit=1000
  310. )
  311. # Re Generate the trial balance line
  312. lines = self._get_report_lines(hierarchy_on='relation')
  313. self.assertEqual(len(lines['receivable']), 1)
  314. self.assertEqual(len(lines['income']), 1)
  315. # Check the initial and final balance
  316. self.assertEqual(lines['receivable'].initial_balance, 1000)
  317. self.assertEqual(lines['receivable'].debit, 0)
  318. self.assertEqual(lines['receivable'].credit, 0)
  319. self.assertEqual(lines['receivable'].final_balance, 1000)
  320. self.assertEqual(lines['group1'].initial_balance, 1000)
  321. self.assertEqual(lines['group1'].debit, 0)
  322. self.assertEqual(lines['group1'].credit, 0)
  323. self.assertEqual(lines['group1'].final_balance, 1000)
  324. # Add reversale move of the initial move the first day of fiscal year
  325. # to check the first day of fiscal year is not used
  326. # to compute the initial balance
  327. self._add_move(
  328. date=self.fy_date_start,
  329. receivable_debit=0,
  330. receivable_credit=1000,
  331. income_debit=1000,
  332. income_credit=0
  333. )
  334. # Re Generate the trial balance line
  335. lines = self._get_report_lines(hierarchy_on='relation')
  336. self.assertEqual(len(lines['receivable']), 1)
  337. self.assertEqual(len(lines['income']), 1)
  338. # Check the initial and final balance
  339. self.assertEqual(lines['receivable'].initial_balance, 1000)
  340. self.assertEqual(lines['receivable'].debit, 0)
  341. self.assertEqual(lines['receivable'].credit, 1000)
  342. self.assertEqual(lines['receivable'].final_balance, 0)
  343. self.assertEqual(lines['income'].initial_balance, 0)
  344. self.assertEqual(lines['income'].debit, 1000)
  345. self.assertEqual(lines['income'].credit, 0)
  346. self.assertEqual(lines['income'].final_balance, 1000)
  347. self.assertEqual(lines['group1'].initial_balance, 1000)
  348. self.assertEqual(lines['group1'].debit, 0)
  349. self.assertEqual(lines['group1'].credit, 1000)
  350. self.assertEqual(lines['group1'].final_balance, 0)
  351. self.assertEqual(lines['group2'].initial_balance, 0)
  352. self.assertEqual(lines['group2'].debit, 2000)
  353. self.assertEqual(lines['group2'].credit, 0)
  354. self.assertEqual(lines['group2'].final_balance, 2000)
  355. self.assertEqual(len(lines['group2'].compute_account_ids), 2)
  356. # Add another move at the end day of fiscal year
  357. # to check that it correctly used on report
  358. self._add_move(
  359. date=self.fy_date_end,
  360. receivable_debit=0,
  361. receivable_credit=1000,
  362. income_debit=1000,
  363. income_credit=0
  364. )
  365. # Re Generate the trial balance line
  366. lines = self._get_report_lines(hierarchy_on='relation')
  367. self.assertEqual(len(lines['receivable']), 1)
  368. self.assertEqual(len(lines['income']), 1)
  369. # Check the initial and final balance
  370. self.assertEqual(lines['receivable'].initial_balance, 1000)
  371. self.assertEqual(lines['receivable'].debit, 0)
  372. self.assertEqual(lines['receivable'].credit, 2000)
  373. self.assertEqual(lines['receivable'].final_balance, -1000)
  374. self.assertEqual(lines['income'].initial_balance, 0)
  375. self.assertEqual(lines['income'].debit, 2000)
  376. self.assertEqual(lines['income'].credit, 0)
  377. self.assertEqual(lines['income'].final_balance, 2000)
  378. self.assertEqual(lines['group1'].initial_balance, 1000)
  379. self.assertEqual(lines['group1'].debit, 0)
  380. self.assertEqual(lines['group1'].credit, 2000)
  381. self.assertEqual(lines['group1'].final_balance, -1000)
  382. self.assertEqual(lines['group2'].initial_balance, 0)
  383. self.assertEqual(lines['group2'].debit, 4000)
  384. self.assertEqual(lines['group2'].credit, 0)
  385. self.assertEqual(lines['group2'].final_balance, 4000)
  386. def test_03_partner_balance(self):
  387. # Generate the trial balance line
  388. lines = self._get_report_lines(with_partners=True)
  389. self.assertEqual(len(lines['partner_receivable']), 0)
  390. # Add a move at the previous day of the first day of fiscal year
  391. # to check the initial balance
  392. self._add_move(
  393. date=self.previous_fy_date_end,
  394. receivable_debit=1000,
  395. receivable_credit=0,
  396. income_debit=0,
  397. income_credit=1000
  398. )
  399. # Re Generate the trial balance line
  400. lines = self._get_report_lines(with_partners=True)
  401. self.assertEqual(len(lines['partner_receivable']), 1)
  402. # Check the initial and final balance
  403. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  404. self.assertEqual(lines['partner_receivable'].debit, 0)
  405. self.assertEqual(lines['partner_receivable'].credit, 0)
  406. self.assertEqual(lines['partner_receivable'].final_balance, 1000)
  407. # Add reversale move of the initial move the first day of fiscal year
  408. # to check the first day of fiscal year is not used
  409. # to compute the initial balance
  410. self._add_move(
  411. date=self.fy_date_start,
  412. receivable_debit=0,
  413. receivable_credit=1000,
  414. income_debit=1000,
  415. income_credit=0
  416. )
  417. # Re Generate the trial balance line
  418. lines = self._get_report_lines(with_partners=True)
  419. self.assertEqual(len(lines['partner_receivable']), 1)
  420. # Check the initial and final balance
  421. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  422. self.assertEqual(lines['partner_receivable'].debit, 0)
  423. self.assertEqual(lines['partner_receivable'].credit, 1000)
  424. self.assertEqual(lines['partner_receivable'].final_balance, 0)
  425. # Add another move at the end day of fiscal year
  426. # to check that it correctly used on report
  427. self._add_move(
  428. date=self.fy_date_end,
  429. receivable_debit=0,
  430. receivable_credit=1000,
  431. income_debit=1000,
  432. income_credit=0
  433. )
  434. # Re Generate the trial balance line
  435. lines = self._get_report_lines(with_partners=True)
  436. self.assertEqual(len(lines['partner_receivable']), 1)
  437. # Check the initial and final balance
  438. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  439. self.assertEqual(lines['partner_receivable'].debit, 0)
  440. self.assertEqual(lines['partner_receivable'].credit, 2000)
  441. self.assertEqual(lines['partner_receivable'].final_balance, -1000)