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.

83 lines
3.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2012 - Now Savoir-faire Linux <https://www.savoirfairelinux.com/>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import fields, models, api, exceptions, _
  5. class KPIThreshold(models.Model):
  6. """KPI Threshold."""
  7. _name = "kpi.threshold"
  8. _description = "KPI Threshold"
  9. @api.multi
  10. def _compute_is_valid_threshold(self):
  11. for obj in self:
  12. # check if ranges overlap
  13. # TODO: This code can be done better
  14. obj.valid = True
  15. for range1 in obj.range_ids:
  16. if not range1.valid:
  17. obj.valid = False
  18. break
  19. for range2 in (obj.range_ids-range1):
  20. if (range1.max_value >= range2.min_value and
  21. range1.min_value <= range2.max_value):
  22. obj.valid = False
  23. break
  24. if obj.valid:
  25. obj.invalid_message = None
  26. else:
  27. obj.invalid_message = (
  28. "Some ranges are invalid or overlapping. "
  29. "Please make sure your ranges do not overlap.")
  30. name = fields.Char('Name', size=50, required=True)
  31. range_ids = fields.Many2many(
  32. 'kpi.threshold.range',
  33. 'kpi_threshold_range_rel',
  34. 'threshold_id',
  35. 'range_id',
  36. 'Ranges'
  37. )
  38. valid = fields.Boolean(string='Valid', required=True,
  39. compute="_compute_is_valid_threshold", default=True)
  40. invalid_message = fields.Char(string='Message', size=100,
  41. compute="_compute_is_valid_threshold")
  42. kpi_ids = fields.One2many('kpi', 'threshold_id', 'KPIs')
  43. company_id = fields.Many2one(
  44. 'res.company', 'Company',
  45. default=lambda self: self.env.user.company_id.id)
  46. @api.model
  47. def create(self, data):
  48. # check if ranges overlap
  49. # TODO: This code can be done better
  50. range_obj1 = self.env['kpi.threshold.range']
  51. range_obj2 = self.env['kpi.threshold.range']
  52. if data.get('range_ids'):
  53. for range1 in data['range_ids'][0][2]:
  54. range_obj1 = range_obj1.browse(range1)
  55. for range2 in data['range_ids'][0][2]:
  56. range_obj2 = range_obj2.browse(range2)
  57. if (range_obj1.valid and range_obj2.valid and
  58. range_obj1.min_value < range_obj2.min_value):
  59. if range_obj1.max_value > range_obj2.min_value:
  60. raise exceptions.Warning(
  61. _("Two of your ranges are overlapping."),
  62. _("Make sure your ranges do not overlap!")
  63. )
  64. range_obj2 = self.env['kpi.threshold.range']
  65. range_obj1 = self.env['kpi.threshold.range']
  66. return super(KPIThreshold, self).create(data)
  67. @api.multi
  68. def get_color(self, kpi_value):
  69. color = '#FFFFFF'
  70. for obj in self:
  71. for range_obj in obj.range_ids:
  72. if (range_obj.min_value <= kpi_value <= range_obj.max_value and
  73. range_obj.valid):
  74. color = range_obj.color
  75. return color