models.py 4.3 KB

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