Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move canceling fulfillment to separate function
Anna Siwik26fda5930cba
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 4f4e192baf..c10dbf63b5 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -11,13 +11,12 @@ from ...account.i18n import (from ...cart.forms import QuantityFieldfrom ...core.exceptions import InsufficientStockfrom ...discount.utils import decrease_voucher_usage-from ...order import FulfillmentStatusfrom ...order.emails import send_note_confirmationfrom ...order.models import Fulfillment, FulfillmentLine, OrderLine, OrderNotefrom ...order.utils import (- add_variant_to_order, change_order_line_quantity,- merge_duplicates_into_order_line, recalculate_order,- restock_fulfillment_lines, cancel_order)+ add_variant_to_order, cancel_fulfillment, cancel_order,+ change_order_line_quantity, merge_duplicates_into_order_line,+ recalculate_order)from ...product.models import Product, ProductVariant, Stockfrom ...product.utils import allocate_stock, deallocate_stockfrom ..forms import AjaxSelect2ChoiceField@@ -225,10 +224,7 @@ class CancelFulfillmentForm(forms.Form):return datadef cancel_fulfillment(self):- if self.cleaned_data.get('restock'):- restock_fulfillment_lines(self.fulfillment)- self.fulfillment.status = FulfillmentStatus.CANCELED- self.fulfillment.save(update_fields=['status'])+ cancel_fulfillment(self.fulfillment, self.cleaned_data.get('restock'))class RemoveVoucherForm(forms.Form):diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 55998b6858..e29e284dec 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -127,6 +127,10 @@ class Order(models.Model):def is_pre_authorized(self):return self.payments.filter(status=PaymentStatus.PREAUTH).exists()+ def is_unfulfilled(self):+ return not self.fulfillments.filter(+ status=FulfillmentStatus.FULFILLED).exists()+def is_fulfilled(self):return all([line.is_fulfilled() for line in self])diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 43bf4d68f4..714edd26ce 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -57,6 +57,20 @@ def cancel_order(order, restock):order.save(update_fields=['status'])+def cancel_fulfillment(fulfillment, restock):+ """Cancel fulfillment.++ Return products to corresponding stocks if restock is set to True."""+ if restock:+ restock_fulfillment_lines(fulfillment)+ fulfillment.status = FulfillmentStatus.CANCELED+ fulfillment.save(update_fields=['status'])+ order = fulfillment.order+ if order.is_unfulfilled():+ order.status = OrderStatus.UNFULFILLED+ order.save(update_fields=['status'])++def attach_order_to_user(order, user):"""Associates existing order with user account."""order.user = userdiff --git a/tests/conftest.py b/tests/conftest.pyindex 292e909abf..1481fba3f4 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -17,6 +17,7 @@ from saleor.cart.models import Cartfrom saleor.checkout.core import Checkoutfrom saleor.dashboard.order.utils import fulfill_order_linefrom saleor.discount.models import Sale, Voucher+from saleor.order import OrderStatusfrom saleor.order.models import Order, OrderLinefrom saleor.order.utils import recalculate_orderfrom saleor.page.models import Page@@ -468,6 +469,8 @@ def fulfilled_order(order_with_lines_and_stock):fulfill_order_line(line_1, line_1.quantity)fulfillment.lines.create(order_line=line_2, quantity=line_2.quantity)fulfill_order_line(line_2, line_2.quantity)+ order.status = OrderStatus.FULFILLED+ order.save(update_fields=['status'])return orderdiff --git a/tests/test_order.py b/tests/test_order.pyindex cdc5779576..816d4fe3de 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -8,7 +8,7 @@ from saleor.order import FulfillmentStatus, models, OrderStatusfrom saleor.order.emails import collect_data_for_emailfrom saleor.order.forms import OrderNoteFormfrom saleor.order.utils import (- add_variant_to_order, cancel_order, recalculate_order,+ add_variant_to_order, cancel_fulfillment, cancel_order, recalculate_order,restock_fulfillment_lines, restock_order_lines)@@ -212,3 +212,10 @@ def test_cancel_order(fulfilled_order):f.status == FulfillmentStatus.CANCELEDfor f in fulfilled_order.fulfillments.all()])assert fulfilled_order.status == OrderStatus.CANCELED+++def test_cancel_fulfillment(fulfilled_order):+ fulfillment = fulfilled_order.fulfillments.first()+ cancel_fulfillment(fulfillment, restock=False)+ assert fulfillment.status == FulfillmentStatus.CANCELED+ assert fulfilled_order.status == OrderStatus.UNFULFILLED