Skip to content

Saleor Core: the high performance, composable, headless commerce API.

fix annotate_quantities qs manager (#13593)

Awais Jibran774f6d81ebdd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 48a5fb2c7b..d83d96fe27 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ All notable, unreleased changes to this project will be documented in this file.
### Saleor Apps
### Other changes
+- Fix error in variant available stock calculation - 13593 by @awaisdar001
# 3.15.0 [Unreleased]
diff --git a/saleor/product/managers.py b/saleor/product/managers.py
index c2d81fb9a8..015959c608 100644
--- a/saleor/product/managers.py
+++ b/saleor/product/managers.py
@@ -241,10 +241,39 @@ ProductManager = models.Manager.from_queryset(ProductsQueryset)
class ProductVariantQueryset(models.QuerySet):
def annotate_quantities(self):
+ """Annotate the queryset with quantity-related fields.
+
+ This method annotates the queryset with the following fields:
+ - `quantity`: The total quantity in stock for each product variant.
+ - `quantity_allocated`: The total quantity allocated from the stock
+ for each product variant.
+ - `available_quantity`: The available quantity for each product variant,
+ which is calculated as `quantity - quantity_allocated`.
+ """
+
+ from saleor.warehouse.models import Allocation
+
+ allocations_subquery = (
+ Allocation.objects.filter(stock__product_variant=OuterRef("pk"))
+ .values("stock__product_variant")
+ .annotate(total_allocated=Coalesce(Sum("quantity_allocated"), 0))
+ .values("total_allocated")
+ )
+
return self.annotate(
- quantity=Coalesce(Sum("stocks__quantity"), 0),
+ quantity=Coalesce(Sum("stocks__quantity"), Value(0)),
quantity_allocated=Coalesce(
- Sum("stocks__allocations__quantity_allocated"), 0
+ Subquery(allocations_subquery, output_field=models.IntegerField()),
+ Value(0),
+ ),
+ available_quantity=Case(
+ When(quantity_allocated=None, then=F("quantity")),
+ default=F("quantity")
+ - Coalesce(
+ Subquery(allocations_subquery, output_field=models.IntegerField()),
+ Value(0),
+ ),
+ output_field=models.IntegerField(),
),
)
diff --git a/saleor/product/tests/test_managers.py b/saleor/product/tests/test_managers.py
new file mode 100644
index 0000000000..f197f996ee
--- /dev/null
+++ b/saleor/product/tests/test_managers.py
@@ -0,0 +1,60 @@
+from saleor.product.models import ProductVariant
+
+
+def test_available_quantity_with_no_allocations(variant, allocations):
+ stocks = variant.stocks.first()
+ stocks.allocations.all().delete()
+ assert stocks.quantity == 15
+
+ assert stocks.allocations.count() == 0
+ # No allocations have been made, so available_quantity should be equal to quantity
+ instance = ProductVariant.objects.annotate_quantities().get(pk=variant.pk)
+ assert instance.available_quantity == 15
+
+
+def test_available_quantity_with_allocations(variant, allocations):
+ stocks = variant.stocks.first()
+
+ allocation_quantity = sum(
+ allocation.quantity_allocated for allocation in stocks.allocations.all()
+ )
+ assert stocks.allocations.count() == 3
+ assert stocks.quantity == 15
+ assert allocation_quantity == 7
+
+ # Available quantity should be quantity - quantity_allocated
+ instance = ProductVariant.objects.annotate_quantities().get(pk=variant.pk)
+ assert instance.available_quantity == 15 - 7
+
+
+def test_available_quantity_with_allocations_quantity_zero(variant, allocations):
+ stocks = variant.stocks.first()
+ stocks.allocations.update(quantity_allocated=0)
+
+ assert stocks.allocations.count() == 3
+ assert stocks.quantity == 15
+
+ # Allocations have been cleared, so available_quantity should be equal to quantity
+ instance = ProductVariant.objects.annotate_quantities().get(pk=variant.pk)
+ assert instance.available_quantity == 15
+
+
+def test_available_quantity_with_insufficient_stock(variant, allocations):
+ stocks = variant.stocks.first()
+ stocks.allocations.update(quantity_allocated=100)
+
+ assert stocks.allocations.count() == 3
+ assert stocks.quantity == 15
+
+ # Allocations are greater than available quantity, so available_quantity should
+ # be negative.
+ instance = ProductVariant.objects.annotate_quantities().get(pk=variant.pk)
+ assert instance.available_quantity == 15 - 300
+
+
+def test_available_quantity_with_no_stock(variant):
+ # Delete the stocks to simulate a product variant with no available stock
+ variant.stocks.all().delete()
+
+ instance = ProductVariant.objects.annotate_quantities().get(pk=variant.pk)
+ assert instance.available_quantity == 0