Skip to content

Commit

Permalink
fix: Coerce None to zero for missing values
Browse files Browse the repository at this point in the history
Necessary to handle the case where balance statements are requested on a date when no members have any balance.
  • Loading branch information
LarsV123 authored and aaarunan committed Apr 1, 2023
1 parent 6ecf404 commit 3ebbaec
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions itkufs/reports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.contrib.auth.decorators import login_required
from django.db import transaction as db_transaction
from django.db.models import Q, Sum, Case, When
from django.db.models.functions import Coalesce
from django.forms.models import inlineformset_factory, model_to_dict
from django.http import Http404, HttpResponseRedirect, HttpResponse, HttpRequest
from django.shortcuts import render
Expand Down Expand Up @@ -334,15 +335,21 @@ def balance(request: HttpRequest, group: Group, is_admin=False):
.filter(filters)
.filter(group_account=False)
.aggregate(
positive_sum=Sum(
Case(
When(normal_balance__gt=0, then="normal_balance"), default=0
)
positive_sum=Coalesce(
Sum(
Case(
When(normal_balance__gt=0, then="normal_balance"),
)
),
0,
),
negative_sum=Sum(
Case(
When(normal_balance__lt=0, then="normal_balance"), default=0
)
negative_sum=Coalesce(
Sum(
Case(
When(normal_balance__lt=0, then="normal_balance"),
)
),
0,
),
)
)
Expand Down

0 comments on commit 3ebbaec

Please sign in to comment.