Check for orders which are cancelled=False instead of those who are paid to avoid "overselling".
This commit is contained in:
parent
707eeb9190
commit
39383e0acb
|
@ -418,7 +418,7 @@ class Product(CreatedUpdatedModel, UUIDModel):
|
|||
if self.stock_amount:
|
||||
sold = OrderProductRelation.objects.filter(
|
||||
product=self,
|
||||
order__paid=True,
|
||||
order__cancelled=False,
|
||||
).aggregate(Sum('quantity'))['quantity__sum']
|
||||
|
||||
total_left = self.stock_amount - (sold or 0)
|
||||
|
|
|
@ -22,16 +22,21 @@ class ProductAvailabilityTest(TestCase):
|
|||
""" If max orders have been made, the product is NOT available. """
|
||||
product = ProductFactory(stock_amount=2)
|
||||
|
||||
for i in range(2):
|
||||
opr = OrderProductRelationFactory(product=product)
|
||||
order = opr.order
|
||||
order.paid = True
|
||||
order.save()
|
||||
opr1 = OrderProductRelationFactory(product=product)
|
||||
opr2 = OrderProductRelationFactory(product=product)
|
||||
|
||||
self.assertEqual(product.left_in_stock, 0)
|
||||
self.assertFalse(product.is_stock_available)
|
||||
self.assertFalse(product.is_available())
|
||||
|
||||
# Cancel one order
|
||||
opr1.order.mark_as_cancelled()
|
||||
|
||||
self.assertEqual(product.left_in_stock, 1)
|
||||
self.assertTrue(product.is_stock_available)
|
||||
self.assertTrue(product.is_available())
|
||||
|
||||
|
||||
def test_product_available_by_time(self):
|
||||
""" The product is available if now is in the right timeframe. """
|
||||
product = ProductFactory()
|
||||
|
|
Loading…
Reference in a new issue