Skip to content

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

Deallocate stock upon shipping

Patryk Zawadzkiac4805004871
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 965b5ba01e..4f9dcb0763 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -2,6 +2,7 @@ from __future__ import unicode_literals
from django import forms
from django.conf import settings
+from django.db.models import F
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import pgettext_lazy
@@ -164,6 +165,14 @@ class ShipGroupForm(forms.ModelForm):
def save(self):
order = self.instance.order
+ for line in self.instance.items.all():
+ stock = line.stock
+ if stock is not None:
+ quantity = line.quantity
+ # remove and deallocate quantity
+ stock.quantity = F('quantity') - quantity
+ stock.quantity_allocated = F('quantity_allocated') - quantity
+ stock.save(update_fields=['quantity', 'quantity_allocated'])
self.instance.change_status('shipped')
statuses = [g.status for g in order.groups.all()]
if 'shipped' in statuses and 'new' not in statuses:
diff --git a/saleor/dashboard/templates/dashboard/product/product_form.html b/saleor/dashboard/templates/dashboard/product/product_form.html
index 05974cb750..7c3f4ab51c 100644
--- a/saleor/dashboard/templates/dashboard/product/product_form.html
+++ b/saleor/dashboard/templates/dashboard/product/product_form.html
@@ -235,8 +235,9 @@
<label for="select-all-stock"></label>
</th>
<th>{% trans "SKU" %}</th>
- <th class="right-align">{% trans "Quantity" %}</th>
<th class="wide">{% trans "Location" %}</th>
+ <th class="right-align">{% trans "Quantity" %}</th>
+ <th class="right-align">{% trans "Allocated" %}</th>
</tr>
</thead>
@@ -251,8 +252,9 @@
href="{% url 'dashboard:product-stock-update' product_pk=product.pk stock_pk=item.pk %}">
{{ item.variant.sku }}
</a></td>
- <td class="right-align">{{ item.quantity }}</td>
<td>{{ item.location }}</td>
+ <td class="right-align">{{ item.quantity }}</td>
+ <td class="right-align">{{ item.quantity_allocated }}</td>
</tr>
{% endfor %}
</tbody>
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 17557d4e8d..519e15cc53 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -9,6 +9,7 @@ from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
+from django.db.models import F
from django.utils.encoding import python_2_unicode_compatible, smart_text
from django.utils.timezone import now
from django.utils.translation import pgettext_lazy
@@ -231,8 +232,8 @@ class DeliveryGroup(models.Model, ItemSet):
stock=stock,
stock_location=stock.location if stock else None)
if stock:
- stock.quantity_allocated = (
- models.F('quantity_allocated') + quantity)
+ # allocate quantity to avoid overselling
+ stock.quantity_allocated = F('quantity_allocated') + quantity
stock.save(update_fields=['quantity_allocated'])
def get_total_quantity(self):
diff --git a/saleor/product/models/base.py b/saleor/product/models/base.py
index ea0ec0993b..a403c59734 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models/base.py
@@ -164,6 +164,8 @@ class ProductVariant(models.Model, Item):
raise InsufficientStock(self)
def get_stock_quantity(self):
+ if not len(self.stock.all()):
+ return 0
return max([stock.quantity_available for stock in self.stock.all()])
def get_price_per_item(self, discounts=None, **kwargs):