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.

153 lines
6.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo.tests import common
  5. from odoo import fields
  6. @common.at_install(False)
  7. @common.post_install(True)
  8. class TestPartnerFinancialRisk(common.SavepointCase):
  9. @classmethod
  10. def setUpClass(cls):
  11. super(TestPartnerFinancialRisk, cls).setUpClass()
  12. cls.env.user.groups_id |= cls.env.ref(
  13. 'account.group_account_manager')
  14. type_revenue = cls.env.ref('account.data_account_type_revenue')
  15. type_receivable = cls.env.ref('account.data_account_type_receivable')
  16. tax_group_taxes = cls.env.ref('account.tax_group_taxes')
  17. cls.account_sale = cls.env['account.account'].create({
  18. 'name': 'Sale',
  19. 'code': 'XX_700',
  20. 'user_type_id': type_revenue.id,
  21. 'reconcile': True,
  22. })
  23. cls.account_customer = cls.env['account.account'].create({
  24. 'name': 'Customer',
  25. 'code': 'XX_430',
  26. 'user_type_id': type_receivable.id,
  27. 'reconcile': True,
  28. })
  29. cls.other_account_customer = cls.env['account.account'].create({
  30. 'name': 'Other Account Customer',
  31. 'code': 'XX_431',
  32. 'user_type_id': type_receivable.id,
  33. 'reconcile': True,
  34. })
  35. cls.partner = cls.env['res.partner'].create({
  36. 'name': 'Partner test',
  37. 'customer': True,
  38. 'property_account_receivable_id': cls.account_customer.id,
  39. })
  40. cls.invoice_address = cls.env['res.partner'].create({
  41. 'name': 'Partner test invoice',
  42. 'parent_id': cls.partner.id,
  43. 'type': 'invoice',
  44. })
  45. cls.journal_sale = cls.env['account.journal'].create({
  46. 'name': 'Test journal for sale',
  47. 'type': 'sale',
  48. 'code': 'TSALE',
  49. 'default_debit_account_id': cls.account_sale.id,
  50. 'default_credit_account_id': cls.account_sale.id,
  51. })
  52. cls.tax = cls.env['account.tax'].create({
  53. 'name': 'Tax for sale 10%',
  54. 'type_tax_use': 'sale',
  55. 'tax_group_id': tax_group_taxes.id,
  56. 'amount_type': 'percent',
  57. 'amount': 10.0,
  58. })
  59. cls.invoice = cls.env['account.invoice'].create({
  60. 'partner_id': cls.partner.id,
  61. 'account_id': cls.account_customer.id,
  62. 'type': 'out_invoice',
  63. 'journal_id': cls.journal_sale.id,
  64. 'payment_term_id': False,
  65. 'invoice_line_ids': [(0, 0, {
  66. 'name': 'Test product',
  67. 'account_id': cls.account_sale.id,
  68. 'price_unit': 50,
  69. 'quantity': 10,
  70. 'invoice_line_tax_ids': [(6, 0, [cls.tax.id])],
  71. })],
  72. })
  73. cls.env.user.lang = False
  74. def test_invoices(self):
  75. self.partner.risk_invoice_draft_include = True
  76. self.assertAlmostEqual(self.partner.risk_invoice_draft, 550.0)
  77. self.assertAlmostEqual(self.partner.risk_total, 550.0)
  78. self.invoice.action_invoice_open()
  79. self.assertAlmostEqual(self.partner.risk_invoice_draft, 0.0)
  80. line = self.invoice.move_id.line_ids.filtered(lambda x: x.debit != 0.0)
  81. line.date_maturity = '2017-01-01'
  82. self.partner.risk_invoice_unpaid_include = True
  83. self.assertAlmostEqual(self.partner.risk_total, 550.0)
  84. self.partner.credit_limit = 100.0
  85. self.assertTrue(self.partner.risk_exception)
  86. self.partner.credit_limit = 1100.0
  87. self.assertFalse(self.partner.risk_exception)
  88. self.partner.risk_invoice_unpaid_limit = 499.0
  89. self.assertTrue(self.partner.risk_exception)
  90. invoice2 = self.invoice.copy({'partner_id': self.invoice_address.id})
  91. self.assertAlmostEqual(self.partner.risk_invoice_draft, 550.0)
  92. self.assertAlmostEqual(self.partner.risk_invoice_unpaid, 550.0)
  93. wiz_dic = invoice2.action_invoice_open()
  94. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  95. self.assertEqual(wiz.exception_msg, "Financial risk exceeded.\n")
  96. self.partner.risk_invoice_unpaid_limit = 0.0
  97. self.assertFalse(self.partner.risk_exception)
  98. self.partner.risk_invoice_open_limit = 300.0
  99. invoice2.date_due = fields.Date.today()
  100. wiz_dic = invoice2.action_invoice_open()
  101. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  102. self.assertEqual(wiz.exception_msg,
  103. "This invoice exceeds the open invoices risk.\n")
  104. self.partner.risk_invoice_open_limit = 0.0
  105. self.partner.risk_invoice_draft_include = False
  106. self.partner.risk_invoice_open_include = True
  107. self.partner.credit_limit = 900.0
  108. wiz_dic = invoice2.action_invoice_open()
  109. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  110. self.assertEqual(wiz.exception_msg,
  111. "This invoice exceeds the financial risk.\n")
  112. self.assertAlmostEqual(self.partner.risk_invoice_open, 0.0)
  113. wiz.button_continue()
  114. self.assertAlmostEqual(self.partner.risk_invoice_open, 550.0)
  115. self.assertTrue(self.partner.risk_allow_edit)
  116. self.partner.process_unpaid_invoices()
  117. self.assertEqual(self.env['ir.config_parameter'].get_param(
  118. 'partner_financial_risk.last_check'),
  119. fields.Date.today())
  120. def test_other_account_amount(self):
  121. self.move = self.env['account.move'].create({
  122. 'journal_id': self.journal_sale.id,
  123. 'date': fields.Date.today(),
  124. 'line_ids': [
  125. (0, 0, {
  126. 'name': 'Debit line',
  127. 'partner_id': self.partner.id,
  128. 'account_id': self.other_account_customer.id,
  129. 'debit': 100,
  130. }),
  131. (0, 0, {
  132. 'name': 'Credit line',
  133. 'partner_id': self.partner.id,
  134. 'account_id': self.account_sale.id,
  135. 'credit': 100,
  136. }),
  137. ],
  138. })
  139. self.assertAlmostEqual(self.partner.risk_account_amount, 100.0)
  140. line = self.move.line_ids.filtered(lambda x: x.debit != 0.0)
  141. line.date_maturity = '2017-01-01'
  142. self.assertAlmostEqual(self.partner.risk_account_amount, 0.0)
  143. self.assertAlmostEqual(self.partner.risk_account_amount_unpaid, 100.0)
  144. def test_recompute_newid(self):
  145. """Computing risk shouldn't fail if record is a NewId."""
  146. new = self.env["res.partner"].new({"customer": True})
  147. new._compute_risk_invoice()