- Создано ядро MVC: App, Router, Controller, Model, View, Database - Созданы модели: User, Product, Category, Cart, Order - Созданы контроллеры: Home, Auth, Product, Cart, Order, Page, Admin - Созданы layouts и partials для представлений - Добавлены все views для страниц - Настроена маршрутизация с чистыми URL - Обновлена конфигурация Docker и Apache для mod_rewrite - Добавлена единая точка входа public/index.php
126 lines
3.6 KiB
PHP
126 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Core\Controller;
|
|
use App\Models\Order;
|
|
use App\Models\Cart;
|
|
|
|
/**
|
|
* OrderController - контроллер заказов
|
|
*/
|
|
class OrderController extends Controller
|
|
{
|
|
private Order $orderModel;
|
|
private Cart $cartModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->orderModel = new Order();
|
|
$this->cartModel = new Cart();
|
|
}
|
|
|
|
/**
|
|
* Страница оформления заказа (корзина)
|
|
*/
|
|
public function checkout(): void
|
|
{
|
|
$this->requireAuth();
|
|
|
|
$user = $this->getCurrentUser();
|
|
$cartItems = $this->cartModel->getUserCart($user['id']);
|
|
$totals = $this->cartModel->getCartTotal($user['id']);
|
|
|
|
$this->view('cart/checkout', [
|
|
'user' => $user,
|
|
'cartItems' => $cartItems,
|
|
'totalQuantity' => $totals['quantity'],
|
|
'totalAmount' => $totals['amount']
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Создание заказа
|
|
*/
|
|
public function create(): void
|
|
{
|
|
if (!$this->isAuthenticated()) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Требуется авторизация'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$user = $this->getCurrentUser();
|
|
$cartItems = $this->cartModel->getUserCart($user['id']);
|
|
|
|
if (empty($cartItems)) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Корзина пуста'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// Получаем данные заказа
|
|
$orderData = [
|
|
'customer_name' => $this->getPost('full_name', $user['full_name']),
|
|
'customer_email' => $this->getPost('email', $user['email']),
|
|
'customer_phone' => $this->getPost('phone', $user['phone']),
|
|
'delivery_address' => $this->getPost('address', ''),
|
|
'delivery_region' => $this->getPost('region', ''),
|
|
'postal_code' => $this->getPost('postal_code', ''),
|
|
'delivery_method' => $this->getPost('delivery', 'courier'),
|
|
'payment_method' => $this->getPost('payment', 'card'),
|
|
'promo_code' => $this->getPost('promo_code', ''),
|
|
'discount' => (float) $this->getPost('discount', 0),
|
|
'delivery_price' => (float) $this->getPost('delivery_price', 2000),
|
|
'notes' => $this->getPost('notes', '')
|
|
];
|
|
|
|
// Валидация
|
|
if (empty($orderData['customer_name'])) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Укажите ФИО'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
if (empty($orderData['customer_phone'])) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Укажите телефон'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
if (empty($orderData['delivery_address'])) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Укажите адрес доставки'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$result = $this->orderModel->createFromCart($user['id'], $cartItems, $orderData);
|
|
|
|
$this->json([
|
|
'success' => true,
|
|
'order_id' => $result['order_id'],
|
|
'order_number' => $result['order_number'],
|
|
'message' => 'Заказ успешно оформлен'
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|