Skip to content

Commit

Permalink
Merge pull request #1013 from ae-utbm/fix-balance-updat
Browse files Browse the repository at this point in the history
fix `CustomerQuerySet.update_balance`
  • Loading branch information
klmp200 authored Jan 23, 2025
2 parents 18967cf + 428fe68 commit 75be645
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 5 additions & 2 deletions counter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class CustomerQuerySet(models.QuerySet):
def update_amount(self) -> int:
"""Update the amount of all customers selected by this queryset.
The result is given as the sum of all refills minus the sum of all purchases.
The result is given as the sum of all refills
minus the sum of all purchases paid with the AE account.
Returns:
The number of updated rows.
Expand All @@ -73,7 +74,9 @@ def update_amount(self) -> int:
.values("res")
)
money_out = Subquery(
Selling.objects.filter(customer=OuterRef("pk"))
Selling.objects.filter(
customer=OuterRef("pk"), payment_method="SITH_ACCOUNT"
)
.values("customer_id")
.annotate(res=Sum(F("unit_price") * F("quantity"), default=0))
.values("res")
Expand Down
19 changes: 18 additions & 1 deletion counter/tests/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,34 @@ def test_update_balance():
_quantity=len(customers),
unit_price=10,
quantity=1,
payment_method="SITH_ACCOUNT",
_save_related=True,
),
*sale_recipe.prepare(
customer=iter(customers[:3]),
_quantity=3,
unit_price=5,
quantity=2,
payment_method="SITH_ACCOUNT",
_save_related=True,
),
sale_recipe.prepare(
customer=customers[4], quantity=1, unit_price=50, _save_related=True
customer=customers[4],
quantity=1,
unit_price=50,
payment_method="SITH_ACCOUNT",
_save_related=True,
),
*sale_recipe.prepare(
# all customers also bought products without using their AE account.
# All purchases made with another mean than the AE account should
# be ignored when updating the account balance.
customer=iter(customers),
_quantity=len(customers),
unit_price=50,
quantity=1,
payment_method="CARD",
_save_related=True,
),
]
Selling.objects.bulk_create(sales)
Expand Down

0 comments on commit 75be645

Please sign in to comment.