models.py 4.6 KB

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