Skip to content

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

Split fixtures for Order module (#16852)

Piotr Zabieglik01d33eb4d42e
diff --git a/conftest.py b/conftest.py
index 27c255dcc2..7837d72825 100644
--- a/conftest.py
+++ b/conftest.py
@@ -29,7 +29,6 @@ pytest_plugins = [
"saleor.graphql.tests.fixtures",
"saleor.graphql.checkout.tests.benchmark.fixtures",
"saleor.graphql.account.tests.benchmark.fixtures",
- "saleor.graphql.order.tests.benchmark.fixtures",
"saleor.graphql.giftcard.tests.benchmark.fixtures",
"saleor.graphql.webhook.tests.benchmark.fixtures",
"saleor.webhook.tests.subscription_webhooks.fixtures",
@@ -39,6 +38,7 @@ pytest_plugins = [
"saleor.menu.tests.fixtures",
"saleor.warehouse.tests.fixtures",
"saleor.thumbnail.tests.fixtures",
+ "saleor.order.tests.fixtures",
]
diff --git a/saleor/graphql/order/tests/queries/test_draft_order.py b/saleor/graphql/order/tests/queries/test_draft_order.py
index 2c1e8aec9a..e694840ec9 100644
--- a/saleor/graphql/order/tests/queries/test_draft_order.py
+++ b/saleor/graphql/order/tests/queries/test_draft_order.py
@@ -1,5 +1,3 @@
-import pytest
-
from .....order.models import Order
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -17,18 +15,6 @@ DRAFT_ORDER_QUERY = """
"""
-def draft_orders_in_different_channels(
- draft_order_list, channel_USD, channel_JPY, channel_PLN
-):
- draft_order_list[0].channel = channel_USD
- draft_order_list[1].channel = channel_JPY
- draft_order_list[2].channel = channel_PLN
-
- Order.objects.bulk_update(draft_order_list, ["channel"])
- return draft_order_list
-
-
def test_draft_order_query(
staff_api_client, permission_group_manage_orders, order, draft_order_list
):
diff --git a/saleor/graphql/order/tests/queries/test_draft_order_with_filter.py b/saleor/graphql/order/tests/queries/test_draft_order_with_filter.py
index 9c69407149..e57e6647f0 100644
--- a/saleor/graphql/order/tests/queries/test_draft_order_with_filter.py
+++ b/saleor/graphql/order/tests/queries/test_draft_order_with_filter.py
@@ -18,49 +18,53 @@ from ....core.connection import where_filter_qs
from ....tests.utils import get_graphql_content
from ...filters import OrderDiscountedObjectWhere
-
-def draft_orders_query_with_filter():
- query = """
- query ($filter: OrderDraftFilterInput!, ) {
- draftOrders(first: 5, filter:$filter) {
- totalCount
- edges {
- node {
- id
- }
- }
+DRAFT_ORDERS_QUERY_WITH_FILTER = """
+ query ($filter: OrderDraftFilterInput!, ) {
+ draftOrders(first: 5, filter:$filter) {
+ totalCount
+ edges {
+ node {
+ id
}
}
- """
- return query
+ }
+ }
+"""
def test_draft_orders_query_with_filter_search_by_number(
- draft_orders_query_with_filter,
draft_order,
staff_api_client,
permission_group_manage_orders,
):
+ # given
update_order_search_vector(draft_order)
variables = {"filter": {"search": draft_order.number}}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(draft_orders_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(DRAFT_ORDERS_QUERY_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
assert content["data"]["draftOrders"]["totalCount"] == 1
def test_draft_orders_query_with_filter_search_by_number_with_hash(
- draft_orders_query_with_filter,
draft_order,
staff_api_client,
permission_group_manage_orders,
):
+ # given
update_order_search_vector(draft_order)
variables = {"filter": {"search": f"#{draft_order.number}"}}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(draft_orders_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(DRAFT_ORDERS_QUERY_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
assert content["data"]["draftOrders"]["totalCount"] == 1
@@ -76,12 +80,12 @@ def test_draft_order_query_with_filter_customer_fields(
orders_filter,
user_field,
user_value,
- draft_orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
customer_user,
channel_USD,
):
+ # given
setattr(customer_user, user_field, user_value)
customer_user.save()
customer_user.refresh_from_db()
@@ -100,11 +104,14 @@ def test_draft_order_query_with_filter_customer_fields(
variables = {"filter": orders_filter}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(draft_orders_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(DRAFT_ORDERS_QUERY_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
orders = content["data"]["draftOrders"]["edges"]
order_id = graphene.Node.to_global_id("Order", order.pk)
-
assert len(orders) == 1
assert orders[0]["node"]["id"] == order_id
@@ -165,20 +172,23 @@ def test_draft_order_query_with_filter_customer_fields(
def test_draft_order_query_with_filter_created(
orders_filter,
count,
- draft_orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
channel_USD,
):
+ # given
Order.objects.create(status=OrderStatus.DRAFT, channel=channel_USD)
with freeze_time("2012-01-14"):
Order.objects.create(status=OrderStatus.DRAFT, channel=channel_USD)
variables = {"filter": orders_filter}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(draft_orders_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(DRAFT_ORDERS_QUERY_WITH_FILTER, variables)
content = get_graphql_content(response)
- orders = content["data"]["draftOrders"]["edges"]
+ # then
+ orders = content["data"]["draftOrders"]["edges"]
assert len(orders) == count
@@ -198,12 +208,12 @@ def test_draft_order_query_with_filter_created(
def test_draft_orders_query_with_filter_search(
draft_orders_filter,
count,
- draft_orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
customer_user,
channel_USD,
):
+ # given
orders = Order.objects.bulk_create(
[
Order(
@@ -246,13 +256,16 @@ def test_draft_orders_query_with_filter_search(
order.search_vector = FlatConcatSearchVector(
*prepare_order_search_vector_value(order)
)
- Order.objects.bulk_update(orders, ["search_vector"])
+ Order.objects.bulk_update(orders, ["search_vector"])
variables = {"filter": draft_orders_filter}
-
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(draft_orders_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(DRAFT_ORDERS_QUERY_WITH_FILTER, variables)
content = get_graphql_content(response)
+
+ # then
assert content["data"]["draftOrders"]["totalCount"] == count
diff --git a/saleor/graphql/order/tests/queries/test_order_with_filter.py b/saleor/graphql/order/tests/queries/test_order_with_filter.py
index d53b4b4c9c..7ae3f91020 100644
--- a/saleor/graphql/order/tests/queries/test_order_with_filter.py
+++ b/saleor/graphql/order/tests/queries/test_order_with_filter.py
@@ -3,7 +3,6 @@ from decimal import Decimal
import graphene
import pytest
-from django.utils import timezone
from freezegun import freeze_time
from prices import Money, TaxedMoney
@@ -23,44 +22,21 @@ from .....product.models import ProductVariant, ProductVariantChannelListing
from ....order.enums import OrderAuthorizeStatusEnum, OrderChargeStatusEnum
from ....tests.utils import get_graphql_content, get_graphql_content_from_response
-
-def orders_query_with_filter():
- query = """
- query ($filter: OrderFilterInput!, ) {
- orders(first: 10, filter:$filter) {
- totalCount
- edges {
- node {
- id
- }
- }
+ORDERS_QUERY_WITH_FILTERS = """
+ query ($filter: OrderFilterInput!, ) {
+ orders(first: 10, filter:$filter) {
+ totalCount
+ edges {
+ node {
+ id
}
}
- """
- return query
-
-
-def order_list_with_cc_orders(orders, warehouse_for_cc):
- order_1 = orders[0]
- order_1.collection_point = warehouse_for_cc
- order_1.collection_point_name = warehouse_for_cc.name
-
- order_2 = orders[1]
- order_2.collection_point_name = warehouse_for_cc.name
-
- order_3 = orders[2]
- order_3.collection_point = warehouse_for_cc
-
- cc_orders = [order_1, order_2, order_3]
-
- Order.objects.bulk_update(cc_orders, ["collection_point", "collection_point_name"])
- return orders
+ }
+ }
+"""
def test_order_query_with_filter_channels_with_one_channel(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
orders,
@@ -72,7 +48,7 @@ def test_order_query_with_filter_channels_with_one_channel(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -81,7 +57,6 @@ def test_order_query_with_filter_channels_with_one_channel(
def test_order_query_with_filter_channels_without_channel(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
orders,
@@ -92,7 +67,7 @@ def test_order_query_with_filter_channels_without_channel(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -101,7 +76,6 @@ def test_order_query_with_filter_channels_without_channel(
def test_order_query_with_filter_channels_with_many_channel(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
orders,
@@ -118,7 +92,7 @@ def test_order_query_with_filter_channels_with_many_channel(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -128,7 +102,6 @@ def test_order_query_with_filter_channels_with_many_channel(
def test_order_query_with_filter_channels_with_empty_channel(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
orders,
@@ -140,7 +113,7 @@ def test_order_query_with_filter_channels_with_empty_channel(
variables = {"filter": {"channels": [channel_id]}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -149,7 +122,6 @@ def test_order_query_with_filter_channels_with_empty_channel(
def test_order_query_with_filter_gift_card_used_true(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
gift_card,
@@ -164,7 +136,7 @@ def test_order_query_with_filter_gift_card_used_true(
variables = {"filter": {"giftCardUsed": True}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -176,7 +148,6 @@ def test_order_query_with_filter_gift_card_used_true(
def test_order_query_with_filter_gift_card_used_false(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
gift_card,
@@ -192,7 +163,7 @@ def test_order_query_with_filter_gift_card_used_false(
variables = {"filter": {"giftCardUsed": False}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -203,7 +174,6 @@ def test_order_query_with_filter_gift_card_used_false(
def test_order_query_with_filter_gift_card_bough_true(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
gift_card,
@@ -216,7 +186,7 @@ def test_order_query_with_filter_gift_card_bough_true(
variables = {"filter": {"giftCardBought": True}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -228,7 +198,6 @@ def test_order_query_with_filter_gift_card_bough_true(
def test_order_query_with_filter_gift_card_bought_false(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
gift_card,
@@ -242,7 +211,7 @@ def test_order_query_with_filter_gift_card_bought_false(
variables = {"filter": {"giftCardBought": False}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -306,20 +275,23 @@ def test_order_query_with_filter_gift_card_bought_false(
def test_order_query_with_filter_created(
orders_filter,
count,
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
channel_USD,
):
+ # given
Order.objects.create(channel=channel_USD)
with freeze_time("2012-01-14"):
Order.objects.create(channel=channel_USD)
variables = {"filter": orders_filter}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
- orders = content["data"]["orders"]["edges"]
+ # then
+ orders = content["data"]["orders"]["edges"]
assert len(orders) == count
@@ -346,11 +318,11 @@ def test_order_query_with_filter_created(
def test_order_query_with_filter_updated_at(
orders_filter,
count,
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
channel_USD,
):
+ # given
with freeze_time("2012-01-14 11:00:00"):
Order.objects.create(channel=channel_USD)
@@ -359,10 +331,13 @@ def test_order_query_with_filter_updated_at(
variables = {"filter": orders_filter}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
- orders = content["data"]["orders"]["edges"]
+ # then
+ orders = content["data"]["orders"]["edges"]
assert len(orders) == count
@@ -382,7 +357,6 @@ def test_order_query_with_filter_payment_status(
orders_filter,
count,
payment_status,
- orders_query_with_filter,
staff_api_client,
payment_dummy,
permission_group_manage_orders,
@@ -398,7 +372,7 @@ def test_order_query_with_filter_payment_status(
variables = {"filter": orders_filter}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
orders = content["data"]["orders"]["edges"]
@@ -406,7 +380,6 @@ def test_order_query_with_filter_payment_status(
def test_order_query_with_filter_payment_fully_refunded_not_active(
- orders_query_with_filter,
staff_api_client,
payment_dummy,
permission_group_manage_orders,
@@ -421,7 +394,7 @@ def test_order_query_with_filter_payment_fully_refunded_not_active(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
orders = content["data"]["orders"]["edges"]
@@ -443,7 +416,6 @@ def test_order_query_with_filter_status(
orders_filter,
count,
status,
- orders_query_with_filter,
staff_api_client,
payment_dummy,
permission_group_manage_orders,
@@ -454,7 +426,7 @@ def test_order_query_with_filter_status(
variables = {"filter": orders_filter}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
orders = content["data"]["orders"]["edges"]
order_id = graphene.Node.to_global_id("Order", order1.pk)
@@ -476,7 +448,6 @@ def test_order_query_with_filter_customer_fields(
orders_filter,
user_field,
user_value,
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
customer_user,
@@ -491,7 +462,7 @@ def test_order_query_with_filter_customer_fields(
variables = {"filter": orders_filter}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
orders = content["data"]["orders"]["edges"]
order_id = graphene.Node.to_global_id("Order", order.pk)
@@ -501,7 +472,6 @@ def test_order_query_with_filter_customer_fields(
def test_order_query_with_filter_is_click_and_collect_true(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
order_list_with_cc_orders,
@@ -512,7 +482,7 @@ def test_order_query_with_filter_is_click_and_collect_true(
variables = {"filter": {"isClickAndCollect": True}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -529,7 +499,6 @@ def test_order_query_with_filter_is_click_and_collect_true(
def test_order_query_with_filter_is_click_and_collect_false(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
order_list_with_cc_orders,
@@ -540,7 +509,7 @@ def test_order_query_with_filter_is_click_and_collect_false(
variables = {"filter": {"isClickAndCollect": False}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -558,53 +527,8 @@ def test_order_query_with_filter_is_click_and_collect_false(
}
-@freeze_time("2021-11-01 12:00:01")
-def preorders(orders, product):
- variants = [
- ProductVariant(
- product=product,
- is_preorder=True,
- sku=f"Preorder product variant #{i}",
- )
- for i in (1, 2, 3, 4)
- ]
- variants[1].preorder_end_date = timezone.now() + datetime.timedelta(days=1)
- variants[2].preorder_end_date = timezone.now()
- variants[3].preorder_end_date = timezone.now() - datetime.timedelta(days=1)
- ProductVariant.objects.bulk_create(variants)
-
- lines = [
- OrderLine(
- order=order,
- product_name=str(product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=1,
- variant=variant,
- unit_price_net_amount=Decimal("10.0"),
- unit_price_gross_amount=Decimal("10.0"),
- currency="USD",
- total_price_net_amount=Decimal("10.0"),
- total_price_gross_amount=Decimal("10.0"),
- undiscounted_unit_price_net_amount=Decimal("10.0"),
- undiscounted_unit_price_gross_amount=Decimal("10.0"),
- undiscounted_total_price_net_amount=Decimal("10.0"),
- undiscounted_total_price_gross_amount=Decimal("10.0"),
- )
- for variant, order in zip(variants, orders)
- ]
- OrderLine.objects.bulk_create(lines)
- preorders = orders[: len(variants) - 1]
- return preorders
-
-
@freeze_time("2021-11-01 12:00:01")
def test_order_query_with_filter_is_preorder_true(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
preorders,
@@ -614,7 +538,7 @@ def test_order_query_with_filter_is_preorder_true(
variables = {"filter": {"isPreorder": True}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -627,7 +551,6 @@ def test_order_query_with_filter_is_preorder_true(
@freeze_time("2021-11-01 12:00:01")
def test_order_query_with_filter_is_preorder_false(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
preorders,
@@ -637,7 +560,7 @@ def test_order_query_with_filter_is_preorder_false(
variables = {"filter": {"isPreorder": False}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -667,7 +590,6 @@ def test_order_query_with_filter_is_preorder_false(
def test_orders_query_with_filter_search(
orders_filter,
count,
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
customer_user,
@@ -747,13 +669,12 @@ def test_orders_query_with_filter_search(
variables = {"filter": orders_filter}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
assert content["data"]["orders"]["totalCount"] == count
def test_orders_query_with_filter_search_by_global_payment_id(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
customer_user,
@@ -793,13 +714,12 @@ def test_orders_query_with_filter_search_by_global_payment_id(
variables = {"filter": {"search": global_id}}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
assert content["data"]["orders"]["totalCount"] == 1
def test_orders_query_with_filter_search_by_number(
- orders_query_with_filter,
order_generator,
staff_api_client,
permission_group_manage_orders,
@@ -807,13 +727,12 @@ def test_orders_query_with_filter_search_by_number(
order = order_generator(search_vector_class=FlatConcatSearchVector)
variables = {"filter": {"search": order.number}}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
assert content["data"]["orders"]["totalCount"] == 1
def test_orders_query_with_filter_search_by_number_with_hash(
- orders_query_with_filter,
order_generator,
staff_api_client,
permission_group_manage_orders,
@@ -821,26 +740,24 @@ def test_orders_query_with_filter_search_by_number_with_hash(
order = order_generator(search_vector_class=FlatConcatSearchVector)
variables = {"filter": {"search": f"#{order.number}"}}
permission_group_manage_orders.user_set.add(staff_api_client.user)
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
assert content["data"]["orders"]["totalCount"] == 1
def test_orders_query_with_filter_search_by_product_sku_with_multiple_identic_sku(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
allocations,
):
permission_group_manage_orders.user_set.add(staff_api_client.user)
variables = {"filter": {"search": allocations[0].order_line.product_sku}}
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
assert content["data"]["orders"]["totalCount"] == 3
def test_order_query_with_filter_search_by_product_sku_order_line(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
order_line,
@@ -875,7 +792,6 @@ def test_order_query_with_filter_search_by_product_sku_order_line(
def test_orders_query_with_filter_by_orders_id(
- orders_query_with_filter,
staff_api_client,
order,
permission_group_manage_orders,
@@ -901,7 +817,7 @@ def test_orders_query_with_filter_by_orders_id(
variables = {"filter": {"ids": orders_ids}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
edges = content["data"]["orders"]["edges"]
response_ids = [edge["node"]["id"] for edge in edges]
@@ -912,7 +828,6 @@ def test_orders_query_with_filter_by_orders_id(
def test_orders_query_with_filter_by_old_orders_id(
- orders_query_with_filter,
staff_api_client,
order,
permission_group_manage_orders,
@@ -940,7 +855,7 @@ def test_orders_query_with_filter_by_old_orders_id(
variables = {"filter": {"ids": orders_ids}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
edges = content["data"]["orders"]["edges"]
response_ids = [edge["node"]["id"] for edge in edges]
@@ -951,7 +866,6 @@ def test_orders_query_with_filter_by_old_orders_id(
def test_orders_query_with_filter_by_old_and_new_orders_id(
- orders_query_with_filter,
staff_api_client,
order,
permission_group_manage_orders,
@@ -981,7 +895,7 @@ def test_orders_query_with_filter_by_old_and_new_orders_id(
variables = {"filter": {"ids": orders_ids}}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
edges = content["data"]["orders"]["edges"]
response_ids = [edge["node"]["id"] for edge in edges]
@@ -994,7 +908,6 @@ def test_orders_query_with_filter_by_old_and_new_orders_id(
def test_order_query_with_filter_search_by_product_sku_multi_order_lines(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
product,
@@ -1075,7 +988,7 @@ def test_order_query_with_filter_search_by_product_sku_multi_order_lines(
permission_group_manage_orders.user_set.add(staff_api_client.user)
variables = {"filter": {"search": lines[0].product_sku}}
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content(response)
assert content["data"]["orders"]["totalCount"] == 1
@@ -1119,7 +1032,6 @@ def test_orders_query_with_filter_authorize_status(
transaction_data,
statuses,
expected_count,
- orders_query_with_filter,
order_with_lines,
order,
staff_api_client,
@@ -1154,7 +1066,7 @@ def test_orders_query_with_filter_authorize_status(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -1200,7 +1112,6 @@ def test_orders_query_with_filter_charge_status(
transaction_data,
statuses,
expected_count,
- orders_query_with_filter,
order_with_lines,
order,
staff_api_client,
@@ -1233,7 +1144,7 @@ def test_orders_query_with_filter_charge_status(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -1241,7 +1152,6 @@ def test_orders_query_with_filter_charge_status(
def test_order_query_with_filter_numbers(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
orders,
@@ -1256,7 +1166,7 @@ def test_order_query_with_filter_numbers(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -1270,7 +1180,6 @@ def test_order_query_with_filter_numbers(
def test_order_query_with_filter_not_allow_numbers_and_ids_together(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
orders,
@@ -1287,7 +1196,7 @@ def test_order_query_with_filter_not_allow_numbers_and_ids_together(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
content = get_graphql_content_from_response(response)
# then
@@ -1296,7 +1205,6 @@ def test_order_query_with_filter_not_allow_numbers_and_ids_together(
def test_order_query_with_filter_by_checkout_token(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
orders_from_checkout,
@@ -1314,7 +1222,7 @@ def test_order_query_with_filter_by_checkout_token(
}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -1331,7 +1239,6 @@ def test_order_query_with_filter_by_checkout_token(
def test_order_query_with_filter_by_multiple_checkout_tokens(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
order_generator,
@@ -1357,7 +1264,7 @@ def test_order_query_with_filter_by_multiple_checkout_tokens(
}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -1374,7 +1281,6 @@ def test_order_query_with_filter_by_multiple_checkout_tokens(
def test_order_query_with_filter_by_empty_list(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
orders_from_checkout,
@@ -1388,7 +1294,7 @@ def test_order_query_with_filter_by_empty_list(
}
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -1396,7 +1302,6 @@ def test_order_query_with_filter_by_empty_list(
def test_order_query_with_filter_checkout_tokens(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
order,
@@ -1414,7 +1319,7 @@ def test_order_query_with_filter_checkout_tokens(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
@@ -1427,7 +1332,6 @@ def test_order_query_with_filter_checkout_tokens(
def test_order_query_with_filter_checkout_tokens_empty_list(
- orders_query_with_filter,
staff_api_client,
permission_group_manage_orders,
order,
@@ -1445,7 +1349,7 @@ def test_order_query_with_filter_checkout_tokens_empty_list(
permission_group_manage_orders.user_set.add(staff_api_client.user)
# when
- response = staff_api_client.post_graphql(orders_query_with_filter, variables)
+ response = staff_api_client.post_graphql(ORDERS_QUERY_WITH_FILTERS, variables)
# then
content = get_graphql_content(response)
diff --git a/saleor/order/tests/fixtures/__init__.py b/saleor/order/tests/fixtures/__init__.py
new file mode 100644
index 0000000000..6b9e2950e4
--- /dev/null
+++ b/saleor/order/tests/fixtures/__init__.py
@@ -0,0 +1,5 @@
+from .benchmark import * # noqa: F403
+from .draft_order import * # noqa: F403
+from .fulfillment import * # noqa: F403
+from .order import * # noqa: F403
+from .order_line import * # noqa: F403
diff --git a/saleor/graphql/order/tests/benchmark/fixtures.py b/saleor/order/tests/fixtures/benchmark.py
similarity index 94%
rename from saleor/graphql/order/tests/benchmark/fixtures.py
rename to saleor/order/tests/fixtures/benchmark.py
index 4735218ae1..9e1772b874 100644
--- a/saleor/graphql/order/tests/benchmark/fixtures.py
+++ b/saleor/order/tests/fixtures/benchmark.py
@@ -4,11 +4,11 @@ from decimal import Decimal
import pytest
from prices import Money, TaxedMoney
-from .....account.models import User
-from .....order import OrderEvents, OrderStatus
-from .....order.models import Fulfillment, FulfillmentLine, Order, OrderEvent, OrderLine
-from .....payment import ChargeStatus
-from .....payment.models import Payment, Transaction
+from ....account.models import User
+from ....payment import ChargeStatus
+from ....payment.models import Payment, Transaction
+from ... import OrderEvents, OrderStatus
+from ...models import Fulfillment, FulfillmentLine, Order, OrderEvent, OrderLine
ORDER_COUNT_IN_BENCHMARKS = 10
EVENTS_PER_ORDER = 5
diff --git a/saleor/order/tests/fixtures/draft_order.py b/saleor/order/tests/fixtures/draft_order.py
new file mode 100644
index 0000000000..06974449a0
--- /dev/null
+++ b/saleor/order/tests/fixtures/draft_order.py
@@ -0,0 +1,160 @@
+from decimal import Decimal
+from functools import partial
+
+import pytest
+from django.conf import settings
+from prices import Money, TaxedMoney, fixed_discount
+
+from ....discount import DiscountType, DiscountValueType
+from ....discount.models import VoucherCode
+from ....discount.utils.voucher import (
+ create_or_update_discount_object_from_order_level_voucher,
+)
+from ....warehouse.models import Allocation, PreorderAllocation
+from ... import OrderOrigin, OrderStatus
+from ...base_calculations import base_order_subtotal
+from ...models import Order
+
+
+def draft_order(order_with_lines):
+ Allocation.objects.filter(order_line__order=order_with_lines).delete()
+ order_with_lines.status = OrderStatus.DRAFT
+ order_with_lines.origin = OrderOrigin.DRAFT
+ order_with_lines.save(update_fields=["status", "origin"])
+ return order_with_lines
+
+
+def draft_order_with_fixed_discount_order(draft_order):
+ value = Decimal("20")
+ discount = partial(fixed_discount, discount=Money(value, draft_order.currency))
+ draft_order.total = discount(draft_order.total)
+ draft_order.discounts.create(
+ value_type=DiscountValueType.FIXED,
+ type=DiscountType.MANUAL,
+ value=value,
+ reason="Discount reason",
+ amount=(draft_order.undiscounted_total - draft_order.total).gross,
+ )
+ draft_order.save()
+ return draft_order
+
+
+def draft_order_with_voucher(
+ draft_order_with_fixed_discount_order, voucher_multiple_use
+):
+ order = draft_order_with_fixed_discount_order
+ voucher_code = voucher_multiple_use.codes.first()
+ discount = order.discounts.first()
+ discount.type = DiscountType.VOUCHER
+ discount.voucher = voucher_multiple_use
+ discount.voucher_code = voucher_code.code
+ discount.save(update_fields=["type", "voucher", "voucher_code"])
+
+ order.voucher = voucher_multiple_use
+ order.voucher_code = voucher_code.code
+ order.save(update_fields=["voucher", "voucher_code"])
+
+ channel = order.channel
+ channel.include_draft_order_in_voucher_usage = True
+ channel.save(update_fields=["include_draft_order_in_voucher_usage"])
+
+ return order
+
+
+def draft_order_with_free_shipping_voucher(
+ draft_order_with_fixed_discount_order, voucher_free_shipping
+):
+ order = draft_order_with_fixed_discount_order
+ voucher_code = voucher_free_shipping.codes.first()
+ discount = order.discounts.first()
+ discount.type = DiscountType.VOUCHER
+ discount.voucher = voucher_free_shipping
+ discount.voucher_code = voucher_code.code
+ discount.save(update_fields=["type", "voucher", "voucher_code"])
+
+ channel = order.channel
+ channel.include_draft_order_in_voucher_usage = True
+ channel.save(update_fields=["include_draft_order_in_voucher_usage"])
+
+ order.voucher = voucher_free_shipping
+ order.voucher_code = voucher_code.code
+ create_or_update_discount_object_from_order_level_voucher(
+ order, settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
+
+ subtotal = base_order_subtotal(order, order.lines.all())
+ shipping_price = order.base_shipping_price
+ order.subtotal = TaxedMoney(gross=subtotal, net=subtotal)
+ order.shipping_price = TaxedMoney(net=shipping_price, gross=shipping_price)
+ total = subtotal + shipping_price
+ order.total = TaxedMoney(net=total, gross=total)
+ order.save()
+
+ return order
+
+
+def draft_order_without_inventory_tracking(order_with_line_without_inventory_tracking):
+ order_with_line_without_inventory_tracking.status = OrderStatus.DRAFT
+ order_with_line_without_inventory_tracking.origin = OrderStatus.DRAFT
+ order_with_line_without_inventory_tracking.save(update_fields=["status", "origin"])
+ return order_with_line_without_inventory_tracking
+
+
+def draft_order_with_preorder_lines(order_with_preorder_lines):
+ PreorderAllocation.objects.filter(
+ order_line__order=order_with_preorder_lines
+ ).delete()
+ order_with_preorder_lines.status = OrderStatus.DRAFT
+ order_with_preorder_lines.origin = OrderOrigin.DRAFT
+ order_with_preorder_lines.save(update_fields=["status", "origin"])
+ return order_with_preorder_lines
+
+
+def draft_order_list_with_multiple_use_voucher(draft_order_list, voucher_multiple_use):
+ codes = voucher_multiple_use.codes.values_list("code", flat=True)
+ for idx, order in enumerate(draft_order_list):
+ order.voucher_code = codes[idx]
+ Order.objects.bulk_update(draft_order_list, ["voucher_code"])
+ return draft_order_list
+
+
+def draft_order_list_with_single_use_voucher(draft_order_list, voucher_single_use):
+ voucher_codes = voucher_single_use.codes.all()
+ codes = voucher_codes.values_list("code", flat=True)
+ for idx, order in enumerate(draft_order_list):
+ order.voucher_code = codes[idx]
+ for voucher_code in voucher_codes:
+ voucher_code.is_active = False
+ Order.objects.bulk_update(draft_order_list, ["voucher_code"])
+ VoucherCode.objects.bulk_update(voucher_codes, ["is_active"])
+ return draft_order_list
+
+
+def draft_order_list(order_list):
+ for order in order_list:
+ order.status = OrderStatus.DRAFT
+ order.origin = OrderOrigin.DRAFT
+
+ Order.objects.bulk_update(order_list, ["status", "origin"])
+ return order_list
+
+
+def draft_orders_in_different_channels(
+ draft_order_list, channel_USD, channel_JPY, channel_PLN
+):
+ draft_order_list[0].channel = channel_USD
+ draft_order_list[1].channel = channel_JPY
+ draft_order_list[2].channel = channel_PLN
+
+ Order.objects.bulk_update(draft_order_list, ["channel"])
+ return draft_order_list
diff --git a/saleor/order/tests/fixtures/fulfillment.py b/saleor/order/tests/fixtures/fulfillment.py
new file mode 100644
index 0000000000..39d5e6249d
--- /dev/null
+++ b/saleor/order/tests/fixtures/fulfillment.py
@@ -0,0 +1,65 @@
+from collections import namedtuple
+
+import graphene
+import pytest
+
+from ...models import FulfillmentLine, FulfillmentStatus, OrderLine
+
+
+def fulfillment(fulfilled_order):
+ return fulfilled_order.fulfillments.first()
+
+
+def fulfillment_awaiting_approval(fulfilled_order):
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL
+ fulfillment.save(update_fields=["status"])
+
+ quantity = 1
+ fulfillment_lines_to_update = []
+ order_lines_to_update = []
+ for f_line in fulfillment.lines.all():
+ f_line.quantity = quantity
+ fulfillment_lines_to_update.append(f_line)
+
+ order_line = f_line.order_line
+ order_line.quantity_fulfilled = quantity
+ order_lines_to_update.append(order_line)
+
+ FulfillmentLine.objects.bulk_update(fulfillment_lines_to_update, ["quantity"])
+ OrderLine.objects.bulk_update(order_lines_to_update, ["quantity_fulfilled"])
+
+ return fulfillment
+
+
+def order_fulfill_data(order_with_lines, warehouse, checkout):
+ FulfillmentData = namedtuple("FulfillmentData", "order variables warehouse")
+ order = order_with_lines
+ order_id = graphene.Node.to_global_id("Order", order.id)
+ order_line, order_line2 = order.lines.all()
+ 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)
+
+ variables = {
+ "order": order_id,
+ "input": {
+ "notifyCustomer": False,
+ "allowStockToBeExceeded": True,
+ "lines": [
+ {
+ "orderLineId": order_line_id,
+ "stocks": [{"quantity": 3, "warehouse": warehouse_id}],
+ },
+ {
+ "orderLineId": order_line2_id,
+ "stocks": [{"quantity": 2, "warehouse": warehouse_id}],
+ },
+ ],
+ },
+ }
+
+ return FulfillmentData(order, variables, warehouse)
diff --git a/saleor/order/tests/fixtures/order.py b/saleor/order/tests/fixtures/order.py
new file mode 100644
index 0000000000..dfb8cd0c70
--- /dev/null
+++ b/saleor/order/tests/fixtures/order.py
@@ -0,0 +1,1053 @@
+import datetime
+from datetime import timedelta
+from decimal import Decimal
+
+import graphene
+import pytest
+from django.utils import timezone
+from freezegun import freeze_time
+from prices import Money, TaxedMoney
+
+from ....checkout.utils import get_prices_of_discounted_specific_product
+from ....core import JobStatus
+from ....core.taxes import zero_money
+from ....discount import DiscountType, RewardType, RewardValueType, VoucherType
+from ....discount.models import NotApplicable, Voucher
+from ....discount.utils.voucher import (
+ get_products_voucher_discount,
+ validate_voucher_in_order,
+)
+from ....payment.model_helpers import get_subtotal
+from ....plugins.manager import get_plugins_manager
+from ....product.models import (
+ Product,
+ ProductChannelListing,
+ ProductVariant,
+ ProductVariantChannelListing,
+)
+from ....tax.utils import calculate_tax_rate, get_tax_class_kwargs_for_order_line
+from ....warehouse.models import Allocation, PreorderAllocation, Stock
+from ... import OrderOrigin, OrderStatus
+from ...actions import cancel_fulfillment, fulfill_order_lines
+from ...events import (
+ OrderEvents,
+ fulfillment_refunded_event,
+ order_added_products_event,
+)
+from ...fetch import OrderLineInfo
+from ...models import FulfillmentStatus, Order, OrderEvent, OrderLine
+from ...search import prepare_order_search_vector_value
+from ...utils import get_voucher_discount_assigned_to_order
+
+
+def recalculate_order(order):
+ lines = OrderLine.objects.filter(order_id=order.pk)
+ prices = [line.total_price for line in lines]
+ total = sum(prices, order.shipping_price)
+ undiscounted_total = TaxedMoney(total.net, total.gross)
+
+ try:
+ discount = get_voucher_discount_for_order(order)
+ except NotApplicable:
+ discount = zero_money(order.currency)
+
+ discount = min(discount, total.gross)
+ total -= discount
+
+ order.total = total
+ order.subtotal = get_subtotal(order.lines.all(), order.currency)
+ order.undiscounted_total = undiscounted_total
+
+ if discount:
+ assigned_order_discount = get_voucher_discount_assigned_to_order(order)
+ if assigned_order_discount:
+ assigned_order_discount.amount_value = discount.amount
+ assigned_order_discount.value = discount.amount
+ assigned_order_discount.save(update_fields=["value", "amount_value"])
+
+ order.save()
+
+
+def get_voucher_discount_for_order(order: Order) -> Money:
+ """Calculate discount value depending on voucher and discount types.
+
+ Raise NotApplicable if voucher of given type cannot be applied.
+ """
+ if not order.voucher:
+ return zero_money(order.currency)
+ validate_voucher_in_order(order)
+ subtotal = order.subtotal
+ if order.voucher.type == VoucherType.ENTIRE_ORDER:
+ return order.voucher.get_discount_amount_for(subtotal.gross, order.channel)
+ if order.voucher.type == VoucherType.SHIPPING:
+ return order.voucher.get_discount_amount_for(
+ order.shipping_price.gross, order.channel
+ )
+ if order.voucher.type == VoucherType.SPECIFIC_PRODUCT:
+ return get_products_voucher_discount_for_order(order, order.voucher)
+ raise NotImplementedError("Unknown discount type")
+
+
+def get_products_voucher_discount_for_order(order: Order, voucher: Voucher) -> Money:
+ """Calculate products discount value for a voucher, depending on its type."""
+ prices = None
+ if voucher and voucher.type == VoucherType.SPECIFIC_PRODUCT:
+ prices = get_prices_of_discounted_specific_product(order.lines.all(), voucher)
+ if not prices:
+ msg = "This offer is only valid for selected items."
+ raise NotApplicable(msg)
+ return get_products_voucher_discount(voucher, prices, order.channel)
+
+
+def orders(customer_user, channel_USD, channel_PLN):
+ return Order.objects.bulk_create(
+ [
+ Order(
+ user=customer_user,
+ status=OrderStatus.CANCELED,
+ channel=channel_USD,
+ ),
+ Order(
+ user=customer_user,
+ status=OrderStatus.UNFULFILLED,
+ channel=channel_USD,
+ ),
+ Order(
+ user=customer_user,
+ status=OrderStatus.PARTIALLY_FULFILLED,
+ channel=channel_USD,
+ ),
+ Order(
+ user=customer_user,
+ status=OrderStatus.FULFILLED,
+ channel=channel_PLN,
+ ),
+ Order(
+ user=customer_user,
+ status=OrderStatus.DRAFT,
+ channel=channel_PLN,
+ ),
+ Order(
+ user=customer_user,
+ status=OrderStatus.UNCONFIRMED,
+ channel=channel_PLN,
+ ),
+ ]
+ )
+
+
+def orders_from_checkout(customer_user, checkout):
+ return Order.objects.bulk_create(
+ [
+ Order(
+ user=customer_user,
+ status=OrderStatus.CANCELED,
+ channel=checkout.channel,
+ checkout_token=checkout.token,
+ ),
+ Order(
+ user=customer_user,
+ status=OrderStatus.UNFULFILLED,
+ channel=checkout.channel,
+ checkout_token=checkout.token,
+ ),
+ Order(
+ user=customer_user,
+ status=OrderStatus.FULFILLED,
+ channel=checkout.channel,
+ checkout_token=checkout.token,
+ ),
+ Order(
+ user=customer_user,
+ status=OrderStatus.FULFILLED,
+ channel=checkout.channel,
+ checkout_token=checkout.token,
+ ),
+ ]
+ )
+
+
+def order_generator(customer_user, channel_USD):
+ address = customer_user.default_billing_address.get_copy()
+
+ def create_order(
+ billing_address=address,
+ channel=channel_USD,
+ currency=channel_USD.currency_code,
+ shipping_address=address,
+ user_email=customer_user.email,
+ user=customer_user,
+ origin=OrderOrigin.CHECKOUT,
+ should_refresh_prices=False,
+ checkout_token="",
+ status=OrderStatus.UNFULFILLED,
+ search_vector_class=None,
+ ):
+ order = Order.objects.create(
+ billing_address=billing_address,
+ channel=channel,
+ currency=currency,
+ shipping_address=shipping_address,
+ user_email=user_email,
+ user=user,
+ origin=origin,
+ should_refresh_prices=should_refresh_prices,
+ metadata={"key": "value"},
+ private_metadata={"secret_key": "secret_value"},
+ checkout_token=checkout_token,
+ status=status,
+ undiscounted_base_shipping_price_amount=Decimal("0.0"),
+ )
+ if search_vector_class:
+ search_vector = search_vector_class(
+ *prepare_order_search_vector_value(order)
+ )
+ order.search_vector = search_vector
+ order.save(update_fields=["search_vector"])
+ return order
+
+ return create_order
+
+
+def order(order_generator):
+ return order_generator()
+
+
+def order_unconfirmed(order):
+ order.status = OrderStatus.UNCONFIRMED
+ order.save(update_fields=["status"])
+ return order
+
+
+def order_list(customer_user, channel_USD):
+ address = customer_user.default_billing_address.get_copy()
+ data = {
+ "billing_address": address,
+ "user": customer_user,
+ "user_email": customer_user.email,
+ "channel": channel_USD,
+ "origin": OrderOrigin.CHECKOUT,
+ }
+ order = Order.objects.create(**data)
+ order1 = Order.objects.create(**data)
+ order2 = Order.objects.create(**data)
+
+ return [order, order1, order2]
+
+
+def order_list_with_cc_orders(orders, warehouse_for_cc):
+ order_1 = orders[0]
+ order_1.collection_point = warehouse_for_cc
+ order_1.collection_point_name = warehouse_for_cc.name
+
+ order_2 = orders[1]
+ order_2.collection_point_name = warehouse_for_cc.name
+
+ order_3 = orders[2]
+ order_3.collection_point = warehouse_for_cc
+
+ cc_orders = [order_1, order_2, order_3]
+
+ Order.objects.bulk_update(cc_orders, ["collection_point", "collection_point_name"])
+ return orders
+
+
+def order_with_lines(
+ order,
+ product_type,
+ category,
+ shipping_zone,
+ warehouse,
+ channel_USD,
+ default_tax_class,
+):
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-8",
+ product_type=product_type,
+ category=category,
+ tax_class=default_tax_class,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=datetime.datetime.now(tz=datetime.UTC),
+ )
+ variant = ProductVariant.objects.create(product=product, sku="SKU_AA")
+ channel_listing = ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ )
+ stock = Stock.objects.create(
+ warehouse=warehouse, product_variant=variant, quantity=5
+ )
+ base_price = variant.get_price(channel_listing)
+ currency = base_price.currency
+ gross = Money(amount=base_price.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=base_price, gross=gross)
+ line = order.lines.create(
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=base_price,
+ undiscounted_base_unit_price=base_price,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(product_type.tax_class),
+ )
+ Allocation.objects.create(
+ order_line=line, stock=stock, quantity_allocated=line.quantity
+ )
+
+ product = Product.objects.create(
+ name="Test product 2",
+ slug="test-product-9",
+ product_type=product_type,
+ category=category,
+ tax_class=default_tax_class,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=timezone.now(),
+ )
+ variant = ProductVariant.objects.create(product=product, sku="SKU_B")
+ channel_listing = ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(20),
+ discounted_price_amount=Decimal(20),
+ cost_price_amount=Decimal(2),
+ currency=channel_USD.currency_code,
+ )
+ stock = Stock.objects.create(
+ product_variant=variant, warehouse=warehouse, quantity=2
+ )
+ stock.refresh_from_db()
+
+ base_price = variant.get_price(channel_listing)
+ currency = base_price.currency
+ gross = Money(amount=base_price.amount * Decimal(1.23), currency=currency)
+ unit_price = TaxedMoney(net=base_price, gross=gross)
+ quantity = 2
+ line = order.lines.create(
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=base_price,
+ undiscounted_base_unit_price=base_price,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(product_type.tax_class),
+ )
+ Allocation.objects.create(
+ order_line=line, stock=stock, quantity_allocated=line.quantity
+ )
+
+ order.shipping_address = order.billing_address.get_copy()
+ order.channel = channel_USD
+ shipping_method = shipping_zone.shipping_methods.first()
+ shipping_price = shipping_method.channel_listings.get(channel_id=channel_USD.id)
+ order.shipping_method_name = shipping_method.name
+ order.shipping_method = shipping_method
+ order.shipping_tax_class = shipping_method.tax_class
+ order.shipping_tax_class_name = shipping_method.tax_class.name
+ order.shipping_tax_class_metadata = shipping_method.tax_class.metadata
+ order.shipping_tax_class_private_metadata = (
+ shipping_method.tax_class.private_metadata
+ ) # noqa: E501
+
+ net = shipping_price.get_total()
+ gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ order.shipping_price = TaxedMoney(net=net, gross=gross)
+ order.base_shipping_price = net
+ order.undiscounted_base_shipping_price = net
+ order.shipping_tax_rate = calculate_tax_rate(order.shipping_price)
+ order.save()
+
+ recalculate_order(order)
+
+ order.refresh_from_db()
+ return order
+
+
+def order_with_lines_for_cc(
+ warehouse_for_cc,
+ channel_USD,
+ customer_user,
+ product_variant_list,
+):
+ address = customer_user.default_billing_address.get_copy()
+
+ order = Order.objects.create(
+ billing_address=address,
+ channel=channel_USD,
+ currency=channel_USD.currency_code,
+ shipping_address=address,
+ user_email=customer_user.email,
+ user=customer_user,
+ origin=OrderOrigin.CHECKOUT,
+ )
+
+ order.collection_point = warehouse_for_cc
+ order.collection_point_name = warehouse_for_cc.name
+ order.save()
+
+ variant = product_variant_list[0]
+ channel_listing = variant.channel_listings.get(channel=channel_USD)
+ quantity = 1
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ unit_price = TaxedMoney(net=net, gross=gross)
+ line = order.lines.create(
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(variant.product.product_type.tax_class),
+ )
+ Allocation.objects.create(
+ order_line=line,
+ stock=warehouse_for_cc.stock_set.filter(product_variant=variant).first(),
+ quantity_allocated=line.quantity,
+ )
+
+ recalculate_order(order)
+
+ order.refresh_from_db()
+ return order
+
+
+def order_with_lines_and_catalogue_promotion(
+ order_with_lines, channel_USD, catalogue_promotion_without_rules
+):
+ order = order_with_lines
+ promotion = catalogue_promotion_without_rules
+ line = order.lines.get(quantity=3)
+ variant = line.variant
+ reward_value = Decimal(3)
+ rule = promotion.rules.create(
+ name="Catalogue rule fixed",
+ catalogue_predicate={
+ "variantPredicate": {
+ "ids": [graphene.Node.to_global_id("ProductVariant", variant)]
+ }
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=reward_value,
+ )
+ rule.channels.add(channel_USD)
+
+ listing = variant.channel_listings.get(channel=channel_USD)
+ listing.discounted_price_amount = listing.price_amount - reward_value
+ listing.save(update_fields=["discounted_price_amount"])
+ listing.variantlistingpromotionrule.create(
+ promotion_rule=rule,
+ discount_amount=reward_value,
+ currency=order.currency,
+ )
+
+ line.discounts.create(
+ type=DiscountType.PROMOTION,
+ value_type=RewardValueType.FIXED,
+ value=reward_value,
+ amount_value=reward_value * line.quantity,
+ currency=order.currency,
+ promotion_rule=rule,
+ )
+ return order
+
+
+def order_with_lines_and_order_promotion(
+ order_with_lines,
+ channel_USD,
+ order_promotion_without_rules,
+):
+ order = order_with_lines
+ promotion = order_promotion_without_rules
+ rule = promotion.rules.create(
+ name="Fixed subtotal rule",
+ order_predicate={
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal(25),
+ reward_type=RewardType.SUBTOTAL_DISCOUNT,
+ )
+ rule.channels.add(channel_USD)
+
+ order.discounts.create(
+ promotion_rule=rule,
+ type=DiscountType.ORDER_PROMOTION,
+ value_type=rule.reward_value_type,
+ value=rule.reward_value,
+ amount_value=rule.reward_value,
+ currency=order.currency,
+ )
+ return order
+
+
+def order_with_lines_and_gift_promotion(
+ order_with_lines,
+ channel_USD,
+ order_promotion_without_rules,
+ variant_with_many_stocks,
+):
+ order = order_with_lines
+ variant = variant_with_many_stocks
+ variant_listing = variant.channel_listings.get(channel=channel_USD)
+ promotion = order_promotion_without_rules
+ rule = promotion.rules.create(
+ name="Gift subtotal rule",
+ order_predicate={
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
+ },
+ reward_type=RewardType.GIFT,
+ )
+ rule.channels.add(channel_USD)
+ rule.gifts.set([variant])
+
+ gift_line = order.lines.create(
+ quantity=1,
+ variant=variant,
+ is_gift=True,
+ currency=order.currency,
+ unit_price_net_amount=0,
+ unit_price_gross_amount=0,
+ total_price_net_amount=0,
+ total_price_gross_amount=0,
+ is_shipping_required=True,
+ is_gift_card=False,
+ )
+ gift_line.discounts.create(
+ promotion_rule=rule,
+ type=DiscountType.ORDER_PROMOTION,
+ value_type=RewardValueType.FIXED,
+ value=variant_listing.price_amount,
+ amount_value=variant_listing.price_amount,
+ currency=order.currency,
+ )
+ return order
+
+
+def order_with_lines_and_events(order_with_lines, staff_user):
+ events = []
+ for event_type, _ in OrderEvents.CHOICES:
+ events.append(
+ OrderEvent(
+ type=event_type,
+ order=order_with_lines,
+ user=staff_user,
+ )
+ )
+ OrderEvent.objects.bulk_create(events)
+ fulfillment_refunded_event(
+ order=order_with_lines,
+ user=staff_user,
+ app=None,
+ refunded_lines=[(1, order_with_lines.lines.first())],
+ amount=Decimal("10.0"),
+ shipping_costs_included=False,
+ )
+ order_added_products_event(
+ order=order_with_lines,
+ user=staff_user,
+ app=None,
+ order_lines=[order_with_lines.lines.first()],
+ quantity_diff=1,
+ )
+ return order_with_lines
+
+
+def order_with_lines_channel_PLN(
+ customer_user,
+ product_type,
+ category,
+ shipping_method_channel_PLN,
+ warehouse,
+ channel_PLN,
+):
+ address = customer_user.default_billing_address.get_copy()
+ order = Order.objects.create(
+ billing_address=address,
+ channel=channel_PLN,
+ shipping_address=address,
+ user_email=customer_user.email,
+ user=customer_user,
+ origin=OrderOrigin.CHECKOUT,
+ )
+ product = Product.objects.create(
+ name="Test product in PLN channel",
+ slug="test-product-8-pln",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_PLN,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=timezone.now(),
+ )
+ variant = ProductVariant.objects.create(product=product, sku="SKU_A_PLN")
+ channel_listing = ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_PLN,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_PLN.currency_code,
+ )
+ stock = Stock.objects.create(
+ warehouse=warehouse, product_variant=variant, quantity=5
+ )
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
+ line = order.lines.create(
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(product_type.tax_class),
+ )
+ Allocation.objects.create(
+ order_line=line, stock=stock, quantity_allocated=line.quantity
+ )
+
+ product = Product.objects.create(
+ name="Test product 2 in PLN channel",
+ slug="test-product-9-pln",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_PLN,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=timezone.now(),
+ )
+ variant = ProductVariant.objects.create(product=product, sku="SKU_B_PLN")
+ channel_listing = ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_PLN,
+ price_amount=Decimal(20),
+ discounted_price_amount=Decimal(20),
+ cost_price_amount=Decimal(2),
+ currency=channel_PLN.currency_code,
+ )
+ stock = Stock.objects.create(
+ product_variant=variant, warehouse=warehouse, quantity=2
+ )
+
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 2
+ unit_price = TaxedMoney(net=net, gross=gross)
+ line = order.lines.create(
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(product_type.tax_class),
+ )
+ Allocation.objects.create(
+ order_line=line, stock=stock, quantity_allocated=line.quantity
+ )
+
+ order.shipping_address = order.billing_address.get_copy()
+ order.channel = channel_PLN
+ shipping_method = shipping_method_channel_PLN
+ shipping_price = shipping_method.channel_listings.get(
+ channel_id=channel_PLN.id,
+ )
+ order.shipping_method_name = shipping_method.name
+ order.shipping_method = shipping_method
+
+ net = shipping_price.get_total()
+ gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ order.shipping_price = TaxedMoney(net=net, gross=gross)
+ order.base_shipping_price = net
+ order.undiscounted_base_shipping_price = net
+ order.shipping_tax_rate = calculate_tax_rate(order.shipping_price)
+ order.save()
+
+ recalculate_order(order)
+
+ order.refresh_from_db()
+ return order
+
+
+def order_with_line_without_inventory_tracking(
+ order, variant_without_inventory_tracking
+):
+ variant = variant_without_inventory_tracking
+ product = variant.product
+ channel = order.channel
+ channel_listing = variant.channel_listings.get(channel=channel)
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
+ order.lines.create(
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(product.product_type.tax_class),
+ )
+
+ recalculate_order(order)
+
+ order.refresh_from_db()
+ return order
+
+
+def order_with_preorder_lines(
+ order, product_type, category, shipping_zone, warehouse, channel_USD
+):
+ product = Product.objects.create(
+ name="Test product",
+ slug="test-product-8",
+ product_type=product_type,
+ category=category,
+ )
+ ProductChannelListing.objects.create(
+ product=product,
+ channel=channel_USD,
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_at=timezone.now(),
+ )
+ variant = ProductVariant.objects.create(
+ product=product, sku="SKU_AA_P", is_preorder=True
+ )
+ channel_listing = ProductVariantChannelListing.objects.create(
+ variant=variant,
+ channel=channel_USD,
+ price_amount=Decimal(10),
+ discounted_price_amount=Decimal(10),
+ cost_price_amount=Decimal(1),
+ currency=channel_USD.currency_code,
+ preorder_quantity_threshold=10,
+ )
+
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
+ line = order.lines.create(
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(product_type.tax_class),
+ )
+ PreorderAllocation.objects.create(
+ order_line=line,
+ product_variant_channel_listing=channel_listing,
+ quantity=line.quantity,
+ )
+
+ order.shipping_address = order.billing_address.get_copy()
+ order.channel = channel_USD
+ shipping_method = shipping_zone.shipping_methods.first()
+ shipping_price = shipping_method.channel_listings.get(channel_id=channel_USD.id)
+ order.shipping_method_name = shipping_method.name
+ order.shipping_method = shipping_method
+
+ net = shipping_price.get_total()
+ gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ order.shipping_price = TaxedMoney(net=net, gross=gross)
+ order.base_shipping_price = net
+ order.undiscounted_base_shipping_price = net
+ order.save()
+
+ recalculate_order(order)
+
+ order.refresh_from_db()
+ return order
+
+
+def order_events(order):
+ order_events = [
+ OrderEvent(type=event_type, order=order)
+ for event_type, _ in OrderEvents.CHOICES
+ ]
+ OrderEvent.objects.bulk_create(order_events)
+ return order_events
+
+
+def fulfilled_order(order_with_lines):
+ order = order_with_lines
+ order.invoices.create(
+ url="http://www.example.com/invoice.pdf",
+ number="01/12/2020/TEST",
+ created_at=datetime.datetime.now(tz=datetime.UTC),
+ status=JobStatus.SUCCESS,
+ )
+ fulfillment = order.fulfillments.create(tracking_number="123")
+ line_1 = order.lines.first()
+ stock_1 = line_1.allocations.get().stock
+ warehouse_1_pk = stock_1.warehouse.pk
+ line_2 = order.lines.last()
+ stock_2 = line_2.allocations.get().stock
+ warehouse_2_pk = stock_2.warehouse.pk
+ fulfillment.lines.create(order_line=line_1, quantity=line_1.quantity, stock=stock_1)
+ fulfillment.lines.create(order_line=line_2, quantity=line_2.quantity, stock=stock_2)
+ fulfill_order_lines(
+ [
+ OrderLineInfo(
+ line=line_1, quantity=line_1.quantity, warehouse_pk=warehouse_1_pk
+ ),
+ OrderLineInfo(
+ line=line_2, quantity=line_2.quantity, warehouse_pk=warehouse_2_pk
+ ),
+ ],
+ manager=get_plugins_manager(allow_replica=False),
+ )
+ order.status = OrderStatus.FULFILLED
+ order.save(update_fields=["status"])
+ return order
+
+
+def unconfirmed_order_with_lines(order_with_lines):
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ order.save(update_fields=["status"])
+ return order
+
+
+def fulfilled_order_without_inventory_tracking(
+ order_with_line_without_inventory_tracking,
+):
+ order = order_with_line_without_inventory_tracking
+ fulfillment = order.fulfillments.create(tracking_number="123")
+ line = order.lines.first()
+ stock = line.variant.stocks.get()
+ warehouse_pk = stock.warehouse.pk
+ fulfillment.lines.create(order_line=line, quantity=line.quantity, stock=stock)
+ fulfill_order_lines(
+ [OrderLineInfo(line=line, quantity=line.quantity, warehouse_pk=warehouse_pk)],
+ get_plugins_manager(allow_replica=False),
+ )
+ order.status = OrderStatus.FULFILLED
+ order.save(update_fields=["status"])
+ return order
+
+
+def fulfilled_order_with_cancelled_fulfillment(fulfilled_order):
+ fulfillment = fulfilled_order.fulfillments.create()
+ line_1 = fulfilled_order.lines.first()
+ line_2 = fulfilled_order.lines.last()
+ fulfillment.lines.create(order_line=line_1, quantity=line_1.quantity)
+ fulfillment.lines.create(order_line=line_2, quantity=line_2.quantity)
+ fulfillment.status = FulfillmentStatus.CANCELED
+ fulfillment.save()
+ return fulfilled_order
+
+
+def fulfilled_order_with_all_cancelled_fulfillments(
+ fulfilled_order, staff_user, warehouse
+):
+ fulfillment = fulfilled_order.fulfillments.get()
+ cancel_fulfillment(
+ fulfillment,
+ staff_user,
+ None,
+ warehouse,
+ get_plugins_manager(allow_replica=False),
+ )
+ return fulfilled_order
+
+
+def order_with_digital_line(order, digital_content, stock, site_settings):
+ site_settings.automatic_fulfillment_digital_products = True
+ site_settings.save()
+
+ variant = stock.product_variant
+ variant.digital_content = digital_content
+ variant.digital_content.save()
+
+ product_type = variant.product.product_type
+ product_type.is_shipping_required = False
+ product_type.is_digital = True
+ product_type.save()
+
+ quantity = 3
+ product = variant.product
+ channel = order.channel
+ variant_channel_listing = variant.channel_listings.get(channel=channel)
+ net = variant.get_price(variant_channel_listing)
+ gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ unit_price = TaxedMoney(net=net, gross=gross)
+ line = order.lines.create(
+ product_name=str(product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ tax_rate=Decimal("0.23"),
+ )
+
+ Allocation.objects.create(order_line=line, stock=stock, quantity_allocated=quantity)
+
+ return order
+
+
+@freeze_time("2021-11-01 12:00:01")
+def preorders(orders, product):
+ variants = [
+ ProductVariant(
+ product=product,
+ is_preorder=True,
+ sku=f"Preorder product variant #{i}",
+ )
+ for i in (1, 2, 3, 4)
+ ]
+ variants[1].preorder_end_date = timezone.now() + timedelta(days=1)
+ variants[2].preorder_end_date = timezone.now()
+ variants[3].preorder_end_date = timezone.now() - timedelta(days=1)
+ ProductVariant.objects.bulk_create(variants)
+
+ lines = [
+ OrderLine(
+ order=order,
+ product_name=str(product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=1,
+ variant=variant,
+ unit_price_net_amount=Decimal("10.0"),
+ unit_price_gross_amount=Decimal("10.0"),
+ currency="USD",
+ total_price_net_amount=Decimal("10.0"),
+ total_price_gross_amount=Decimal("10.0"),
+ undiscounted_unit_price_net_amount=Decimal("10.0"),
+ undiscounted_unit_price_gross_amount=Decimal("10.0"),
+ undiscounted_total_price_net_amount=Decimal("10.0"),
+ undiscounted_total_price_gross_amount=Decimal("10.0"),
+ )
+ for variant, order in zip(variants, orders)
+ ]
+ OrderLine.objects.bulk_create(lines)
+ preorders = orders[: len(variants) - 1]
+ return preorders
diff --git a/saleor/order/tests/fixtures/order_line.py b/saleor/order/tests/fixtures/order_line.py
new file mode 100644
index 0000000000..b3a95b3a1e
--- /dev/null
+++ b/saleor/order/tests/fixtures/order_line.py
@@ -0,0 +1,311 @@
+from decimal import Decimal
+
+import pytest
+from prices import Money, TaxedMoney
+
+from ....warehouse.models import Allocation, Stock
+from ... import OrderOrigin
+from ...fetch import OrderLineInfo
+from ...models import Order
+
+
+def order_line(order, variant):
+ product = variant.product
+ channel = order.channel
+ channel_listing = variant.channel_listings.get(channel=channel)
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
+ return order.lines.create(
+ product_name=str(product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ tax_class=variant.product.tax_class,
+ )
+
+
+def order_line_on_promotion(order_line, catalogue_promotion):
+ variant = order_line.variant
+
+ channel = order_line.order.channel
+ reward_value = Decimal("1.0")
+ rule = catalogue_promotion.rules.first()
+ variant_channel_listing = variant.channel_listings.get(channel=channel)
+
+ variant_channel_listing.discounted_price_amount = (
+ variant_channel_listing.price_amount - reward_value
+ )
+ variant_channel_listing.save(update_fields=["discounted_price_amount"])
+
+ variant_channel_listing.variantlistingpromotionrule.create(
+ promotion_rule=rule,
+ discount_amount=reward_value,
+ currency=channel.currency_code,
+ )
+ order_line.total_price_gross_amount = (
+ variant_channel_listing.discounted_price_amount * order_line.quantity
+ )
+ order_line.total_price_net_amount = (
+ variant_channel_listing.discounted_price_amount * order_line.quantity
+ )
+ order_line.undiscounted_total_price_gross_amount = (
+ variant_channel_listing.price_amount * order_line.quantity
+ )
+ order_line.undiscounted_total_price_net_amount = (
+ variant_channel_listing.price_amount * order_line.quantity
+ )
+
+ order_line.unit_price_gross_amount = variant_channel_listing.discounted_price_amount
+ order_line.unit_price_net_amount = variant_channel_listing.discounted_price_amount
+ order_line.undiscounted_unit_price_gross_amount = (
+ variant_channel_listing.price_amount
+ )
+ order_line.undiscounted_unit_price_net_amount = variant_channel_listing.price_amount
+
+ order_line.base_unit_price_amount = variant_channel_listing.discounted_price_amount
+ order_line.undiscounted_base_unit_price_amount = (
+ variant_channel_listing.price_amount
+ )
+
+ order_line.unit_discount_amount = reward_value
+ order_line.save()
+ return order_line
+
+
+def order_line_JPY(order_generator, channel_JPY, product_in_channel_JPY):
+ order_JPY = order_generator(
+ channel=channel_JPY,
+ currency=channel_JPY.currency_code,
+ )
+ product = product_in_channel_JPY
+ variant = product_in_channel_JPY.variants.get()
+ channel = order_JPY.channel
+ channel_listing = variant.channel_listings.get(channel=channel)
+ base_price = variant.get_price(channel_listing)
+ currency = base_price.currency
+ gross = Money(amount=base_price.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=base_price, gross=gross)
+ return order_JPY.lines.create(
+ product_name=str(product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=base_price,
+ undiscounted_base_unit_price=base_price,
+ tax_rate=Decimal("0.23"),
+ )
+
+
+def order_line_with_allocation_in_many_stocks(
+ customer_user, variant_with_many_stocks, channel_USD
+):
+ address = customer_user.default_billing_address.get_copy()
+ variant = variant_with_many_stocks
+ stocks = variant.stocks.all().order_by("pk")
+
+ order = Order.objects.create(
+ billing_address=address,
+ user_email=customer_user.email,
+ user=customer_user,
+ channel=channel_USD,
+ origin=OrderOrigin.CHECKOUT,
+ undiscounted_base_shipping_price_amount=Decimal("0.0"),
+ )
+
+ product = variant.product
+ channel_listing = variant.channel_listings.get(channel=channel_USD)
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
+ order_line = order.lines.create(
+ product_name=str(product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ )
+
+ Allocation.objects.bulk_create(
+ [
+ Allocation(order_line=order_line, stock=stocks[0], quantity_allocated=2),
+ Allocation(order_line=order_line, stock=stocks[1], quantity_allocated=1),
+ ]
+ )
+
+ stocks_to_update = list(stocks)
+ stocks_to_update[0].quantity_allocated = 2
+ stocks_to_update[1].quantity_allocated = 1
+
+ Stock.objects.bulk_update(stocks_to_update, ["quantity_allocated"])
+
+ return order_line
+
+
+def order_line_with_one_allocation(
+ customer_user, variant_with_many_stocks, channel_USD
+):
+ address = customer_user.default_billing_address.get_copy()
+ variant = variant_with_many_stocks
+ stocks = variant.stocks.all().order_by("pk")
+
+ order = Order.objects.create(
+ billing_address=address,
+ user_email=customer_user.email,
+ user=customer_user,
+ channel=channel_USD,
+ origin=OrderOrigin.CHECKOUT,
+ )
+
+ product = variant.product
+ channel_listing = variant.channel_listings.get(channel=channel_USD)
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 2
+ unit_price = TaxedMoney(net=net, gross=gross)
+ order_line = order.lines.create(
+ product_name=str(product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ )
+
+ Allocation.objects.create(
+ order_line=order_line, stock=stocks[0], quantity_allocated=1
+ )
+ stock = stocks[0]
+ stock.quantity_allocated = 1
+ stock.save(update_fields=["quantity_allocated"])
+
+ return order_line
+
+
+def lines_info(order_with_lines):
+ return [
+ OrderLineInfo(
+ line=line,
+ quantity=line.quantity,
+ variant=line.variant,
+ warehouse_pk=line.allocations.first().stock.warehouse.pk,
+ )
+ for line in order_with_lines.lines.all()
+ ]
+
+
+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
+ channel_listing = variant.channel_listings.get(channel=channel)
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 1
+ unit_price = TaxedMoney(net=net, gross=gross)
+ line = order.lines.create(
+ product_name=str(product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ )
+ Allocation.objects.create(
+ order_line=line, stock=variant.stocks.first(), quantity_allocated=line.quantity
+ )
+ return line
+
+
+def gift_card_shippable_order_line(order, gift_card_shippable_variant, warehouse):
+ variant = gift_card_shippable_variant
+ product = variant.product
+ channel = order.channel
+ channel_listing = variant.channel_listings.get(channel=channel)
+ net = variant.get_price(channel_listing)
+ currency = net.currency
+ gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
+ quantity = 3
+ unit_price = TaxedMoney(net=net, gross=gross)
+ line = order.lines.create(
+ product_name=str(product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ quantity=quantity,
+ variant=variant,
+ unit_price=unit_price,
+ total_price=unit_price * quantity,
+ undiscounted_unit_price=unit_price,
+ undiscounted_total_price=unit_price * quantity,
+ base_unit_price=unit_price.gross,
+ undiscounted_base_unit_price=unit_price.gross,
+ tax_rate=Decimal("0.23"),
+ )
+ Allocation.objects.create(
+ order_line=line, stock=variant.stocks.first(), quantity_allocated=line.quantity
+ )
+ return line
diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.py
index 95aefd8968..945103210f 100644
--- a/saleor/order/tests/test_order.py
+++ b/saleor/order/tests/test_order.py
@@ -19,7 +19,6 @@ from ...graphql.tests.utils import get_graphql_content
from ...payment import ChargeStatus
from ...payment.models import Payment
from ...plugins.manager import get_plugins_manager
-from ...tests.fixtures import recalculate_order
from ...warehouse import WarehouseClickAndCollectOption
from ...warehouse.models import Stock, Warehouse
from ...warehouse.tests.utils import get_quantity_allocated_for_stock
@@ -48,6 +47,7 @@ from ..utils import (
update_order_charge_data,
update_order_status,
)
+from .fixtures import recalculate_order
def test_total_setter():
diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.py
index a92198c96c..cb98d508eb 100644
--- a/saleor/order/tests/test_order_actions.py
+++ b/saleor/order/tests/test_order_actions.py
@@ -3,7 +3,6 @@ from unittest.mock import ANY, call, patch
import pytest
from django.test import override_settings
-from prices import Money, TaxedMoney
from ...channel import MarkAsPaidStrategy
from ...core.models import EventDelivery
@@ -62,46 +61,6 @@ from ..notifications import (
from ..utils import updates_amounts_for_order
-def order_with_digital_line(order, digital_content, stock, site_settings):
- site_settings.automatic_fulfillment_digital_products = True
- site_settings.save()
-
- variant = stock.product_variant
- variant.digital_content = digital_content
- variant.digital_content.save()
-
- product_type = variant.product.product_type
- product_type.is_shipping_required = False
- product_type.is_digital = True
- product_type.save()
-
- quantity = 3
- product = variant.product
- channel = order.channel
- variant_channel_listing = variant.channel_listings.get(channel=channel)
- net = variant.get_price(variant_channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
- unit_price = TaxedMoney(net=net, gross=gross)
- line = order.lines.create(
- product_name=str(product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- tax_rate=Decimal("0.23"),
- )
-
- Allocation.objects.create(order_line=line, stock=stock, quantity_allocated=quantity)
-
- return order
-
-
@patch(
"saleor.order.actions.send_fulfillment_confirmation_to_customer",
wraps=send_fulfillment_confirmation_to_customer,
diff --git a/saleor/payment/gateways/np_atobarai/tests/conftest.py b/saleor/payment/gateways/np_atobarai/tests/conftest.py
index 6e31fe500e..5578a32fad 100644
--- a/saleor/payment/gateways/np_atobarai/tests/conftest.py
+++ b/saleor/payment/gateways/np_atobarai/tests/conftest.py
@@ -5,9 +5,9 @@ from unittest.mock import patch
import pytest
from .....order.actions import create_refund_fulfillment
+from .....order.tests.fixtures import recalculate_order
from .....plugins.manager import get_plugins_manager
from .....plugins.models import PluginConfiguration
-from .....tests.fixtures import recalculate_order
from ....interface import AddressData, PaymentLineData, PaymentLinesData
from ..api_types import get_api_config
from ..const import (
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index b1d06db20b..6907fd5b1a 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -2,7 +2,6 @@ import datetime
import itertools
import random
import uuid
-from collections import namedtuple
from contextlib import contextmanager
from decimal import Decimal
from functools import partial
@@ -23,7 +22,7 @@ from django.utils import timezone
from django_countries import countries
from freezegun import freeze_time
from PIL import Image
-from prices import Money, TaxedMoney, fixed_discount
+from prices import Money
from ..account.models import Address, Group, StaffNotificationRecipient, User
from ..app.models import App, AppExtension, AppInstallation
@@ -42,13 +41,11 @@ from ..checkout.models import Checkout, CheckoutLine, CheckoutMetadata
from ..checkout.utils import (
add_variant_to_checkout,
add_voucher_to_checkout,
- get_prices_of_discounted_specific_product,
)
from ..core import EventDeliveryStatus, JobStatus
from ..core.models import EventDelivery, EventDeliveryAttempt, EventPayload
from ..core.payments import PaymentInterface
from ..core.postgres import FlatConcatSearchVector
-from ..core.taxes import zero_money
from ..core.units import MeasurementUnits
from ..core.utils.editorjs import clean_editor_js
from ..csv.events import ExportEvents
@@ -66,7 +63,6 @@ from ..discount.interface import VariantPromotionRuleInfo
from ..discount.models import (
CheckoutDiscount,
CheckoutLineDiscount,
- NotApplicable,
Promotion,
PromotionEvent,
PromotionRule,
@@ -78,36 +74,12 @@ from ..discount.models import (
VoucherCustomer,
VoucherTranslation,
)
-from ..discount.utils.voucher import (
- create_or_update_discount_object_from_order_level_voucher,
- get_products_voucher_discount,
- validate_voucher_in_order,
-)
from ..giftcard import GiftCardEvents
from ..giftcard.models import GiftCard, GiftCardEvent, GiftCardTag
from ..graphql.core.utils import to_global_id_or_none
from ..menu.models import Menu
-from ..order import OrderOrigin, OrderStatus
-from ..order.actions import cancel_fulfillment, fulfill_order_lines
-from ..order.base_calculations import base_order_subtotal
-from ..order.events import (
- OrderEvents,
- fulfillment_refunded_event,
- order_added_products_event,
-)
-from ..order.fetch import OrderLineInfo
-from ..order.models import (
- FulfillmentLine,
- FulfillmentStatus,
- Order,
- OrderEvent,
- OrderLine,
-)
-from ..order.search import prepare_order_search_vector_value
-from ..order.utils import get_voucher_discount_assigned_to_order
from ..payment import ChargeStatus, TransactionKind
from ..payment.interface import AddressData, GatewayConfig, GatewayResponse, PaymentData
-from ..payment.model_helpers import get_subtotal
from ..payment.models import Payment, TransactionEvent, TransactionItem
from ..payment.transaction_item_calculations import recalculate_transaction_amounts
from ..payment.utils import create_manual_adjustment_events
@@ -146,10 +118,7 @@ from ..shipping.models import (
from ..shipping.utils import convert_to_shipping_method_data
from ..site.models import SiteSettings
from ..tax import TaxCalculationStrategy
-from ..tax.utils import calculate_tax_rate, get_tax_class_kwargs_for_order_line
from ..warehouse.models import (
- Allocation,
- PreorderAllocation,
PreorderReservation,
Reservation,
Stock,
@@ -1345,137 +1314,6 @@ def user_checkouts(request, user_checkout_with_items, user_checkout_with_items_f
raise ValueError("Internal test error")
-def orders(customer_user, channel_USD, channel_PLN):
- return Order.objects.bulk_create(
- [
- Order(
- user=customer_user,
- status=OrderStatus.CANCELED,
- channel=channel_USD,
- ),
- Order(
- user=customer_user,
- status=OrderStatus.UNFULFILLED,
- channel=channel_USD,
- ),
- Order(
- user=customer_user,
- status=OrderStatus.PARTIALLY_FULFILLED,
- channel=channel_USD,
- ),
- Order(
- user=customer_user,
- status=OrderStatus.FULFILLED,
- channel=channel_PLN,
- ),
- Order(
- user=customer_user,
- status=OrderStatus.DRAFT,
- channel=channel_PLN,
- ),
- Order(
- user=customer_user,
- status=OrderStatus.UNCONFIRMED,
- channel=channel_PLN,
- ),
- ]
- )
-
-
-def orders_from_checkout(customer_user, checkout):
- return Order.objects.bulk_create(
- [
- Order(
- user=customer_user,
- status=OrderStatus.CANCELED,
- channel=checkout.channel,
- checkout_token=checkout.token,
- ),
- Order(
- user=customer_user,
- status=OrderStatus.UNFULFILLED,
- channel=checkout.channel,
- checkout_token=checkout.token,
- ),
- Order(
- user=customer_user,
- status=OrderStatus.FULFILLED,
- channel=checkout.channel,
- checkout_token=checkout.token,
- ),
- Order(
- user=customer_user,
- status=OrderStatus.FULFILLED,
- channel=checkout.channel,
- checkout_token=checkout.token,
- ),
- ]
- )
-
-
-def order_generator(customer_user, channel_USD):
- address = customer_user.default_billing_address.get_copy()
-
- def create_order(
- billing_address=address,
- channel=channel_USD,
- currency=channel_USD.currency_code,
- shipping_address=address,
- user_email=customer_user.email,
- user=customer_user,
- origin=OrderOrigin.CHECKOUT,
- should_refresh_prices=False,
- metadata=None,
- private_metadata=None,
- checkout_token="",
- status=OrderStatus.UNFULFILLED,
- search_vector_class=None,
- ):
- if metadata is None:
- metadata = {"key": "value"}
- if private_metadata is None:
- private_metadata = {"secret_key": "secret_value"}
- order = Order.objects.create(
- billing_address=billing_address,
- channel=channel,
- currency=currency,
- shipping_address=shipping_address,
- user_email=user_email,
- user=user,
- origin=origin,
- should_refresh_prices=should_refresh_prices,
- metadata=metadata,
- private_metadata=private_metadata,
- checkout_token=checkout_token,
- status=status,
- undiscounted_base_shipping_price_amount=Decimal("0.0"),
- )
- if search_vector_class:
- search_vector = search_vector_class(
- *prepare_order_search_vector_value(order)
- )
- order.search_vector = search_vector
- order.save(update_fields=["search_vector"])
- return order
-
- return create_order
-
-
-def order(order_generator):
- return order_generator()
-
-
-def order_unconfirmed(order):
- order.status = OrderStatus.UNCONFIRMED
- order.save(update_fields=["status"])
- return order
-
-
@pytest.fixture
def product_type_generator(
attribute_generator, attribute_value_generator, default_tax_class
@@ -4089,33 +3927,6 @@ def product_list_published(product_list, channel_USD):
return products
-def order_list(customer_user, channel_USD):
- address = customer_user.default_billing_address.get_copy()
- data = {
- "billing_address": address,
- "user": customer_user,
- "user_email": customer_user.email,
- "channel": channel_USD,
- "origin": OrderOrigin.CHECKOUT,
- }
- order = Order.objects.create(**data)
- order1 = Order.objects.create(**data)
- order2 = Order.objects.create(**data)
-
- return [order, order1, order2]
-
-
-def draft_order_list(order_list):
- for order in order_list:
- order.status = OrderStatus.DRAFT
- order.origin = OrderOrigin.DRAFT
-
- Order.objects.bulk_update(order_list, ["status", "origin"])
- return order_list
-
-
@pytest.fixture
def product_with_image(product, image, media_root):
ProductMedia.objects.create(product=product, image=image)
@@ -4370,317 +4181,6 @@ def voucher_single_use(voucher_with_many_codes):
return voucher
-def draft_order_list_with_multiple_use_voucher(draft_order_list, voucher_multiple_use):
- codes = voucher_multiple_use.codes.values_list("code", flat=True)
- for idx, order in enumerate(draft_order_list):
- order.voucher_code = codes[idx]
- Order.objects.bulk_update(draft_order_list, ["voucher_code"])
- return draft_order_list
-
-
-def draft_order_list_with_single_use_voucher(draft_order_list, voucher_single_use):
- voucher_codes = voucher_single_use.codes.all()
- codes = voucher_codes.values_list("code", flat=True)
- for idx, order in enumerate(draft_order_list):
- order.voucher_code = codes[idx]
- for voucher_code in voucher_codes:
- voucher_code.is_active = False
- Order.objects.bulk_update(draft_order_list, ["voucher_code"])
- VoucherCode.objects.bulk_update(voucher_codes, ["is_active"])
- return draft_order_list
-
-
-def order_line(order, variant):
- product = variant.product
- channel = order.channel
- channel_listing = variant.channel_listings.get(channel=channel)
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 3
- unit_price = TaxedMoney(net=net, gross=gross)
- return order.lines.create(
- product_name=str(product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- tax_class=variant.product.tax_class,
- )
-
-
-def order_line_on_promotion(order_line, catalogue_promotion):
- variant = order_line.variant
-
- channel = order_line.order.channel
- reward_value = Decimal("1.0")
- rule = catalogue_promotion.rules.first()
- variant_channel_listing = variant.channel_listings.get(channel=channel)
-
- variant_channel_listing.discounted_price_amount = (
- variant_channel_listing.price_amount - reward_value
- )
- variant_channel_listing.save(update_fields=["discounted_price_amount"])
-
- variant_channel_listing.variantlistingpromotionrule.create(
- promotion_rule=rule,
- discount_amount=reward_value,
- currency=channel.currency_code,
- )
- order_line.total_price_gross_amount = (
- variant_channel_listing.discounted_price_amount * order_line.quantity
- )
- order_line.total_price_net_amount = (
- variant_channel_listing.discounted_price_amount * order_line.quantity
- )
- order_line.undiscounted_total_price_gross_amount = (
- variant_channel_listing.price_amount * order_line.quantity
- )
- order_line.undiscounted_total_price_net_amount = (
- variant_channel_listing.price_amount * order_line.quantity
- )
-
- order_line.unit_price_gross_amount = variant_channel_listing.discounted_price_amount
- order_line.unit_price_net_amount = variant_channel_listing.discounted_price_amount
- order_line.undiscounted_unit_price_gross_amount = (
- variant_channel_listing.price_amount
- )
- order_line.undiscounted_unit_price_net_amount = variant_channel_listing.price_amount
-
- order_line.base_unit_price_amount = variant_channel_listing.discounted_price_amount
- order_line.undiscounted_base_unit_price_amount = (
- variant_channel_listing.price_amount
- )
-
- order_line.unit_discount_amount = reward_value
- order_line.save()
- return order_line
-
-
-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
- channel_listing = variant.channel_listings.get(channel=channel)
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 1
- unit_price = TaxedMoney(net=net, gross=gross)
- line = order.lines.create(
- product_name=str(product),
- variant_name=str(variant),
- product_sku=variant.sku,
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- )
- Allocation.objects.create(
- order_line=line, stock=variant.stocks.first(), quantity_allocated=line.quantity
- )
- return line
-
-
-def gift_card_shippable_order_line(order, gift_card_shippable_variant, warehouse):
- variant = gift_card_shippable_variant
- product = variant.product
- channel = order.channel
- channel_listing = variant.channel_listings.get(channel=channel)
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 3
- unit_price = TaxedMoney(net=net, gross=gross)
- line = order.lines.create(
- product_name=str(product),
- variant_name=str(variant),
- product_sku=variant.sku,
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- )
- Allocation.objects.create(
- order_line=line, stock=variant.stocks.first(), quantity_allocated=line.quantity
- )
- return line
-
-
-def order_line_JPY(order_generator, channel_JPY, product_in_channel_JPY):
- order_JPY = order_generator(
- channel=channel_JPY,
- currency=channel_JPY.currency_code,
- )
- product = product_in_channel_JPY
- variant = product_in_channel_JPY.variants.get()
- channel = order_JPY.channel
- channel_listing = variant.channel_listings.get(channel=channel)
- base_price = variant.get_price(channel_listing)
- currency = base_price.currency
- gross = Money(amount=base_price.amount * Decimal(1.23), currency=currency)
- quantity = 3
- unit_price = TaxedMoney(net=base_price, gross=gross)
- return order_JPY.lines.create(
- product_name=str(product),
- variant_name=str(variant),
- product_sku=variant.sku,
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=base_price,
- undiscounted_base_unit_price=base_price,
- tax_rate=Decimal("0.23"),
- )
-
-
-def order_line_with_allocation_in_many_stocks(
- customer_user, variant_with_many_stocks, channel_USD
-):
- address = customer_user.default_billing_address.get_copy()
- variant = variant_with_many_stocks
- stocks = variant.stocks.all().order_by("pk")
-
- order = Order.objects.create(
- billing_address=address,
- user_email=customer_user.email,
- user=customer_user,
- channel=channel_USD,
- origin=OrderOrigin.CHECKOUT,
- undiscounted_base_shipping_price_amount=Decimal("0.0"),
- )
-
- product = variant.product
- channel_listing = variant.channel_listings.get(channel=channel_USD)
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 3
- unit_price = TaxedMoney(net=net, gross=gross)
- order_line = order.lines.create(
- product_name=str(product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- )
-
- Allocation.objects.bulk_create(
- [
- Allocation(order_line=order_line, stock=stocks[0], quantity_allocated=2),
- Allocation(order_line=order_line, stock=stocks[1], quantity_allocated=1),
- ]
- )
-
- stocks_to_update = list(stocks)
- stocks_to_update[0].quantity_allocated = 2
- stocks_to_update[1].quantity_allocated = 1
-
- Stock.objects.bulk_update(stocks_to_update, ["quantity_allocated"])
-
- return order_line
-
-
-def order_line_with_one_allocation(
- customer_user, variant_with_many_stocks, channel_USD
-):
- address = customer_user.default_billing_address.get_copy()
- variant = variant_with_many_stocks
- stocks = variant.stocks.all().order_by("pk")
-
- order = Order.objects.create(
- billing_address=address,
- user_email=customer_user.email,
- user=customer_user,
- channel=channel_USD,
- origin=OrderOrigin.CHECKOUT,
- )
-
- product = variant.product
- channel_listing = variant.channel_listings.get(channel=channel_USD)
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 2
- unit_price = TaxedMoney(net=net, gross=gross)
- order_line = order.lines.create(
- product_name=str(product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- )
-
- Allocation.objects.create(
- order_line=order_line, stock=stocks[0], quantity_allocated=1
- )
- stock = stocks[0]
- stock.quantity_allocated = 1
- stock.save(update_fields=["quantity_allocated"])
-
- return order_line
-
-
@pytest.fixture
def checkout_line_with_reservation_in_many_stocks(
customer_user, variant_with_many_stocks, checkout
@@ -4902,947 +4402,6 @@ def gift_card_list():
return gift_cards
-def recalculate_order(order):
- lines = OrderLine.objects.filter(order_id=order.pk)
- prices = [line.total_price for line in lines]
- total = sum(prices, order.shipping_price)
- undiscounted_total = TaxedMoney(total.net, total.gross)
-
- try:
- discount = get_voucher_discount_for_order(order)
- except NotApplicable:
- discount = zero_money(order.currency)
-
- discount = min(discount, total.gross)
- total -= discount
-
- order.total = total
- order.subtotal = get_subtotal(order.lines.all(), order.currency)
- order.undiscounted_total = undiscounted_total
-
- if discount:
- assigned_order_discount = get_voucher_discount_assigned_to_order(order)
- if assigned_order_discount:
- assigned_order_discount.amount_value = discount.amount
- assigned_order_discount.value = discount.amount
- assigned_order_discount.save(update_fields=["value", "amount_value"])
-
- order.save()
-
-
-def get_voucher_discount_for_order(order: Order) -> Money:
- """Calculate discount value depending on voucher and discount types.
-
- Raise NotApplicable if voucher of given type cannot be applied.
- """
- if not order.voucher:
- return zero_money(order.currency)
- validate_voucher_in_order(order)
- subtotal = order.subtotal
- if order.voucher.type == VoucherType.ENTIRE_ORDER:
- return order.voucher.get_discount_amount_for(subtotal.gross, order.channel)
- if order.voucher.type == VoucherType.SHIPPING:
- return order.voucher.get_discount_amount_for(
- order.shipping_price.gross, order.channel
- )
- if order.voucher.type == VoucherType.SPECIFIC_PRODUCT:
- return get_products_voucher_discount_for_order(order, order.voucher)
- raise NotImplementedError("Unknown discount type")
-
-
-def get_products_voucher_discount_for_order(order: Order, voucher: Voucher) -> Money:
- """Calculate products discount value for a voucher, depending on its type."""
- prices = None
- if voucher and voucher.type == VoucherType.SPECIFIC_PRODUCT:
- prices = get_prices_of_discounted_specific_product(order.lines.all(), voucher)
- if not prices:
- msg = "This offer is only valid for selected items."
- raise NotApplicable(msg)
- return get_products_voucher_discount(voucher, prices, order.channel)
-
-
-def order_with_lines(
- order,
- product_type,
- category,
- shipping_zone,
- warehouse,
- channel_USD,
- default_tax_class,
-):
- product = Product.objects.create(
- name="Test product",
- slug="test-product-8",
- product_type=product_type,
- category=category,
- tax_class=default_tax_class,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=datetime.datetime.now(tz=datetime.UTC),
- )
- variant = ProductVariant.objects.create(product=product, sku="SKU_AA")
- channel_listing = ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- )
- stock = Stock.objects.create(
- warehouse=warehouse, product_variant=variant, quantity=5
- )
- base_price = variant.get_price(channel_listing)
- currency = base_price.currency
- gross = Money(amount=base_price.amount * Decimal(1.23), currency=currency)
- quantity = 3
- unit_price = TaxedMoney(net=base_price, gross=gross)
- line = order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=base_price,
- undiscounted_base_unit_price=base_price,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(product_type.tax_class),
- )
- Allocation.objects.create(
- order_line=line, stock=stock, quantity_allocated=line.quantity
- )
-
- product = Product.objects.create(
- name="Test product 2",
- slug="test-product-9",
- product_type=product_type,
- category=category,
- tax_class=default_tax_class,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=timezone.now(),
- )
- variant = ProductVariant.objects.create(product=product, sku="SKU_B")
- channel_listing = ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(20),
- discounted_price_amount=Decimal(20),
- cost_price_amount=Decimal(2),
- currency=channel_USD.currency_code,
- )
- stock = Stock.objects.create(
- product_variant=variant, warehouse=warehouse, quantity=2
- )
- stock.refresh_from_db()
-
- base_price = variant.get_price(channel_listing)
- currency = base_price.currency
- gross = Money(amount=base_price.amount * Decimal(1.23), currency=currency)
- unit_price = TaxedMoney(net=base_price, gross=gross)
- quantity = 2
- line = order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=base_price,
- undiscounted_base_unit_price=base_price,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(product_type.tax_class),
- )
- Allocation.objects.create(
- order_line=line, stock=stock, quantity_allocated=line.quantity
- )
-
- order.shipping_address = order.billing_address.get_copy()
- order.channel = channel_USD
- shipping_method = shipping_zone.shipping_methods.first()
- shipping_price = shipping_method.channel_listings.get(channel_id=channel_USD.id)
- order.shipping_method_name = shipping_method.name
- order.shipping_method = shipping_method
- order.shipping_tax_class = shipping_method.tax_class
- order.shipping_tax_class_name = shipping_method.tax_class.name
- order.shipping_tax_class_metadata = shipping_method.tax_class.metadata
- order.shipping_tax_class_private_metadata = (
- shipping_method.tax_class.private_metadata
- ) # noqa: E501
-
- net = shipping_price.get_total()
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
- order.shipping_price = TaxedMoney(net=net, gross=gross)
- order.base_shipping_price = net
- order.undiscounted_base_shipping_price = net
- order.shipping_tax_rate = calculate_tax_rate(order.shipping_price)
- order.save()
-
- recalculate_order(order)
-
- order.refresh_from_db()
- return order
-
-
-def order_with_lines_for_cc(
- warehouse_for_cc,
- channel_USD,
- customer_user,
- product_variant_list,
-):
- address = customer_user.default_billing_address.get_copy()
-
- order = Order.objects.create(
- billing_address=address,
- channel=channel_USD,
- currency=channel_USD.currency_code,
- shipping_address=address,
- user_email=customer_user.email,
- user=customer_user,
- origin=OrderOrigin.CHECKOUT,
- )
-
- order.collection_point = warehouse_for_cc
- order.collection_point_name = warehouse_for_cc.name
- order.save()
-
- variant = product_variant_list[0]
- channel_listing = variant.channel_listings.get(channel=channel_USD)
- quantity = 1
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- unit_price = TaxedMoney(net=net, gross=gross)
- line = order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(variant.product.product_type.tax_class),
- )
- Allocation.objects.create(
- order_line=line,
- stock=warehouse_for_cc.stock_set.filter(product_variant=variant).first(),
- quantity_allocated=line.quantity,
- )
-
- recalculate_order(order)
-
- order.refresh_from_db()
- return order
-
-
-def order_with_lines_and_catalogue_promotion(
- order_with_lines, channel_USD, catalogue_promotion_without_rules
-):
- order = order_with_lines
- promotion = catalogue_promotion_without_rules
- line = order.lines.get(quantity=3)
- variant = line.variant
- reward_value = Decimal(3)
- rule = promotion.rules.create(
- name="Catalogue rule fixed",
- catalogue_predicate={
- "variantPredicate": {
- "ids": [graphene.Node.to_global_id("ProductVariant", variant)]
- }
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=reward_value,
- )
- rule.channels.add(channel_USD)
-
- listing = variant.channel_listings.get(channel=channel_USD)
- listing.discounted_price_amount = listing.price_amount - reward_value
- listing.save(update_fields=["discounted_price_amount"])
- listing.variantlistingpromotionrule.create(
- promotion_rule=rule,
- discount_amount=reward_value,
- currency=order.currency,
- )
-
- line.discounts.create(
- type=DiscountType.PROMOTION,
- value_type=RewardValueType.FIXED,
- value=reward_value,
- amount_value=reward_value * line.quantity,
- currency=order.currency,
- promotion_rule=rule,
- )
- return order
-
-
-def order_with_lines_and_order_promotion(
- order_with_lines,
- channel_USD,
- order_promotion_without_rules,
-):
- order = order_with_lines
- promotion = order_promotion_without_rules
- rule = promotion.rules.create(
- name="Fixed subtotal rule",
- order_predicate={
- "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal(25),
- reward_type=RewardType.SUBTOTAL_DISCOUNT,
- )
- rule.channels.add(channel_USD)
-
- order.discounts.create(
- promotion_rule=rule,
- type=DiscountType.ORDER_PROMOTION,
- value_type=rule.reward_value_type,
- value=rule.reward_value,
- amount_value=rule.reward_value,
- currency=order.currency,
- )
- return order
-
-
-def order_with_lines_and_gift_promotion(
- order_with_lines,
- channel_USD,
- order_promotion_without_rules,
- variant_with_many_stocks,
-):
- order = order_with_lines
- variant = variant_with_many_stocks
- variant_listing = variant.channel_listings.get(channel=channel_USD)
- promotion = order_promotion_without_rules
- rule = promotion.rules.create(
- name="Gift subtotal rule",
- order_predicate={
- "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
- },
- reward_type=RewardType.GIFT,
- )
- rule.channels.add(channel_USD)
- rule.gifts.set([variant])
-
- gift_line = order.lines.create(
- quantity=1,
- variant=variant,
- is_gift=True,
- currency=order.currency,
- unit_price_net_amount=0,
- unit_price_gross_amount=0,
- total_price_net_amount=0,
- total_price_gross_amount=0,
- is_shipping_required=True,
- is_gift_card=False,
- )
- gift_line.discounts.create(
- promotion_rule=rule,
- type=DiscountType.ORDER_PROMOTION,
- value_type=RewardValueType.FIXED,
- value=variant_listing.price_amount,
- amount_value=variant_listing.price_amount,
- currency=order.currency,
- )
- return order
-
-
-def order_fulfill_data(order_with_lines, warehouse, checkout):
- FulfillmentData = namedtuple("FulfillmentData", "order variables warehouse")
- order = order_with_lines
- order_id = graphene.Node.to_global_id("Order", order.id)
- order_line, order_line2 = order.lines.all()
- 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)
-
- variables = {
- "order": order_id,
- "input": {
- "notifyCustomer": False,
- "allowStockToBeExceeded": True,
- "lines": [
- {
- "orderLineId": order_line_id,
- "stocks": [{"quantity": 3, "warehouse": warehouse_id}],
- },
- {
- "orderLineId": order_line2_id,
- "stocks": [{"quantity": 2, "warehouse": warehouse_id}],
- },
- ],
- },
- }
-
- return FulfillmentData(order, variables, warehouse)
-
-
-def lines_info(order_with_lines):
- return [
- OrderLineInfo(
- line=line,
- quantity=line.quantity,
- variant=line.variant,
- warehouse_pk=line.allocations.first().stock.warehouse.pk,
- )
- for line in order_with_lines.lines.all()
- ]
-
-
-def order_with_lines_and_events(order_with_lines, staff_user):
- events = []
- for event_type, _ in OrderEvents.CHOICES:
- events.append(
- OrderEvent(
- type=event_type,
- order=order_with_lines,
- user=staff_user,
- )
- )
- OrderEvent.objects.bulk_create(events)
- fulfillment_refunded_event(
- order=order_with_lines,
- user=staff_user,
- app=None,
- refunded_lines=[(1, order_with_lines.lines.first())],
- amount=Decimal("10.0"),
- shipping_costs_included=False,
- )
- order_added_products_event(
- order=order_with_lines,
- user=staff_user,
- app=None,
- order_lines=[order_with_lines.lines.first()],
- quantity_diff=1,
- )
- return order_with_lines
-
-
-def order_with_lines_channel_PLN(
- customer_user,
- product_type,
- category,
- shipping_method_channel_PLN,
- warehouse,
- channel_PLN,
-):
- address = customer_user.default_billing_address.get_copy()
- order = Order.objects.create(
- billing_address=address,
- channel=channel_PLN,
- shipping_address=address,
- user_email=customer_user.email,
- user=customer_user,
- origin=OrderOrigin.CHECKOUT,
- )
- product = Product.objects.create(
- name="Test product in PLN channel",
- slug="test-product-8-pln",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_PLN,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=timezone.now(),
- )
- variant = ProductVariant.objects.create(product=product, sku="SKU_A_PLN")
- channel_listing = ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_PLN,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_PLN.currency_code,
- )
- stock = Stock.objects.create(
- warehouse=warehouse, product_variant=variant, quantity=5
- )
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 3
- unit_price = TaxedMoney(net=net, gross=gross)
- line = order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(product_type.tax_class),
- )
- Allocation.objects.create(
- order_line=line, stock=stock, quantity_allocated=line.quantity
- )
-
- product = Product.objects.create(
- name="Test product 2 in PLN channel",
- slug="test-product-9-pln",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_PLN,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=timezone.now(),
- )
- variant = ProductVariant.objects.create(product=product, sku="SKU_B_PLN")
- channel_listing = ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_PLN,
- price_amount=Decimal(20),
- discounted_price_amount=Decimal(20),
- cost_price_amount=Decimal(2),
- currency=channel_PLN.currency_code,
- )
- stock = Stock.objects.create(
- product_variant=variant, warehouse=warehouse, quantity=2
- )
-
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 2
- unit_price = TaxedMoney(net=net, gross=gross)
- line = order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(product_type.tax_class),
- )
- Allocation.objects.create(
- order_line=line, stock=stock, quantity_allocated=line.quantity
- )
-
- order.shipping_address = order.billing_address.get_copy()
- order.channel = channel_PLN
- shipping_method = shipping_method_channel_PLN
- shipping_price = shipping_method.channel_listings.get(
- channel_id=channel_PLN.id,
- )
- order.shipping_method_name = shipping_method.name
- order.shipping_method = shipping_method
-
- net = shipping_price.get_total()
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
- order.shipping_price = TaxedMoney(net=net, gross=gross)
- order.base_shipping_price = net
- order.undiscounted_base_shipping_price = net
- order.shipping_tax_rate = calculate_tax_rate(order.shipping_price)
- order.save()
-
- recalculate_order(order)
-
- order.refresh_from_db()
- return order
-
-
-def order_with_line_without_inventory_tracking(
- order, variant_without_inventory_tracking
-):
- variant = variant_without_inventory_tracking
- product = variant.product
- channel = order.channel
- channel_listing = variant.channel_listings.get(channel=channel)
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 3
- unit_price = TaxedMoney(net=net, gross=gross)
- order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(product.product_type.tax_class),
- )
-
- recalculate_order(order)
-
- order.refresh_from_db()
- return order
-
-
-def order_with_preorder_lines(
- order, product_type, category, shipping_zone, warehouse, channel_USD
-):
- product = Product.objects.create(
- name="Test product",
- slug="test-product-8",
- product_type=product_type,
- category=category,
- )
- ProductChannelListing.objects.create(
- product=product,
- channel=channel_USD,
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_at=timezone.now(),
- )
- variant = ProductVariant.objects.create(
- product=product, sku="SKU_AA_P", is_preorder=True
- )
- channel_listing = ProductVariantChannelListing.objects.create(
- variant=variant,
- channel=channel_USD,
- price_amount=Decimal(10),
- discounted_price_amount=Decimal(10),
- cost_price_amount=Decimal(1),
- currency=channel_USD.currency_code,
- preorder_quantity_threshold=10,
- )
-
- net = variant.get_price(channel_listing)
- currency = net.currency
- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)
- quantity = 3
- unit_price = TaxedMoney(net=net, gross=gross)
- line = order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- quantity=quantity,
- variant=variant,
- unit_price=unit_price,
- total_price=unit_price * quantity,
- undiscounted_unit_price=unit_price,
- undiscounted_total_price=unit_price * quantity,
- base_unit_price=unit_price.gross,
- undiscounted_base_unit_price=unit_price.gross,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(product_type.tax_class),
- )
- PreorderAllocation.objects.create(
- order_line=line,
- product_variant_channel_listing=channel_listing,
- quantity=line.quantity,
- )
-
- order.shipping_address = order.billing_address.get_copy()
- order.channel = channel_USD
- shipping_method = shipping_zone.shipping_methods.first()
- shipping_price = shipping_method.channel_listings.get(channel_id=channel_USD.id)
- order.shipping_method_name = shipping_method.name
- order.shipping_method = shipping_method
-
- net = shipping_price.get_total()
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
- order.shipping_price = TaxedMoney(net=net, gross=gross)
- order.base_shipping_price = net
- order.undiscounted_base_shipping_price = net
- order.save()
-
- recalculate_order(order)
-
- order.refresh_from_db()
- return order
-
-
-def order_events(order):
- order_events = [
- OrderEvent(type=event_type, order=order)
- for event_type, _ in OrderEvents.CHOICES
- ]
- OrderEvent.objects.bulk_create(order_events)
- return order_events
-
-
-def fulfilled_order(order_with_lines):
- order = order_with_lines
- order.invoices.create(
- url="http://www.example.com/invoice.pdf",
- number="01/12/2020/TEST",
- created_at=datetime.datetime.now(tz=datetime.UTC),
- status=JobStatus.SUCCESS,
- )
- fulfillment = order.fulfillments.create(tracking_number="123")
- line_1 = order.lines.first()
- stock_1 = line_1.allocations.get().stock
- warehouse_1_pk = stock_1.warehouse.pk
- line_2 = order.lines.last()
- stock_2 = line_2.allocations.get().stock
- warehouse_2_pk = stock_2.warehouse.pk
- fulfillment.lines.create(order_line=line_1, quantity=line_1.quantity, stock=stock_1)
- fulfillment.lines.create(order_line=line_2, quantity=line_2.quantity, stock=stock_2)
- fulfill_order_lines(
- [
- OrderLineInfo(
- line=line_1, quantity=line_1.quantity, warehouse_pk=warehouse_1_pk
- ),
- OrderLineInfo(
- line=line_2, quantity=line_2.quantity, warehouse_pk=warehouse_2_pk
- ),
- ],
- manager=get_plugins_manager(allow_replica=False),
- )
- order.status = OrderStatus.FULFILLED
- order.save(update_fields=["status"])
- return order
-
-
-def unconfirmed_order_with_lines(order_with_lines):
- order = order_with_lines
- order.status = OrderStatus.UNCONFIRMED
- order.save(update_fields=["status"])
- return order
-
-
-def fulfilled_order_without_inventory_tracking(
- order_with_line_without_inventory_tracking,
-):
- order = order_with_line_without_inventory_tracking
- fulfillment = order.fulfillments.create(tracking_number="123")
- line = order.lines.first()
- stock = line.variant.stocks.get()
- warehouse_pk = stock.warehouse.pk
- fulfillment.lines.create(order_line=line, quantity=line.quantity, stock=stock)
- fulfill_order_lines(
- [OrderLineInfo(line=line, quantity=line.quantity, warehouse_pk=warehouse_pk)],
- get_plugins_manager(allow_replica=False),
- )
- order.status = OrderStatus.FULFILLED
- order.save(update_fields=["status"])
- return order
-
-
-def fulfilled_order_with_cancelled_fulfillment(fulfilled_order):
- fulfillment = fulfilled_order.fulfillments.create()
- line_1 = fulfilled_order.lines.first()
- line_2 = fulfilled_order.lines.last()
- fulfillment.lines.create(order_line=line_1, quantity=line_1.quantity)
- fulfillment.lines.create(order_line=line_2, quantity=line_2.quantity)
- fulfillment.status = FulfillmentStatus.CANCELED
- fulfillment.save()
- return fulfilled_order
-
-
-def fulfilled_order_with_all_cancelled_fulfillments(
- fulfilled_order, staff_user, warehouse
-):
- fulfillment = fulfilled_order.fulfillments.get()
- cancel_fulfillment(
- fulfillment,
- staff_user,
- None,
- warehouse,
- get_plugins_manager(allow_replica=False),
- )
- return fulfilled_order
-
-
-def fulfillment(fulfilled_order):
- return fulfilled_order.fulfillments.first()
-
-
-def fulfillment_awaiting_approval(fulfilled_order):
- fulfillment = fulfilled_order.fulfillments.first()
- fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL
- fulfillment.save(update_fields=["status"])
-
- quantity = 1
- fulfillment_lines_to_update = []
- order_lines_to_update = []
- for f_line in fulfillment.lines.all():
- f_line.quantity = quantity
- fulfillment_lines_to_update.append(f_line)
-
- order_line = f_line.order_line
- order_line.quantity_fulfilled = quantity
- order_lines_to_update.append(order_line)
-
- FulfillmentLine.objects.bulk_update(fulfillment_lines_to_update, ["quantity"])
- OrderLine.objects.bulk_update(order_lines_to_update, ["quantity_fulfilled"])
-
- return fulfillment
-
-
-def draft_order(order_with_lines):
- Allocation.objects.filter(order_line__order=order_with_lines).delete()
- order_with_lines.status = OrderStatus.DRAFT
- order_with_lines.origin = OrderOrigin.DRAFT
- order_with_lines.save(update_fields=["status", "origin"])
- return order_with_lines
-
-
-def draft_order_with_fixed_discount_order(draft_order):
- value = Decimal("20")
- discount = partial(fixed_discount, discount=Money(value, draft_order.currency))
- draft_order.total = discount(draft_order.total)
- draft_order.discounts.create(
- value_type=DiscountValueType.FIXED,
- type=DiscountType.MANUAL,
- value=value,
- reason="Discount reason",
- amount=(draft_order.undiscounted_total - draft_order.total).gross,
- )
- draft_order.save()
- return draft_order
-
-
-def draft_order_with_voucher(
- draft_order_with_fixed_discount_order, voucher_multiple_use
-):
- order = draft_order_with_fixed_discount_order
- voucher_code = voucher_multiple_use.codes.first()
- discount = order.discounts.first()
- discount.type = DiscountType.VOUCHER
- discount.voucher = voucher_multiple_use
- discount.voucher_code = voucher_code.code
- discount.save(update_fields=["type", "voucher", "voucher_code"])
-
- order.voucher = voucher_multiple_use
- order.voucher_code = voucher_code.code
- order.save(update_fields=["voucher", "voucher_code"])
-
- channel = order.channel
- channel.include_draft_order_in_voucher_usage = True
- channel.save(update_fields=["include_draft_order_in_voucher_usage"])
-
- return order
-
-
-def draft_order_with_free_shipping_voucher(
- draft_order_with_fixed_discount_order, voucher_free_shipping
-):
- order = draft_order_with_fixed_discount_order
- voucher_code = voucher_free_shipping.codes.first()
- discount = order.discounts.first()
- discount.type = DiscountType.VOUCHER
- discount.voucher = voucher_free_shipping
- discount.voucher_code = voucher_code.code
- discount.save(update_fields=["type", "voucher", "voucher_code"])
-
- channel = order.channel
- channel.include_draft_order_in_voucher_usage = True
- channel.save(update_fields=["include_draft_order_in_voucher_usage"])
-
- order.voucher = voucher_free_shipping
- order.voucher_code = voucher_code.code
- create_or_update_discount_object_from_order_level_voucher(
- order, settings.DATABASE_CONNECTION_DEFAULT_NAME
- )
-
- subtotal = base_order_subtotal(order, order.lines.all())
- shipping_price = order.base_shipping_price
- order.subtotal = TaxedMoney(gross=subtotal, net=subtotal)
- order.shipping_price = TaxedMoney(net=shipping_price, gross=shipping_price)
- total = subtotal + shipping_price
- order.total = TaxedMoney(net=total, gross=total)
- order.save()
-
- return order
-
-
-def draft_order_without_inventory_tracking(order_with_line_without_inventory_tracking):
- order_with_line_without_inventory_tracking.status = OrderStatus.DRAFT
- order_with_line_without_inventory_tracking.origin = OrderStatus.DRAFT
- order_with_line_without_inventory_tracking.save(update_fields=["status", "origin"])
- return order_with_line_without_inventory_tracking
-
-
-def draft_order_with_preorder_lines(order_with_preorder_lines):
- PreorderAllocation.objects.filter(
- order_line__order=order_with_preorder_lines
- ).delete()
- order_with_preorder_lines.status = OrderStatus.DRAFT
- order_with_preorder_lines.origin = OrderOrigin.DRAFT
- order_with_preorder_lines.save(update_fields=["status", "origin"])
- return order_with_preorder_lines
-
-
@pytest.fixture
def payment_txn_preauth(order_with_lines, payment_dummy):
order = order_with_lines