POS deferred order feature

This commit is contained in:
2025-12-08 18:56:14 +03:00
parent a244d82e49
commit 6c19c9e093
4 changed files with 165 additions and 1 deletions

View File

@@ -1416,3 +1416,59 @@ def pos_checkout(request):
except Exception as e:
logger.error(f'Ошибка при проведении продажи POS: {str(e)}', exc_info=True)
return JsonResponse({'success': False, 'error': f'Ошибка: {str(e)}'}, status=500)
@login_required
@require_http_methods(["POST"])
def create_order_draft(request):
"""
Создает черновик заказа из корзины POS и сохраняет в Redis.
Возвращает токен для передачи в orders/create/.
Payload (JSON):
{
"customer_id": int,
"items": [
{"type": "product"|"kit"|"showcase_kit", "id": int, "quantity": float, "price": float},
...
]
}
Response:
{
"success": true,
"token": "abc123..."
}
"""
from django.core.cache import cache
import secrets
try:
data = json.loads(request.body)
customer_id = data.get('customer_id')
items = data.get('items', [])
if not items:
return JsonResponse({'success': False, 'error': 'Корзина пуста'}, status=400)
# Генерируем уникальный токен
token = secrets.token_urlsafe(16)
# Сохраняем в Redis с TTL 1 час
cache_key = f'pos_draft:{token}'
draft_data = {
'customer_id': customer_id,
'items': items,
}
cache.set(cache_key, draft_data, timeout=3600) # 1 час
return JsonResponse({
'success': True,
'token': token
})
except json.JSONDecodeError:
return JsonResponse({'success': False, 'error': 'Неверный формат JSON'}, status=400)
except Exception as e:
logger.error(f'Ошибка при создании черновика заказа: {str(e)}', exc_info=True)
return JsonResponse({'success': False, 'error': f'Ошибка: {str(e)}'}, status=500)