|
|
@@ -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
|