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.

87 lines
3.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  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 openerp.tests.common import TransactionCase
  5. from openerp import fields
  6. from dateutil.relativedelta import relativedelta
  7. from datetime import datetime
  8. class TestPartnerFinancialRisk(TransactionCase):
  9. def setUp(self):
  10. super(TestPartnerFinancialRisk, self).setUp()
  11. self.env.user.groups_id |= self.env.ref('base.group_sale_manager')
  12. self.partner = self.env['res.partner'].create({
  13. 'name': 'Partner test',
  14. 'customer': True,
  15. })
  16. self.journal = self.env['account.journal'].create({
  17. 'type': 'sale',
  18. 'name': 'Test Sales',
  19. 'code': 'TSALE',
  20. })
  21. self.prod_account = self.env.ref('account.a_sale')
  22. self.inv_account = self.env.ref('account.a_recv')
  23. date_inv = datetime.now() - relativedelta(days=7)
  24. date_due = datetime.now() - relativedelta(days=3)
  25. self.invoice = self.env['account.invoice'].create({
  26. 'journal_id': self.journal.id,
  27. 'company_id': self.env.user.company_id.id,
  28. 'currency_id': self.env.user.company_id.currency_id.id,
  29. 'date_invoice': date_inv.strftime("%Y-%m-%d"),
  30. 'date_due': date_due.strftime("%Y-%m-%d"),
  31. 'partner_id': self.partner.id,
  32. 'account_id': self.inv_account.id,
  33. 'invoice_line': [(0, 0, {
  34. 'account_id': self.prod_account.id,
  35. 'name': 'Test line',
  36. 'price_unit': 50,
  37. 'quantity': 10,
  38. })]
  39. })
  40. def test_invoices(self):
  41. self.partner.risk_invoice_draft_include = True
  42. self.assertAlmostEqual(self.partner.risk_invoice_draft, 500.0)
  43. self.assertAlmostEqual(self.partner.risk_total, 500.0)
  44. self.invoice.signal_workflow('invoice_open')
  45. self.assertAlmostEqual(self.partner.risk_invoice_draft, 0.0)
  46. self.partner.risk_invoice_unpaid_include = True
  47. self.assertAlmostEqual(self.partner.risk_invoice_unpaid, 500.0)
  48. self.assertAlmostEqual(self.partner.risk_total, 500.0)
  49. self.partner.credit_limit = 100.0
  50. self.assertTrue(self.partner.risk_exception)
  51. self.partner.credit_limit = 1000.0
  52. self.assertFalse(self.partner.risk_exception)
  53. self.partner.risk_invoice_unpaid_limit = 499.0
  54. self.assertTrue(self.partner.risk_exception)
  55. invoice2 = self.invoice.copy()
  56. wiz_dic = invoice2.invoice_open()
  57. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  58. self.assertEqual(wiz.exception_msg, "Financial risk exceeded.\n")
  59. self.partner.risk_invoice_unpaid_limit = 0.0
  60. self.assertFalse(self.partner.risk_exception)
  61. self.partner.risk_invoice_open_limit = 300.0
  62. invoice2.date_due = fields.Date.today()
  63. wiz_dic = invoice2.invoice_open()
  64. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  65. self.assertEqual(wiz.exception_msg,
  66. "This invoice exceeds the open invoices risk.\n")
  67. self.partner.risk_invoice_open_limit = 0.0
  68. self.partner.risk_invoice_draft_include = False
  69. self.partner.risk_invoice_open_include = True
  70. self.partner.credit_limit = 900.0
  71. wiz_dic = invoice2.invoice_open()
  72. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  73. self.assertEqual(wiz.exception_msg,
  74. "This invoice exceeds the financial risk.\n")
  75. self.assertAlmostEqual(self.partner.risk_invoice_open, 0.0)
  76. wiz.button_continue()
  77. self.assertAlmostEqual(self.partner.risk_invoice_open, 500.0)
  78. self.assertTrue(self.partner.risk_allow_edit)
  79. self.partner.process_unpaid_invoices()
  80. self.assertEqual(self.env['ir.config_parameter'].get_param(
  81. 'partner_financial_risk.last_check'),
  82. fields.Date.today())