Skip to content

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

Add stock to fulfillmentline model

fowczrek2cc79bcc5180
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index e49053eeb5..b19ad90bc4 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -12,7 +12,7 @@ from django.conf import settings
from django.contrib.auth.models import Group, Permission
from django.contrib.sites.models import Site
from django.core.files import File
-from django.db.models import Q
+from django.db.models import F, Q
from django.urls import reverse
from django.utils import timezone
from django.utils.text import slugify
@@ -67,7 +67,7 @@ from ...product.thumbnails import (
create_product_thumbnails,
)
from ...shipping.models import ShippingMethod, ShippingMethodType, ShippingZone
-from ...warehouse.management import deallocate_stock, increase_stock
+from ...warehouse.management import increase_stock
from ...warehouse.models import Stock, Warehouse
fake = Factory.create()
@@ -500,11 +500,15 @@ def create_fulfillments(order):
if random.choice([False, True]):
fulfillment, _ = Fulfillment.objects.get_or_create(order=order)
quantity = random.randrange(0, line.quantity) + 1
- fulfillment.lines.create(order_line=line, quantity=quantity)
+ allocation = line.allocations.get()
+ fulfillment.lines.create(
+ order_line=line, quantity=quantity, stock=allocation.stock
+ )
line.quantity_fulfilled = quantity
line.save(update_fields=["quantity_fulfilled"])
- deallocate_stock(line, quantity)
+ allocation.quantity_allocated = F("quantity_allocated") - quantity
+ allocation.save(update_fields=["quantity_allocated"])
update_order_status(order)
diff --git a/saleor/order/migrations/0082_fulfillmentline_stock.py b/saleor/order/migrations/0082_fulfillmentline_stock.py
new file mode 100644
index 0000000000..3e62f22703
--- /dev/null
+++ b/saleor/order/migrations/0082_fulfillmentline_stock.py
@@ -0,0 +1,26 @@
+# Generated by Django 3.0.4 on 2020-04-17 11:16
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("warehouse", "0007_auto_20200406_0341"),
+ ("order", "0081_auto_20200406_0456"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="fulfillmentline",
+ name="stock",
+ field=models.ForeignKey(
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ related_name="fulfillment_lines",
+ to="warehouse.Stock",
+ ),
+ ),
+ ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 6f9b8b0b12..50d8e44890 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -462,6 +462,13 @@ class FulfillmentLine(models.Model):
Fulfillment, related_name="lines", on_delete=models.CASCADE
)
quantity = models.PositiveIntegerField()
+ stock = models.ForeignKey(
+ "warehouse.Stock",
+ related_name="fulfillment_lines",
+ on_delete=models.SET_NULL,
+ blank=True,
+ null=True,
+ )
class OrderEvent(models.Model):
diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.py
index c7acb3fd1d..75bac83214 100644
--- a/saleor/webhook/payloads.py
+++ b/saleor/webhook/payloads.py
@@ -237,7 +237,11 @@ def generate_fulfillment_payload(fulfillment: Fulfillment):
# fulfillment fields to serialize
fulfillment_fields = ("status", "tracking_code", "order__user_email")
order_country = get_order_country(fulfillment.order)
- warehouse = Warehouse.objects.for_country(order_country).first()
+ fulfillment_line = fulfillment.lines.first()
+ if fulfillment_line and fulfillment_line.stock:
+ warehouse = fulfillment_line.stock.warehouse
+ else:
+ warehouse = Warehouse.objects.for_country(order_country).first()
fulfillment_data = serializer.serialize(
[fulfillment],
fields=fulfillment_fields,