Browse Source

Add Service.subscriptions_count field

To account how many of each service is subscribed and do per-unit math.
Jocelyn Delande 9 years ago
parent
commit
78816a628d

+ 1 - 0
costs/admin.py

@@ -28,4 +28,5 @@ class GoodAdmin(admin.ModelAdmin):
 
 @admin.register(Service)
 class ServiceAdmin(admin.ModelAdmin):
+    list_display = ('name', 'subscriptions_count')
     inlines = (CostUseInline, GoodUseInline)

+ 25 - 0
costs/migrations/0002_auto_20151128_1015.py

@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('costs', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='service',
+            name='subscriptions_count',
+            field=models.PositiveIntegerField(default=0),
+            preserve_default=False,
+        ),
+        migrations.AlterField(
+            model_name='good',
+            name='price',
+            field=models.PositiveIntegerField(),
+        ),
+    ]

+ 17 - 0
costs/models.py

@@ -85,6 +85,13 @@ class CostUse(AbstractUse):
     def cost_share(self):
         return self.share*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
+
 
 class GoodUse(AbstractUse):
     resource = models.ForeignKey(Good)
@@ -92,6 +99,14 @@ class GoodUse(AbstractUse):
     def monthly_provision_share(self):
         return self.real_share()*self.resource.monthly_provision()
 
+    def unit_monthly_provision_share(self):
+        subscriptions_count = self.service.subscriptions_count
+        monthly_share = self.monthly_provision_share()
+        if subscriptions_count == 0:
+            return 0
+        else:
+            return monthly_share/subscriptions_count
+
 
 class Service(AbstractItem):
     """ A service we sell
@@ -108,5 +123,7 @@ class Service(AbstractItem):
         related_name='using_services')
     # services = models.ManyToMany('Service') #TODO
 
+    subscriptions_count = models.PositiveIntegerField(default=0)
+
     def get_absolute_url(self):
         return reverse('detail-service', kwargs={'pk': self.pk})

+ 5 - 4
costs/templates/costs/service_detail.html

@@ -1,21 +1,22 @@
 <h1>Service {{ service.name }}</h1>
+<p><em>({{ service.subscriptions_count }} souscriptions)</em></p>
 <table>
   {% for usage in goods_uses %}
   <tr>
     <td>matériel</td>
     <td>{{ usage.resource.name }}</td>
-    <td>{{ usage.monthly_provision_share }}€</td>
+    <td>{{ usage.unit_monthly_provision_share }}€</td>
   </tr>
   {% endfor %}
   {% for usage in costs_uses %}
   <tr>
     <td>coût récurent</td>
     <td>{{ usage.resource.name }}</td>
-    <td>{{ usage.cost_share }}€</td>
+    <td>{{ usage.unit_cost_share }}€</td>
   </tr>
   {% endfor %}
   <tr>
-    <td colspan="2">Coût TOTAL</td>
-    <td>{{ total_costs_price }}€</td>
+    <td colspan="2"><strong>Coût de revient par abonnement</strong></td>
+    <td><strong>{{ unit_costs_price }}€</strong></td>
   </tr>
 </table>

+ 12 - 4
costs/views.py

@@ -30,13 +30,21 @@ def detail_service(request, pk):
     costs_uses = CostUse.objects.filter(service=service)
     goods_uses = GoodUse.objects.filter(service=service)
 
+    total_costs_price = sum(chain(
+        (i.monthly_provision_share() for i in goods_uses),
+        (i.cost_share() for i in costs_uses),
+    ))
+
+    if service.subscriptions_count == 0:
+        unit_costs_price = 0
+    else:
+        unit_costs_price = total_costs_price/service.subscriptions_count
+
     context = {
         'service': service,
         'costs_uses': costs_uses,
         'goods_uses': goods_uses,
-        'total_costs_price': sum(chain(
-            (i.monthly_provision_share() for i in goods_uses),
-            (i.cost_share() for i in costs_uses)
-        ))
+        'total_costs_price': total_costs_price,
+        'unit_costs_price': unit_costs_price,
     }
     return render(request, 'costs/service_detail.html', context)