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.

366 lines
12 KiB

  1. # Copyright 2017 ACSONE SA/NV
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import time
  4. from datetime import datetime
  5. from dateutil.relativedelta import relativedelta
  6. from odoo.fields import Date
  7. from odoo.tests.common import TransactionCase
  8. from . import abstract_test_foreign_currency as a_t_f_c
  9. class TestJournalLedger(a_t_f_c.AbstractTestForeignCurrency):
  10. """
  11. Technical tests for General Ledger Report.
  12. """
  13. def _getReportModel(self):
  14. return self.env['report_journal_ledger']
  15. def _getQwebReportName(self):
  16. return 'account_financial_report.report_journal_ledger_qweb'
  17. def _getXlsxReportName(self):
  18. return 'a_f_r.report_journal_ledger_xlsx'
  19. def _getXlsxReportActionName(self):
  20. return 'account_financial_report.' \
  21. 'action_report_journal_ledger_xlsx'
  22. def _getReportTitle(self):
  23. return 'Odoo'
  24. def _getBaseFilters(self):
  25. return {
  26. 'date_from': time.strftime('%Y-01-01'),
  27. 'date_to': time.strftime('%Y-12-31'),
  28. 'company_id': self.company.id,
  29. 'journal_ids': [(6, 0, self.journal_sale.ids)]
  30. }
  31. def _getAdditionalFiltersToBeTested(self):
  32. return [
  33. {'move_target': "All",
  34. 'sort_option': "Date",
  35. 'group_option': "Journal",
  36. 'with_account_name': True,
  37. 'foreign_currency': True},
  38. ]
  39. def test_02_generation_report_html(self):
  40. """Check if report HTML is correctly generated"""
  41. # Check if returned report action is correct
  42. report_type = 'qweb-html'
  43. report_action = self.report.print_report(report_type)
  44. self.assertDictContainsSubset(
  45. {
  46. 'type': 'ir.actions.report',
  47. 'report_name': self.qweb_report_name,
  48. 'report_type': 'qweb-html',
  49. },
  50. report_action
  51. )
  52. # Check if report template is correct
  53. report = self.env['ir.actions.report'].search(
  54. [('report_name', '=', self.qweb_report_name),
  55. ('report_type', '=', report_type)], limit=1)
  56. self.assertEqual(report.report_type, 'qweb-html')
  57. rep = report.render(self.report.ids, {})
  58. self.assertTrue(self.report_title.encode('utf8') in rep[0])
  59. self.assertTrue(
  60. self.report.journal_ids[0].name.encode('utf8') in rep[0]
  61. )
  62. def test_04_compute_data(self):
  63. return True
  64. class TestJournalReport(TransactionCase):
  65. def setUp(self):
  66. super(TestJournalReport, self).setUp()
  67. self.AccountObj = self.env['account.account']
  68. self.InvoiceObj = self.env['account.invoice']
  69. self.JournalObj = self.env['account.journal']
  70. self.JournalReportObj = self.env['journal.ledger.report.wizard']
  71. self.MoveObj = self.env['account.move']
  72. self.ReportJournalLedger = self.env['report_journal_ledger']
  73. self.TaxObj = self.env['account.tax']
  74. self.company = self.env.ref('base.main_company')
  75. today = datetime.today()
  76. last_year = today - relativedelta(years=1)
  77. self.previous_fy_date_start = Date.to_string(
  78. last_year.replace(month=1, day=1))
  79. self.previous_fy_date_end = Date.to_string(
  80. last_year.replace(month=12, day=31))
  81. self.fy_date_start = Date.to_string(
  82. today.replace(month=1, day=1))
  83. self.fy_date_end = Date.to_string(
  84. today.replace(month=12, day=31))
  85. self.receivable_account = self.AccountObj.search([
  86. ('user_type_id.name', '=', 'Receivable')
  87. ], limit=1)
  88. self.income_account = self.AccountObj.search([
  89. ('user_type_id.name', '=', 'Income')
  90. ], limit=1)
  91. self.payable_account = self.AccountObj.search([
  92. ('user_type_id.name', '=', 'Payable')
  93. ], limit=1)
  94. self.journal_sale = self.JournalObj.create({
  95. 'name': "Test journal sale",
  96. 'code': "TST-JRNL-S",
  97. 'type': 'sale',
  98. 'company_id': self.company.id,
  99. })
  100. self.journal_purchase = self.JournalObj.create({
  101. 'name': "Test journal purchase",
  102. 'code': "TST-JRNL-P",
  103. 'type': 'sale',
  104. 'company_id': self.company.id,
  105. })
  106. self.tax_15_s = self.TaxObj.create({
  107. 'sequence': 30,
  108. 'name': 'Tax 15.0% (Percentage of Price)',
  109. 'amount': 15.0,
  110. 'amount_type': 'percent',
  111. 'include_base_amount': False,
  112. 'type_tax_use': 'sale',
  113. })
  114. self.tax_20_s = self.TaxObj.create({
  115. 'sequence': 30,
  116. 'name': 'Tax 20.0% (Percentage of Price)',
  117. 'amount': 20.0,
  118. 'amount_type': 'percent',
  119. 'include_base_amount': False,
  120. 'type_tax_use': 'sale',
  121. })
  122. self.tax_15_p = self.TaxObj.create({
  123. 'sequence': 30,
  124. 'name': 'Tax 15.0% (Percentage of Price)',
  125. 'amount': 15.0,
  126. 'amount_type': 'percent',
  127. 'include_base_amount': False,
  128. 'type_tax_use': 'purchase',
  129. })
  130. self.tax_20_p = self.TaxObj.create({
  131. 'sequence': 30,
  132. 'name': 'Tax 20.0% (Percentage of Price)',
  133. 'amount': 20.0,
  134. 'amount_type': 'percent',
  135. 'include_base_amount': False,
  136. 'type_tax_use': 'purchase',
  137. })
  138. self.partner_2 = self.env.ref('base.res_partner_2')
  139. def _add_move(
  140. self, date, journal,
  141. receivable_debit, receivable_credit, income_debit, income_credit):
  142. move_name = 'move name'
  143. move_vals = {
  144. 'journal_id': journal.id,
  145. 'date': date,
  146. 'line_ids': [
  147. (0, 0, {
  148. 'name': move_name,
  149. 'debit': receivable_debit,
  150. 'credit': receivable_credit,
  151. 'account_id': self.receivable_account.id
  152. }),
  153. (0, 0, {
  154. 'name': move_name,
  155. 'debit': income_debit,
  156. 'credit': income_credit,
  157. 'account_id': self.income_account.id
  158. }),
  159. ]
  160. }
  161. return self.MoveObj.create(move_vals)
  162. def check_report_journal_debit_credit(
  163. self, report, expected_debit, expected_credit):
  164. self.assertEqual(
  165. expected_debit,
  166. sum([journal.debit for journal in
  167. report.report_journal_ledger_ids])
  168. )
  169. self.assertEqual(
  170. expected_credit,
  171. sum([journal.credit for journal in
  172. report.report_journal_ledger_ids])
  173. )
  174. def check_report_journal_debit_credit_taxes(
  175. self, report,
  176. expected_base_debit, expected_base_credit,
  177. expected_tax_debit, expected_tax_credit):
  178. self.assertEqual(
  179. expected_base_debit,
  180. sum([
  181. journal.base_debit
  182. for journal in report.report_journal_ledger_tax_line_ids
  183. ])
  184. )
  185. self.assertEqual(
  186. expected_base_credit,
  187. sum([
  188. journal.base_credit
  189. for journal in report.report_journal_ledger_tax_line_ids
  190. ])
  191. )
  192. self.assertEqual(
  193. expected_tax_debit,
  194. sum([
  195. journal.tax_debit
  196. for journal in report.report_journal_ledger_tax_line_ids
  197. ])
  198. )
  199. self.assertEqual(
  200. expected_tax_credit,
  201. sum([
  202. journal.tax_credit
  203. for journal in report.report_journal_ledger_tax_line_ids
  204. ])
  205. )
  206. def test_01_test_total(self):
  207. today_date = Date.today()
  208. last_year_date = Date.to_string(
  209. datetime.today() - relativedelta(years=1))
  210. move1 = self._add_move(
  211. today_date, self.journal_sale,
  212. 0, 100, 100, 0)
  213. move2 = self._add_move(
  214. last_year_date, self.journal_sale,
  215. 0, 100, 100, 0)
  216. report = self.ReportJournalLedger.create({
  217. 'date_from': self.fy_date_start,
  218. 'date_to': self.fy_date_end,
  219. 'company_id': self.company.id,
  220. 'journal_ids': [(6, 0, self.journal_sale.ids)]
  221. })
  222. report.compute_data_for_report()
  223. self.check_report_journal_debit_credit(report, 100, 100)
  224. move3 = self._add_move(
  225. today_date, self.journal_sale,
  226. 0, 100, 100, 0)
  227. report.compute_data_for_report()
  228. self.check_report_journal_debit_credit(report, 200, 200)
  229. report.move_target = 'posted'
  230. report.compute_data_for_report()
  231. self.check_report_journal_debit_credit(report, 0, 0)
  232. move1.post()
  233. report.compute_data_for_report()
  234. self.check_report_journal_debit_credit(report, 100, 100)
  235. move2.post()
  236. report.compute_data_for_report()
  237. self.check_report_journal_debit_credit(report, 100, 100)
  238. move3.post()
  239. report.compute_data_for_report()
  240. self.check_report_journal_debit_credit(report, 200, 200)
  241. report.date_from = self.previous_fy_date_start
  242. report.compute_data_for_report()
  243. self.check_report_journal_debit_credit(report, 300, 300)
  244. def test_02_test_taxes_out_invoice(self):
  245. invoice_values = {
  246. 'journal_id': self.journal_sale.id,
  247. 'partner_id': self.partner_2.id,
  248. 'type': 'out_invoice',
  249. 'invoice_line_ids': [
  250. (0, 0, {
  251. 'quantity': 1.0,
  252. 'price_unit': 100,
  253. 'account_id': self.receivable_account.id,
  254. 'name': "Test",
  255. 'invoice_line_tax_ids': [(6, 0, [self.tax_15_s.id])],
  256. }),
  257. (0, 0, {
  258. 'quantity': 1.0,
  259. 'price_unit': 100,
  260. 'account_id': self.receivable_account.id,
  261. 'name': "Test",
  262. 'invoice_line_tax_ids': [(6, 0, [
  263. self.tax_15_s.id, self.tax_20_s.id
  264. ])],
  265. })
  266. ]
  267. }
  268. invoice = self.InvoiceObj.create(invoice_values)
  269. invoice.action_invoice_open()
  270. report = self.ReportJournalLedger.create({
  271. 'date_from': self.fy_date_start,
  272. 'date_to': self.fy_date_end,
  273. 'company_id': self.company.id,
  274. 'journal_ids': [(6, 0, self.journal_sale.ids)]
  275. })
  276. report.compute_data_for_report()
  277. self.check_report_journal_debit_credit(report, 250, 250)
  278. self.check_report_journal_debit_credit_taxes(report, 0, 300, 0, 50)
  279. def test_03_test_taxes_in_invoice(self):
  280. invoice_values = {
  281. 'journal_id': self.journal_sale.id,
  282. 'partner_id': self.partner_2.id,
  283. 'type': 'in_invoice',
  284. 'invoice_line_ids': [
  285. (0, 0, {
  286. 'quantity': 1.0,
  287. 'price_unit': 100,
  288. 'account_id': self.payable_account.id,
  289. 'name': "Test",
  290. 'invoice_line_tax_ids': [(6, 0, [self.tax_15_p.id])],
  291. }),
  292. (0, 0, {
  293. 'quantity': 1.0,
  294. 'price_unit': 100,
  295. 'account_id': self.payable_account.id,
  296. 'name': "Test",
  297. 'invoice_line_tax_ids': [(6, 0, [
  298. self.tax_15_p.id, self.tax_20_p.id
  299. ])],
  300. })
  301. ]
  302. }
  303. invoice = self.InvoiceObj.create(invoice_values)
  304. invoice.action_invoice_open()
  305. report = self.ReportJournalLedger.create({
  306. 'date_from': self.fy_date_start,
  307. 'date_to': self.fy_date_end,
  308. 'company_id': self.company.id,
  309. 'journal_ids': [(6, 0, self.journal_sale.ids)]
  310. })
  311. report.compute_data_for_report()
  312. self.check_report_journal_debit_credit(report, 250, 250)
  313. self.check_report_journal_debit_credit_taxes(report, 300, 0, 50, 0)