Skip to content

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

Move fulfill_order_line function from dashboard.order.utils to order.utils

Maciej Korycinski59917ad8bec9
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index f1bd639c1f..241c7d7587 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -16,7 +16,8 @@ from ...order import OrderStatus
from ...order.models import Fulfillment, FulfillmentLine, Order, OrderLine
from ...order.utils import (
add_variant_to_order, cancel_fulfillment, cancel_order,
- change_order_line_quantity, delete_order_line, recalculate_order)
+ change_order_line_quantity, delete_order_line, fulfill_order_line,
+ recalculate_order)
from ...payment import ChargeStatus, CustomPaymentChoices, PaymentError
from ...payment.utils import (
clean_mark_order_as_paid, gateway_capture, gateway_refund, gateway_void,
@@ -26,9 +27,7 @@ from ...product.utils import allocate_stock, deallocate_stock
from ...shipping.models import ShippingMethod
from ..forms import AjaxSelect2ChoiceField
from ..widgets import PhonePrefixWidget
-from .utils import (
- fulfill_order_line, remove_customer_from_order,
- update_order_with_user_addresses)
+from .utils import remove_customer_from_order, update_order_with_user_addresses
class CreateOrderFromDraftForm(forms.ModelForm):
diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.py
index 77cf6c3da0..1610230824 100644
--- a/saleor/dashboard/order/utils.py
+++ b/saleor/dashboard/order/utils.py
@@ -50,14 +50,6 @@ def create_packing_slip_pdf(order, fulfillment, absolute_url):
return pdf_file, order
-def fulfill_order_line(order_line, quantity):
- """Fulfill order line with given quantity."""
- if order_line.variant and order_line.variant.track_inventory:
- decrease_stock(order_line.variant, quantity)
- order_line.quantity_fulfilled += quantity
- order_line.save(update_fields=['quantity_fulfilled'])
-
-
def update_order_with_user_addresses(order):
"""Update addresses in an order based on a user assigned to an order."""
if order.shipping_address:
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index b738bb2d5e..e47185211a 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -4,10 +4,10 @@ import graphene
from django.utils.translation import npgettext_lazy, pgettext_lazy
from graphql_jwt.decorators import permission_required
-from ....dashboard.order.utils import fulfill_order_line
from ....order import OrderEvents, OrderEventsEmails, models
from ....order.emails import send_fulfillment_confirmation
-from ....order.utils import cancel_fulfillment, update_order_status
+from ....order.utils import (
+ cancel_fulfillment, update_order_status, fulfill_order_line)
from ...core.mutations import BaseMutation
from ...order.types import Fulfillment, Order
from ..types import OrderLine
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 9d3a690247..7f84b65513 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -16,7 +16,16 @@ from ..order import FulfillmentStatus, OrderStatus
from ..order.models import OrderLine
from ..payment import ChargeStatus
from ..payment.utils import gateway_refund, gateway_void
-from ..product.utils import allocate_stock, deallocate_stock, increase_stock
+from ..product.utils import (
+ allocate_stock, deallocate_stock, increase_stock, decrease_stock)
+
+
+def fulfill_order_line(order_line, quantity):
+ """Fulfill order line with given quantity."""
+ if order_line.variant and order_line.variant.track_inventory:
+ decrease_stock(order_line.variant, quantity)
+ order_line.quantity_fulfilled += quantity
+ order_line.save(update_fields=['quantity_fulfilled'])
def check_order_status(func):
diff --git a/tests/conftest.py b/tests/conftest.py
index 9aff66b911..9aab57b3bb 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -21,13 +21,12 @@ from saleor.checkout import utils
from saleor.checkout.models import Cart
from saleor.checkout.utils import add_variant_to_cart
from saleor.dashboard.menu.utils import update_menu
-from saleor.dashboard.order.utils import fulfill_order_line
from saleor.discount import VoucherType
from saleor.discount.models import Sale, Voucher, VoucherTranslation
from saleor.menu.models import Menu, MenuItem
from saleor.order import OrderEvents, OrderStatus
from saleor.order.models import FulfillmentStatus, Order, OrderEvent
-from saleor.order.utils import recalculate_order
+from saleor.order.utils import recalculate_order, fulfill_order_line
from saleor.page.models import Page
from saleor.payment import ChargeStatus, TransactionKind
from saleor.payment.models import Payment
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index 29d736545c..a7eabb4fa7 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -9,7 +9,7 @@ from saleor.checkout import AddressType
from saleor.core.utils.taxes import ZERO_MONEY, ZERO_TAXED_MONEY
from saleor.dashboard.order.forms import ChangeQuantityForm
from saleor.dashboard.order.utils import (
- fulfill_order_line, remove_customer_from_order, save_address_in_order,
+ remove_customer_from_order, save_address_in_order,
update_order_with_user_addresses)
from saleor.discount.utils import increase_voucher_usage
from saleor.order import (
@@ -483,47 +483,6 @@ def test_view_add_variant_to_order(admin_client, order_with_lines):
assert line.quantity == line_quantity_before + added_quantity
-def test_fulfill_order_line(order_with_lines):
- order = order_with_lines
- line = order.lines.first()
- quantity_fulfilled_before = line.quantity_fulfilled
- variant = line.variant
- stock_quantity_after = variant.quantity - line.quantity
-
- fulfill_order_line(line, line.quantity)
-
- variant.refresh_from_db()
- assert variant.quantity == stock_quantity_after
- assert line.quantity_fulfilled == quantity_fulfilled_before + line.quantity
-
-
-def test_fulfill_order_line_with_variant_deleted(order_with_lines):
- line = order_with_lines.lines.first()
- line.variant.delete()
-
- line.refresh_from_db()
-
- fulfill_order_line(line, line.quantity)
-
-
-def test_fulfill_order_line_without_inventory_tracking(order_with_lines):
- order = order_with_lines
- line = order.lines.first()
- quantity_fulfilled_before = line.quantity_fulfilled
- variant = line.variant
- variant.track_inventory = False
- variant.save()
-
- # stock should not change
- stock_quantity_after = variant.quantity
-
- fulfill_order_line(line, line.quantity)
-
- variant.refresh_from_db()
- assert variant.quantity == stock_quantity_after
- assert line.quantity_fulfilled == quantity_fulfilled_before + line.quantity
-
-
def test_view_change_fulfillment_tracking(admin_client, fulfilled_order):
fulfillment = fulfilled_order.fulfillments.first()
url = reverse(
diff --git a/tests/test_order.py b/tests/test_order.py
index eaaf9c6298..98d38c9aab 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -16,7 +16,7 @@ from saleor.order.models import Order
from saleor.order.utils import (
add_variant_to_order, cancel_fulfillment, cancel_order,
change_order_line_quantity, delete_order_line, recalculate_order,
- recalculate_order_weight, restock_fulfillment_lines, restock_order_lines,
+ fulfill_order_line, restock_fulfillment_lines, restock_order_lines,
update_order_prices, update_order_status)
from saleor.payment import ChargeStatus
from saleor.payment.models import Payment
@@ -577,3 +577,45 @@ def test_get_order_weight_non_existing_product(order_with_lines, product):
new_weight = order.get_total_weight()
assert old_weight == new_weight
+
+
+
+def test_fulfill_order_line(order_with_lines):
+ order = order_with_lines
+ line = order.lines.first()
+ quantity_fulfilled_before = line.quantity_fulfilled
+ variant = line.variant
+ stock_quantity_after = variant.quantity - line.quantity
+
+ fulfill_order_line(line, line.quantity)
+
+ variant.refresh_from_db()
+ assert variant.quantity == stock_quantity_after
+ assert line.quantity_fulfilled == quantity_fulfilled_before + line.quantity
+
+
+def test_fulfill_order_line_with_variant_deleted(order_with_lines):
+ line = order_with_lines.lines.first()
+ line.variant.delete()
+
+ line.refresh_from_db()
+
+ fulfill_order_line(line, line.quantity)
+
+
+def test_fulfill_order_line_without_inventory_tracking(order_with_lines):
+ order = order_with_lines
+ line = order.lines.first()
+ quantity_fulfilled_before = line.quantity_fulfilled
+ variant = line.variant
+ variant.track_inventory = False
+ variant.save()
+
+ # stock should not change
+ stock_quantity_after = variant.quantity
+
+ fulfill_order_line(line, line.quantity)
+
+ variant.refresh_from_db()
+ assert variant.quantity == stock_quantity_after
+ assert line.quantity_fulfilled == quantity_fulfilled_before + line.quantity