Browse Source

Add services using services in cost calculation view

Jocelyn Delande 9 years ago
parent
commit
0745388330
4 changed files with 58 additions and 5 deletions
  1. 19 2
      costs/admin.py
  2. 17 2
      costs/models.py
  3. 19 0
      costs/templates/costs/service_detail.html
  4. 3 1
      costs/views.py

+ 19 - 2
costs/admin.py

@@ -1,6 +1,7 @@
 from django.contrib import admin
 from django.contrib import admin
 
 
-from .models import Cost, Good, Service, CostUse, GoodUse, Document
+from .models import (
+    Document, Cost, Good, CostUse, GoodUse, Service, ServiceUse)
 
 
 
 
 @admin.register(Document)
 @admin.register(Document)
@@ -18,6 +19,12 @@ class CostUseInline(admin.TabularInline):
     extra = 1
     extra = 1
 
 
 
 
+class ServiceUseInline(admin.TabularInline):
+    model = ServiceUse
+    extra = 1
+    fk_name = 'service'
+
+
 class DirectDocumentFilter(admin.SimpleListFilter):
 class DirectDocumentFilter(admin.SimpleListFilter):
     title = 'Document'
     title = 'Document'
 
 
@@ -53,5 +60,15 @@ class GoodAdmin(admin.ModelAdmin):
 @admin.register(Service)
 @admin.register(Service)
 class ServiceAdmin(admin.ModelAdmin):
 class ServiceAdmin(admin.ModelAdmin):
     list_display = ('name', 'subscriptions_count', 'document')
     list_display = ('name', 'subscriptions_count', 'document')
-    inlines = (CostUseInline, GoodUseInline)
+    inlines = (CostUseInline, GoodUseInline, ServiceUseInline)
     list_filter = [DirectDocumentFilter]
     list_filter = [DirectDocumentFilter]
+
+    fieldsets = (
+        (None, {
+            'fields': (
+                ('name', 'document'), 'description', 'subscriptions_count'),
+        }),
+        ('Ré-utilisabilité', {
+            'fields': ('reusable',)
+        })
+    )

+ 17 - 2
costs/models.py

@@ -56,7 +56,7 @@ class AbstractResource(AbstractItem):
             (UNIT_U, 'U'),
             (UNIT_U, 'U'),
             (UNIT_IPV4, 'IPv4'),
             (UNIT_IPV4, 'IPv4'),
             (UNIT_ETHERNET_PORT, 'ports'),
             (UNIT_ETHERNET_PORT, 'ports'),
-            (UNIT_SERVICE, 'services'),
+            (UNIT_SERVICE, 'abonnement'),
         ),
         ),
         blank=True,
         blank=True,
     )
     )
@@ -232,7 +232,7 @@ class Service(AbstractResource):
 
 
     subscriptions_count = models.PositiveIntegerField(default=0)
     subscriptions_count = models.PositiveIntegerField(default=0)
     reusable = models.BooleanField(
     reusable = models.BooleanField(
-        "Ré-utilisable", default=False,
+        default=False,
         help_text="Peut-être utilisé par d'autres services")
         help_text="Peut-être utilisé par d'autres services")
 
 
     @property
     @property
@@ -254,10 +254,12 @@ class Service(AbstractResource):
     def get_prices(self):
     def get_prices(self):
         costs_uses = CostUse.objects.filter(service=self)
         costs_uses = CostUse.objects.filter(service=self)
         goods_uses = GoodUse.objects.filter(service=self)
         goods_uses = GoodUse.objects.filter(service=self)
+        services_uses = ServiceUse.objects.filter(service=self)
 
 
         total_costs_price = sum(chain(
         total_costs_price = sum(chain(
             (i.monthly_provision_share() for i in goods_uses),
             (i.monthly_provision_share() for i in goods_uses),
             (i.cost_share() for i in costs_uses),
             (i.cost_share() for i in costs_uses),
+            (i.cost_share() for i in services_uses)
         ))
         ))
 
 
         total_goods_value_share = sum(i.value_share() for i in goods_uses)
         total_goods_value_share = sum(i.value_share() for i in goods_uses)
@@ -288,6 +290,19 @@ class ServiceUse(AbstractUse):
         limit_choices_to={'reusable': True},
         limit_choices_to={'reusable': True},
         validators=[validate_reusable_service])
         validators=[validate_reusable_service])
 
 
+    def cost_share(self):
+        return (
+            self.share / self.resource.total_capacity
+            * self.resource.price
+        )
+
+    def unit_cost_share(self):
+        subscriptions_count = self.service.subscriptions_count
+        if subscriptions_count == 0:
+            return 0
+        else:
+            return self.cost_share()/self.service.subscriptions_count
+
     def clean(self):
     def clean(self):
         """ Checks for cycles in service using services
         """ Checks for cycles in service using services
         """
         """

+ 19 - 0
costs/templates/costs/service_detail.html

@@ -109,6 +109,25 @@ matériel et des investissements à l'échéance de leur durée de vie.
         <td class="right aligned">{{ usage.unit_cost_share|price }}</td>
         <td class="right aligned">{{ usage.unit_cost_share|price }}</td>
       </tr>
       </tr>
   {% endfor %}
   {% endfor %}
+  {% for usage in services_uses %}
+      <tr>
+      {% if forloop.first %}
+        <td rowspan="{{ costs_uses|length }}">Récurent (service)</td>
+      {% endif %}
+        <td>
+          <a href="{{ usage.resource.get_absolute_url }}">service {{ usage.resource.name }}</a>
+          <span class="bare-info">
+         {% if usage.resource.capacity_unit %}
+           ({{ usage.unit_share|human_round }} {{ usage.resource.get_capacity_unit_display }})
+         {% else %}
+           ({{ usage.unit_real_share|percent }})
+         {% endif %}
+          </span>
+        </td>
+        <td class="right aligned">{{ usage.unit_cost_share|price }}</td>
+      </tr>
+  {% endfor %}
+
     </tbody>
     </tbody>
     <tfoot>
     <tfoot>
       <tr>
       <tr>

+ 3 - 1
costs/views.py

@@ -1,7 +1,7 @@
 from django.core.urlresolvers import reverse
 from django.core.urlresolvers import reverse
 from django.shortcuts import render, get_object_or_404
 from django.shortcuts import render, get_object_or_404
 
 
-from .models import Document, Service, CostUse, GoodUse
+from .models import Document, Service, ServiceUse, CostUse, GoodUse
 
 
 
 
 def index(request):
 def index(request):
@@ -46,6 +46,7 @@ def detail_service(request, pk):
     )
     )
     costs_uses = CostUse.objects.filter(service=service)
     costs_uses = CostUse.objects.filter(service=service)
     goods_uses = GoodUse.objects.filter(service=service)
     goods_uses = GoodUse.objects.filter(service=service)
+    services_uses = ServiceUse.objects.filter(service=service)
 
 
     context = {}
     context = {}
     context.update(service.get_prices())
     context.update(service.get_prices())
@@ -55,6 +56,7 @@ def detail_service(request, pk):
         'service': service,
         'service': service,
         'costs_uses': costs_uses,
         'costs_uses': costs_uses,
         'goods_uses': goods_uses,
         'goods_uses': goods_uses,
+        'services_uses': services_uses,
         'monthly_fas': context['unit_goods_value_share']/12,
         'monthly_fas': context['unit_goods_value_share']/12,
     })
     })