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.

469 lines
18 KiB

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