Skip to content

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

Add gift card fulfillment line relation (#8246)

Iga Karbowiakadf3ae073465
diff --git a/saleor/checkout/tests/test_checkout_complete.py b/saleor/checkout/tests/test_checkout_complete.py
index 5d3ae09579..fe63a42980 100644
--- a/saleor/checkout/tests/test_checkout_complete.py
+++ b/saleor/checkout/tests/test_checkout_complete.py
@@ -819,6 +819,7 @@ def test_create_order_gift_card_bought(
)
assert order.total.gross == total_gross
+ flush_post_commit_hooks()
gift_card = GiftCard.objects.get()
assert (
gift_card.initial_balance
diff --git a/saleor/giftcard/__init__.py b/saleor/giftcard/__init__.py
index 9ac37d7361..00af1441ce 100644
--- a/saleor/giftcard/__init__.py
+++ b/saleor/giftcard/__init__.py
@@ -1,3 +1,11 @@
+from dataclasses import dataclass
+from typing import TYPE_CHECKING, Optional
+
+if TYPE_CHECKING:
+ from ..order.models import FulfillmentLine, OrderLine
+ from ..product.models import ProductVariant
+
+
class GiftCardEvents:
"""The different gift card event types."""
@@ -28,3 +36,11 @@ class GiftCardEvents:
(NOTE_ADDED, "A note was added to the gift card."),
(USED_IN_ORDER, "The gift card was used in order."),
]
+
+
+@dataclass
+class GiftCardLineData:
+ quantity: int
+ order_line: "OrderLine"
+ variant: Optional["ProductVariant"]
+ fulfillment_line: "FulfillmentLine"
diff --git a/saleor/giftcard/migrations/0009_giftcard_fulfillment_line.py b/saleor/giftcard/migrations/0009_giftcard_fulfillment_line.py
new file mode 100644
index 0000000000..2576247033
--- /dev/null
+++ b/saleor/giftcard/migrations/0009_giftcard_fulfillment_line.py
@@ -0,0 +1,26 @@
+# Generated by Django 3.2.6 on 2021-09-29 08:19
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("order", "0119_orderline_is_gift_card"),
+ ("giftcard", "0008_auto_20210818_0633"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="giftcard",
+ name="fulfillment_line",
+ field=models.ForeignKey(
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ related_name="gift_cards",
+ to="order.fulfillmentline",
+ ),
+ ),
+ ]
diff --git a/saleor/giftcard/models.py b/saleor/giftcard/models.py
index d9f331b1e0..9b733a0135 100644
--- a/saleor/giftcard/models.py
+++ b/saleor/giftcard/models.py
@@ -61,6 +61,13 @@ class GiftCard(ModelWithMetadata):
on_delete=models.SET_NULL,
related_name="gift_cards",
)
+ fulfillment_line = models.ForeignKey(
+ "order.FulfillmentLine",
+ related_name="gift_cards",
+ on_delete=models.SET_NULL,
+ blank=True,
+ null=True,
+ )
currency = models.CharField(
max_length=settings.DEFAULT_CURRENCY_CODE_LENGTH,
diff --git a/saleor/giftcard/tests/test_utils.py b/saleor/giftcard/tests/test_utils.py
index 6be14a87d4..0d59797396 100644
--- a/saleor/giftcard/tests/test_utils.py
+++ b/saleor/giftcard/tests/test_utils.py
@@ -1,5 +1,5 @@
from datetime import date, timedelta
-from unittest.mock import ANY, patch
+from unittest.mock import patch
import pytest
from dateutil.relativedelta import relativedelta
@@ -11,9 +11,9 @@ from ...core.utils.promo_code import InvalidPromoCode
from ...order.models import OrderLine
from ...plugins.manager import get_plugins_manager
from ...site import GiftCardSettingsExpiryType
-from ...warehouse.models import Allocation
-from .. import GiftCardEvents, events
-from ..models import GiftCardEvent
+from ...tests.utils import flush_post_commit_hooks
+from .. import GiftCardEvents, GiftCardLineData, events
+from ..models import GiftCard, GiftCardEvent
from ..utils import (
add_gift_card_code_to_checkout,
calculate_expiry_date,
@@ -190,20 +190,41 @@ def test_gift_cards_create(
):
# given
manager = get_plugins_manager()
- order_lines = [gift_card_shippable_order_line, gift_card_non_shippable_order_line]
- quantities = {
- gift_card_shippable_order_line.pk: 1,
- gift_card_non_shippable_order_line.pk: 1,
- }
+ line_1, line_2 = gift_card_shippable_order_line, gift_card_non_shippable_order_line
user_email = order.user_email
+ fulfillment = order.fulfillments.create(tracking_number="123")
+ fulfillment_line_1 = fulfillment.lines.create(
+ order_line=line_1,
+ quantity=line_1.quantity,
+ stock=line_1.allocations.get().stock,
+ )
+ fulfillment_line_2 = fulfillment.lines.create(
+ order_line=line_2,
+ quantity=line_2.quantity,
+ stock=line_2.allocations.get().stock,
+ )
+ lines_data = [
+ GiftCardLineData(
+ quantity=1,
+ order_line=line_1,
+ variant=line_1.variant,
+ fulfillment_line=fulfillment_line_1,
+ ),
+ GiftCardLineData(
+ quantity=1,
+ order_line=line_2,
+ variant=line_2.variant,
+ fulfillment_line=fulfillment_line_2,
+ ),
+ ]
# when
gift_cards = gift_cards_create(
- order, order_lines, quantities, site_settings, staff_user, None, manager
+ order, lines_data, site_settings, staff_user, None, manager
)
# then
- assert len(gift_cards) == len(order_lines)
+ assert len(gift_cards) == len(lines_data)
shippable_gift_card = gift_cards[0]
shippable_price = gift_card_shippable_order_line.unit_price_gross
@@ -212,6 +233,7 @@ def test_gift_cards_create(
assert shippable_gift_card.created_by == order.user
assert shippable_gift_card.created_by_email == user_email
assert shippable_gift_card.expiry_date is None
+ assert shippable_gift_card.fulfillment_line == fulfillment_line_1
bought_event_for_shippable_card = GiftCardEvent.objects.get(
gift_card=shippable_gift_card
@@ -231,13 +253,14 @@ def test_gift_cards_create(
assert non_shippable_gift_card.created_by == order.user
assert non_shippable_gift_card.created_by_email == user_email
assert non_shippable_gift_card.expiry_date is None
+ assert non_shippable_gift_card.fulfillment_line == fulfillment_line_2
- shippable_event = GiftCardEvent.objects.get(
+ non_shippable_event = GiftCardEvent.objects.get(
gift_card=non_shippable_gift_card, type=GiftCardEvents.BOUGHT
)
- assert shippable_event.user == staff_user
- assert shippable_event.app is None
- assert shippable_event.parameters == {"order_id": order.id, "expiry_date": None}
+ assert non_shippable_event.user == staff_user
+ assert non_shippable_event.app is None
+ assert non_shippable_event.parameters == {"order_id": order.id, "expiry_date": None}
send_notification_mock.assert_called_once_with(
staff_user,
@@ -272,37 +295,46 @@ def test_gift_cards_create_expiry_date_set(
"gift_card_expiry_period",
]
)
- order_lines = [gift_card_non_shippable_order_line]
- quantities = {
- gift_card_shippable_order_line.pk: 1,
- gift_card_non_shippable_order_line.pk: 1,
- }
+ line_1 = gift_card_non_shippable_order_line
user_email = order.user_email
+ fulfillment = order.fulfillments.create(tracking_number="123")
+ fulfillment_line_1 = fulfillment.lines.create(
+ order_line=line_1,
+ quantity=line_1.quantity,
+ stock=line_1.allocations.get().stock,
+ )
+ lines_data = [
+ GiftCardLineData(
+ quantity=1,
+ order_line=line_1,
+ variant=line_1.variant,
+ fulfillment_line=fulfillment_line_1,
+ )
+ ]
# when
gift_cards = gift_cards_create(
- order, order_lines, quantities, site_settings, staff_user, None, manager
+ order, lines_data, site_settings, staff_user, None, manager
)
# then
- assert len(gift_cards) == len(order_lines)
-
- non_shippable_gift_card = gift_cards[0]
- non_shippable_price = gift_card_non_shippable_order_line.total_price_gross
- assert non_shippable_gift_card.initial_balance == non_shippable_price
- assert non_shippable_gift_card.current_balance == non_shippable_price
- assert non_shippable_gift_card.created_by == order.user
- assert non_shippable_gift_card.created_by_email == user_email
- assert non_shippable_gift_card.expiry_date
-
- shippable_event = GiftCardEvent.objects.get(
- gift_card=non_shippable_gift_card, type=GiftCardEvents.BOUGHT
- )
- assert shippable_event.user == staff_user
- assert shippable_event.app is None
- assert shippable_event.parameters == {
+ assert len(gift_cards) == len(lines_data)
+
+ gift_card = gift_cards[0]
+ price = gift_card_non_shippable_order_line.total_price_gross
+ assert gift_card.initial_balance == price
+ assert gift_card.current_balance == price
+ assert gift_card.created_by == order.user
+ assert gift_card.created_by_email == user_email
+ assert gift_card.expiry_date
+ assert gift_card.fulfillment_line == fulfillment_line_1
+
+ event = GiftCardEvent.objects.get(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
+ assert event.user == staff_user
+ assert event.app is None
+ assert event.parameters == {
"order_id": order.id,
- "expiry_date": non_shippable_gift_card.expiry_date.isoformat(),
+ "expiry_date": gift_card.expiry_date.isoformat(),
}
send_notification_mock.assert_called_once_with(
@@ -310,7 +342,7 @@ def test_gift_cards_create_expiry_date_set(
None,
order.user,
user_email,
- non_shippable_gift_card,
+ gift_card,
manager,
order.channel.slug,
resending=False,
@@ -330,12 +362,23 @@ def test_gift_cards_create_multiple_quantity(
quantity = 3
gift_card_non_shippable_order_line.quantity = quantity
gift_card_non_shippable_order_line.save(update_fields=["quantity"])
- order_lines = [gift_card_non_shippable_order_line]
- quantities = {gift_card_non_shippable_order_line.pk: quantity}
+ fulfillment = order.fulfillments.create(tracking_number="123")
+ stock = gift_card_non_shippable_order_line.allocations.get().stock
+ fulfillment_line = fulfillment.lines.create(
+ order_line=gift_card_non_shippable_order_line, quantity=quantity, stock=stock
+ )
+ lines_data = [
+ GiftCardLineData(
+ quantity=quantity,
+ order_line=gift_card_non_shippable_order_line,
+ variant=gift_card_non_shippable_order_line.variant,
+ fulfillment_line=fulfillment_line,
+ )
+ ]
# when
gift_cards = gift_cards_create(
- order, order_lines, quantities, site_settings, staff_user, None, manager
+ order, lines_data, site_settings, staff_user, None, manager
)
# then
@@ -344,6 +387,7 @@ def test_gift_cards_create_multiple_quantity(
for gift_card in gift_cards:
assert gift_card.initial_balance == price
assert gift_card.current_balance == price
+ assert gift_card.fulfillment_line == fulfillment_line
assert GiftCardEvent.objects.filter(type=GiftCardEvents.BOUGHT).count() == quantity
assert send_notification_mock.call_count == quantity
@@ -413,9 +457,7 @@ def test_get_non_shippable_gift_card_lines_no_gift_card_lines(
@patch("saleor.giftcard.utils.create_fulfillments")
-@patch("saleor.giftcard.utils.gift_cards_create")
def test_fulfill_non_shippable_gift_cards(
- gift_cards_create_mock,
create_fulfillments_mock,
order,
gift_card_shippable_order_line,
@@ -427,9 +469,6 @@ def test_fulfill_non_shippable_gift_cards(
# given
manager = get_plugins_manager()
order_lines = [gift_card_shippable_order_line, gift_card_non_shippable_order_line]
- quantities = {
- gift_card_non_shippable_order_line.pk: 1,
- }
# when
fulfill_non_shippable_gift_cards(
@@ -437,16 +476,6 @@ def test_fulfill_non_shippable_gift_cards(
)
# then
- gift_cards_create_mock.assert_called_once()
- args, _kwargs = gift_cards_create_mock.call_args
- assert args[0] == order
- assert {line.pk for line in args[1]} == {gift_card_non_shippable_order_line.pk}
- assert args[2] == quantities
- assert args[3] == site_settings
- assert args[4] == staff_user
- assert args[5] is None
- assert args[6] == manager
-
fulfillment_lines_for_warehouses = {
str(warehouse.pk): [
{
@@ -456,20 +485,19 @@ def test_fulfill_non_shippable_gift_cards(
]
}
- create_fulfillments_mock.assert_called_once_with(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- ANY,
- notify_customer=True,
- )
+ create_fulfillments_mock.assert_called_once()
+ args, kwargs = create_fulfillments_mock.call_args
+ assert args[0] == staff_user
+ assert args[1] is None
+ assert args[2] == order
+ assert args[3] == fulfillment_lines_for_warehouses
+ assert args[4] == manager
+ assert args[5] == site_settings
+ assert kwargs["notify_customer"] is True
@patch("saleor.giftcard.utils.create_fulfillments")
-@patch("saleor.giftcard.utils.gift_cards_create")
def test_fulfill_non_shippable_gift_cards_line_with_allocation(
- gift_cards_create_mock,
create_fulfillments_mock,
order,
gift_card_shippable_order_line,
@@ -481,9 +509,6 @@ def test_fulfill_non_shippable_gift_cards_line_with_allocation(
# given
manager = get_plugins_manager()
order_lines = [gift_card_shippable_order_line, gift_card_non_shippable_order_line]
- quantities = {
- gift_card_non_shippable_order_line.pk: 1,
- }
order = gift_card_non_shippable_order_line.order
non_shippable_variant = gift_card_non_shippable_order_line.variant
@@ -491,28 +516,12 @@ def test_fulfill_non_shippable_gift_cards_line_with_allocation(
non_shippable_variant.save(update_fields=["track_inventory"])
stock = non_shippable_variant.stocks.first()
- Allocation.objects.create(
- order_line=gift_card_non_shippable_order_line,
- stock=stock,
- quantity_allocated=gift_card_non_shippable_order_line.quantity,
- )
# when
fulfill_non_shippable_gift_cards(
order, order_lines, site_settings, staff_user, None, manager
)
- # then
- gift_cards_create_mock.assert_called_once()
- args, kwargs = gift_cards_create_mock.call_args
- assert args[0] == order
- assert {line.pk for line in args[1]} == {gift_card_non_shippable_order_line.pk}
- assert args[2] == quantities
- assert args[3] == site_settings
- assert args[4] == staff_user
- assert args[5] is None
- assert args[6] == manager
-
fulfillment_lines_for_warehouses = {
str(stock.warehouse.pk): [
{
@@ -522,18 +531,22 @@ def test_fulfill_non_shippable_gift_cards_line_with_allocation(
]
}
- create_fulfillments_mock.assert_called_once_with(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- ANY,
- notify_customer=True,
- )
+ create_fulfillments_mock.assert_called_once()
+ args, kwargs = create_fulfillments_mock.call_args
+ assert args[0] == staff_user
+ assert args[1] is None
+ assert args[2] == order
+ assert args[3] == fulfillment_lines_for_warehouses
+ assert args[4] == manager
+ assert args[5] == site_settings
+ assert kwargs["notify_customer"] is True
def test_fulfill_gift_card_lines(
- staff_user, gift_card_non_shippable_order_line, gift_card_shippable_order_line
+ staff_user,
+ gift_card_non_shippable_order_line,
+ gift_card_shippable_order_line,
+ site_settings,
):
# given
manager = get_plugins_manager()
@@ -542,12 +555,6 @@ def test_fulfill_gift_card_lines(
non_shippable_variant.track_inventory = True
non_shippable_variant.save(update_fields=["track_inventory"])
- Allocation.objects.create(
- order_line=gift_card_non_shippable_order_line,
- stock=non_shippable_variant.stocks.first(),
- quantity_allocated=gift_card_non_shippable_order_line.quantity,
- )
-
lines = OrderLine.objects.filter(
pk__in=[
gift_card_non_shippable_order_line.pk,
@@ -556,15 +563,40 @@ def test_fulfill_gift_card_lines(
)
# when
- fulfillments = fulfill_gift_card_lines(lines, staff_user, None, order, manager)
+ fulfillments = fulfill_gift_card_lines(
+ lines, staff_user, None, order, site_settings, manager
+ )
# then
assert len(fulfillments) == 1
assert fulfillments[0].lines.count() == len(lines)
+ flush_post_commit_hooks()
+ gift_cards = GiftCard.objects.all()
+ assert gift_cards.count() == sum([line.quantity for line in lines])
+ shippable_gift_cards = gift_cards.filter(
+ product_id=gift_card_shippable_order_line.variant.product_id
+ )
+ assert len(shippable_gift_cards) == gift_card_shippable_order_line.quantity
+ non_shippable_gift_cards = gift_cards.filter(
+ product_id=gift_card_non_shippable_order_line.variant.product_id
+ )
+ assert len(non_shippable_gift_cards) == gift_card_non_shippable_order_line.quantity
+ for card in gift_cards:
+ assert card.initial_balance.amount == round(
+ gift_card_non_shippable_order_line.unit_price_gross.amount, 2
+ )
+ assert card.current_balance.amount == round(
+ gift_card_non_shippable_order_line.unit_price_gross.amount, 2
+ )
+ assert card.fulfillment_line
+ assert GiftCardEvent.objects.filter(gift_card=card, type=GiftCardEvents.BOUGHT)
def test_fulfill_gift_card_lines_lack_of_stock(
- staff_user, gift_card_non_shippable_order_line, gift_card_shippable_order_line
+ staff_user,
+ gift_card_non_shippable_order_line,
+ gift_card_shippable_order_line,
+ site_settings,
):
# given
manager = get_plugins_manager()
@@ -580,7 +612,7 @@ def test_fulfill_gift_card_lines_lack_of_stock(
# when
with pytest.raises(ValidationError):
- fulfill_gift_card_lines(lines, staff_user, None, order, manager)
+ fulfill_gift_card_lines(lines, staff_user, None, order, site_settings, manager)
def test_deactivate_order_gift_cards(
diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.py
index 6af538e9af..6aeffc5ec6 100644
--- a/saleor/giftcard/utils.py
+++ b/saleor/giftcard/utils.py
@@ -1,6 +1,6 @@
from collections import defaultdict
from datetime import date
-from typing import TYPE_CHECKING, Dict, Iterable, Optional
+from typing import TYPE_CHECKING, Iterable, Optional
from dateutil.relativedelta import relativedelta
from django.core.exceptions import ValidationError
@@ -14,7 +14,7 @@ from ..core.utils.validators import user_is_valid
from ..order.actions import create_fulfillments
from ..order.models import OrderLine
from ..site import GiftCardSettingsExpiryType
-from . import GiftCardEvents, events
+from . import GiftCardEvents, GiftCardLineData, events
from .models import GiftCard, GiftCardEvent
from .notifications import send_gift_card_notification
@@ -87,10 +87,8 @@ def fulfill_non_shippable_gift_cards(
gift_card_lines = get_non_shippable_gift_card_lines(order_lines)
if not gift_card_lines:
return
- fulfill_gift_card_lines(gift_card_lines, requestor_user, app, order, manager)
- quantities = {line.pk: line.quantity for line in gift_card_lines}
- return gift_cards_create(
- order, gift_card_lines, quantities, settings, requestor_user, app, manager
+ fulfill_gift_card_lines(
+ gift_card_lines, requestor_user, app, order, settings, manager
)
@@ -112,6 +110,7 @@ def fulfill_gift_card_lines(
requestor_user: Optional["User"],
app: Optional["App"],
order: "Order",
+ settings: "SiteSettings",
manager: "PluginsManager",
):
lines_for_warehouses = defaultdict(list)
@@ -145,14 +144,14 @@ def fulfill_gift_card_lines(
order,
dict(lines_for_warehouses),
manager,
+ settings,
notify_customer=True,
)
def gift_cards_create(
order: "Order",
- gift_card_lines: Iterable["OrderLine"],
- quantities: Dict[int, int],
+ gift_card_lines_info: Iterable["GiftCardLineData"],
settings: "SiteSettings",
requestor_user: Optional["User"],
app: Optional["App"],
@@ -164,7 +163,8 @@ def gift_cards_create(
gift_cards = []
non_shippable_gift_cards = []
expiry_date = calculate_expiry_date(settings)
- for order_line in gift_card_lines:
+ for line_data in gift_card_lines_info:
+ order_line = line_data.order_line
price = order_line.unit_price_gross
line_gift_cards = [
GiftCard( # type: ignore
@@ -173,10 +173,11 @@ def gift_cards_create(
current_balance=price,
created_by=customer_user,
created_by_email=user_email,
- product=order_line.variant.product if order_line.variant else None,
+ product=line_data.variant.product if line_data.variant else None,
+ fulfillment_line=line_data.fulfillment_line,
expiry_date=expiry_date,
)
- for _ in range(quantities[order_line.pk])
+ for _ in range(line_data.quantity)
]
gift_cards.extend(line_gift_cards)
if not order_line.is_shipping_required:
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index bf31fa70da..78b3ab54b4 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -7,11 +7,7 @@ from django.template.defaultfilters import pluralize
from ....core.exceptions import InsufficientStock
from ....core.permissions import OrderPermissions
from ....core.tracing import traced_atomic_transaction
-from ....giftcard.utils import (
- get_gift_card_lines,
- gift_cards_create,
- order_has_gift_card_lines,
-)
+from ....giftcard.utils import order_has_gift_card_lines
from ....order import FulfillmentLineData, FulfillmentStatus, OrderLineData
from ....order import models as order_models
from ....order.actions import (
@@ -102,9 +98,9 @@ class OrderFulfill(BaseMutation):
error_type_field = "order_errors"
@classmethod
- def clean_lines(cls, order_lines, quantities):
- for order_line in order_lines:
- line_total_quantity = quantities[order_line.pk]
+ def clean_lines(cls, order_lines, quantities_for_lines):
+ for order_line, line_quantities in zip(order_lines, quantities_for_lines):
+ line_total_quantity = sum(line_quantities)
line_quantity_unfulfilled = order_line.quantity_unfulfilled
if line_total_quantity > line_quantity_unfulfilled:
@@ -220,12 +216,8 @@ class OrderFulfill(BaseMutation):
order_lines = cls.get_nodes_or_error(
lines_ids, field="lines", only_type=OrderLine
)
- order_line_id_to_total_quantity = {
- order_line.pk: sum(line_quantities)
- for order_line, line_quantities in zip(order_lines, quantities_for_lines)
- }
- cls.clean_lines(order_lines, order_line_id_to_total_quantity)
+ cls.clean_lines(order_lines, quantities_for_lines)
if site_settings.fulfillment_auto_approve:
cls.check_lines_for_preorder(order_lines)
@@ -244,8 +236,6 @@ class OrderFulfill(BaseMutation):
)
data["order_lines"] = order_lines
- data["gift_card_lines"] = get_gift_card_lines(order_lines)
- data["quantities"] = order_line_id_to_total_quantity
data["lines_for_warehouses"] = lines_for_warehouses
return data
@@ -272,22 +262,9 @@ class OrderFulfill(BaseMutation):
allow_stock_to_be_exceeded = cleaned_input.get(
"allow_stock_to_be_exceeded", False
)
- gift_card_lines = cleaned_input["gift_card_lines"]
- quantities = cleaned_input["quantities"]
approved = info.context.site.settings.fulfillment_auto_approve
- if approved:
- gift_cards_create(
- order,
- gift_card_lines,
- quantities,
- context.site.settings,
- user,
- app,
- manager,
- )
-
try:
fulfillments = create_fulfillments(
user,
@@ -295,6 +272,7 @@ class OrderFulfill(BaseMutation):
order,
dict(lines_for_warehouses),
manager,
+ context.site.settings,
notify_customer,
allow_stock_to_be_exceeded=allow_stock_to_be_exceeded,
approved=approved,
diff --git a/saleor/graphql/order/tests/test_fulfillment.py b/saleor/graphql/order/tests/test_fulfillment.py
index 1e8d14600a..8b5b26dc5e 100644
--- a/saleor/graphql/order/tests/test_fulfillment.py
+++ b/saleor/graphql/order/tests/test_fulfillment.py
@@ -127,6 +127,7 @@ def test_order_fulfill(
order,
fulfillment_lines_for_warehouses,
ANY,
+ site_settings,
True,
allow_stock_to_be_exceeded=False,
approved=fulfillment_auto_approve,
@@ -311,6 +312,7 @@ def test_order_fulfill_as_app(
order_with_lines,
permission_manage_orders,
warehouse,
+ site_settings,
):
order = order_with_lines
query = ORDER_FULFILL_QUERY
@@ -354,6 +356,7 @@ def test_order_fulfill_as_app(
order,
fulfillment_lines_for_warehouses,
ANY,
+ site_settings,
True,
allow_stock_to_be_exceeded=False,
approved=True,
@@ -368,6 +371,7 @@ def test_order_fulfill_many_warehouses(
order_with_lines,
permission_manage_orders,
warehouses,
+ site_settings,
):
order = order_with_lines
query = ORDER_FULFILL_QUERY
@@ -420,6 +424,7 @@ def test_order_fulfill_many_warehouses(
order,
fulfillment_lines_for_warehouses,
ANY,
+ site_settings,
True,
allow_stock_to_be_exceeded=False,
approved=True,
@@ -427,9 +432,7 @@ def test_order_fulfill_many_warehouses(
@patch("saleor.giftcard.utils.send_gift_card_notification")
-@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments")
def test_order_fulfill_with_gift_cards(
- mock_create_fulfillments,
mock_send_notification,
staff_api_client,
staff_user,
@@ -484,12 +487,14 @@ def test_order_fulfill_with_gift_cards(
assert non_shippable_gift_card.current_balance.amount == round(
gift_card_non_shippable_order_line.unit_price_gross.amount, 2
)
+ assert non_shippable_gift_card.fulfillment_line
assert shippable_gift_card.initial_balance.amount == round(
gift_card_shippable_order_line.unit_price_gross.amount, 2
)
assert shippable_gift_card.current_balance.amount == round(
gift_card_shippable_order_line.unit_price_gross.amount, 2
)
+ assert shippable_gift_card.fulfillment_line
assert GiftCardEvent.objects.filter(
gift_card=shippable_gift_card, type=GiftCardEvents.BOUGHT
@@ -498,23 +503,6 @@ def test_order_fulfill_with_gift_cards(
gift_card=non_shippable_gift_card, type=GiftCardEvents.BOUGHT
)
- fulfillment_lines_for_warehouses = {
- str(warehouse.pk): [
- {"order_line": order_line, "quantity": 1},
- {"order_line": order_line2, "quantity": 1},
- ]
- }
- mock_create_fulfillments.assert_called_once_with(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- ANY,
- True,
- allow_stock_to_be_exceeded=False,
- approved=True,
- )
-
mock_send_notification.assert_called_once_with(
staff_user,
None,
@@ -528,9 +516,7 @@ def test_order_fulfill_with_gift_cards(
@patch("saleor.giftcard.utils.send_gift_card_notification")
-@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments")
def test_order_fulfill_with_gift_card_lines_waiting_for_approval(
- mock_create_fulfillments,
mock_send_notification,
staff_api_client,
staff_user,
@@ -554,6 +540,7 @@ def test_order_fulfill_with_gift_card_lines_waiting_for_approval(
order_line_id = graphene.Node.to_global_id("OrderLine", order_line.id)
order_line2_id = graphene.Node.to_global_id("OrderLine", order_line2.id)
warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+ quantity = 1
variables = {
"order": order_id,
"input": {
@@ -561,11 +548,11 @@ def test_order_fulfill_with_gift_card_lines_waiting_for_approval(
"lines": [
{
"orderLineId": order_line_id,
- "stocks": [{"quantity": 1, "warehouse": warehouse_id}],
+ "stocks": [{"quantity": quantity, "warehouse": warehouse_id}],
},
{
"orderLineId": order_line2_id,
- "stocks": [{"quantity": 1, "warehouse": warehouse_id}],
+ "stocks": [{"quantity": quantity, "warehouse": warehouse_id}],
},
],
},
@@ -576,41 +563,23 @@ def test_order_fulfill_with_gift_card_lines_waiting_for_approval(
content = get_graphql_content(response)
data = content["data"]["orderFulfill"]
assert not data["errors"]
+ # ensure gift card weren't created
assert GiftCard.objects.count() == 0
- fulfillment_lines_for_warehouses = {
- str(warehouse.pk): [
- {"order_line": order_line, "quantity": 1},
- {"order_line": order_line2, "quantity": 1},
- ]
- }
- mock_create_fulfillments.assert_called_once_with(
- staff_user,
- None,
- order,
- fulfillment_lines_for_warehouses,
- ANY,
- True,
- approved=False,
- allow_stock_to_be_exceeded=False,
- )
-
mock_send_notification.assert_not_called()
@patch("saleor.giftcard.utils.send_gift_card_notification")
-@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments")
def test_order_fulfill_with_gift_cards_by_app(
- mock_create_fulfillments,
mock_send_notification,
app_api_client,
order,
gift_card_shippable_order_line,
permission_manage_orders,
warehouse,
+ site_settings,
):
query = ORDER_FULFILL_QUERY
- app = app_api_client.app
order_id = graphene.Node.to_global_id("Order", order.id)
order_line = gift_card_shippable_order_line
order_line_id = graphene.Node.to_global_id("OrderLine", order_line.id)
@@ -634,59 +603,42 @@ def test_order_fulfill_with_gift_cards_by_app(
content = get_graphql_content(response)
data = content["data"]["orderFulfill"]
assert not data["errors"]
- gift_cards = GiftCard.objects.all()
- assert gift_cards.count() == quantity
- for card in gift_cards:
- assert card.initial_balance.amount == round(
- gift_card_shippable_order_line.unit_price_gross.amount, 2
- )
- assert card.current_balance.amount == round(
- gift_card_shippable_order_line.unit_price_gross.amount, 2
- )
-
- assert GiftCardEvent.objects.filter(
- gift_card=card,
- type=GiftCardEvents.BOUGHT,
- user=None,
- app=app_api_client.app,
- )
-
- fulfillment_lines_for_warehouses = {
- str(warehouse.pk): [
- {"order_line": order_line, "quantity": 2},
- ]
- }
- mock_create_fulfillments.assert_called_once_with(
- None,
- app,
- order,
- fulfillment_lines_for_warehouses,
- ANY,
- True,
- allow_stock_to_be_exceeded=False,
- approved=True,
- )
+ assert GiftCard.objects.count() == quantity
mock_send_notification.assert_not_called
@patch("saleor.giftcard.utils.send_gift_card_notification")
-@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments")
def test_order_fulfill_with_gift_cards_multiple_warehouses(
- mock_create_fulfillments,
mock_send_notification,
app_api_client,
order,
gift_card_shippable_order_line,
permission_manage_orders,
warehouses,
+ shipping_zone,
+ site_settings,
):
query = ORDER_FULFILL_QUERY
- app = app_api_client.app
order_id = graphene.Node.to_global_id("Order", order.id)
order_line = gift_card_shippable_order_line
order_line_id = graphene.Node.to_global_id("OrderLine", order_line.id)
warehouse1, warehouse2 = warehouses
+ for warehouse in warehouses:
+ warehouse.shipping_zones.add(shipping_zone)
+ warehouse.save()
+ stock_1 = Stock.objects.create(
+ warehouse=warehouse1, product_variant=order_line.variant, quantity=1
+ )
+ Allocation.objects.create(
+ order_line=order_line, stock=stock_1, quantity_allocated=1
+ )
+ stock_2 = Stock.objects.create(
+ warehouse=warehouse2, product_variant=order_line.variant, quantity=1
+ )
+ Allocation.objects.create(
+ order_line=order_line, stock=stock_2, quantity_allocated=1
+ )
warehouse1_id = graphene.Node.to_global_id("Warehouse", warehouse1.pk)
warehouse2_id = graphene.Node.to_global_id("Warehouse", warehouse2.pk)
quantity_1 = 2
@@ -712,41 +664,7 @@ def test_order_fulfill_with_gift_cards_multiple_warehouses(
content = get_graphql_content(response)
data = content["data"]["orderFulfill"]
assert not data["errors"]
- gift_cards = GiftCard.objects.all()
- assert gift_cards.count() == quantity_1 + quantity_2
- for card in gift_cards:
- assert card.initial_balance.amount == round(
- gift_card_shippable_order_line.unit_price_gross.amount, 2
- )
- assert card.current_balance.amount == round(
- gift_card_shippable_order_line.unit_price_gross.amount, 2
- )
-
- assert GiftCardEvent.objects.filter(
- gift_card=card,
- type=GiftCardEvents.BOUGHT,
- user=None,
- app=app_api_client.app,
- )
-
- fulfillment_lines_for_warehouses = {
- str(warehouse1.pk): [
- {"order_line": order_line, "quantity": quantity_1},
- ],
- str(warehouse2.pk): [
- {"order_line": order_line, "quantity": quantity_2},
- ],
- }
- mock_create_fulfillments.assert_called_once_with(
- None,
- app,
- order,
- fulfillment_lines_for_warehouses,
- ANY,
- True,
- allow_stock_to_be_exceeded=False,
- approved=True,
- )
+ assert GiftCard.objects.count() == quantity_1 + quantity_2
mock_send_notification.assert_not_called
@@ -759,6 +677,7 @@ def test_order_fulfill_without_notification(
order_with_lines,
permission_manage_orders,
warehouse,
+ site_settings,
):
order = order_with_lines
query = ORDER_FULFILL_QUERY
@@ -794,6 +713,7 @@ def test_order_fulfill_without_notification(
order,
fulfillment_lines_for_warehouses,
ANY,
+ site_settings,
False,
allow_stock_to_be_exceeded=False,
approved=True,
@@ -809,6 +729,7 @@ def test_order_fulfill_lines_with_empty_quantity(
permission_manage_orders,
warehouse,
warehouse_no_shipping_zone,
+ site_settings,
):
order = order_with_lines
query = ORDER_FULFILL_QUERY
@@ -859,6 +780,7 @@ def test_order_fulfill_lines_with_empty_quantity(
order,
fulfillment_lines_for_warehouses,
ANY,
+ site_settings,
True,
allow_stock_to_be_exceeded=False,
approved=True,
@@ -923,6 +845,7 @@ def test_order_fulfill_without_sku(
order,
fulfillment_lines_for_warehouses,
ANY,
+ site_settings,
True,
allow_stock_to_be_exceeded=False,
approved=fulfillment_auto_approve,
@@ -1741,10 +1664,10 @@ def test_fulfillment_approve_gift_cards_created(
gift_card_line_2 = gift_card_non_shippable_order_line
stock_1 = gift_card_line_1.variant.stocks.first()
stock_2 = gift_card_line_2.variant.stocks.first()
- fulfillment.lines.create(
+ fulfillment_line_1 = fulfillment.lines.create(
order_line=gift_card_line_1, quantity=gift_card_line_1.quantity, stock=stock_1
)
- fulfillment.lines.create(
+ fulfillment_line_2 = fulfillment.lines.create(
order_line=gift_card_line_2, quantity=gift_card_line_2.quantity, stock=stock_2
)
@@ -1786,10 +1709,13 @@ def test_fulfillment_approve_gift_cards_created(
event = events[0]
assert event.type == OrderEvents.FULFILLMENT_FULFILLED_ITEMS
assert event.user == staff_api_client.user
- assert (
- GiftCard.objects.count()
- == gift_card_line_1.quantity + gift_card_line_2.quantity
- )
+ gift_cards = GiftCard.objects.all()
+ assert gift_cards.count() == gift_card_line_1.quantity + gift_card_line_2.quantity
+ for gift_card in gift_cards:
+ if gift_card.product == gift_card_line_1.variant.product:
+ assert gift_card.fulfillment_line == fulfillment_line_1
+ else:
+ assert gift_card.fulfillment_line == fulfillment_line_2
@patch("saleor.order.actions.send_fulfillment_confirmation_to_customer", autospec=True)
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 9cb4191817..55552e5c17 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -13,6 +13,7 @@ from ..core import analytics
from ..core.exceptions import AllocationError, InsufficientStock, InsufficientStockData
from ..core.tracing import traced_atomic_transaction
from ..core.transactions import transaction_with_commit_on_errors
+from ..giftcard import GiftCardLineData
from ..payment import (
ChargeStatus,
CustomPaymentChoices,
@@ -215,10 +216,22 @@ def order_fulfilled(
app: Optional["App"],
fulfillment_lines: List["FulfillmentLine"],
manager: "PluginsManager",
+ gift_card_lines_info: List[GiftCardLineData],
+ site_settings: "SiteSettings",
notify_customer=True,
):
+ from ..giftcard.utils import gift_cards_create
+
order = fulfillments[0].order
update_order_status(order)
+ gift_cards_create(
+ order,
+ gift_card_lines_info,
+ site_settings,
+ user,
+ app,
+ manager,
+ )
events.fulfillment_fulfilled_items_event(
order=order, user=user, app=app, fulfillment_lines=fulfillment_lines
)
@@ -244,6 +257,9 @@ def order_awaits_fulfillment_approval(
app: Optional["App"],
fulfillment_lines: List["FulfillmentLine"],
manager: "PluginsManager",
+ _gift_card_lines: Iterable["OrderLine"],
+ _order_line_quantities: Dict[int, int],
+ _site_settings: "SiteSettings",
_notify_customer=True,
):
order = fulfillments[0].order
@@ -375,6 +391,8 @@ def approve_fulfillment(
notify_customer=True,
allow_stock_to_be_exceeded: bool = False,
):
+ from ..giftcard.utils import gift_cards_create
+
fulfillment.status = FulfillmentStatus.FULFILLED
fulfillment.save()
order = fulfillment.order
@@ -385,15 +403,30 @@ def approve_fulfillment(
events.fulfillment_fulfilled_items_event(
order=order, user=user, app=app, fulfillment_lines=list(fulfillment.lines.all())
)
- lines_to_fulfill = [
- OrderLineData(
- line=f_line.order_line,
- quantity=f_line.quantity,
- variant=f_line.order_line.variant,
- warehouse_pk=str(f_line.stock.warehouse_id), # type: ignore
+ lines_to_fulfill = []
+ gift_card_lines_info = []
+ for fulfillment_line in fulfillment.lines.all().prefetch_related(
+ "order_line__variant"
+ ):
+ order_line = fulfillment_line.order_line
+ variant = fulfillment_line.order_line.variant
+ lines_to_fulfill.append(
+ OrderLineData(
+ line=order_line,
+ quantity=fulfillment_line.quantity,
+ variant=variant,
+ warehouse_pk=str(fulfillment_line.stock.warehouse_id), # type: ignore
+ )
)
- for f_line in fulfillment.lines.all()
- ]
+ if order_line.is_gift_card:
+ gift_card_lines_info.append(
+ GiftCardLineData(
+ quantity=fulfillment_line.quantity,
+ order_line=order_line,
+ variant=variant,
+ fulfillment_line=fulfillment_line,
+ )
+ )
_decrease_stocks(lines_to_fulfill, manager, allow_stock_to_be_exceeded)
order.refresh_from_db()
update_order_status(order)
@@ -402,42 +435,19 @@ def approve_fulfillment(
if order.status == OrderStatus.FULFILLED:
transaction.on_commit(lambda: manager.order_fulfilled(order))
- create_gift_cards_when_approving_fulfillment(
- fulfillment.order, lines_to_fulfill, user, app, manager, settings
- )
+ if gift_card_lines_info:
+ gift_cards_create(
+ order,
+ gift_card_lines_info,
+ settings,
+ user,
+ app,
+ manager,
+ )
return fulfillment
-def create_gift_cards_when_approving_fulfillment(
- order: "Order",
- lines_data: List[OrderLineData],
- user: "User",
- app: Optional["App"],
- manager: "PluginsManager",
- settings: "SiteSettings",
-):
- from ..giftcard.utils import gift_cards_create
-
- gift_card_lines = []
- quantities = {}
- for line_data in lines_data:
- if line_data.line.is_gift_card:
- line = line_data.line
- gift_card_lines.append(line)
- quantities[line.pk] = line_data.quantity
-
- gift_cards_create(
- order,
- gift_card_lines,
- quantities,
- settings,
- user,
- app,
- manager,
- )
-
-
@traced_atomic_transaction()
def mark_order_as_paid(
order: "Order",
@@ -577,6 +587,7 @@ def _create_fulfillment_lines(
warehouse_pk: str,
lines_data: List[Dict],
channel_slug: str,
+ gift_card_lines_info: List[GiftCardLineData],
manager: "PluginsManager",
decrease_stock: bool = True,
allow_stock_to_be_exceeded: bool = False,
@@ -596,6 +607,8 @@ def _create_fulfillment_lines(
...
]
channel_slug (str): Channel for which fulfillment lines should be created.
+ gift_card_lines_info (List): List with information required
+ to create gift cards.
manager (PluginsManager): Plugin manager from given context
decrease_stock (Bool): Stocks will get decreased if this is True.
allow_stock_to_be_exceeded (bool): If `True` then stock quantity could exceed.
@@ -629,9 +642,10 @@ def _create_fulfillment_lines(
order_line = line["order_line"]
if quantity > 0:
line_stocks = variant_to_stock.get(order_line.variant_id)
+ variant = order_line.variant
if line_stocks is None:
error_data = InsufficientStockData(
- variant=order_line.variant,
+ variant=variant,
order_line=order_line,
warehouse_pk=warehouse_pk,
)
@@ -642,20 +656,28 @@ def _create_fulfillment_lines(
OrderLineData(
line=order_line,
quantity=quantity,
- variant=order_line.variant,
+ variant=variant,
warehouse_pk=warehouse_pk,
)
)
if order_line.is_digital:
- order_line.variant.digital_content.urls.create(line=order_line)
- fulfillment_lines.append(
- FulfillmentLine(
- order_line=order_line,
- fulfillment=fulfillment,
- quantity=quantity,
- stock=stock,
- )
+ variant.digital_content.urls.create(line=order_line)
+ fulfillment_line = FulfillmentLine(
+ order_line=order_line,
+ fulfillment=fulfillment,
+ quantity=quantity,
+ stock=stock,
)
+ fulfillment_lines.append(fulfillment_line)
+ if order_line.is_gift_card:
+ gift_card_lines_info.append(
+ GiftCardLineData(
+ quantity=quantity,
+ order_line=order_line,
+ variant=variant,
+ fulfillment_line=fulfillment_line,
+ )
+ )
if insufficient_stocks:
raise InsufficientStock(insufficient_stocks)
@@ -675,6 +697,7 @@ def create_fulfillments(
order: "Order",
fulfillment_lines_for_warehouses: Dict,
manager: "PluginsManager",
+ site_settings: "SiteSettings",
notify_customer: bool = True,
approved: bool = True,
allow_stock_to_be_exceeded: bool = False,
@@ -702,6 +725,7 @@ def create_fulfillments(
manager (PluginsManager): Base manager for handling plugins logic.
notify_customer (bool): If `True` system send email about
fulfillments to customer.
+ site_settings (SiteSettings): Site settings used for creating gift cards.
approved (Boolean): fulfillments will have status fulfilled if it's True,
otherwise waiting_for_approval.
allow_stock_to_be_exceeded (bool): If `True` then stock quantity could exceed.
@@ -718,6 +742,7 @@ def create_fulfillments(
"""
fulfillments: List[Fulfillment] = []
fulfillment_lines: List[FulfillmentLine] = []
+ gift_card_lines_info: List[GiftCardLineData] = []
status = (
FulfillmentStatus.FULFILLED
if approved
@@ -732,6 +757,7 @@ def create_fulfillments(
warehouse_pk,
fulfillment_lines_for_warehouses[warehouse_pk],
order.channel.slug,
+ gift_card_lines_info,
manager,
decrease_stock=approved,
allow_stock_to_be_exceeded=allow_stock_to_be_exceeded,
@@ -750,9 +776,12 @@ def create_fulfillments(
app,
fulfillment_lines,
manager,
+ gift_card_lines_info,
+ site_settings,
notify_customer,
)
)
+
return fulfillments
diff --git a/saleor/order/tests/test_fulfillments_actions.py b/saleor/order/tests/test_fulfillments_actions.py
index ba7887bc62..53ef681599 100644
--- a/saleor/order/tests/test_fulfillments_actions.py
+++ b/saleor/order/tests/test_fulfillments_actions.py
@@ -17,6 +17,7 @@ def test_create_fulfillments(
staff_user,
order_with_lines,
warehouse,
+ site_settings,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -28,7 +29,13 @@ def test_create_fulfillments(
}
manager = get_plugins_manager()
[fulfillment] = create_fulfillments(
- staff_user, None, order, fulfillment_lines_for_warehouses, manager, True
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
)
flush_post_commit_hooks()
@@ -75,6 +82,7 @@ def test_create_fulfillments_require_approval(
staff_user,
order_with_lines,
warehouse,
+ site_settings,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -86,7 +94,14 @@ def test_create_fulfillments_require_approval(
}
manager = get_plugins_manager()
[fulfillment] = create_fulfillments(
- staff_user, None, order, fulfillment_lines_for_warehouses, manager, True, False
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
+ False,
)
flush_post_commit_hooks()
@@ -131,6 +146,7 @@ def test_create_fulfillments_require_approval_as_app(
app,
order_with_lines,
warehouse,
+ site_settings,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -142,7 +158,14 @@ def test_create_fulfillments_require_approval_as_app(
}
manager = get_plugins_manager()
[fulfillment] = create_fulfillments(
- None, app, order, fulfillment_lines_for_warehouses, manager, True, False
+ None,
+ app,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
+ False,
)
flush_post_commit_hooks()
@@ -188,6 +211,7 @@ def test_create_fulfillments_without_notification(
staff_user,
order_with_lines,
warehouse,
+ site_settings,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -204,6 +228,7 @@ def test_create_fulfillments_without_notification(
order,
fulfillment_lines_for_warehouses,
get_plugins_manager(),
+ site_settings,
False,
)
flush_post_commit_hooks()
@@ -238,6 +263,7 @@ def test_create_fulfillments_many_warehouses(
staff_user,
order_with_lines,
warehouses_with_shipping_zone,
+ site_settings,
):
order = order_with_lines
warehouse1, warehouse2 = warehouses_with_shipping_zone
@@ -267,6 +293,7 @@ def test_create_fulfillments_many_warehouses(
order,
fulfillment_lines_for_warehouses,
get_plugins_manager(),
+ site_settings,
False,
)
flush_post_commit_hooks()
@@ -304,6 +331,7 @@ def test_create_fulfillments_with_one_line_empty_quantity(
staff_user,
order_with_lines,
warehouse,
+ site_settings,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -316,7 +344,13 @@ def test_create_fulfillments_with_one_line_empty_quantity(
manager = get_plugins_manager()
[fulfillment] = create_fulfillments(
- staff_user, None, order, fulfillment_lines_for_warehouses, manager, True
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
)
flush_post_commit_hooks()
@@ -352,6 +386,7 @@ def test_create_fulfillments_with_variant_without_inventory_tracking(
staff_user,
order_with_line_without_inventory_tracking,
warehouse,
+ site_settings,
):
order = order_with_line_without_inventory_tracking
order_line = order.lines.get()
@@ -363,7 +398,13 @@ def test_create_fulfillments_with_variant_without_inventory_tracking(
manager = get_plugins_manager()
[fulfillment] = create_fulfillments(
- staff_user, None, order, fulfillment_lines_for_warehouses, manager, True
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
)
flush_post_commit_hooks()
@@ -394,6 +435,7 @@ def test_create_fulfillments_without_allocations(
staff_user,
order_with_lines,
warehouse,
+ site_settings,
):
order = order_with_lines
@@ -408,7 +450,13 @@ def test_create_fulfillments_without_allocations(
manager = get_plugins_manager()
[fulfillment] = create_fulfillments(
- staff_user, None, order, fulfillment_lines_for_warehouses, manager, True
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ manager,
+ site_settings,
+ True,
)
flush_post_commit_hooks()
@@ -446,6 +494,7 @@ def test_create_fulfillments_warehouse_without_stock(
staff_user,
order_with_lines,
warehouse_no_shipping_zone,
+ site_settings,
):
order = order_with_lines
order_line1, order_line2 = order.lines.all()
@@ -463,6 +512,7 @@ def test_create_fulfillments_warehouse_without_stock(
order,
fulfillment_lines_for_warehouses,
get_plugins_manager(),
+ site_settings,
True,
)
@@ -502,6 +552,7 @@ def test_create_fulfillments_with_variant_without_inventory_tracking_and_without
staff_user,
order_with_line_without_inventory_tracking,
warehouse_no_shipping_zone,
+ site_settings,
):
order = order_with_line_without_inventory_tracking
order_line = order.lines.get()
@@ -516,6 +567,7 @@ def test_create_fulfillments_with_variant_without_inventory_tracking_and_without
order,
fulfillment_lines_for_warehouses,
get_plugins_manager(),
+ site_settings,
True,
)
@@ -545,7 +597,11 @@ def test_create_fulfillments_with_variant_without_inventory_tracking_and_without
@patch("saleor.plugins.manager.PluginsManager.product_variant_out_of_stock")
def test_create_fullfilment_with_out_of_stock_webhook(
- product_variant_out_of_stock_webhook, staff_user, order_with_lines, warehouse
+ product_variant_out_of_stock_webhook,
+ staff_user,
+ order_with_lines,
+ warehouse,
+ site_settings,
):
order = order_with_lines
@@ -563,6 +619,7 @@ def test_create_fullfilment_with_out_of_stock_webhook(
order=order,
fulfillment_lines_for_warehouses=fulfillment_lines_for_warehouses,
manager=manager,
+ site_settings=site_settings,
)
flush_post_commit_hooks()
@@ -571,7 +628,11 @@ def test_create_fullfilment_with_out_of_stock_webhook(
@patch("saleor.plugins.manager.PluginsManager.product_variant_out_of_stock")
def test_create_fullfilment_with_out_of_stock_webhook_not_triggered(
- product_variant_out_of_stock_webhook, staff_user, order_with_lines, warehouse
+ product_variant_out_of_stock_webhook,
+ staff_user,
+ order_with_lines,
+ warehouse,
+ site_settings,
):
order = order_with_lines
@@ -589,6 +650,7 @@ def test_create_fullfilment_with_out_of_stock_webhook_not_triggered(
order=order,
fulfillment_lines_for_warehouses=fulfillment_lines_for_warehouses,
manager=manager,
+ site_settings=site_settings,
approved=False,
)
flush_post_commit_hooks()
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 3394a8cdd1..9d3befb37c 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -2806,7 +2806,9 @@ def order_line(order, variant):
@pytest.fixture
-def gift_card_non_shippable_order_line(order, gift_card_non_shippable_variant):
+def gift_card_non_shippable_order_line(
+ order, gift_card_non_shippable_variant, warehouse
+):
variant = gift_card_non_shippable_variant
product = variant.product
channel = order.channel
@@ -2816,7 +2818,7 @@ def gift_card_non_shippable_order_line(order, gift_card_non_shippable_variant):
gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
quantity = 1
unit_price = TaxedMoney(net=net, gross=gross)
- return order.lines.create(
+ line = order.lines.create(
product_name=str(product),
variant_name=str(variant),
product_sku=variant.sku,
@@ -2830,10 +2832,14 @@ def gift_card_non_shippable_order_line(order, gift_card_non_shippable_variant):
undiscounted_total_price=unit_price * quantity,
tax_rate=Decimal("0.23"),
)
+ Allocation.objects.create(
+ order_line=line, stock=variant.stocks.first(), quantity_allocated=line.quantity
+ )
+ return line
@pytest.fixture
-def gift_card_shippable_order_line(order, gift_card_shippable_variant):
+def gift_card_shippable_order_line(order, gift_card_shippable_variant, warehouse):
variant = gift_card_shippable_variant
product = variant.product
channel = order.channel
@@ -2843,7 +2849,7 @@ def gift_card_shippable_order_line(order, gift_card_shippable_variant):
gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
quantity = 3
unit_price = TaxedMoney(net=net, gross=gross)
- return order.lines.create(
+ line = order.lines.create(
product_name=str(product),
variant_name=str(variant),
product_sku=variant.sku,
@@ -2857,6 +2863,10 @@ def gift_card_shippable_order_line(order, gift_card_shippable_variant):
undiscounted_total_price=unit_price * quantity,
tax_rate=Decimal("0.23"),
)
+ Allocation.objects.create(
+ order_line=line, stock=variant.stocks.first(), quantity_allocated=line.quantity
+ )
+ return line
@pytest.fixture