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.

297 lines
11 KiB

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