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.

85 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. result = {}
  12. for obj in self:
  13. # check if ranges overlap
  14. # TODO: This code can be done better
  15. for range1 in obj.range_ids:
  16. for range2 in obj.range_ids:
  17. if (range1.valid and range2.valid and
  18. range1.min_value < range2.min_value):
  19. result[obj.id] = range1.max_value <= range2.min_value
  20. return result
  21. @api.multi
  22. def _compute_generate_invalid_message(self):
  23. result = {}
  24. for obj in self:
  25. if obj.valid:
  26. result[obj.id] = ""
  27. else:
  28. result[obj.id] = ("Two of your ranges are overlapping. Please "
  29. "make sure your ranges do not overlap.")
  30. return result
  31. name = fields.Char('Name', size=50, required=True)
  32. range_ids = fields.Many2many(
  33. 'kpi.threshold.range',
  34. 'kpi_threshold_range_rel',
  35. 'threshold_id',
  36. 'range_id',
  37. 'Ranges'
  38. )
  39. valid = fields.Boolean(string='Valid', required=True,
  40. compute="_compute_is_valid_threshold", default=True)
  41. invalid_message = fields.Char(string='Message', size=100,
  42. compute="_compute_generate_invalid_message")
  43. kpi_ids = fields.One2many('kpi', 'threshold_id', 'KPIs')
  44. company_id = fields.Many2one(
  45. 'res.company', 'Company',
  46. default=lambda self: self.env.user.company_id.id)
  47. @api.model
  48. def create(self, data):
  49. # check if ranges overlap
  50. # TODO: This code can be done better
  51. range_obj1 = self.env['kpi.threshold.range']
  52. range_obj2 = self.env['kpi.threshold.range']
  53. if data.get('range_ids'):
  54. for range1 in data['range_ids'][0][2]:
  55. range_obj1 = range_obj1.browse(range1)
  56. for range2 in data['range_ids'][0][2]:
  57. range_obj2 = range_obj2.browse(range2)
  58. if (range_obj1.valid and range_obj2.valid and
  59. range_obj1.min_value < range_obj2.min_value):
  60. if range_obj1.max_value > range_obj2.min_value:
  61. raise exceptions.Warning(
  62. _("Two of your ranges are overlapping."),
  63. _("Make sure your ranges do not overlap!")
  64. )
  65. range_obj2 = self.env['kpi.threshold.range']
  66. range_obj1 = self.env['kpi.threshold.range']
  67. return super(KPIThreshold, self).create(data)
  68. @api.multi
  69. def get_color(self, kpi_value):
  70. color = '#FFFFFF'
  71. for obj in self:
  72. for range_obj in obj.range_ids:
  73. if (range_obj.min_value <= kpi_value <= range_obj.max_value and
  74. range_obj.valid):
  75. color = range_obj.color
  76. return color