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.

553 lines
22 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 odoo import fields
  7. from datetime import date
  8. from . import abstract_test_foreign_currency as a_t_f_c
  9. class TestGeneralLedger(a_t_f_c.AbstractTestForeignCurrency):
  10. """
  11. Technical tests for General Ledger Report.
  12. """
  13. def _getReportModel(self):
  14. return self.env['report_general_ledger']
  15. def _getQwebReportName(self):
  16. return 'account_financial_report.report_general_ledger_qweb'
  17. def _getXlsxReportName(self):
  18. return 'a_f_r.report_general_ledger_xlsx'
  19. def _getXlsxReportActionName(self):
  20. return 'account_financial_report.' \
  21. 'action_report_general_ledger_xlsx'
  22. def _getReportTitle(self):
  23. return 'Odoo'
  24. def _getBaseFilters(self):
  25. return {
  26. 'date_from': date(date.today().year, 1, 1),
  27. 'date_to': date(date.today().year, 12, 31),
  28. 'company_id': self.company.id,
  29. 'fy_start_date': date(date.today().year, 1, 1),
  30. 'foreign_currency': True,
  31. }
  32. def _getAdditionalFiltersToBeTested(self):
  33. additional_filters = [
  34. {'only_posted_moves': True},
  35. {'hide_account_at_0': True},
  36. {'centralize': True},
  37. {'only_posted_moves': True, 'hide_account_at_0': True},
  38. {'only_posted_moves': True, 'centralize': True},
  39. {'hide_account_at_0': True, 'centralize': True},
  40. {
  41. 'only_posted_moves': True,
  42. 'hide_account_at_0': True,
  43. 'centralize': True
  44. },
  45. ]
  46. # Add `show_analytic_tags` filter on each cases
  47. additional_filters_with_show_tags = []
  48. for additional_filter in additional_filters:
  49. additional_filter['show_analytic_tags'] = True
  50. additional_filters_with_show_tags.append(
  51. additional_filter
  52. )
  53. additional_filters += additional_filters_with_show_tags
  54. # Add `filter_analytic_tag_ids` filter on each cases
  55. analytic_tag = self.env['account.analytic.tag'].create({
  56. 'name': 'TEST tag'
  57. })
  58. # Define all move lines on this tag
  59. # (this test just check with the all filters, all works technically)
  60. move_lines = self.env['account.move.line'].search([])
  61. move_lines.write({
  62. 'analytic_tag_ids': [(6, False, analytic_tag.ids)],
  63. })
  64. additional_filters_with_filter_tags = []
  65. for additional_filter in additional_filters:
  66. additional_filter['filter_analytic_tag_ids'] = [
  67. (6, False, analytic_tag.ids)
  68. ]
  69. additional_filters_with_filter_tags.append(
  70. additional_filter
  71. )
  72. additional_filters += additional_filters_with_filter_tags
  73. return additional_filters
  74. class TestGeneralLedgerReport(common.TransactionCase):
  75. def setUp(self):
  76. super(TestGeneralLedgerReport, self).setUp()
  77. self.before_previous_fy_year = fields.Date.from_string('2014-05-05')
  78. self.previous_fy_date_start = fields.Date.from_string('2015-01-01')
  79. self.previous_fy_date_end = fields.Date.from_string('2015-12-31')
  80. self.fy_date_start = fields.Date.from_string('2016-01-01')
  81. self.fy_date_end = fields.Date.from_string('2016-12-31')
  82. self.receivable_account = self.env['account.account'].search([
  83. ('user_type_id.name', '=', 'Receivable')
  84. ], limit=1)
  85. self.income_account = self.env['account.account'].search([
  86. ('user_type_id.name', '=', 'Income')
  87. ], limit=1)
  88. self.unaffected_account = self.env['account.account'].search([
  89. (
  90. 'user_type_id',
  91. '=',
  92. self.env.ref('account.data_unaffected_earnings').id
  93. )], limit=1)
  94. def _add_move(
  95. self,
  96. date,
  97. receivable_debit,
  98. receivable_credit,
  99. income_debit,
  100. income_credit,
  101. unaffected_debit=0,
  102. unaffected_credit=0
  103. ):
  104. move_name = 'expense accrual'
  105. journal = self.env['account.journal'].search([], limit=1)
  106. partner = self.env.ref('base.res_partner_12')
  107. move_vals = {
  108. 'journal_id': journal.id,
  109. 'name': move_name,
  110. 'date': date,
  111. 'line_ids': [
  112. (0, 0, {
  113. 'name': move_name,
  114. 'debit': receivable_debit,
  115. 'credit': receivable_credit,
  116. 'account_id': self.receivable_account.id,
  117. 'partner_id': partner.id}),
  118. (0, 0, {
  119. 'name': move_name,
  120. 'debit': income_debit,
  121. 'credit': income_credit,
  122. 'account_id': self.income_account.id,
  123. 'partner_id': partner.id}),
  124. (0, 0, {
  125. 'name': move_name,
  126. 'debit': unaffected_debit,
  127. 'credit': unaffected_credit,
  128. 'account_id': self.unaffected_account.id,
  129. 'partner_id': partner.id}),
  130. ]}
  131. move = self.env['account.move'].create(move_vals)
  132. move.post()
  133. def _get_report_lines(self, with_partners=False):
  134. company = self.env.ref('base.main_company')
  135. general_ledger = self.env['report_general_ledger'].create({
  136. 'date_from': self.fy_date_start,
  137. 'date_to': self.fy_date_end,
  138. 'only_posted_moves': True,
  139. 'hide_account_at_0': False,
  140. 'company_id': company.id,
  141. 'fy_start_date': self.fy_date_start,
  142. })
  143. general_ledger.compute_data_for_report(
  144. with_line_details=True, with_partners=with_partners
  145. )
  146. lines = {}
  147. report_account_model = self.env['report_general_ledger_account']
  148. lines['receivable'] = report_account_model.search([
  149. ('report_id', '=', general_ledger.id),
  150. ('account_id', '=', self.receivable_account.id),
  151. ])
  152. lines['income'] = report_account_model.search([
  153. ('report_id', '=', general_ledger.id),
  154. ('account_id', '=', self.income_account.id),
  155. ])
  156. lines['unaffected'] = report_account_model.search([
  157. ('report_id', '=', general_ledger.id),
  158. ('account_id', '=', self.unaffected_account.id),
  159. ])
  160. if with_partners:
  161. report_partner_model = self.env[
  162. 'report_general_ledger_partner'
  163. ]
  164. lines['partner_receivable'] = report_partner_model.search([
  165. ('report_account_id', '=', lines['receivable'].id),
  166. ('partner_id', '=', self.env.ref('base.res_partner_12').id),
  167. ])
  168. return lines
  169. def test_01_account_balance(self):
  170. # Generate the general ledger line
  171. lines = self._get_report_lines()
  172. self.assertEqual(len(lines['receivable']), 0)
  173. self.assertEqual(len(lines['income']), 0)
  174. # Add a move at the previous day of the first day of fiscal year
  175. # to check the initial balance
  176. self._add_move(
  177. date=self.previous_fy_date_end,
  178. receivable_debit=1000,
  179. receivable_credit=0,
  180. income_debit=0,
  181. income_credit=1000
  182. )
  183. # Re Generate the general ledger line
  184. lines = self._get_report_lines()
  185. self.assertEqual(len(lines['receivable']), 1)
  186. self.assertEqual(len(lines['income']), 0)
  187. # Check the initial and final balance
  188. self.assertEqual(lines['receivable'].initial_debit, 1000)
  189. self.assertEqual(lines['receivable'].initial_credit, 0)
  190. self.assertEqual(lines['receivable'].initial_balance, 1000)
  191. self.assertEqual(lines['receivable'].final_debit, 1000)
  192. self.assertEqual(lines['receivable'].final_credit, 0)
  193. self.assertEqual(lines['receivable'].final_balance, 1000)
  194. # Add reversale move of the initial move the first day of fiscal year
  195. # to check the first day of fiscal year is not used
  196. # to compute the initial balance
  197. self._add_move(
  198. date=self.fy_date_start,
  199. receivable_debit=0,
  200. receivable_credit=1000,
  201. income_debit=1000,
  202. income_credit=0
  203. )
  204. # Re Generate the general ledger line
  205. lines = self._get_report_lines()
  206. self.assertEqual(len(lines['receivable']), 1)
  207. self.assertEqual(len(lines['income']), 1)
  208. # Check the initial and final balance
  209. self.assertEqual(lines['receivable'].initial_debit, 1000)
  210. self.assertEqual(lines['receivable'].initial_credit, 0)
  211. self.assertEqual(lines['receivable'].initial_balance, 1000)
  212. self.assertEqual(lines['receivable'].final_debit, 1000)
  213. self.assertEqual(lines['receivable'].final_credit, 1000)
  214. self.assertEqual(lines['receivable'].final_balance, 0)
  215. self.assertEqual(lines['income'].initial_debit, 0)
  216. self.assertEqual(lines['income'].initial_credit, 0)
  217. self.assertEqual(lines['income'].initial_balance, 0)
  218. self.assertEqual(lines['income'].final_debit, 1000)
  219. self.assertEqual(lines['income'].final_credit, 0)
  220. self.assertEqual(lines['income'].final_balance, 1000)
  221. # Add another move at the end day of fiscal year
  222. # to check that it correctly used on report
  223. self._add_move(
  224. date=self.fy_date_end,
  225. receivable_debit=0,
  226. receivable_credit=1000,
  227. income_debit=1000,
  228. income_credit=0
  229. )
  230. # Re Generate the general ledger line
  231. lines = self._get_report_lines()
  232. self.assertEqual(len(lines['receivable']), 1)
  233. self.assertEqual(len(lines['income']), 1)
  234. # Check the initial and final balance
  235. self.assertEqual(lines['receivable'].initial_debit, 1000)
  236. self.assertEqual(lines['receivable'].initial_credit, 0)
  237. self.assertEqual(lines['receivable'].initial_balance, 1000)
  238. self.assertEqual(lines['receivable'].final_debit, 1000)
  239. self.assertEqual(lines['receivable'].final_credit, 2000)
  240. self.assertEqual(lines['receivable'].final_balance, -1000)
  241. self.assertEqual(lines['income'].initial_debit, 0)
  242. self.assertEqual(lines['income'].initial_credit, 0)
  243. self.assertEqual(lines['income'].initial_balance, 0)
  244. self.assertEqual(lines['income'].final_debit, 2000)
  245. self.assertEqual(lines['income'].final_credit, 0)
  246. self.assertEqual(lines['income'].final_balance, 2000)
  247. def test_02_partner_balance(self):
  248. # Generate the general ledger line
  249. lines = self._get_report_lines(with_partners=True)
  250. self.assertEqual(len(lines['partner_receivable']), 0)
  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 general ledger line
  261. lines = self._get_report_lines(with_partners=True)
  262. self.assertEqual(len(lines['partner_receivable']), 1)
  263. # Check the initial and final balance
  264. self.assertEqual(lines['partner_receivable'].initial_debit, 1000)
  265. self.assertEqual(lines['partner_receivable'].initial_credit, 0)
  266. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  267. self.assertEqual(lines['partner_receivable'].final_debit, 1000)
  268. self.assertEqual(lines['partner_receivable'].final_credit, 0)
  269. self.assertEqual(lines['partner_receivable'].final_balance, 1000)
  270. # Add reversale move of the initial move the first day of fiscal year
  271. # to check the first day of fiscal year is not used
  272. # to compute the initial balance
  273. self._add_move(
  274. date=self.fy_date_start,
  275. receivable_debit=0,
  276. receivable_credit=1000,
  277. income_debit=1000,
  278. income_credit=0
  279. )
  280. # Re Generate the general ledger line
  281. lines = self._get_report_lines(with_partners=True)
  282. self.assertEqual(len(lines['partner_receivable']), 1)
  283. # Check the initial and final balance
  284. self.assertEqual(lines['partner_receivable'].initial_debit, 1000)
  285. self.assertEqual(lines['partner_receivable'].initial_credit, 0)
  286. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  287. self.assertEqual(lines['partner_receivable'].final_debit, 1000)
  288. self.assertEqual(lines['partner_receivable'].final_credit, 1000)
  289. self.assertEqual(lines['partner_receivable'].final_balance, 0)
  290. # Add another move at the end day of fiscal year
  291. # to check that it correctly used on report
  292. self._add_move(
  293. date=self.fy_date_end,
  294. receivable_debit=0,
  295. receivable_credit=1000,
  296. income_debit=1000,
  297. income_credit=0
  298. )
  299. # Re Generate the general ledger line
  300. lines = self._get_report_lines(with_partners=True)
  301. self.assertEqual(len(lines['partner_receivable']), 1)
  302. # Check the initial and final balance
  303. self.assertEqual(lines['partner_receivable'].initial_debit, 1000)
  304. self.assertEqual(lines['partner_receivable'].initial_credit, 0)
  305. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  306. self.assertEqual(lines['partner_receivable'].final_debit, 1000)
  307. self.assertEqual(lines['partner_receivable'].final_credit, 2000)
  308. self.assertEqual(lines['partner_receivable'].final_balance, -1000)
  309. def test_03_unaffected_account_balance(self):
  310. # Generate the general ledger line
  311. lines = self._get_report_lines()
  312. self.assertEqual(len(lines['unaffected']), 1)
  313. # Check the initial and final balance
  314. self.assertEqual(lines['unaffected'].initial_debit, 0)
  315. self.assertEqual(lines['unaffected'].initial_credit, 0)
  316. self.assertEqual(lines['unaffected'].initial_balance, 0)
  317. self.assertEqual(lines['unaffected'].final_debit, 0)
  318. self.assertEqual(lines['unaffected'].final_credit, 0)
  319. self.assertEqual(lines['unaffected'].final_balance, 0)
  320. # Add a move at the previous day of the first day of fiscal year
  321. # to check the initial balance
  322. self._add_move(
  323. date=self.previous_fy_date_end,
  324. receivable_debit=1000,
  325. receivable_credit=0,
  326. income_debit=0,
  327. income_credit=1000
  328. )
  329. # Re Generate the general ledger line
  330. lines = self._get_report_lines()
  331. self.assertEqual(len(lines['unaffected']), 1)
  332. # Check the initial and final balance
  333. self.assertEqual(lines['unaffected'].initial_debit, 0)
  334. self.assertEqual(lines['unaffected'].initial_credit, 1000)
  335. self.assertEqual(lines['unaffected'].initial_balance, -1000)
  336. self.assertEqual(lines['unaffected'].final_debit, 0)
  337. self.assertEqual(lines['unaffected'].final_credit, 1000)
  338. self.assertEqual(lines['unaffected'].final_balance, -1000)
  339. # Add reversale move of the initial move the first day of fiscal year
  340. # to check the first day of fiscal year is not used
  341. # to compute the initial balance
  342. self._add_move(
  343. date=self.fy_date_start,
  344. receivable_debit=0,
  345. receivable_credit=0,
  346. income_debit=0,
  347. income_credit=1000,
  348. unaffected_debit=1000,
  349. unaffected_credit=0
  350. )
  351. # Re Generate the general ledger line
  352. lines = self._get_report_lines()
  353. self.assertEqual(len(lines['unaffected']), 1)
  354. # Check the initial and final balance
  355. self.assertEqual(lines['unaffected'].initial_debit, 0)
  356. self.assertEqual(lines['unaffected'].initial_credit, 1000)
  357. self.assertEqual(lines['unaffected'].initial_balance, -1000)
  358. self.assertEqual(lines['unaffected'].final_debit, 1000)
  359. self.assertEqual(lines['unaffected'].final_credit, 1000)
  360. self.assertEqual(lines['unaffected'].final_balance, 0)
  361. # Add another move at the end day of fiscal year
  362. # to check that it correctly used on report
  363. self._add_move(
  364. date=self.fy_date_end,
  365. receivable_debit=3000,
  366. receivable_credit=0,
  367. income_debit=0,
  368. income_credit=0,
  369. unaffected_debit=0,
  370. unaffected_credit=3000
  371. )
  372. # Re Generate the general ledger line
  373. lines = self._get_report_lines()
  374. self.assertEqual(len(lines['unaffected']), 1)
  375. # Check the initial and final balance
  376. self.assertEqual(lines['unaffected'].initial_debit, 0)
  377. self.assertEqual(lines['unaffected'].initial_credit, 1000)
  378. self.assertEqual(lines['unaffected'].initial_balance, -1000)
  379. self.assertEqual(lines['unaffected'].final_debit, 1000)
  380. self.assertEqual(lines['unaffected'].final_credit, 4000)
  381. self.assertEqual(lines['unaffected'].final_balance, -3000)
  382. def test_04_unaffected_account_balance_2_years(self):
  383. # Generate the general ledger line
  384. lines = self._get_report_lines()
  385. self.assertEqual(len(lines['unaffected']), 1)
  386. # Check the initial and final balance
  387. self.assertEqual(lines['unaffected'].initial_debit, 0)
  388. self.assertEqual(lines['unaffected'].initial_credit, 0)
  389. self.assertEqual(lines['unaffected'].initial_balance, 0)
  390. self.assertEqual(lines['unaffected'].final_debit, 0)
  391. self.assertEqual(lines['unaffected'].final_credit, 0)
  392. self.assertEqual(lines['unaffected'].final_balance, 0)
  393. # Add a move at any date 2 years before the balance
  394. # (to create an historic)
  395. self._add_move(
  396. date=self.before_previous_fy_year,
  397. receivable_debit=0,
  398. receivable_credit=1000,
  399. income_debit=1000,
  400. income_credit=0
  401. )
  402. # Re Generate the general ledger line
  403. lines = self._get_report_lines()
  404. self.assertEqual(len(lines['unaffected']), 1)
  405. # Check the initial and final balance
  406. self.assertEqual(lines['unaffected'].initial_debit, 1000)
  407. self.assertEqual(lines['unaffected'].initial_credit, 0)
  408. self.assertEqual(lines['unaffected'].initial_balance, 1000)
  409. self.assertEqual(lines['unaffected'].final_debit, 1000)
  410. self.assertEqual(lines['unaffected'].final_credit, 0)
  411. self.assertEqual(lines['unaffected'].final_balance, 1000)
  412. # Affect the company's result last year
  413. self._add_move(
  414. date=self.previous_fy_date_start,
  415. receivable_debit=1000,
  416. receivable_credit=0,
  417. income_debit=0,
  418. income_credit=0,
  419. unaffected_debit=0,
  420. unaffected_credit=1000
  421. )
  422. # Add another move last year to test the initial balance this year
  423. self._add_move(
  424. date=self.previous_fy_date_start,
  425. receivable_debit=0,
  426. receivable_credit=500,
  427. income_debit=500,
  428. income_credit=0,
  429. unaffected_debit=0,
  430. unaffected_credit=0
  431. )
  432. # Re Generate the general ledger line
  433. lines = self._get_report_lines()
  434. self.assertEqual(len(lines['unaffected']), 1)
  435. # Check the initial and final balance
  436. self.assertEqual(lines['unaffected'].initial_debit, 500)
  437. self.assertEqual(lines['unaffected'].initial_credit, 0)
  438. self.assertEqual(lines['unaffected'].initial_balance, 500)
  439. self.assertEqual(lines['unaffected'].final_debit, 500)
  440. self.assertEqual(lines['unaffected'].final_credit, 0)
  441. self.assertEqual(lines['unaffected'].final_balance, 500)
  442. def test_partner_filter(self):
  443. partner_1 = self.env.ref('base.res_partner_1')
  444. partner_2 = self.env.ref('base.res_partner_2')
  445. partner_3 = self.env.ref('base.res_partner_3')
  446. partner_4 = self.env.ref('base.res_partner_4')
  447. partner_1.write({'is_company': False,
  448. 'parent_id': partner_2.id})
  449. partner_3.write({'is_company': False})
  450. expected_list = [partner_2.id, partner_3.id, partner_4.id]
  451. context = {
  452. 'active_ids': [
  453. partner_1.id, partner_2.id, partner_3.id, partner_4.id
  454. ],
  455. 'active_model': 'res.partner'
  456. }
  457. wizard = self.env["general.ledger.report.wizard"].with_context(context)
  458. self.assertEqual(wizard._default_partners(), expected_list)
  459. def test_validate_date(self):
  460. company_id = self.env.ref('base.main_company')
  461. company_id.write({
  462. 'fiscalyear_last_day': 31,
  463. 'fiscalyear_last_month': 12,
  464. })
  465. user = self.env.ref('base.user_root').with_context(
  466. company_id=company_id.id)
  467. wizard = self.env["general.ledger.report.wizard"].with_context(
  468. user=user.id
  469. )
  470. self.assertEqual(wizard._init_date_from(),
  471. time.strftime('%Y') + '-01-01')
  472. def test_validate_date_range(self):
  473. data_type = self.env['date.range.type'].create({
  474. 'name': 'Fiscal year',
  475. 'company_id': False,
  476. 'allow_overlap': False
  477. })
  478. dr = self.env['date.range'].create({
  479. 'name': 'FS2015',
  480. 'date_start': '2018-01-01',
  481. 'date_end': '2018-12-31',
  482. 'type_id': data_type.id,
  483. })
  484. wizard = self.env["general.ledger.report.wizard"].create({
  485. 'date_range_id': dr.id})
  486. wizard.onchange_date_range_id()
  487. self.assertEqual(wizard.date_from, date(2018, 1, 1))
  488. self.assertEqual(wizard.date_to, date(2018, 12, 31))