Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fulfill order line functionality
Anna Siwik0ee8d6a970f9
diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.pyindex 98b7c95d74..3f0aff5fc6 100644--- a/saleor/dashboard/order/utils.py+++ b/saleor/dashboard/order/utils.py@@ -2,6 +2,8 @@ from django.conf import settingsfrom django.contrib.sites.shortcuts import get_current_sitefrom django.template.loader import get_template+from ...product.utils import decrease_stock+INVOICE_TEMPLATE = 'dashboard/order/pdf/invoice.html'PACKING_SLIP_TEMPLATE = 'dashboard/order/pdf/packing_slip.html'@@ -35,3 +37,9 @@ def create_packing_slip_pdf(order, absolute_url):rendered_template = get_template(PACKING_SLIP_TEMPLATE).render(ctx)pdf_file = _create_pdf(rendered_template, absolute_url)return pdf_file, order+++def fulfill_order_line(order_line, quantity, fulfillment):+ """Fulfill order line with given quantity."""+ decrease_stock(order_line.stock, quantity)+ return fulfillment.lines.create(order_line=order_line, quantity=quantity)diff --git a/saleor/product/utils.py b/saleor/product/utils.pyindex ab09152774..d5c71d16d7 100644--- a/saleor/product/utils.py+++ b/saleor/product/utils.py@@ -387,11 +387,6 @@ def deallocate_stock(stock, quantity):stock.save(update_fields=['quantity_allocated'])-def increase_stock(stock, quantity):- stock.quantity = F('quantity') + quantity- stock.save(update_fields=['quantity'])--def decrease_stock(stock, quantity):stock.quantity = F('quantity') - quantitystock.quantity_allocated = F('quantity_allocated') - quantitydiff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 0102c07241..19b3720241 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -9,7 +9,9 @@ from prices import Money, TaxedMoneyfrom tests.utils import get_redirect_location, get_url_pathfrom saleor.dashboard.order.forms import ChangeQuantityForm, OrderNoteForm-from saleor.order.models import Order, OrderHistoryEntry, OrderLine, OrderNote+from saleor.dashboard.order.utils import fulfill_order_line+from saleor.order.models import (+ Fulfillment, Order, OrderHistoryEntry, OrderLine, OrderNote)from saleor.order.utils import (add_variant_to_existing_lines, add_variant_to_order,change_order_line_quantity)@@ -467,3 +469,14 @@ def test_note_form_sent_email(form = OrderNoteForm({'content': 'test_note'}, instance=note)form.send_confirmation_email()assert mock_send_note_confirmation.called_once()+++def test_fulfill_order_line(order_with_lines_and_stock):+ order = order_with_lines_and_stock+ fulfillment = Fulfillment.objects.create(order=order)+ line = order.lines.first()+ stock = line.stock+ stock_quantity_after = stock.quantity - line.quantity+ fulfill_order_line(line, line.quantity, fulfillment)+ stock.refresh_from_db()+ assert stock.quantity == stock_quantity_afterdiff --git a/tests/test_product.py b/tests/test_product.pyindex a5c9ad3781..d6fd872db9 100644--- a/tests/test_product.py+++ b/tests/test_product.py@@ -16,7 +16,7 @@ from saleor.product.utils import (allocate_stock, deallocate_stock, decrease_stock,get_attributes_display_map, get_availability,get_product_availability_status, get_variant_availability_status,- get_variant_picker_data, increase_stock)+ get_variant_picker_data)@pytest.fixture()@@ -53,17 +53,6 @@ def test_decrease_stock(product_in_stock):assert stock.quantity_allocated == 30-def test_increase_stock(product_in_stock):- stock = product_in_stock.variants.first().stock.first()- stock.quantity = 100- stock.quantity_allocated = 80- stock.save()- increase_stock(stock, 50)- stock.refresh_from_db()- assert stock.quantity == 150- assert stock.quantity_allocated == 80--def test_deallocate_stock(product_in_stock):stock = product_in_stock.variants.first().stock.first()stock.quantity = 100