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.

112 lines
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo.tests.common import SavepointCase
  5. from odoo import fields
  6. class TestPartnerFinancialRisk(SavepointCase):
  7. @classmethod
  8. def setUpClass(cls):
  9. super(TestPartnerFinancialRisk, cls).setUpClass()
  10. cls.env.user.groups_id |= cls.env.ref('sales_team.group_sale_manager')
  11. cls.partner = cls.env['res.partner'].create({
  12. 'name': 'Partner test',
  13. 'customer': True,
  14. })
  15. cls.invoice_address = cls.env['res.partner'].create({
  16. 'name': 'Partner test invoice',
  17. 'parent_id': cls.partner.id,
  18. 'type': 'invoice',
  19. })
  20. type_revenue = cls.env.ref('account.data_account_type_revenue')
  21. type_payable = cls.env.ref('account.data_account_type_payable')
  22. tax_group_taxes = cls.env.ref('account.tax_group_taxes')
  23. cls.account_sale = cls.env['account.account'].create({
  24. 'name': 'Sale',
  25. 'code': 'XX_700',
  26. 'user_type_id': type_revenue.id,
  27. 'reconcile': True,
  28. })
  29. cls.account_customer = cls.env['account.account'].create({
  30. 'name': 'Customer',
  31. 'code': 'XX_430',
  32. 'user_type_id': type_payable.id,
  33. 'reconcile': True,
  34. })
  35. cls.partner.property_account_payable_id = cls.account_customer.id
  36. cls.journal_sale = cls.env['account.journal'].create({
  37. 'name': 'Test journal for sale',
  38. 'type': 'sale',
  39. 'code': 'TSALE',
  40. 'default_debit_account_id': cls.account_sale.id,
  41. 'default_credit_account_id': cls.account_sale.id,
  42. })
  43. cls.tax = cls.env['account.tax'].create({
  44. 'name': 'Tax for sale 10%',
  45. 'type_tax_use': 'sale',
  46. 'tax_group_id': tax_group_taxes.id,
  47. 'amount_type': 'percent',
  48. 'amount': 10.0,
  49. })
  50. cls.invoice = cls.env['account.invoice'].create({
  51. 'partner_id': cls.partner.id,
  52. 'account_id': cls.account_customer.id,
  53. 'type': 'out_invoice',
  54. 'journal_id': cls.journal_sale.id,
  55. 'payment_term_id': False,
  56. 'invoice_line_ids': [(0, 0, {
  57. 'name': 'Test product',
  58. 'account_id': cls.account_sale.id,
  59. 'price_unit': 50,
  60. 'quantity': 10,
  61. 'invoice_line_tax_ids': [(6, 0, [cls.tax.id])],
  62. })],
  63. })
  64. def test_invoices(self):
  65. self.partner.risk_invoice_draft_include = True
  66. self.assertAlmostEqual(self.partner.risk_invoice_draft, 550.0)
  67. self.assertAlmostEqual(self.partner.risk_total, 550.0)
  68. self.invoice.action_invoice_open()
  69. self.assertAlmostEqual(self.partner.risk_invoice_draft, 0.0)
  70. self.assertFalse(self.invoice.date_due)
  71. self.partner.risk_invoice_unpaid_include = True
  72. self.assertAlmostEqual(self.partner.risk_total, 550.0)
  73. self.partner.credit_limit = 100.0
  74. self.assertTrue(self.partner.risk_exception)
  75. self.partner.credit_limit = 1100.0
  76. self.assertFalse(self.partner.risk_exception)
  77. self.partner.risk_invoice_unpaid_limit = 499.0
  78. self.assertTrue(self.partner.risk_exception)
  79. invoice2 = self.invoice.copy({'partner_id': self.invoice_address.id})
  80. self.assertAlmostEqual(self.partner.risk_invoice_draft, 550.0)
  81. self.assertAlmostEqual(self.partner.risk_invoice_unpaid, 550.0)
  82. wiz_dic = invoice2.action_invoice_open()
  83. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  84. self.assertEqual(wiz.exception_msg, "Financial risk exceeded.\n")
  85. self.partner.risk_invoice_unpaid_limit = 0.0
  86. self.assertFalse(self.partner.risk_exception)
  87. self.partner.risk_invoice_open_limit = 300.0
  88. invoice2.date_due = fields.Date.today()
  89. wiz_dic = invoice2.action_invoice_open()
  90. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  91. self.assertEqual(wiz.exception_msg,
  92. "This invoice exceeds the open invoices risk.\n")
  93. self.partner.risk_invoice_open_limit = 0.0
  94. self.partner.risk_invoice_draft_include = False
  95. self.partner.risk_invoice_open_include = True
  96. self.partner.credit_limit = 900.0
  97. wiz_dic = invoice2.action_invoice_open()
  98. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  99. self.assertEqual(wiz.exception_msg,
  100. "This invoice exceeds the financial risk.\n")
  101. self.assertAlmostEqual(self.partner.risk_invoice_open, 0.0)
  102. wiz.button_continue()
  103. self.assertAlmostEqual(self.partner.risk_invoice_open, 550.0)
  104. self.assertTrue(self.partner.risk_allow_edit)
  105. self.partner.process_unpaid_invoices()
  106. self.assertEqual(self.env['ir.config_parameter'].get_param(
  107. 'partner_financial_risk.last_check'),
  108. fields.Date.today())