Przeglądaj źródła

Updated stock handling to allow checking for valid allocations

master
David Winterbottom 13 lat temu
rodzic
commit
eeab700bfa

+ 11
- 0
oscar/apps/order/processing.py Wyświetl plik

@@ -64,6 +64,17 @@ class EventHandler(object):
64 64
         """
65 65
         self.create_payment_event(order, event_type, amount, lines, line_quantities, **kwargs)
66 66
 
67
+    def are_stock_allocations_available(self, lines, line_quantities):
68
+        """
69
+        Check whether stock records still have enough stock to honour the
70
+        requested allocations.
71
+        """
72
+        for line, qty in zip(lines, line_quantities):
73
+            record = line.product.stockrecord
74
+            if not record.is_allocation_consumption_possible(qty):
75
+                return False
76
+        return True
77
+
67 78
     def consume_stock_allocations(self, order, lines, line_quantities):
68 79
         """
69 80
         Consume the stock allocations for the passed lines

+ 10
- 9
oscar/apps/partner/abstract_models.py Wyświetl plik

@@ -6,6 +6,7 @@ from django.utils.translation import ugettext_lazy as _
6 6
 from django.utils.importlib import import_module as django_import_module
7 7
 
8 8
 from oscar.core.loading import get_class
9
+from oscar.apps.partner.exceptions import InvalidStockAdjustment
9 10
 DefaultWrapper = get_class('partner.wrappers', 'DefaultWrapper')
10 11
 
11 12
 
@@ -118,6 +119,9 @@ class AbstractStockRecord(models.Model):
118 119
         self.num_allocated += quantity
119 120
         self.save()
120 121
 
122
+    def is_allocation_consumption_possible(self, quantity):
123
+        return quantity <= min(self.num_allocated, self.num_in_stock)
124
+
121 125
     def consume_allocation(self, quantity):
122 126
         """
123 127
         Consume a previous allocation
@@ -125,19 +129,16 @@ class AbstractStockRecord(models.Model):
125 129
         This is used when an item is shipped.  We remove the original allocation
126 130
         and adjust the number in stock accordingly
127 131
         """
128
-        if quantity > self.num_allocated:
129
-            raise ValueError('No more than %d units can be consumed' % self.num_allocated)
132
+        if not self.is_allocation_consumption_possible(quantity):
133
+            raise InvalidStockAdjustment('Invalid stock consumption request')
130 134
         self.num_allocated -= quantity
131
-        if quantity >= self.num_in_stock:
132
-            self.num_in_stock = 0
133
-        else:
134
-            self.num_in_stock -= quantity
135
+        self.num_in_stock -= quantity
135 136
         self.save()
136 137
 
137 138
     def cancel_allocation(self, quantity):
138
-        if quantity > self.num_allocated:
139
-            raise ValueError('No more than %d units can be cancelled' % self.num_allocated)
140
-        self.num_allocated -= quantity
139
+        # We ignore requests that request a cancellation of more than the amount already
140
+        # allocated.
141
+        self.num_allocated -= min(self.num_allocated, quantity)
141 142
         self.save()
142 143
 
143 144
     @property

+ 4
- 0
oscar/apps/partner/exceptions.py Wyświetl plik

@@ -4,3 +4,7 @@ class ImportError(Exception):
4 4
 
5 5
 class CatalogueImportError(Exception):
6 6
     pass
7
+
8
+
9
+class InvalidStockAdjustment(Exception):
10
+    pass

+ 6
- 0
oscar/apps/partner/tests/models.py Wyświetl plik

@@ -52,6 +52,12 @@ class StockRecordTests(TestCase):
52 52
         self.assertEqual(1, self.stockrecord.num_allocated)
53 53
         self.assertEqual(10, self.stockrecord.num_in_stock)
54 54
 
55
+    def test_cancelling_allocation_ignores_too_big_allocations(self):
56
+        self.stockrecord.allocate(5)
57
+        self.stockrecord.cancel_allocation(6)
58
+        self.assertEqual(0, self.stockrecord.num_allocated)
59
+        self.assertEqual(10, self.stockrecord.num_in_stock)
60
+
55 61
 
56 62
 class DefaultWrapperTests(TestCase):
57 63
 

Ładowanie…
Anuluj
Zapisz