Skip to content

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

Create gift card based on provided quantities in OrderFulfill

IKarbowiakfa44ca5761b6
diff --git a/saleor/giftcard/tests/test_utils.py b/saleor/giftcard/tests/test_utils.py
index 04ed204fb7..3aebd13692 100644
--- a/saleor/giftcard/tests/test_utils.py
+++ b/saleor/giftcard/tests/test_utils.py
@@ -178,18 +178,22 @@ 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,
+ }
user_email = order.user_email
# when
gift_cards = gift_cards_create(
- order, order_lines, site_settings, staff_user, None, manager
+ order, order_lines, quantities, site_settings, staff_user, None, manager
)
# then
assert len(gift_cards) == len(order_lines)
shippable_gift_card = gift_cards[0]
- shippable_price = gift_card_shippable_order_line.total_price_gross
+ shippable_price = gift_card_shippable_order_line.unit_price_gross
assert shippable_gift_card.initial_balance == shippable_price
assert shippable_gift_card.current_balance == shippable_price
assert shippable_gift_card.created_by == order.user
@@ -249,14 +253,20 @@ def test_gift_cards_create_multiple_quantity(
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}
# when
gift_cards = gift_cards_create(
- order, order_lines, site_settings, staff_user, None, manager
+ order, order_lines, quantities, site_settings, staff_user, None, manager
)
# then
assert len(gift_cards) == quantity
+ price = gift_card_non_shippable_order_line.unit_price_gross
+ for gift_card in gift_cards:
+ assert gift_card.initial_balance == price
+ assert gift_card.current_balance == price
+
assert GiftCardEvent.objects.filter(type=GiftCardEvents.BOUGHT).count() == quantity
assert (
GiftCardEvent.objects.filter(type=GiftCardEvents.SENT_TO_CUSTOMER).count()
diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.py
index 6c98cae6a1..ab5a1098cd 100644
--- a/saleor/giftcard/utils.py
+++ b/saleor/giftcard/utils.py
@@ -1,5 +1,5 @@
from datetime import date
-from typing import TYPE_CHECKING, Iterable, Optional
+from typing import TYPE_CHECKING, Dict, Iterable, Optional
from dateutil.relativedelta import relativedelta
from django.utils import timezone
@@ -80,6 +80,7 @@ def calculate_expiry_date(gift_card: GiftCard):
def gift_cards_create(
order: "Order",
gift_card_lines: Iterable["OrderLine"],
+ quantities: Dict[int, int],
settings: "SiteSettings",
requestor_user: Optional["User"],
app: Optional["App"],
@@ -91,7 +92,7 @@ def gift_cards_create(
gift_cards = []
non_shippable_gift_cards = []
for order_line in gift_card_lines:
- price = order_line.total_price_gross
+ price = order_line.unit_price_gross
line_gift_cards = [
GiftCard( # type: ignore
code=generate_promo_code(),
@@ -104,7 +105,7 @@ def gift_cards_create(
expiry_period_type=settings.gift_card_expiry_period_type,
expiry_period=settings.gift_card_expiry_period,
)
- for _ in range(order_line.quantity)
+ for _ in range(quantities[order_line.pk])
]
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 a4967a3d57..deca918cbc 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -98,10 +98,11 @@ class OrderFulfill(BaseMutation):
@classmethod
def clean_lines(cls, order_lines, quantities):
- for order_line, line_quantities in zip(order_lines, quantities):
+ for order_line in order_lines:
line_quantity_unfulfilled = order_line.quantity_unfulfilled
+ line_total_quantity = quantities[order_line.pk]
- if sum(line_quantities) > line_quantity_unfulfilled:
+ if line_total_quantity > line_quantity_unfulfilled:
msg = (
"Only %(quantity)d item%(item_pluralize)s remaining "
"to fulfill: %(order_line)s."
@@ -197,8 +198,12 @@ 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, quantities_for_lines)
+ cls.clean_lines(order_lines, order_line_id_to_total_quantity)
cls.check_total_quantity_of_items(quantities_for_lines)
@@ -215,7 +220,7 @@ class OrderFulfill(BaseMutation):
data["order_lines"] = order_lines
data["gift_card_lines"] = cls.get_gift_card_lines(lines_ids)
- data["quantities"] = quantities_for_lines
+ data["quantities"] = order_line_id_to_total_quantity
data["lines_for_warehouses"] = lines_for_warehouses
return data
@@ -254,10 +259,12 @@ class OrderFulfill(BaseMutation):
lines_for_warehouses = cleaned_input["lines_for_warehouses"]
notify_customer = cleaned_input.get("notify_customer", True)
gift_card_lines = cleaned_input["gift_card_lines"]
+ quantities = cleaned_input["quantities"]
gift_cards_create(
order,
gift_card_lines,
+ quantities,
context.site.settings,
user,
app,
diff --git a/saleor/graphql/order/tests/benchmark/test_order_fulfill.py b/saleor/graphql/order/tests/benchmark/test_order_fulfill.py
index a9b787cb26..fd26a1e6b3 100644
--- a/saleor/graphql/order/tests/benchmark/test_order_fulfill.py
+++ b/saleor/graphql/order/tests/benchmark/test_order_fulfill.py
@@ -86,6 +86,10 @@ def test_order_fulfill_with_gift_cards(
gift_card_non_shippable_order_line,
gift_card_shippable_order_line,
)
+
+ order_line2.quantity = 10
+ order_line2.save(update_fields=["quantity"])
+
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)
@@ -100,7 +104,7 @@ def test_order_fulfill_with_gift_cards(
},
{
"orderLineId": order_line2_id,
- "stocks": [{"quantity": 1, "warehouse": warehouse_id}],
+ "stocks": [{"quantity": 10, "warehouse": warehouse_id}],
},
],
},
diff --git a/saleor/graphql/order/tests/test_fulfillment.py b/saleor/graphql/order/tests/test_fulfillment.py
index 5335ce95ca..5ecc936fe5 100644
--- a/saleor/graphql/order/tests/test_fulfillment.py
+++ b/saleor/graphql/order/tests/test_fulfillment.py
@@ -306,16 +306,16 @@ def test_order_fulfill_with_gift_cards(
product_id=gift_card_shippable_order_line.variant.product_id
)
assert non_shippable_gift_card.initial_balance.amount == round(
- gift_card_non_shippable_order_line.total_price_gross.amount, 2
+ gift_card_non_shippable_order_line.unit_price_gross.amount, 2
)
assert non_shippable_gift_card.current_balance.amount == round(
- gift_card_non_shippable_order_line.total_price_gross.amount, 2
+ gift_card_non_shippable_order_line.unit_price_gross.amount, 2
)
assert shippable_gift_card.initial_balance.amount == round(
- gift_card_shippable_order_line.total_price_gross.amount, 2
+ gift_card_shippable_order_line.unit_price_gross.amount, 2
)
assert shippable_gift_card.current_balance.amount == round(
- gift_card_shippable_order_line.total_price_gross.amount, 2
+ gift_card_shippable_order_line.unit_price_gross.amount, 2
)
assert GiftCardEvent.objects.filter(
@@ -366,6 +366,7 @@ def test_order_fulfill_with_gift_cards_by_app(
order_line = gift_card_shippable_order_line
order_line_id = graphene.Node.to_global_id("OrderLine", order_line.id)
warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+ quantity = 2
variables = {
"order": order_id,
"input": {
@@ -373,7 +374,7 @@ def test_order_fulfill_with_gift_cards_by_app(
"lines": [
{
"orderLineId": order_line_id,
- "stocks": [{"quantity": 1, "warehouse": warehouse_id}],
+ "stocks": [{"quantity": quantity, "warehouse": warehouse_id}],
},
],
},
@@ -384,25 +385,101 @@ def test_order_fulfill_with_gift_cards_by_app(
content = get_graphql_content(response)
data = content["data"]["orderFulfill"]
assert not data["errors"]
- shippable_gift_card = GiftCard.objects.get()
- assert shippable_gift_card.initial_balance.amount == round(
- gift_card_shippable_order_line.total_price_gross.amount, 2
- )
- assert shippable_gift_card.current_balance.amount == round(
- gift_card_shippable_order_line.total_price_gross.amount, 2
+ 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, approved=True
)
- assert GiftCardEvent.objects.filter(
- gift_card=shippable_gift_card,
- type=GiftCardEvents.BOUGHT,
- user=None,
- app=app_api_client.app,
+ 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,
+):
+ 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
+ warehouse1_id = graphene.Node.to_global_id("Warehouse", warehouse1.pk)
+ warehouse2_id = graphene.Node.to_global_id("Warehouse", warehouse2.pk)
+ quantity_1 = 2
+ quantity_2 = 1
+ variables = {
+ "order": order_id,
+ "input": {
+ "notifyCustomer": True,
+ "lines": [
+ {
+ "orderLineId": order_line_id,
+ "stocks": [
+ {"quantity": quantity_1, "warehouse": warehouse1_id},
+ {"quantity": quantity_2, "warehouse": warehouse2_id},
+ ],
+ },
+ ],
+ },
+ }
+ response = app_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_orders]
)
+ 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(warehouse.pk): [
- {"order_line": order_line, "quantity": 1},
- ]
+ 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, approved=True
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 27d1da635c..54148b2a83 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -2055,7 +2055,7 @@ def variant_with_many_stocks_different_shipping_zones(
@pytest.fixture
-def gift_card_shippable_variant(shippable_gift_card_product, channel_USD):
+def gift_card_shippable_variant(shippable_gift_card_product, channel_USD, warehouse):
product = shippable_gift_card_product
product_variant = ProductVariant.objects.create(
product=product, sku="SKU_CARD_A", track_inventory=False
@@ -2067,11 +2067,16 @@ def gift_card_shippable_variant(shippable_gift_card_product, channel_USD):
cost_price_amount=Decimal(1),
currency=channel_USD.currency_code,
)
+ Stock.objects.create(
+ warehouse=warehouse, product_variant=product_variant, quantity=1
+ )
return product_variant
@pytest.fixture
-def gift_card_non_shippable_variant(non_shippable_gift_card_product, channel_USD):
+def gift_card_non_shippable_variant(
+ non_shippable_gift_card_product, channel_USD, warehouse
+):
product = non_shippable_gift_card_product
product_variant = ProductVariant.objects.create(
product=product, sku="SKU_CARD_B", track_inventory=False
@@ -2083,6 +2088,9 @@ def gift_card_non_shippable_variant(non_shippable_gift_card_product, channel_USD
cost_price_amount=Decimal(1),
currency=channel_USD.currency_code,
)
+ Stock.objects.create(
+ warehouse=warehouse, product_variant=product_variant, quantity=1
+ )
return product_variant
@@ -2685,7 +2693,7 @@ def gift_card_shippable_order_line(order, gift_card_shippable_variant):
net = variant.get_price(product, [], channel, channel_listing)
currency = net.currency
gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 1
+ quantity = 3
unit_price = TaxedMoney(net=net, gross=gross)
return order.lines.create(
product_name=str(product),