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.

296 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. })
  167. data = wiz._prepare_report_journal_ledger()
  168. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  169. self.check_report_journal_debit_credit(res_data, 100, 100)
  170. move3 = self._add_move(
  171. today_date, self.journal_sale,
  172. 0, 100, 100, 0)
  173. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  174. self.check_report_journal_debit_credit(res_data, 200, 200)
  175. wiz.move_target = 'posted'
  176. data = wiz._prepare_report_journal_ledger()
  177. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  178. self.check_report_journal_debit_credit(res_data, 0, 0)
  179. move1.post()
  180. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  181. self.check_report_journal_debit_credit(res_data, 100, 100)
  182. move2.post()
  183. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  184. self.check_report_journal_debit_credit(res_data, 100, 100)
  185. move3.post()
  186. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  187. self.check_report_journal_debit_credit(res_data, 200, 200)
  188. wiz.date_from = self.previous_fy_date_start
  189. data = wiz._prepare_report_journal_ledger()
  190. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  191. self.check_report_journal_debit_credit(res_data, 300, 300)
  192. def test_02_test_taxes_out_invoice(self):
  193. invoice_values = {
  194. 'journal_id': self.journal_sale.id,
  195. 'partner_id': self.partner_2.id,
  196. 'type': 'out_invoice',
  197. 'invoice_line_ids': [
  198. (0, 0, {
  199. 'quantity': 1.0,
  200. 'price_unit': 100,
  201. 'account_id': self.receivable_account.id,
  202. 'name': "Test",
  203. 'invoice_line_tax_ids': [(6, 0, [self.tax_15_s.id])],
  204. }),
  205. (0, 0, {
  206. 'quantity': 1.0,
  207. 'price_unit': 100,
  208. 'account_id': self.receivable_account.id,
  209. 'name': "Test",
  210. 'invoice_line_tax_ids': [(6, 0, [
  211. self.tax_15_s.id, self.tax_20_s.id
  212. ])],
  213. })
  214. ]
  215. }
  216. invoice = self.InvoiceObj.create(invoice_values)
  217. invoice.action_invoice_open()
  218. wiz = self.JournalLedgerReportWizard.create({
  219. 'date_from': self.fy_date_start,
  220. 'date_to': self.fy_date_end,
  221. 'company_id': self.company.id,
  222. 'journal_ids': [(6, 0, self.journal_sale.ids)]
  223. })
  224. data = wiz._prepare_report_journal_ledger()
  225. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  226. self.check_report_journal_debit_credit(res_data, 250, 250)
  227. self.check_report_journal_debit_credit_taxes(res_data, 0, 300, 0, 50)
  228. def test_03_test_taxes_in_invoice(self):
  229. invoice_values = {
  230. 'journal_id': self.journal_sale.id,
  231. 'partner_id': self.partner_2.id,
  232. 'type': 'in_invoice',
  233. 'invoice_line_ids': [
  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, [self.tax_15_p.id])],
  240. }),
  241. (0, 0, {
  242. 'quantity': 1.0,
  243. 'price_unit': 100,
  244. 'account_id': self.payable_account.id,
  245. 'name': "Test",
  246. 'invoice_line_tax_ids': [(6, 0, [
  247. self.tax_15_p.id, self.tax_20_p.id
  248. ])],
  249. })
  250. ]
  251. }
  252. invoice = self.InvoiceObj.create(invoice_values)
  253. invoice.action_invoice_open()
  254. wiz = self.JournalLedgerReportWizard.create({
  255. 'date_from': self.fy_date_start,
  256. 'date_to': self.fy_date_end,
  257. 'company_id': self.company.id,
  258. 'journal_ids': [(6, 0, self.journal_sale.ids)]
  259. })
  260. data = wiz._prepare_report_journal_ledger()
  261. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  262. self.check_report_journal_debit_credit(res_data, 250, 250)
  263. self.check_report_journal_debit_credit_taxes(res_data, 300, 0, 50, 0)