Skip to content

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

Merge duplicated lines after stock modification

Anna Siwik47f182010a22
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index ec242e8c61..79679d0b82 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -346,4 +346,6 @@ class ChangeStockForm(forms.ModelForm):
self.instance.stock_location = (
stock.location.name if stock.location else '')
Stock.objects.allocate_stock(stock, quantity)
- return super(ChangeStockForm, self).save(commit)
+ super(ChangeStockForm, self).save(commit)
+ OrderedItem.objects.merge_duplicates(self.instance)
+ return self.instance
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 8d4dc31f10..baad52f83c 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -300,13 +300,14 @@ class OrderedItemManager(models.Manager):
try:
target_item = target_group.items.get(
product=item.product, product_name=item.product_name,
- product_sku=item.product_sku)
+ product_sku=item.product_sku, stock=item.stock)
except ObjectDoesNotExist:
target_group.items.create(
delivery_group=target_group, product=item.product,
product_name=item.product_name, product_sku=item.product_sku,
quantity=quantity, unit_price_net=item.unit_price_net,
stock=item.stock,
+ stock_location=item.stock_location,
unit_price_gross=item.unit_price_gross)
else:
target_item.quantity += quantity
@@ -314,6 +315,15 @@ class OrderedItemManager(models.Manager):
item.quantity -= quantity
self.remove_empty_groups(item)
+ def merge_duplicates(self, item):
+ lines = item.delivery_group.items.filter(
+ product=item.product, product_name=item.product_name,
+ product_sku=item.product_sku, stock=item.stock)
+ if lines.count() > 1:
+ item.quantity = sum([line.quantity for line in lines])
+ item.save()
+ lines.exclude(pk=item.pk).delete()
+
def remove_empty_groups(self, item, force=False):
source_group = item.delivery_group
order = source_group.order
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index 8ccb0dd1a5..94753247ef 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -1,5 +1,7 @@
from __future__ import unicode_literals
+from decimal import Decimal
+
import pytest
from django.urls import reverse
@@ -411,3 +413,44 @@ def test_view_change_order_line_stock_insufficient_stock(
stock.refresh_from_db()
assert stock.quantity_allocated == 1
+
+
+def test_view_change_order_line_stock_merges_lines(
+ admin_client, order_with_items_and_stock):
+ order = order_with_items_and_stock
+ line = order.get_items().first()
+ group = line.delivery_group
+ old_stock = line.stock
+ variant = ProductVariant.objects.get(sku=line.product_sku)
+ stock_location = StockLocation.objects.create(name='Warehouse 2')
+ stock = Stock.objects.create(
+ variant=variant, cost_price=2, quantity=2, quantity_allocated=2,
+ location=stock_location)
+ line_2 = group.items.create(
+ delivery_group=group,
+ product=line.product,
+ product_name=line.product.name,
+ product_sku='SKU_A',
+ quantity=2,
+ unit_price_net=Decimal('30.00'),
+ unit_price_gross=Decimal('30.00'),
+ stock=stock,
+ stock_location=stock.location.name
+ )
+ lines_before = group.items.count()
+
+ url = reverse(
+ 'dashboard:orderline-change-stock', kwargs={
+ 'order_pk': order.pk,
+ 'line_pk': line_2.pk})
+ data = {'stock': old_stock.pk}
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 200
+ assert group.items.count() == lines_before - 1
+
+ old_stock.refresh_from_db()
+ assert old_stock.quantity_allocated == 5
+
+ stock.refresh_from_db()
+ assert stock.quantity_allocated == 0