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.

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