mirror of
https://github.com/ae-utbm/sith.git
synced 2025-01-31 03:11:10 +00:00
Merge pull request #1013 from ae-utbm/fix-balance-updat
fix `CustomerQuerySet.update_balance`
This commit is contained in:
commit
75be6454eb
@ -52,7 +52,8 @@ class CustomerQuerySet(models.QuerySet):
|
|||||||
def update_amount(self) -> int:
|
def update_amount(self) -> int:
|
||||||
"""Update the amount of all customers selected by this queryset.
|
"""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:
|
Returns:
|
||||||
The number of updated rows.
|
The number of updated rows.
|
||||||
@ -73,7 +74,9 @@ class CustomerQuerySet(models.QuerySet):
|
|||||||
.values("res")
|
.values("res")
|
||||||
)
|
)
|
||||||
money_out = Subquery(
|
money_out = Subquery(
|
||||||
Selling.objects.filter(customer=OuterRef("pk"))
|
Selling.objects.filter(
|
||||||
|
customer=OuterRef("pk"), payment_method="SITH_ACCOUNT"
|
||||||
|
)
|
||||||
.values("customer_id")
|
.values("customer_id")
|
||||||
.annotate(res=Sum(F("unit_price") * F("quantity"), default=0))
|
.annotate(res=Sum(F("unit_price") * F("quantity"), default=0))
|
||||||
.values("res")
|
.values("res")
|
||||||
|
@ -442,6 +442,7 @@ def test_update_balance():
|
|||||||
_quantity=len(customers),
|
_quantity=len(customers),
|
||||||
unit_price=10,
|
unit_price=10,
|
||||||
quantity=1,
|
quantity=1,
|
||||||
|
payment_method="SITH_ACCOUNT",
|
||||||
_save_related=True,
|
_save_related=True,
|
||||||
),
|
),
|
||||||
*sale_recipe.prepare(
|
*sale_recipe.prepare(
|
||||||
@ -449,10 +450,26 @@ def test_update_balance():
|
|||||||
_quantity=3,
|
_quantity=3,
|
||||||
unit_price=5,
|
unit_price=5,
|
||||||
quantity=2,
|
quantity=2,
|
||||||
|
payment_method="SITH_ACCOUNT",
|
||||||
_save_related=True,
|
_save_related=True,
|
||||||
),
|
),
|
||||||
sale_recipe.prepare(
|
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)
|
Selling.objects.bulk_create(sales)
|
||||||
|
Loading…
Reference in New Issue
Block a user