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.

364 lines
12 KiB

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