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.

49 lines
2.1 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2015 UAB Versada
  6. # (<http://www.versada.lt>).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp import models, api
  23. from openerp.exceptions import ValidationError
  24. class ResPartner(models.Model):
  25. _inherit = 'res.partner'
  26. @api.multi
  27. def credit_limit_reached(self, credit_increase=0.0, raise_error=True):
  28. """
  29. Returns True (or exception) if credit limit is reached othervise False
  30. """
  31. for each in self:
  32. # credit_limit is synchronized between company and contacts but
  33. # credit is not
  34. credit = each.parent_id and each.parent_id.credit or each.credit
  35. credit_increased = credit + credit_increase
  36. if each.credit_limit > 0 and credit_increased > each.credit_limit:
  37. if raise_error:
  38. raise ValidationError(
  39. "Credit Limit exceeded for partner %s%s!\n\n"
  40. "Credit Limit: %.2f\nExceeding Credit: %.2f\n" % (
  41. self.display_name,
  42. self.ref and ' (%s)' % self.ref or '',
  43. each.credit_limit,
  44. credit_increased))
  45. return True