Check for orders which are cancelled=False instead of those who are paid to avoid "overselling".

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-27 12:57:50 +01:00
parent 707eeb9190
commit 39383e0acb
2 changed files with 11 additions and 6 deletions

View file

@ -418,7 +418,7 @@ class Product(CreatedUpdatedModel, UUIDModel):
if self.stock_amount: if self.stock_amount:
sold = OrderProductRelation.objects.filter( sold = OrderProductRelation.objects.filter(
product=self, product=self,
order__paid=True, order__cancelled=False,
).aggregate(Sum('quantity'))['quantity__sum'] ).aggregate(Sum('quantity'))['quantity__sum']
total_left = self.stock_amount - (sold or 0) total_left = self.stock_amount - (sold or 0)

View file

@ -22,16 +22,21 @@ class ProductAvailabilityTest(TestCase):
""" If max orders have been made, the product is NOT available. """ """ If max orders have been made, the product is NOT available. """
product = ProductFactory(stock_amount=2) product = ProductFactory(stock_amount=2)
for i in range(2): opr1 = OrderProductRelationFactory(product=product)
opr = OrderProductRelationFactory(product=product) opr2 = OrderProductRelationFactory(product=product)
order = opr.order
order.paid = True
order.save()
self.assertEqual(product.left_in_stock, 0) self.assertEqual(product.left_in_stock, 0)
self.assertFalse(product.is_stock_available) self.assertFalse(product.is_stock_available)
self.assertFalse(product.is_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): def test_product_available_by_time(self):
""" The product is available if now is in the right timeframe. """ """ The product is available if now is in the right timeframe. """
product = ProductFactory() product = ProductFactory()