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.

468 lines
18 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 TestGeneralLedger(abstract_test.AbstractTest):
  8. """
  9. Technical tests for General Ledger Report.
  10. """
  11. def _getReportModel(self):
  12. return self.env['report_general_ledger']
  13. def _getQwebReportName(self):
  14. return 'account_financial_report.report_general_ledger_qweb'
  15. def _getXlsxReportName(self):
  16. return 'a_f_r.report_general_ledger_xlsx'
  17. def _getXlsxReportActionName(self):
  18. return 'account_financial_report.' \
  19. 'action_report_general_ledger_xlsx'
  20. def _getReportTitle(self):
  21. return 'Odoo'
  22. def _getBaseFilters(self):
  23. return {
  24. 'date_from': time.strftime('%Y-01-01'),
  25. 'date_to': time.strftime('%Y-12-31'),
  26. 'company_id': self.company.id,
  27. 'fy_start_date': time.strftime('%Y-01-01'),
  28. }
  29. def _getAdditionalFiltersToBeTested(self):
  30. return [
  31. {'only_posted_moves': True},
  32. {'hide_account_balance_at_0': True},
  33. {'centralize': True},
  34. {'only_posted_moves': True, 'hide_account_balance_at_0': True},
  35. {'only_posted_moves': True, 'centralize': True},
  36. {'hide_account_balance_at_0': True, 'centralize': True},
  37. {
  38. 'only_posted_moves': True,
  39. 'hide_account_balance_at_0': True,
  40. 'centralize': True
  41. },
  42. ]
  43. @common.at_install(False)
  44. @common.post_install(True)
  45. class TestGeneralLedgerReport(common.TransactionCase):
  46. def setUp(self):
  47. super(TestGeneralLedgerReport, self).setUp()
  48. self.before_previous_fy_year = '2014-05-05'
  49. self.previous_fy_date_start = '2015-01-01'
  50. self.previous_fy_date_end = '2015-12-31'
  51. self.fy_date_start = '2016-01-01'
  52. self.fy_date_end = '2016-12-31'
  53. self.receivable_account = self.env['account.account'].search([
  54. ('user_type_id.name', '=', 'Receivable')
  55. ], limit=1)
  56. self.income_account = self.env['account.account'].search([
  57. ('user_type_id.name', '=', 'Income')
  58. ], limit=1)
  59. self.unaffected_account = self.env['account.account'].search([
  60. (
  61. 'user_type_id',
  62. '=',
  63. self.env.ref('account.data_unaffected_earnings').id
  64. )], limit=1)
  65. def _add_move(
  66. self,
  67. date,
  68. receivable_debit,
  69. receivable_credit,
  70. income_debit,
  71. income_credit,
  72. unaffected_debit=0,
  73. unaffected_credit=0
  74. ):
  75. move_name = 'expense accrual'
  76. journal = self.env['account.journal'].search([
  77. ('code', '=', 'MISC')])
  78. partner = self.env.ref('base.res_partner_12')
  79. move_vals = {
  80. 'journal_id': journal.id,
  81. 'partner_id': partner.id,
  82. 'name': move_name,
  83. 'date': date,
  84. 'line_ids': [
  85. (0, 0, {
  86. 'name': move_name,
  87. 'debit': receivable_debit,
  88. 'credit': receivable_credit,
  89. 'account_id': self.receivable_account.id}),
  90. (0, 0, {
  91. 'name': move_name,
  92. 'debit': income_debit,
  93. 'credit': income_credit,
  94. 'account_id': self.income_account.id}),
  95. (0, 0, {
  96. 'name': move_name,
  97. 'debit': unaffected_debit,
  98. 'credit': unaffected_credit,
  99. 'account_id': self.unaffected_account.id}),
  100. ]}
  101. move = self.env['account.move'].create(move_vals)
  102. move.post()
  103. def _get_report_lines(self, with_partners=False):
  104. company = self.env.ref('base.main_company')
  105. general_ledger = self.env['report_general_ledger'].create({
  106. 'date_from': self.fy_date_start,
  107. 'date_to': self.fy_date_end,
  108. 'only_posted_moves': True,
  109. 'hide_account_balance_at_0': False,
  110. 'company_id': company.id,
  111. 'fy_start_date': self.fy_date_start,
  112. })
  113. general_ledger.compute_data_for_report(
  114. with_line_details=True, with_partners=with_partners
  115. )
  116. lines = {}
  117. report_account_model = self.env['report_general_ledger_account']
  118. lines['receivable'] = report_account_model.search([
  119. ('report_id', '=', general_ledger.id),
  120. ('account_id', '=', self.receivable_account.id),
  121. ])
  122. lines['income'] = report_account_model.search([
  123. ('report_id', '=', general_ledger.id),
  124. ('account_id', '=', self.income_account.id),
  125. ])
  126. lines['unaffected'] = report_account_model.search([
  127. ('report_id', '=', general_ledger.id),
  128. ('account_id', '=', self.unaffected_account.id),
  129. ])
  130. if with_partners:
  131. report_partner_model = self.env[
  132. 'report_general_ledger_partner'
  133. ]
  134. lines['partner_receivable'] = report_partner_model.search([
  135. ('report_account_id', '=', lines['receivable'].id),
  136. ('partner_id', '=', self.env.ref('base.res_partner_12').id),
  137. ])
  138. return lines
  139. def test_01_account_balance(self):
  140. # Generate the general ledger line
  141. lines = self._get_report_lines()
  142. self.assertEqual(len(lines['receivable']), 0)
  143. self.assertEqual(len(lines['income']), 0)
  144. # Add a move at the previous day of the first day of fiscal year
  145. # to check the initial balance
  146. self._add_move(
  147. date=self.previous_fy_date_end,
  148. receivable_debit=1000,
  149. receivable_credit=0,
  150. income_debit=0,
  151. income_credit=1000
  152. )
  153. # Re Generate the general ledger line
  154. lines = self._get_report_lines()
  155. self.assertEqual(len(lines['receivable']), 1)
  156. self.assertEqual(len(lines['income']), 0)
  157. # Check the initial and final balance
  158. self.assertEqual(lines['receivable'].initial_debit, 1000)
  159. self.assertEqual(lines['receivable'].initial_credit, 0)
  160. self.assertEqual(lines['receivable'].initial_balance, 1000)
  161. self.assertEqual(lines['receivable'].final_debit, 1000)
  162. self.assertEqual(lines['receivable'].final_credit, 0)
  163. self.assertEqual(lines['receivable'].final_balance, 1000)
  164. # Add reversale move of the initial move the first day of fiscal year
  165. # to check the first day of fiscal year is not used
  166. # to compute the initial balance
  167. self._add_move(
  168. date=self.fy_date_start,
  169. receivable_debit=0,
  170. receivable_credit=1000,
  171. income_debit=1000,
  172. income_credit=0
  173. )
  174. # Re Generate the general ledger line
  175. lines = self._get_report_lines()
  176. self.assertEqual(len(lines['receivable']), 1)
  177. self.assertEqual(len(lines['income']), 1)
  178. # Check the initial and final balance
  179. self.assertEqual(lines['receivable'].initial_debit, 1000)
  180. self.assertEqual(lines['receivable'].initial_credit, 0)
  181. self.assertEqual(lines['receivable'].initial_balance, 1000)
  182. self.assertEqual(lines['receivable'].final_debit, 1000)
  183. self.assertEqual(lines['receivable'].final_credit, 1000)
  184. self.assertEqual(lines['receivable'].final_balance, 0)
  185. self.assertEqual(lines['income'].initial_debit, 0)
  186. self.assertEqual(lines['income'].initial_credit, 0)
  187. self.assertEqual(lines['income'].initial_balance, 0)
  188. self.assertEqual(lines['income'].final_debit, 1000)
  189. self.assertEqual(lines['income'].final_credit, 0)
  190. self.assertEqual(lines['income'].final_balance, 1000)
  191. # Add another move at the end day of fiscal year
  192. # to check that it correctly used on report
  193. self._add_move(
  194. date=self.fy_date_end,
  195. receivable_debit=0,
  196. receivable_credit=1000,
  197. income_debit=1000,
  198. income_credit=0
  199. )
  200. # Re Generate the general ledger line
  201. lines = self._get_report_lines()
  202. self.assertEqual(len(lines['receivable']), 1)
  203. self.assertEqual(len(lines['income']), 1)
  204. # Check the initial and final balance
  205. self.assertEqual(lines['receivable'].initial_debit, 1000)
  206. self.assertEqual(lines['receivable'].initial_credit, 0)
  207. self.assertEqual(lines['receivable'].initial_balance, 1000)
  208. self.assertEqual(lines['receivable'].final_debit, 1000)
  209. self.assertEqual(lines['receivable'].final_credit, 2000)
  210. self.assertEqual(lines['receivable'].final_balance, -1000)
  211. self.assertEqual(lines['income'].initial_debit, 0)
  212. self.assertEqual(lines['income'].initial_credit, 0)
  213. self.assertEqual(lines['income'].initial_balance, 0)
  214. self.assertEqual(lines['income'].final_debit, 2000)
  215. self.assertEqual(lines['income'].final_credit, 0)
  216. self.assertEqual(lines['income'].final_balance, 2000)
  217. def test_02_partner_balance(self):
  218. # Generate the general ledger line
  219. lines = self._get_report_lines(with_partners=True)
  220. self.assertEqual(len(lines['partner_receivable']), 0)
  221. # Add a move at the previous day of the first day of fiscal year
  222. # to check the initial balance
  223. self._add_move(
  224. date=self.previous_fy_date_end,
  225. receivable_debit=1000,
  226. receivable_credit=0,
  227. income_debit=0,
  228. income_credit=1000
  229. )
  230. # Re Generate the general ledger line
  231. lines = self._get_report_lines(with_partners=True)
  232. self.assertEqual(len(lines['partner_receivable']), 1)
  233. # Check the initial and final balance
  234. self.assertEqual(lines['partner_receivable'].initial_debit, 1000)
  235. self.assertEqual(lines['partner_receivable'].initial_credit, 0)
  236. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  237. self.assertEqual(lines['partner_receivable'].final_debit, 1000)
  238. self.assertEqual(lines['partner_receivable'].final_credit, 0)
  239. self.assertEqual(lines['partner_receivable'].final_balance, 1000)
  240. # Add reversale move of the initial move the first day of fiscal year
  241. # to check the first day of fiscal year is not used
  242. # to compute the initial balance
  243. self._add_move(
  244. date=self.fy_date_start,
  245. receivable_debit=0,
  246. receivable_credit=1000,
  247. income_debit=1000,
  248. income_credit=0
  249. )
  250. # Re Generate the general ledger line
  251. lines = self._get_report_lines(with_partners=True)
  252. self.assertEqual(len(lines['partner_receivable']), 1)
  253. # Check the initial and final balance
  254. self.assertEqual(lines['partner_receivable'].initial_debit, 1000)
  255. self.assertEqual(lines['partner_receivable'].initial_credit, 0)
  256. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  257. self.assertEqual(lines['partner_receivable'].final_debit, 1000)
  258. self.assertEqual(lines['partner_receivable'].final_credit, 1000)
  259. self.assertEqual(lines['partner_receivable'].final_balance, 0)
  260. # Add another move at the end day of fiscal year
  261. # to check that it correctly used on report
  262. self._add_move(
  263. date=self.fy_date_end,
  264. receivable_debit=0,
  265. receivable_credit=1000,
  266. income_debit=1000,
  267. income_credit=0
  268. )
  269. # Re Generate the general ledger line
  270. lines = self._get_report_lines(with_partners=True)
  271. self.assertEqual(len(lines['partner_receivable']), 1)
  272. # Check the initial and final balance
  273. self.assertEqual(lines['partner_receivable'].initial_debit, 1000)
  274. self.assertEqual(lines['partner_receivable'].initial_credit, 0)
  275. self.assertEqual(lines['partner_receivable'].initial_balance, 1000)
  276. self.assertEqual(lines['partner_receivable'].final_debit, 1000)
  277. self.assertEqual(lines['partner_receivable'].final_credit, 2000)
  278. self.assertEqual(lines['partner_receivable'].final_balance, -1000)
  279. def test_03_unaffected_account_balance(self):
  280. # Generate the general ledger line
  281. lines = self._get_report_lines()
  282. self.assertEqual(len(lines['unaffected']), 1)
  283. # Check the initial and final balance
  284. self.assertEqual(lines['unaffected'].initial_debit, 0)
  285. self.assertEqual(lines['unaffected'].initial_credit, 0)
  286. self.assertEqual(lines['unaffected'].initial_balance, 0)
  287. self.assertEqual(lines['unaffected'].final_debit, 0)
  288. self.assertEqual(lines['unaffected'].final_credit, 0)
  289. self.assertEqual(lines['unaffected'].final_balance, 0)
  290. # Add a move at the previous day of the first day of fiscal year
  291. # to check the initial balance
  292. self._add_move(
  293. date=self.previous_fy_date_end,
  294. receivable_debit=1000,
  295. receivable_credit=0,
  296. income_debit=0,
  297. income_credit=1000
  298. )
  299. # Re Generate the general ledger line
  300. lines = self._get_report_lines()
  301. self.assertEqual(len(lines['unaffected']), 1)
  302. # Check the initial and final balance
  303. self.assertEqual(lines['unaffected'].initial_debit, 0)
  304. self.assertEqual(lines['unaffected'].initial_credit, 0)
  305. self.assertEqual(lines['unaffected'].initial_balance, -1000)
  306. self.assertEqual(lines['unaffected'].final_debit, -0)
  307. self.assertEqual(lines['unaffected'].final_credit, 0)
  308. self.assertEqual(lines['unaffected'].final_balance, -1000)
  309. # Add reversale move of the initial move the first day of fiscal year
  310. # to check the first day of fiscal year is not used
  311. # to compute the initial balance
  312. self._add_move(
  313. date=self.fy_date_start,
  314. receivable_debit=0,
  315. receivable_credit=0,
  316. income_debit=0,
  317. income_credit=1000,
  318. unaffected_debit=1000,
  319. unaffected_credit=0
  320. )
  321. # Re Generate the general ledger line
  322. lines = self._get_report_lines()
  323. self.assertEqual(len(lines['unaffected']), 1)
  324. # Check the initial and final balance
  325. self.assertEqual(lines['unaffected'].initial_debit, 0)
  326. self.assertEqual(lines['unaffected'].initial_credit, 0)
  327. self.assertEqual(lines['unaffected'].initial_balance, -1000)
  328. self.assertEqual(lines['unaffected'].final_debit, 1000)
  329. self.assertEqual(lines['unaffected'].final_credit, 0)
  330. self.assertEqual(lines['unaffected'].final_balance, 0)
  331. # Add another move at the end day of fiscal year
  332. # to check that it correctly used on report
  333. self._add_move(
  334. date=self.fy_date_end,
  335. receivable_debit=3000,
  336. receivable_credit=0,
  337. income_debit=0,
  338. income_credit=0,
  339. unaffected_debit=0,
  340. unaffected_credit=3000
  341. )
  342. # Re Generate the general ledger line
  343. lines = self._get_report_lines()
  344. self.assertEqual(len(lines['unaffected']), 1)
  345. # Check the initial and final balance
  346. self.assertEqual(lines['unaffected'].initial_debit, 0)
  347. self.assertEqual(lines['unaffected'].initial_credit, 0)
  348. self.assertEqual(lines['unaffected'].initial_balance, -1000)
  349. self.assertEqual(lines['unaffected'].final_debit, 1000)
  350. self.assertEqual(lines['unaffected'].final_credit, 3000)
  351. self.assertEqual(lines['unaffected'].final_balance, -3000)
  352. def test_04_unaffected_account_balance_2_years(self):
  353. # Generate the general ledger line
  354. lines = self._get_report_lines()
  355. self.assertEqual(len(lines['unaffected']), 1)
  356. # Check the initial and final balance
  357. self.assertEqual(lines['unaffected'].initial_debit, 0)
  358. self.assertEqual(lines['unaffected'].initial_credit, 0)
  359. self.assertEqual(lines['unaffected'].initial_balance, 0)
  360. self.assertEqual(lines['unaffected'].final_debit, 0)
  361. self.assertEqual(lines['unaffected'].final_credit, 0)
  362. self.assertEqual(lines['unaffected'].final_balance, 0)
  363. # Add a move at any date 2 years before the balance
  364. # (to create an historic)
  365. self._add_move(
  366. date=self.before_previous_fy_year,
  367. receivable_debit=0,
  368. receivable_credit=1000,
  369. income_debit=1000,
  370. income_credit=0
  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, 0)
  378. self.assertEqual(lines['unaffected'].initial_balance, 1000)
  379. self.assertEqual(lines['unaffected'].final_debit, 0)
  380. self.assertEqual(lines['unaffected'].final_credit, 0)
  381. self.assertEqual(lines['unaffected'].final_balance, 1000)
  382. # Affect the company's result last year
  383. self._add_move(
  384. date=self.previous_fy_date_start,
  385. receivable_debit=1000,
  386. receivable_credit=0,
  387. income_debit=0,
  388. income_credit=0,
  389. unaffected_debit=0,
  390. unaffected_credit=1000
  391. )
  392. # Add another move last year to test the initial balance this year
  393. self._add_move(
  394. date=self.previous_fy_date_start,
  395. receivable_debit=0,
  396. receivable_credit=500,
  397. income_debit=500,
  398. income_credit=0,
  399. unaffected_debit=0,
  400. unaffected_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, 0)
  407. self.assertEqual(lines['unaffected'].initial_credit, 0)
  408. self.assertEqual(lines['unaffected'].initial_balance, 500)
  409. self.assertEqual(lines['unaffected'].final_debit, 0)
  410. self.assertEqual(lines['unaffected'].final_credit, 0)
  411. self.assertEqual(lines['unaffected'].final_balance, 500)