models.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from django.conf import settings
  2. from django.core.exceptions import ValidationError
  3. from django.core.urlresolvers import reverse
  4. from django.db import models
  5. from .validators import less_than_one
  6. class Document(models.Model):
  7. """ A document is a scenario or a record from facts, on 1 month.
  8. """
  9. TYPE_FACT = 'fact'
  10. TYPE_PLAN = 'plan'
  11. name = models.CharField(max_length=130)
  12. comment = models.TextField(blank=True)
  13. date = models.DateField(auto_now_add=True)
  14. type = models.CharField(max_length=10, choices=(
  15. (TYPE_FACT, 'relevé'),
  16. (TYPE_PLAN, 'scénario/estimation'),
  17. ))
  18. def __str__(self):
  19. return '{} {:%b %Y}'.format(self.name, self.date)
  20. class AbstractItem(models.Model):
  21. name = models.CharField(max_length=130)
  22. description = models.TextField(blank=True)
  23. document = models.ForeignKey(Document)
  24. def __str__(self):
  25. return self.name
  26. def get_use_class(self):
  27. raise NotImplemented
  28. def used(self, except_by=None):
  29. """ Return the used fraction of an item
  30. :type: Service
  31. :param except_by: exclude this service from the math
  32. :rtype: float
  33. """
  34. sharing_costs = self.get_use_class().objects.filter(resource=self)
  35. if except_by:
  36. sharing_costs = sharing_costs.exclude(service=except_by)
  37. existing_uses_sum = sum(
  38. sharing_costs.values_list('share', flat=True))
  39. return existing_uses_sum
  40. def unused(self):
  41. return 1-self.used()
  42. class Meta:
  43. abstract = True
  44. class Cost(AbstractItem):
  45. """ A monthtly cost we have to pay
  46. """
  47. price = models.FloatField(help_text="Coût mensuel")
  48. def get_use_class(self):
  49. return CostUse
  50. class Meta:
  51. verbose_name = 'Coût'
  52. class Good(AbstractItem):
  53. """ A good, which replacement is provisioned
  54. """
  55. price = models.FloatField()
  56. provisioning_duration = models.DurationField(
  57. choices=settings.PROVISIONING_DURATIONS)
  58. def get_use_class(self):
  59. return GoodUse
  60. def monthly_provision(self):
  61. return self.price/self.provisioning_duration.days*(365.25/12)
  62. class Meta:
  63. verbose_name = 'Bien'
  64. class AbstractUse(models.Model):
  65. share = models.FloatField(validators=[less_than_one])
  66. service = models.ForeignKey('Service')
  67. class Meta:
  68. abstract = True
  69. def clean(self):
  70. if hasattr(self, 'resource'):
  71. if (self.resource.used(except_by=self.service) + self.share) > 1:
  72. raise ValidationError(
  73. "Cannot use more than 100% of {})".format(self.resource))
  74. def real_share(self):
  75. """The share, + wasted space share
  76. Taking into account that the unused space is
  77. wasted and has to be divided among actual users
  78. """
  79. return (
  80. self.share +
  81. (self.share/self.resource.used())*self.resource.unused()
  82. )
  83. def value_share(self):
  84. return self.resource.price*self.real_share()
  85. def unit_value_share(self):
  86. if self.service.subscriptions_count == 0:
  87. return 0
  88. else:
  89. return self.value_share()/self.service.subscriptions_count
  90. class CostUse(AbstractUse):
  91. resource = models.ForeignKey(Cost)
  92. def cost_share(self):
  93. return self.real_share()*self.resource.price
  94. def unit_cost_share(self):
  95. subscriptions_count = self.service.subscriptions_count
  96. if subscriptions_count == 0:
  97. return 0
  98. else:
  99. return self.cost_share()/self.service.subscriptions_count
  100. class GoodUse(AbstractUse):
  101. resource = models.ForeignKey(Good)
  102. def monthly_provision_share(self):
  103. return self.real_share()*self.resource.monthly_provision()
  104. def unit_monthly_provision_share(self):
  105. subscriptions_count = self.service.subscriptions_count
  106. monthly_share = self.monthly_provision_share()
  107. if subscriptions_count == 0:
  108. return 0
  109. else:
  110. return monthly_share/subscriptions_count
  111. class Service(AbstractItem):
  112. """ A service we sell
  113. (considered monthly)
  114. """
  115. costs = models.ManyToManyField(
  116. Cost,
  117. through=CostUse,
  118. related_name='using_services')
  119. goods = models.ManyToManyField(
  120. Good,
  121. through=GoodUse,
  122. related_name='using_services')
  123. # services = models.ManyToMany('Service') #TODO
  124. subscriptions_count = models.PositiveIntegerField(default=0)
  125. def get_absolute_url(self):
  126. return reverse('detail-service', kwargs={'pk': self.pk})