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.

292 lines
9.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 ACSONE SA/NV
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  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. class TestJournalReport(TransactionCase):
  9. def setUp(self):
  10. super(TestJournalReport, self).setUp()
  11. self.AccountObj = self.env['account.account']
  12. self.InvoiceObj = self.env['account.invoice']
  13. self.JournalObj = self.env['account.journal']
  14. self.JournalReportObj = self.env['journal.report.wizard']
  15. self.MoveObj = self.env['account.move']
  16. self.ReportJournalQweb = self.env['report_journal_qweb']
  17. self.TaxObj = self.env['account.tax']
  18. self.company = self.env.ref('base.main_company')
  19. today = datetime.today()
  20. last_year = today - relativedelta(years=1)
  21. self.previous_fy_date_start = Date.to_string(
  22. last_year.replace(month=1, day=1))
  23. self.previous_fy_date_end = Date.to_string(
  24. last_year.replace(month=12, day=31))
  25. self.fy_date_start = Date.to_string(
  26. today.replace(month=1, day=1))
  27. self.fy_date_end = Date.to_string(
  28. today.replace(month=12, day=31))
  29. self.receivable_account = self.AccountObj.search([
  30. ('user_type_id.name', '=', 'Receivable')
  31. ], limit=1)
  32. self.income_account = self.AccountObj.search([
  33. ('user_type_id.name', '=', 'Income')
  34. ], limit=1)
  35. self.payable_account = self.AccountObj.search([
  36. ('user_type_id.name', '=', 'Payable')
  37. ], limit=1)
  38. self.journal_sale = self.JournalObj.create({
  39. 'name': "Test journal sale",
  40. 'code': "TST-JRNL-S",
  41. 'type': 'sale',
  42. 'company_id': self.company.id,
  43. })
  44. self.journal_purchase = self.JournalObj.create({
  45. 'name': "Test journal purchase",
  46. 'code': "TST-JRNL-P",
  47. 'type': 'sale',
  48. 'company_id': self.company.id,
  49. })
  50. self.tax_15_s = self.TaxObj.create({
  51. 'sequence': 30,
  52. 'name': 'Tax 15.0% (Percentage of Price)',
  53. 'amount': 15.0,
  54. 'amount_type': 'percent',
  55. 'include_base_amount': False,
  56. 'type_tax_use': 'sale',
  57. })
  58. self.tax_20_s = self.TaxObj.create({
  59. 'sequence': 30,
  60. 'name': 'Tax 20.0% (Percentage of Price)',
  61. 'amount': 20.0,
  62. 'amount_type': 'percent',
  63. 'include_base_amount': False,
  64. 'type_tax_use': 'sale',
  65. })
  66. self.tax_15_p = self.TaxObj.create({
  67. 'sequence': 30,
  68. 'name': 'Tax 15.0% (Percentage of Price)',
  69. 'amount': 15.0,
  70. 'amount_type': 'percent',
  71. 'include_base_amount': False,
  72. 'type_tax_use': 'purchase',
  73. })
  74. self.tax_20_p = self.TaxObj.create({
  75. 'sequence': 30,
  76. 'name': 'Tax 20.0% (Percentage of Price)',
  77. 'amount': 20.0,
  78. 'amount_type': 'percent',
  79. 'include_base_amount': False,
  80. 'type_tax_use': 'purchase',
  81. })
  82. self.partner_2 = self.env.ref('base.res_partner_2')
  83. def _add_move(
  84. self, date, journal,
  85. receivable_debit, receivable_credit, income_debit, income_credit):
  86. move_name = 'move name'
  87. move_vals = {
  88. 'journal_id': journal.id,
  89. 'date': date,
  90. 'line_ids': [
  91. (0, 0, {
  92. 'name': move_name,
  93. 'debit': receivable_debit,
  94. 'credit': receivable_credit,
  95. 'account_id': self.receivable_account.id
  96. }),
  97. (0, 0, {
  98. 'name': move_name,
  99. 'debit': income_debit,
  100. 'credit': income_credit,
  101. 'account_id': self.income_account.id
  102. }),
  103. ]
  104. }
  105. return self.MoveObj.create(move_vals)
  106. def check_report_journal_debit_credit(
  107. self, report, expected_debit, expected_credit):
  108. self.assertEqual(
  109. expected_debit,
  110. sum([journal.debit for journal in report.report_journal_ids])
  111. )
  112. self.assertEqual(
  113. expected_credit,
  114. sum([journal.credit for journal in report.report_journal_ids])
  115. )
  116. def check_report_journal_debit_credit_taxes(
  117. self, report,
  118. expected_base_debit, expected_base_credit,
  119. expected_tax_debit, expected_tax_credit):
  120. self.assertEqual(
  121. expected_base_debit,
  122. sum([
  123. journal.base_debit
  124. for journal in report.report_journal_tax_line_ids
  125. ])
  126. )
  127. self.assertEqual(
  128. expected_base_credit,
  129. sum([
  130. journal.base_credit
  131. for journal in report.report_journal_tax_line_ids
  132. ])
  133. )
  134. self.assertEqual(
  135. expected_tax_debit,
  136. sum([
  137. journal.tax_debit
  138. for journal in report.report_journal_tax_line_ids
  139. ])
  140. )
  141. self.assertEqual(
  142. expected_tax_credit,
  143. sum([
  144. journal.tax_credit
  145. for journal in report.report_journal_tax_line_ids
  146. ])
  147. )
  148. def test_01_test_total(self):
  149. today_date = Date.today()
  150. last_year_date = Date.to_string(
  151. datetime.today() - relativedelta(years=1))
  152. move1 = self._add_move(
  153. today_date, self.journal_sale,
  154. 0, 100, 100, 0)
  155. move2 = self._add_move(
  156. last_year_date, self.journal_sale,
  157. 0, 100, 100, 0)
  158. report = self.ReportJournalQweb.create({
  159. 'date_from': self.fy_date_start,
  160. 'date_to': self.fy_date_end,
  161. 'company_id': self.company.id,
  162. 'journal_ids': [(6, 0, [self.journal_sale.ids])]
  163. })
  164. report.compute_data_for_report()
  165. self.check_report_journal_debit_credit(report, 100, 100)
  166. move3 = self._add_move(
  167. today_date, self.journal_sale,
  168. 0, 100, 100, 0)
  169. report.refresh()
  170. self.check_report_journal_debit_credit(report, 200, 200)
  171. report.move_target = 'posted'
  172. report.refresh()
  173. self.check_report_journal_debit_credit(report, 0, 0)
  174. move1.post()
  175. report.refresh()
  176. self.check_report_journal_debit_credit(report, 100, 100)
  177. move2.post()
  178. report.refresh()
  179. self.check_report_journal_debit_credit(report, 100, 100)
  180. move3.post()
  181. report.refresh()
  182. self.check_report_journal_debit_credit(report, 200, 200)
  183. report.date_from = self.previous_fy_date_start
  184. report.refresh()
  185. self.check_report_journal_debit_credit(report, 300, 300)
  186. def test_02_test_taxes_out_invoice(self):
  187. invoice_values = {
  188. 'journal_id': self.journal_sale.id,
  189. 'partner_id': self.partner_2.id,
  190. 'type': 'out_invoice',
  191. 'invoice_line_ids': [
  192. (0, 0, {
  193. 'quantity': 1.0,
  194. 'price_unit': 100,
  195. 'account_id': self.receivable_account.id,
  196. 'name': "Test",
  197. 'invoice_line_tax_ids': [(6, 0, [self.tax_15_s.id])],
  198. }),
  199. (0, 0, {
  200. 'quantity': 1.0,
  201. 'price_unit': 100,
  202. 'account_id': self.receivable_account.id,
  203. 'name': "Test",
  204. 'invoice_line_tax_ids': [(6, 0, [
  205. self.tax_15_s.id, self.tax_20_s.id
  206. ])],
  207. })
  208. ]
  209. }
  210. invoice = self.InvoiceObj.create(invoice_values)
  211. invoice.action_invoice_open()
  212. report = self.ReportJournalQweb.create({
  213. 'date_from': self.fy_date_start,
  214. 'date_to': self.fy_date_end,
  215. 'company_id': self.company.id,
  216. 'journal_ids': [(6, 0, [self.journal_sale.ids])]
  217. })
  218. report.compute_data_for_report()
  219. self.check_report_journal_debit_credit(report, 250, 250)
  220. self.check_report_journal_debit_credit_taxes(report, 0, 300, 0, 50)
  221. def test_03_test_taxes_in_invoice(self):
  222. invoice_values = {
  223. 'journal_id': self.journal_sale.id,
  224. 'partner_id': self.partner_2.id,
  225. 'type': 'in_invoice',
  226. 'invoice_line_ids': [
  227. (0, 0, {
  228. 'quantity': 1.0,
  229. 'price_unit': 100,
  230. 'account_id': self.payable_account.id,
  231. 'name': "Test",
  232. 'invoice_line_tax_ids': [(6, 0, [self.tax_15_p.id])],
  233. }),
  234. (0, 0, {
  235. 'quantity': 1.0,
  236. 'price_unit': 100,
  237. 'account_id': self.payable_account.id,
  238. 'name': "Test",
  239. 'invoice_line_tax_ids': [(6, 0, [
  240. self.tax_15_p.id, self.tax_20_p.id
  241. ])],
  242. })
  243. ]
  244. }
  245. invoice = self.InvoiceObj.create(invoice_values)
  246. invoice.action_invoice_open()
  247. report = self.ReportJournalQweb.create({
  248. 'date_from': self.fy_date_start,
  249. 'date_to': self.fy_date_end,
  250. 'company_id': self.company.id,
  251. 'journal_ids': [(6, 0, [self.journal_sale.ids])]
  252. })
  253. report.compute_data_for_report()
  254. self.check_report_journal_debit_credit(report, 250, 250)
  255. self.check_report_journal_debit_credit_taxes(report, 300, 0, 50, 0)