[FIX] website_sale_aplicoop: fix home delivery draft flow

This commit is contained in:
snt 2026-04-08 00:07:43 +02:00
parent 135967019e
commit e9809b90e9
2 changed files with 33 additions and 124 deletions

View file

@ -892,8 +892,8 @@ class AplicoopWebsiteSale(WebsiteSale):
if not items:
raise ValueError("No items in cart")
# Get delivery flag
is_delivery = data.get("is_delivery", False)
# Get delivery flag (normalize to strict boolean)
is_delivery = self._to_bool(data.get("is_delivery", False))
_logger.info(
"_validate_confirm_json: Valid request for order %d with %d items (is_delivery=%s)",
@ -904,6 +904,23 @@ class AplicoopWebsiteSale(WebsiteSale):
return order_id, group_order, current_user, items, is_delivery
def _to_bool(self, value):
"""Convert common JSON/form boolean representations to bool.
Accepts booleans, numeric values and common string variants.
"""
if isinstance(value, bool):
return value
if isinstance(value, (int, float)):
return value != 0
if isinstance(value, str):
normalized = value.strip().lower()
if normalized in {"1", "true", "t", "yes", "y", "on"}:
return True
if normalized in {"0", "false", "f", "no", "n", "off", ""}:
return False
return bool(value)
def _process_cart_items(self, items, group_order, pricelist=None):
"""Process cart items and build sale.order line data.
@ -1351,6 +1368,7 @@ class AplicoopWebsiteSale(WebsiteSale):
sale_order_lines,
existing_drafts,
order_id,
is_delivery=False,
):
"""Replace existing draft (if any) with new lines, else create it.
@ -1370,11 +1388,9 @@ class AplicoopWebsiteSale(WebsiteSale):
consumer_group_id,
)
# commitment_date comes directly from group_order
# commitment_date comes directly from selected flow
commitment_date = (
group_order.delivery_date
if group_order.home_delivery
else group_order.pickup_date
group_order.delivery_date if is_delivery else group_order.pickup_date
)
if existing_drafts:
@ -1390,7 +1406,7 @@ class AplicoopWebsiteSale(WebsiteSale):
"group_order_id": order_id,
"pickup_day": group_order.pickup_day,
"pickup_date": group_order.pickup_date,
"home_delivery": group_order.home_delivery,
"home_delivery": is_delivery,
"consumer_group_id": consumer_group_id,
"commitment_date": commitment_date,
}
@ -1404,7 +1420,7 @@ class AplicoopWebsiteSale(WebsiteSale):
"group_order_id": order_id,
"pickup_day": group_order.pickup_day,
"pickup_date": group_order.pickup_date,
"home_delivery": group_order.home_delivery,
"home_delivery": is_delivery,
"consumer_group_id": consumer_group_id,
"commitment_date": commitment_date,
}
@ -2615,6 +2631,7 @@ class AplicoopWebsiteSale(WebsiteSale):
# Get cart items
items = data.get("items", [])
is_delivery = self._to_bool(data.get("is_delivery", False))
if not items:
_logger.warning(
@ -2658,6 +2675,7 @@ class AplicoopWebsiteSale(WebsiteSale):
sale_order_lines,
existing_drafts,
order_id,
is_delivery=is_delivery,
)
_logger.info(
@ -2777,9 +2795,9 @@ class AplicoopWebsiteSale(WebsiteSale):
# If delivery, use delivery_date; otherwise use pickup_date
commitment_date = None
if is_delivery and group_order.delivery_date:
commitment_date = group_order.delivery_date.isoformat()
commitment_date = group_order.delivery_date
elif group_order.pickup_date:
commitment_date = group_order.pickup_date.isoformat()
commitment_date = group_order.pickup_date
# Create or update sale.order using helper
sale_order = self._create_or_update_sale_order(