Skip to content

Commit

Permalink
Fix tax calculation for Click and Collect option. (#15488)
Browse files Browse the repository at this point in the history
  • Loading branch information
Air-t committed Feb 29, 2024
1 parent c09cb4a commit 39abb0f
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 41 deletions.
1 change: 1 addition & 0 deletions saleor/checkout/error_codes.py
Expand Up @@ -30,6 +30,7 @@ class CheckoutErrorCode(Enum):
EMAIL_NOT_SET = "email_not_set"
NO_LINES = "no_lines"
INACTIVE_PAYMENT = "inactive_payment"
SHIPPING_CHANGE_FORBIDDEN = "shipping_change_forbidden"


class OrderCreateFromCheckoutErrorCode(Enum):
Expand Down
Expand Up @@ -21,6 +21,7 @@
from ....shipping import interface as shipping_interface
from ....shipping import models as shipping_models
from ....shipping.utils import convert_to_shipping_method_data
from ....warehouse import WarehouseClickAndCollectOption
from ....warehouse import models as warehouse_models
from ....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ...core import ResolveInfo
Expand Down Expand Up @@ -58,7 +59,9 @@ class Arguments:
class Meta:
description = (
"Updates the delivery method (shipping method or pick up point) "
"of the checkout." + ADDED_IN_31
"of the checkout. "
"Updates the checkout shipping_address for click and collect delivery "
"for a warehouse address. " + ADDED_IN_31
)
doc_category = DOC_CATEGORY_CHECKOUT
error_type_class = CheckoutError
Expand Down Expand Up @@ -237,6 +240,7 @@ def _update_delivery_method(
external_shipping_method: Optional[shipping_interface.ShippingMethodData],
collection_point: Optional[Warehouse],
) -> None:
checkout_fields_to_update = ["shipping_method", "collection_point"]
checkout = checkout_info.checkout
if external_shipping_method:
set_external_shipping_id(
Expand All @@ -246,15 +250,19 @@ def _update_delivery_method(
delete_external_shipping_id(checkout=checkout)
checkout.shipping_method = shipping_method
checkout.collection_point = collection_point
if (
collection_point is not None
and collection_point.click_and_collect_option
== WarehouseClickAndCollectOption.LOCAL_STOCK
):
checkout.shipping_address = collection_point.address
checkout_info.shipping_address = collection_point.address
checkout_fields_to_update += ["shipping_address"]
invalidate_prices_updated_fields = invalidate_checkout_prices(
checkout_info, lines, manager, save=False
)
checkout.save(
update_fields=[
"shipping_method",
"collection_point",
]
+ invalidate_prices_updated_fields
update_fields=checkout_fields_to_update + invalidate_prices_updated_fields
)
get_or_create_checkout_metadata(checkout).save()
cls.call_event(manager.checkout_updated, checkout)
Expand Down Expand Up @@ -323,7 +331,6 @@ def perform_mutation(
}
)
type_name = cls._resolve_delivery_method_type(delivery_method_id)

checkout_info = fetch_checkout_info(checkout, lines, manager)
if type_name == "Warehouse":
return cls.perform_on_collection_point(
Expand Down
Expand Up @@ -32,6 +32,7 @@
from ..types import Checkout
from .checkout_create import CheckoutAddressValidationRules
from .utils import (
ERROR_CC_ADDRESS_CHANGE_FORBIDDEN,
ERROR_DOES_NOT_SHIP,
check_lines_quantity,
get_checkout,
Expand Down Expand Up @@ -152,6 +153,16 @@ def perform_mutation(
)
}
)
# prevent from changing the shipping address when click and collect is used.
if checkout.collection_point_id:
raise ValidationError(
{
"shipping_address": ValidationError(
ERROR_CC_ADDRESS_CHANGE_FORBIDDEN,
code=CheckoutErrorCode.SHIPPING_CHANGE_FORBIDDEN.value,
)
}
)
address_validation_rules = validation_rules or {}
shipping_address_instance = cls.validate_address(
shipping_address,
Expand Down
4 changes: 4 additions & 0 deletions saleor/graphql/checkout/mutations/utils.py
Expand Up @@ -45,6 +45,10 @@


ERROR_DOES_NOT_SHIP = "This checkout doesn't need shipping"
ERROR_CC_ADDRESS_CHANGE_FORBIDDEN = (
"Can't change shipping address manually. "
"For click and collect delivery, address is set to a warehouse address."
)


@dataclass
Expand Down

0 comments on commit 39abb0f

Please sign in to comment.