Files
bildirchin/src/Controllers/OrderController.php
T

120 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Bildirchin\Controllers;
use Bildirchin\Models\Order;
use Bildirchin\Models\Setting;
use Bildirchin\Helpers;
class OrderController
{
public function checkout(): void
{
$cartData = CartController::getCartDetails();
if (empty($cartData['items'])) {
header('Location: /cart');
exit;
}
$settings = Setting::getAll();
require __DIR__ . '/../../views/pages/checkout.php';
}
public function submit(): void
{
$cartData = CartController::getCartDetails();
if (empty($cartData['items'])) {
header('Location: /cart');
exit;
}
$name = Helpers::sanitize($_POST['customer_name'] ?? '');
$phone = Helpers::sanitize($_POST['customer_phone'] ?? '');
$province = Helpers::sanitize($_POST['province'] ?? '');
$city = Helpers::sanitize($_POST['city'] ?? '');
$address = Helpers::sanitize($_POST['address'] ?? '');
$postalCode = Helpers::sanitize($_POST['postal_code'] ?? '');
$notes = Helpers::sanitize($_POST['notes'] ?? '');
$shippingMethod = Helpers::sanitize($_POST['shipping_method'] ?? 'باربری');
$paymentMethod = Helpers::sanitize($_POST['payment_method'] ?? 'کارت به کارت');
if (!$name || !$phone || !$address) {
Helpers::flash('error', 'لطفا نام، شماره تماس و آدرس را به طور کامل وارد نمایید.');
header('Location: /checkout');
exit;
}
$orderCode = Helpers::generateOrderCode();
$orderData = [
'order_code' => $orderCode,
'customer_name' => $name,
'customer_phone' => $phone,
'customer_email' => Helpers::sanitize($_POST['customer_email'] ?? ''),
'province' => $province,
'city' => $city,
'address' => $address,
'postal_code' => $postalCode,
'notes' => $notes,
'shipping_method' => $shippingMethod,
'payment_method' => $paymentMethod,
'total_amount' => $cartData['total_retail'],
'discount_amount' => $cartData['total_savings'],
'final_amount' => $cartData['total_final']
];
$orderItems = [];
foreach ($cartData['items'] as $item) {
$orderItems[] = [
'product_id' => $item['product']['id'],
'product_name' => $item['product']['name'],
'quantity' => $item['quantity'],
'unit_price' => $item['unit_price'],
'total_price' => $item['total_price'],
'is_wholesale' => $item['is_wholesale']
];
}
try {
$orderId = Order::create($orderData, $orderItems);
CartController::clear();
header('Location: /order/success?code=' . urlencode($orderCode));
exit;
} catch (\Throwable $e) {
Helpers::flash('error', 'خطایی در ثبت سفارش رخ داد: ' . $e->getMessage());
header('Location: /checkout');
exit;
}
}
public function success(): void
{
$code = $_GET['code'] ?? '';
$order = Order::findByCode($code);
if (!$order) {
header('Location: /');
exit;
}
$settings = Setting::getAll();
require __DIR__ . '/../../views/pages/order_success.php';
}
public function track(): void
{
$order = null;
$searched = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST' || isset($_GET['code'])) {
$code = trim($_POST['order_code'] ?? ($_GET['code'] ?? ''));
if ($code) {
$searched = true;
$order = Order::findByCode($code);
}
}
require __DIR__ . '/../../views/pages/track_order.php';
}
}