Skip to content

Commit

Permalink
add total_unfunded, serializer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
emaciupe committed Jun 7, 2023
1 parent 6079ea1 commit 268de7a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.6 on 2023-06-07 07:36
# Generated by Django 3.2.6 on 2023-06-07 15:23

from django.db import migrations, models

Expand All @@ -15,6 +15,11 @@ class Migration(migrations.Migration):
name='has_unfunded_cash',
field=models.BooleanField(default=False, verbose_name='Unfunded Cash'),
),
migrations.AddField(
model_name='interventionbudget',
name='total_unfunded',
field=models.DecimalField(decimal_places=2, default=0, max_digits=20, verbose_name='Total Unfunded'),
),
migrations.AddField(
model_name='interventionbudget',
name='unfunded_cash_local',
Expand Down
15 changes: 12 additions & 3 deletions src/etools/applications/partners/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3079,7 +3079,7 @@ class InterventionBudget(TimeStampedModel):
verbose_name=_('UNICEF Supplies')
)
total = models.DecimalField(max_digits=20, decimal_places=2, verbose_name=_('Total'))
total_unfunded = models.DecimalField(max_digits=20, decimal_places=2, verbose_name=_('Total Unfunded'))
total_unfunded = models.DecimalField(max_digits=20, decimal_places=2, default=0, verbose_name=_('Total Unfunded'))

# sum of all activity/management budget cso/partner values
partner_contribution_local = models.DecimalField(max_digits=20, decimal_places=2, default=0,
Expand Down Expand Up @@ -3700,9 +3700,13 @@ def partner_total(self):
def unicef_total(self):
return self.act1_unicef + self.act2_unicef + self.act3_unicef

@property
def unfunded_total(self):
return self.act1_unfunded + self.act2_unfunded + self.act3_unfunded

@property
def total(self):
return self.partner_total + self.unicef_total
return self.partner_total + self.unicef_total + self.unfunded_total

def save(self, *args, **kwargs):
create = not self.pk
Expand All @@ -3714,17 +3718,22 @@ def save(self, *args, **kwargs):

def update_cash(self):
aggregated_items = self.items.values('kind').order_by('kind')
aggregated_items = aggregated_items.annotate(unicef_cash=Sum('unicef_cash'), cso_cash=Sum('cso_cash'))
aggregated_items = aggregated_items.annotate(
unicef_cash=Sum('unicef_cash'), cso_cash=Sum('cso_cash'), unfunded_cash=Sum('unfunded_cash')
)
for item in aggregated_items:
if item['kind'] == InterventionManagementBudgetItem.KIND_CHOICES.in_country:
self.act1_unicef = item['unicef_cash']
self.act1_partner = item['cso_cash']
self.act1_unfunded = item['unfunded_cash']
elif item['kind'] == InterventionManagementBudgetItem.KIND_CHOICES.operational:
self.act2_unicef = item['unicef_cash']
self.act2_partner = item['cso_cash']
self.act2_unfunded = item['unfunded_cash']
elif item['kind'] == InterventionManagementBudgetItem.KIND_CHOICES.planning:
self.act3_unicef = item['unicef_cash']
self.act3_partner = item['cso_cash']
self.act3_unfunded = item['unfunded_cash']
self.save()


Expand Down
10 changes: 7 additions & 3 deletions src/etools/applications/partners/serializers/interventions_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class InterventionManagementBudgetSerializer(
act3_total = serializers.SerializerMethodField()
partner_total = serializers.DecimalField(max_digits=20, decimal_places=2, read_only=True)
unicef_total = serializers.DecimalField(max_digits=20, decimal_places=2, read_only=True)
unfunded_total = serializers.DecimalField(max_digits=20, decimal_places=2, read_only=True)
total = serializers.DecimalField(max_digits=20, decimal_places=2, read_only=True)

class Meta:
Expand All @@ -199,26 +200,29 @@ class Meta:
"items",
"act1_unicef",
"act1_partner",
"act1_unfunded",
"act1_total",
"act2_unicef",
"act2_partner",
"act2_unfunded",
"act2_total",
"act3_unicef",
"act3_partner",
"act3_unfunded",
"act3_total",
"partner_total",
"unicef_total",
"total",
)

def get_act1_total(self, obj):
return str(obj.act1_unicef + obj.act1_partner)
return str(obj.act1_unicef + obj.act1_partner + obj.act1_unfunded)

def get_act2_total(self, obj):
return str(obj.act2_unicef + obj.act2_partner)
return str(obj.act2_unicef + obj.act2_partner + obj.act2_unfunded)

def get_act3_total(self, obj):
return str(obj.act3_unicef + obj.act3_partner)
return str(obj.act3_unicef + obj.act3_partner + obj.act3_unfunded)

@transaction.atomic
def update(self, instance, validated_data):
Expand Down

0 comments on commit 268de7a

Please sign in to comment.