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.

149 lines
6.5 KiB

[FIX] partner_financial_risk: Allow contact creation Without this patch, when creating a new contact from the partner form, users got this traceback: ``` Traceback (most recent call last): File "<console>", line 1, in <module> File "/opt/odoo/custom/src/odoo/openerp/api.py", line 248, in wrapper return new_api(self, *args, **kwargs) File "/opt/odoo/auto/addons/partner_financial_risk/models/res_partner.py", line 119, in _compute_risk_invoice active_test=False).search([('id', 'child_of', partner.id)]).ids File "/opt/odoo/custom/src/odoo/openerp/api.py", line 248, in wrapper return new_api(self, *args, **kwargs) File "/opt/odoo/custom/src/odoo/openerp/api.py", line 490, in new_api result = method(self._model, cr, uid, *args, **old_kwargs) File "/opt/odoo/custom/src/odoo/openerp/models.py", line 1668, in search return self._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count) File "/opt/odoo/custom/src/odoo/openerp/api.py", line 250, in wrapper return old_api(self, *args, **kwargs) File "/opt/odoo/custom/src/odoo/openerp/addons/base/res/res_partner.py", line 626, in _search count=count, access_rights_uid=access_rights_uid) File "/opt/odoo/custom/src/odoo/openerp/api.py", line 250, in wrapper return old_api(self, *args, **kwargs) File "/opt/odoo/custom/src/odoo/openerp/models.py", line 4793, in _search query = self._where_calc(cr, user, args, context=context) File "/opt/odoo/custom/src/odoo/openerp/api.py", line 250, in wrapper return old_api(self, *args, **kwargs) File "/opt/odoo/custom/src/odoo/openerp/models.py", line 4564, in _where_calc e = expression.expression(cr, user, domain, self, context) File "/opt/odoo/custom/src/odoo/openerp/osv/expression.py", line 644, in __init__ self.parse(cr, uid, context=context) File "/opt/odoo/custom/src/odoo/openerp/osv/expression.py", line 834, in parse ids2 = to_ids(right, model, context) File "/opt/odoo/custom/src/odoo/openerp/osv/expression.py", line 708, in to_ids return list(value) TypeError: 'NewId' object is not iterable ``` Patch includes a recomputation when the partner is or not set as customer, since the field is used in the method.
7 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 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. type_revenue = cls.env.ref('account.data_account_type_revenue')
  12. type_receivable = cls.env.ref('account.data_account_type_receivable')
  13. tax_group_taxes = cls.env.ref('account.tax_group_taxes')
  14. cls.account_sale = cls.env['account.account'].create({
  15. 'name': 'Sale',
  16. 'code': 'XX_700',
  17. 'user_type_id': type_revenue.id,
  18. 'reconcile': True,
  19. })
  20. cls.account_customer = cls.env['account.account'].create({
  21. 'name': 'Customer',
  22. 'code': 'XX_430',
  23. 'user_type_id': type_receivable.id,
  24. 'reconcile': True,
  25. })
  26. cls.other_account_customer = cls.env['account.account'].create({
  27. 'name': 'Other Account Customer',
  28. 'code': 'XX_431',
  29. 'user_type_id': type_receivable.id,
  30. 'reconcile': True,
  31. })
  32. cls.partner = cls.env['res.partner'].create({
  33. 'name': 'Partner test',
  34. 'customer': True,
  35. 'property_account_receivable_id': cls.account_customer.id,
  36. })
  37. cls.invoice_address = cls.env['res.partner'].create({
  38. 'name': 'Partner test invoice',
  39. 'parent_id': cls.partner.id,
  40. 'type': 'invoice',
  41. })
  42. cls.journal_sale = cls.env['account.journal'].create({
  43. 'name': 'Test journal for sale',
  44. 'type': 'sale',
  45. 'code': 'TSALE',
  46. 'default_debit_account_id': cls.account_sale.id,
  47. 'default_credit_account_id': cls.account_sale.id,
  48. })
  49. cls.tax = cls.env['account.tax'].create({
  50. 'name': 'Tax for sale 10%',
  51. 'type_tax_use': 'sale',
  52. 'tax_group_id': tax_group_taxes.id,
  53. 'amount_type': 'percent',
  54. 'amount': 10.0,
  55. })
  56. cls.invoice = cls.env['account.invoice'].create({
  57. 'partner_id': cls.partner.id,
  58. 'account_id': cls.account_customer.id,
  59. 'type': 'out_invoice',
  60. 'journal_id': cls.journal_sale.id,
  61. 'payment_term_id': False,
  62. 'invoice_line_ids': [(0, 0, {
  63. 'name': 'Test product',
  64. 'account_id': cls.account_sale.id,
  65. 'price_unit': 50,
  66. 'quantity': 10,
  67. 'invoice_line_tax_ids': [(6, 0, [cls.tax.id])],
  68. })],
  69. })
  70. def test_invoices(self):
  71. self.partner.risk_invoice_draft_include = True
  72. self.assertAlmostEqual(self.partner.risk_invoice_draft, 550.0)
  73. self.assertAlmostEqual(self.partner.risk_total, 550.0)
  74. self.invoice.signal_workflow('invoice_open')
  75. self.assertAlmostEqual(self.partner.risk_invoice_draft, 0.0)
  76. line = self.invoice.move_id.line_ids.filtered(lambda x: x.debit != 0.0)
  77. line.date_maturity = '2017-01-01'
  78. self.partner.risk_invoice_unpaid_include = True
  79. self.assertAlmostEqual(self.partner.risk_total, 550.0)
  80. self.partner.credit_limit = 100.0
  81. self.assertTrue(self.partner.risk_exception)
  82. self.partner.credit_limit = 1100.0
  83. self.assertFalse(self.partner.risk_exception)
  84. self.partner.risk_invoice_unpaid_limit = 499.0
  85. self.assertTrue(self.partner.risk_exception)
  86. invoice2 = self.invoice.copy({'partner_id': self.invoice_address.id})
  87. self.assertAlmostEqual(self.partner.risk_invoice_draft, 550.0)
  88. self.assertAlmostEqual(self.partner.risk_invoice_unpaid, 550.0)
  89. wiz_dic = invoice2.invoice_open()
  90. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  91. self.assertEqual(wiz.exception_msg, "Financial risk exceeded.\n")
  92. self.partner.risk_invoice_unpaid_limit = 0.0
  93. self.assertFalse(self.partner.risk_exception)
  94. self.partner.risk_invoice_open_limit = 300.0
  95. invoice2.date_due = fields.Date.today()
  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 open invoices risk.\n")
  100. self.partner.risk_invoice_open_limit = 0.0
  101. self.partner.risk_invoice_draft_include = False
  102. self.partner.risk_invoice_open_include = True
  103. self.partner.credit_limit = 900.0
  104. wiz_dic = invoice2.invoice_open()
  105. wiz = self.env[wiz_dic['res_model']].browse(wiz_dic['res_id'])
  106. self.assertEqual(wiz.exception_msg,
  107. "This invoice exceeds the financial risk.\n")
  108. self.assertAlmostEqual(self.partner.risk_invoice_open, 0.0)
  109. wiz.button_continue()
  110. self.assertAlmostEqual(self.partner.risk_invoice_open, 550.0)
  111. self.assertTrue(self.partner.risk_allow_edit)
  112. self.partner.process_unpaid_invoices()
  113. self.assertEqual(self.env['ir.config_parameter'].get_param(
  114. 'partner_financial_risk.last_check'),
  115. fields.Date.today())
  116. def test_other_account_amount(self):
  117. self.move = self.env['account.move'].create({
  118. 'journal_id': self.journal_sale.id,
  119. 'date': fields.Date.today(),
  120. 'line_ids': [
  121. (0, 0, {
  122. 'name': 'Debit line',
  123. 'partner_id': self.partner.id,
  124. 'account_id': self.other_account_customer.id,
  125. 'debit': 100,
  126. }),
  127. (0, 0, {
  128. 'name': 'Credit line',
  129. 'partner_id': self.partner.id,
  130. 'account_id': self.account_sale.id,
  131. 'credit': 100,
  132. }),
  133. ],
  134. })
  135. self.assertAlmostEqual(self.partner.risk_account_amount, 100.0)
  136. line = self.move.line_ids.filtered(lambda x: x.debit != 0.0)
  137. line.date_maturity = '2017-01-01'
  138. self.assertAlmostEqual(self.partner.risk_account_amount, 0.0)
  139. self.assertAlmostEqual(self.partner.risk_account_amount_unpaid, 100.0)
  140. def test_recompute_newid(self):
  141. """Computing risk shouldn't fail if record is a NewId."""
  142. new = self.env["res.partner"].new({"customer": True})
  143. new._compute_risk_invoice()