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.

511 lines
20 KiB

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