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.

111 lines
4.9 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 openerp.tests.common import SavepointCase
  5. from openerp 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('base.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.journal_sale = cls.env['account.journal'].create({
  36. 'name': 'Test journal for sale',
  37. 'type': 'sale',
  38. 'code': 'TSALE',
  39. 'default_debit_account_id': cls.account_sale.id,
  40. 'default_credit_account_id': cls.account_sale.id,
  41. })
  42. cls.tax = cls.env['account.tax'].create({
  43. 'name': 'Tax for sale 10%',
  44. 'type_tax_use': 'sale',
  45. 'tax_group_id': tax_group_taxes.id,
  46. 'amount_type': 'percent',
  47. 'amount': 10.0,
  48. })
  49. cls.invoice = cls.env['account.invoice'].create({
  50. 'partner_id': cls.partner.id,
  51. 'account_id': cls.account_customer.id,
  52. 'type': 'out_invoice',
  53. 'journal_id': cls.journal_sale.id,
  54. 'payment_term_id': False,
  55. 'invoice_line_ids': [(0, 0, {
  56. 'name': 'Test product',
  57. 'account_id': cls.account_sale.id,
  58. 'price_unit': 50,
  59. 'quantity': 10,
  60. 'invoice_line_tax_ids': [(6, 0, [cls.tax.id])],
  61. })],
  62. })
  63. def test_invoices(self):
  64. self.partner.risk_invoice_draft_include = True
  65. self.assertAlmostEqual(self.partner.risk_invoice_draft, 550.0)
  66. self.assertAlmostEqual(self.partner.risk_total, 550.0)
  67. self.invoice.signal_workflow('invoice_open')
  68. self.assertAlmostEqual(self.partner.risk_invoice_draft, 0.0)
  69. self.assertFalse(self.invoice.date_due)
  70. self.partner.risk_invoice_unpaid_include = True
  71. self.assertAlmostEqual(self.partner.risk_total, 550.0)
  72. self.partner.credit_limit = 100.0
  73. self.assertTrue(self.partner.risk_exception)
  74. self.partner.credit_limit = 1100.0
  75. self.assertFalse(self.partner.risk_exception)
  76. self.partner.risk_invoice_unpaid_limit = 499.0
  77. self.assertTrue(self.partner.risk_exception)
  78. invoice2 = self.invoice.copy({'partner_id': self.invoice_address.id})
  79. self.assertAlmostEqual(self.partner.risk_invoice_draft, 550.0)
  80. self.assertAlmostEqual(self.partner.risk_invoice_unpaid, 550.0)
  81. wiz_dic = invoice2.invoice_open()
  82. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  83. self.assertEqual(wiz.exception_msg, "Financial risk exceeded.\n")
  84. self.partner.risk_invoice_unpaid_limit = 0.0
  85. self.assertFalse(self.partner.risk_exception)
  86. self.partner.risk_invoice_open_limit = 300.0
  87. invoice2.date_due = fields.Date.today()
  88. wiz_dic = invoice2.invoice_open()
  89. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  90. self.assertEqual(wiz.exception_msg,
  91. "This invoice exceeds the open invoices risk.\n")
  92. self.partner.risk_invoice_open_limit = 0.0
  93. self.partner.risk_invoice_draft_include = False
  94. self.partner.risk_invoice_open_include = True
  95. self.partner.credit_limit = 900.0
  96. wiz_dic = invoice2.invoice_open()
  97. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  98. self.assertEqual(wiz.exception_msg,
  99. "This invoice exceeds the financial risk.\n")
  100. self.assertAlmostEqual(self.partner.risk_invoice_open, 0.0)
  101. wiz.button_continue()
  102. self.assertAlmostEqual(self.partner.risk_invoice_open, 550.0)
  103. self.assertTrue(self.partner.risk_allow_edit)
  104. self.partner.process_unpaid_invoices()
  105. self.assertEqual(self.env['ir.config_parameter'].get_param(
  106. 'partner_financial_risk.last_check'),
  107. fields.Date.today())