Skip to content

Commit

Permalink
Apply changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Korycinski committed Jan 20, 2021
1 parent 0f282a8 commit 6568968
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
35 changes: 22 additions & 13 deletions saleor/order/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,7 @@ def create_refund_fulfillment(
return refunded_fulfillment


@transaction.atomic
def create_replace_order(
requester: Optional["User"],
original_order: "Order",
order_lines_to_replace: List[OrderLineData],
fulfillment_lines_to_replace: List[FulfillmentLineData],
) -> "Order":
"""Create draft order with lines to replace."""
def _populate_replace_order_fields(original_order: "Order"):
replace_order = Order()
replace_order.status = OrderStatus.DRAFT
replace_order.user_id = original_order.user_id
Expand All @@ -680,7 +673,19 @@ def create_replace_order(
replace_order.shipping_address.save()
replace_order.save()
original_order.refresh_from_db()
return replace_order


@transaction.atomic
def create_replace_order(
requester: Optional["User"],
original_order: "Order",
order_lines_to_replace: List[OrderLineData],
fulfillment_lines_to_replace: List[FulfillmentLineData],
) -> "Order":
"""Create draft order with lines to replace."""

replace_order = _populate_replace_order_fields(original_order)
order_line_to_create: Dict[OrderLineIDType, OrderLine] = dict()

# iterate over lines without fulfillment to get the items for replace.
Expand Down Expand Up @@ -845,12 +850,16 @@ def create_return_fulfillment(
line_data.quantity,
order_line,
)
order_returned(
order,
user=requester,
returned_lines=list(returned_lines.values()),
manager=manager,
returned_lines_list = list(returned_lines.values())
transaction.on_commit(
lambda: order_returned(
order,
user=requester,
returned_lines=returned_lines_list,
manager=manager,
)
)

return return_fulfillment


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from prices import Money, TaxedMoney

from ...plugins.manager import get_plugins_manager
from ...tests.utils import flush_post_commit_hooks
from ...warehouse.models import Allocation, Stock
from .. import FulfillmentLineData, FulfillmentStatus, OrderEvents, OrderLineData
from ..actions import create_fulfillments_for_returned_products
Expand Down Expand Up @@ -62,6 +63,7 @@ def test_create_return_fulfillment_only_order_lines(
assert not replace_order

# check if we have correct events
flush_post_commit_hooks()
events = order_with_lines.events.all()
assert events.count() == 1
returned_event = events[0]
Expand Down
14 changes: 12 additions & 2 deletions saleor/order/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ def update_order_prices(order, discounts):
recalculate_order(order)


def update_order_status(order):
"""Update order status depending on fulfillments."""
def _calculate_quantity_including_returns(order):
lines = list(order.lines.all())
total_quantity = sum([line.quantity for line in lines])
quantity_fulfilled = sum([line.quantity_fulfilled for line in lines])
Expand All @@ -182,6 +181,17 @@ def update_order_status(order):
# calculating the order status
total_quantity -= quantity_replaced
quantity_fulfilled -= quantity_replaced
return total_quantity, quantity_fulfilled, quantity_returned


def update_order_status(order):
"""Update order status depending on fulfillments."""

(
total_quantity,
quantity_fulfilled,
quantity_returned,
) = _calculate_quantity_including_returns(order)

# total_quantity == 0 means that all products have been replaced, we don't change
# the order status in that case
Expand Down

0 comments on commit 6568968

Please sign in to comment.