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.

416 lines
13 KiB

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