Skip to content

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

Save quantity_fulfilled to database

Anna Siwikd1b32525a08b
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index b25a15d634..78c8fbf364 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -414,6 +414,8 @@ def create_fulfillments(order):
fulfillment, _ = Fulfillment.objects.get_or_create(order=order)
quantity = random.randrange(0, line.quantity) + 1
fulfillment.lines.create(order_line=line, quantity=quantity)
+ line.quantity_fulfilled = quantity
+ line.save(update_fields=['quantity_fulfilled'])
update_order_status(order)
diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.py
index dc4b79a7fe..5b23815149 100644
--- a/saleor/dashboard/order/utils.py
+++ b/saleor/dashboard/order/utils.py
@@ -42,3 +42,5 @@ def create_packing_slip_pdf(order, fulfillment, absolute_url):
def fulfill_order_line(order_line, quantity):
"""Fulfill order line with given quantity."""
decrease_stock(order_line.stock, quantity)
+ order_line.quantity_fulfilled += quantity
+ order_line.save(update_fields=['quantity_fulfilled'])
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index f280d00c71..f2b55570f5 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -2,6 +2,7 @@ from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.db import transaction
+from django.db.models import F
from django.forms import modelformset_factory
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
@@ -386,18 +387,18 @@ def orderline_change_stock(request, order_pk, line_pk):
@staff_member_required
@permission_required('order.edit_order')
def fulfill_order_lines(request, order_pk):
- qs = Order.objects.prefetch_related('lines')
- order = get_object_or_404(qs, pk=order_pk)
- lines = order.get_unfulfilled_lines()
+ order = get_object_or_404(Order, pk=order_pk)
+ unfulfilled_lines = order.lines.filter(
+ quantity_fulfilled__lt=F('quantity'))
status = 200
form = FulfillmentForm(
request.POST or None, order=order, instance=Fulfillment())
FulfillmentLineFormSet = modelformset_factory(
- FulfillmentLine, form=FulfillmentLineForm, extra=len(lines),
+ FulfillmentLine, form=FulfillmentLineForm, extra=len(unfulfilled_lines),
formset=BaseFulfillmentLineFormSet)
initial = [
{'order_line': line, 'quantity': line.quantity_unfulfilled}
- for line in lines]
+ for line in unfulfilled_lines]
formset = FulfillmentLineFormSet(
request.POST or None, queryset=FulfillmentLine.objects.none(),
initial=initial)
@@ -436,7 +437,9 @@ def fulfill_order_lines(request, order_pk):
return redirect('dashboard:order-details', order_pk=order.pk)
elif form.errors:
status = 400
- ctx = {'form': form, 'formset': formset, 'order': order}
+ ctx = {
+ 'form': form, 'formset': formset, 'order': order,
+ 'unfulfilled_lines': unfulfilled_lines}
template = 'dashboard/order/fulfillment.html'
return TemplateResponse(request, template, ctx, status=status)
diff --git a/saleor/order/migrations/0039_auto_20180210_0422.py b/saleor/order/migrations/0039_auto_20180210_0422.py
index 6837dfc1ab..6a2dcd36f8 100644
--- a/saleor/order/migrations/0039_auto_20180210_0422.py
+++ b/saleor/order/migrations/0039_auto_20180210_0422.py
@@ -42,6 +42,11 @@ class Migration(migrations.Migration):
name='order',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='lines', to='order.Order'),
),
+ migrations.AddField(
+ model_name='orderline',
+ name='quantity_fulfilled',
+ field=models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(999)]),
+ ),
migrations.AddField(
model_name='order',
name='status',
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 9ac51e510e..ffb440b720 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -106,9 +106,6 @@ class Order(models.Model):
def __str__(self):
return '#%d' % (self.id,)
- def get_unfulfilled_lines(self):
- return [line for line in self if not line.is_fulfilled()]
-
def get_absolute_url(self):
return reverse('order:details', kwargs={'token': self.token})
@@ -170,6 +167,8 @@ class OrderLine(models.Model):
'product.Stock', on_delete=models.SET_NULL, null=True)
quantity = models.IntegerField(
validators=[MinValueValidator(0), MaxValueValidator(999)])
+ quantity_fulfilled = models.IntegerField(
+ validators=[MinValueValidator(0), MaxValueValidator(999)], default=0)
unit_price_net = MoneyField(
currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=4)
unit_price_gross = MoneyField(
@@ -187,15 +186,6 @@ class OrderLine(models.Model):
def quantity_unfulfilled(self):
return self.quantity - self.quantity_fulfilled
- @property
- def quantity_fulfilled(self):
- lines = FulfillmentLine.objects.filter(
- order_line=self, fulfillment__status=FulfillmentStatus.FULFILLED)
- return sum([line.quantity for line in lines])
-
- def is_fulfilled(self):
- return self.quantity_fulfilled >= self.quantity
-
class Fulfillment(models.Model):
fulfillment_order = models.PositiveIntegerField(editable=False)
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 94f6fe08aa..691ccd7c38 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -175,14 +175,13 @@ def merge_duplicates_into_order_line(line):
is_shipping_required=line.is_shipping_required)
if lines.count() > 1:
line.quantity = sum([line.quantity for line in lines])
- line.save()
+ line.save(update_fields=['quantity_fulfilled'])
lines.exclude(pk=line.pk).delete()
def change_order_line_quantity(line, new_quantity):
"""Change the quantity of ordered items in a order line."""
line.quantity = new_quantity
- order = line.order
if line.quantity:
line.save()
@@ -196,6 +195,8 @@ def restock_order_lines(order):
if line.stock:
if line.quantity_fulfilled > 0:
increase_stock(line.stock, line.quantity_fulfilled)
+ line.quantity_fulfilled = 0
+ line.save()
if line.quantity_unfulfilled > 0:
deallocate_stock(line.stock, line.quantity_unfulfilled)
@@ -203,5 +204,8 @@ def restock_order_lines(order):
def restock_fulfillment_lines(fulfillment):
"""Return fulfilled products to corresponding stocks."""
for line in fulfillment:
- if line.order_line.stock:
+ order_line = line.order_line
+ if order_line.stock:
increase_stock(line.order_line.stock, line.quantity)
+ order_line.quantity_fulfilled -= line.quantity
+ order_line.save(update_fields=['quantity_fulfilled'])
diff --git a/templates/dashboard/order/fulfillment.html b/templates/dashboard/order/fulfillment.html
index 11da61e4c8..0938d64386 100644
--- a/templates/dashboard/order/fulfillment.html
+++ b/templates/dashboard/order/fulfillment.html
@@ -74,7 +74,7 @@
</tr>
</thead>
<tbody>
- {% for line in order.get_unfulfilled_lines %}
+ {% for line in unfulfilled_lines %}
<tr>
<td>
{% if line.product %}