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.

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