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.

271 lines
10 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
  8. from odoo.addons.account.tests.common import AccountTestInvoicingCommon
  9. class TestJournalReport(AccountTestInvoicingCommon):
  10. @classmethod
  11. def setUpClass(cls, chart_template_ref=None):
  12. super().setUpClass(chart_template_ref=chart_template_ref)
  13. cls.AccountObj = cls.env["account.account"]
  14. cls.InvoiceObj = cls.env["account.move"]
  15. cls.JournalObj = cls.env["account.journal"]
  16. cls.MoveObj = cls.env["account.move"]
  17. cls.TaxObj = cls.env["account.tax"]
  18. cls.JournalLedgerReportWizard = cls.env["journal.ledger.report.wizard"]
  19. cls.JournalLedgerReport = cls.env[
  20. "report.account_financial_report.journal_ledger"
  21. ]
  22. cls.company = cls.company_data["company"]
  23. cls.company.account_sale_tax_id = False
  24. cls.company.account_purchase_tax_id = False
  25. today = datetime.today()
  26. last_year = today - relativedelta(years=1)
  27. cls.previous_fy_date_start = Date.to_string(last_year.replace(month=1, day=1))
  28. cls.previous_fy_date_end = Date.to_string(last_year.replace(month=12, day=31))
  29. cls.fy_date_start = Date.to_string(today.replace(month=1, day=1))
  30. cls.fy_date_end = Date.to_string(today.replace(month=12, day=31))
  31. cls.receivable_account = cls.company_data["default_account_receivable"]
  32. cls.income_account = cls.company_data["default_account_revenue"]
  33. cls.expense_account = cls.company_data["default_account_expense"]
  34. cls.payable_account = cls.company_data["default_account_payable"]
  35. cls.journal_sale = cls.company_data["default_journal_sale"]
  36. cls.journal_purchase = cls.company_data["default_journal_purchase"]
  37. cls.tax_15_s = cls.company_data["default_tax_sale"]
  38. cls.tax_15_s.sequence = 30
  39. cls.tax_15_s.amount = 15.0
  40. cls.tax_15_s.amount_type = "percent"
  41. cls.tax_15_s.include_base_amount = False
  42. cls.tax_15_s.type_tax_use = "sale"
  43. cls.tax_20_s = cls.tax_15_s.copy(
  44. {
  45. "sequence": 30,
  46. "name": "Tax 20.0% (Percentage of Price)",
  47. "amount": 20.0,
  48. "amount_type": "percent",
  49. "include_base_amount": False,
  50. "type_tax_use": "sale",
  51. }
  52. )
  53. cls.tax_15_p = cls.company_data["default_tax_purchase"]
  54. cls.tax_15_p.sequence = 30
  55. cls.tax_15_p.amount = 15.0
  56. cls.tax_15_p.amount_type = "percent"
  57. cls.tax_15_p.include_base_amount = False
  58. cls.tax_15_p.type_tax_use = "purchase"
  59. cls.tax_20_p = cls.tax_15_p.copy(
  60. {
  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": "purchase",
  67. }
  68. )
  69. cls.partner_2 = cls.env.ref("base.res_partner_2")
  70. def _add_move(
  71. self,
  72. date,
  73. journal,
  74. receivable_debit,
  75. receivable_credit,
  76. income_debit,
  77. income_credit,
  78. ):
  79. move_name = "move name"
  80. move_vals = {
  81. "journal_id": journal.id,
  82. "date": date,
  83. "line_ids": [
  84. (
  85. 0,
  86. 0,
  87. {
  88. "name": move_name,
  89. "debit": receivable_debit,
  90. "credit": receivable_credit,
  91. "account_id": self.receivable_account.id,
  92. },
  93. ),
  94. (
  95. 0,
  96. 0,
  97. {
  98. "name": move_name,
  99. "debit": income_debit,
  100. "credit": income_credit,
  101. "account_id": self.income_account.id,
  102. },
  103. ),
  104. ],
  105. }
  106. return self.MoveObj.create(move_vals)
  107. def check_report_journal_debit_credit(
  108. self, res_data, expected_debit, expected_credit
  109. ):
  110. self.assertEqual(
  111. expected_debit, sum([rec["debit"] for rec in res_data["Journal_Ledgers"]])
  112. )
  113. self.assertEqual(
  114. expected_credit, sum([rec["credit"] for rec in res_data["Journal_Ledgers"]])
  115. )
  116. def check_report_journal_debit_credit_taxes(
  117. self,
  118. res_data,
  119. expected_base_debit,
  120. expected_base_credit,
  121. expected_tax_debit,
  122. expected_tax_credit,
  123. ):
  124. for rec in res_data["Journal_Ledgers"]:
  125. self.assertEqual(
  126. expected_base_debit,
  127. sum([tax_line["base_debit"] for tax_line in rec["tax_lines"]]),
  128. )
  129. self.assertEqual(
  130. expected_base_credit,
  131. sum([tax_line["base_credit"] for tax_line in rec["tax_lines"]]),
  132. )
  133. self.assertEqual(
  134. expected_tax_debit,
  135. sum([tax_line["tax_debit"] for tax_line in rec["tax_lines"]]),
  136. )
  137. self.assertEqual(
  138. expected_tax_credit,
  139. sum([tax_line["tax_credit"] for tax_line in rec["tax_lines"]]),
  140. )
  141. def test_01_test_total(self):
  142. today_date = Date.today()
  143. last_year_date = Date.to_string(datetime.today() - relativedelta(years=1))
  144. move1 = self._add_move(today_date, self.journal_sale, 0, 100, 100, 0)
  145. move2 = self._add_move(last_year_date, self.journal_sale, 0, 100, 100, 0)
  146. wiz = self.JournalLedgerReportWizard.create(
  147. {
  148. "date_from": self.fy_date_start,
  149. "date_to": self.fy_date_end,
  150. "company_id": self.company.id,
  151. "journal_ids": [(6, 0, self.journal_sale.ids)],
  152. "move_target": "all",
  153. }
  154. )
  155. data = wiz._prepare_report_journal_ledger()
  156. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  157. self.check_report_journal_debit_credit(res_data, 100, 100)
  158. move3 = self._add_move(today_date, self.journal_sale, 0, 100, 100, 0)
  159. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  160. self.check_report_journal_debit_credit(res_data, 200, 200)
  161. wiz.move_target = "posted"
  162. data = wiz._prepare_report_journal_ledger()
  163. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  164. self.check_report_journal_debit_credit(res_data, 0, 0)
  165. move1.action_post()
  166. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  167. self.check_report_journal_debit_credit(res_data, 100, 100)
  168. move2.action_post()
  169. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  170. self.check_report_journal_debit_credit(res_data, 100, 100)
  171. move3.action_post()
  172. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  173. self.check_report_journal_debit_credit(res_data, 200, 200)
  174. wiz.date_from = self.previous_fy_date_start
  175. data = wiz._prepare_report_journal_ledger()
  176. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  177. self.check_report_journal_debit_credit(res_data, 300, 300)
  178. def test_02_test_taxes_out_invoice(self):
  179. move_form = Form(
  180. self.env["account.move"].with_context(default_move_type="out_invoice")
  181. )
  182. move_form.partner_id = self.partner_2
  183. move_form.journal_id = self.journal_sale
  184. with move_form.invoice_line_ids.new() as line_form:
  185. line_form.name = "test"
  186. line_form.quantity = 1.0
  187. line_form.price_unit = 100
  188. line_form.account_id = self.income_account
  189. line_form.tax_ids.add(self.tax_15_s)
  190. with move_form.invoice_line_ids.new() as line_form:
  191. line_form.name = "test"
  192. line_form.quantity = 1.0
  193. line_form.price_unit = 100
  194. line_form.account_id = self.income_account
  195. line_form.tax_ids.add(self.tax_15_s)
  196. line_form.tax_ids.add(self.tax_20_s)
  197. invoice = move_form.save()
  198. invoice.action_post()
  199. wiz = self.JournalLedgerReportWizard.create(
  200. {
  201. "date_from": self.fy_date_start,
  202. "date_to": self.fy_date_end,
  203. "company_id": self.company.id,
  204. "journal_ids": [(6, 0, self.journal_sale.ids)],
  205. "move_target": "all",
  206. }
  207. )
  208. data = wiz._prepare_report_journal_ledger()
  209. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  210. self.check_report_journal_debit_credit(res_data, 250, 250)
  211. self.check_report_journal_debit_credit_taxes(res_data, 0, 300, 0, 50)
  212. def test_03_test_taxes_in_invoice(self):
  213. move_form = Form(
  214. self.env["account.move"].with_context(default_move_type="in_invoice")
  215. )
  216. move_form.partner_id = self.partner_2
  217. move_form.journal_id = self.journal_purchase
  218. move_form.invoice_date = Date.today()
  219. with move_form.invoice_line_ids.new() as line_form:
  220. line_form.name = "test"
  221. line_form.quantity = 1.0
  222. line_form.price_unit = 100
  223. line_form.account_id = self.expense_account
  224. line_form.tax_ids.add(self.tax_15_p)
  225. with move_form.invoice_line_ids.new() as line_form:
  226. line_form.name = "test"
  227. line_form.quantity = 1.0
  228. line_form.price_unit = 100
  229. line_form.account_id = self.expense_account
  230. line_form.tax_ids.add(self.tax_15_p)
  231. line_form.tax_ids.add(self.tax_20_p)
  232. move_form.invoice_date = move_form.date
  233. invoice = move_form.save()
  234. invoice.action_post()
  235. wiz = self.JournalLedgerReportWizard.create(
  236. {
  237. "date_from": self.fy_date_start,
  238. "date_to": self.fy_date_end,
  239. "company_id": self.company.id,
  240. "journal_ids": [(6, 0, self.journal_purchase.ids)],
  241. "move_target": "all",
  242. }
  243. )
  244. data = wiz._prepare_report_journal_ledger()
  245. res_data = self.JournalLedgerReport._get_report_values(wiz, data)
  246. self.check_report_journal_debit_credit(res_data, 250, 250)
  247. self.check_report_journal_debit_credit_taxes(res_data, 300, 0, 50, 0)