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.

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