prepare("SELECT u.full_name, p.company_name FROM users u LEFT JOIN shipper_profiles p ON u.id = p.user_id WHERE u.id = ?"); $stmt->execute([$_SESSION['user_id']]); $profile = $stmt->fetch(); if ($profile) { $prefillName = $profile['full_name']; $prefillCompany = $profile['company_name'] ?? ''; } } catch (Throwable $e) {} } if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'create_shipment') { $shipperName = trim($_POST['shipper_name'] ?? ''); $shipperCompany = trim($_POST['shipper_company'] ?? ''); $origin = trim($_POST['origin_city'] ?? ''); $destination = trim($_POST['destination_city'] ?? ''); $shipmentType = trim($_POST['shipment_type'] ?? 'Dry'); $cargo = trim($_POST['cargo_description'] ?? ''); $weight = trim($_POST['weight_tons'] ?? ''); $pickupDate = trim($_POST['pickup_date'] ?? ''); $deliveryDate = trim($_POST['delivery_date'] ?? ''); $payment = $_POST['payment_method'] ?? 'thawani'; $targetPrice = ($pricingModel === 'fixed_price') ? trim($_POST['target_price'] ?? '') : null; $initialStatus = ($pricingModel === 'fixed_price') ? 'pending_approval' : 'posted'; if ($shipperName === '' || $shipperCompany === '' || $origin === '' || $destination === '' || $cargo === '' || $weight === '' || $pickupDate === '' || $deliveryDate === '') { $errors[] = t('error_required'); } elseif (!is_numeric($weight)) { $errors[] = t('error_invalid'); } if ($pricingModel === 'fixed_price' && (!is_numeric($targetPrice) || $targetPrice <= 0)) { $errors[] = t('error_invalid'); } if (!$errors) { $shipperId = isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : null; $stmt = db()->prepare( "INSERT INTO shipments (shipper_id, shipper_name, shipper_company, origin_city, destination_city, shipment_type, cargo_description, weight_tons, pickup_date, delivery_date, payment_method, status, target_price) VALUES (:shipper_id, :shipper_name, :shipper_company, :origin_city, :destination_city, :shipment_type, :cargo_description, :weight_tons, :pickup_date, :delivery_date, :payment_method, :status, :target_price)" ); $stmt->execute([ ':shipper_id' => $shipperId, ':shipper_name' => $shipperName, ':shipper_company' => $shipperCompany, ':origin_city' => $origin, ':destination_city' => $destination, ':shipment_type' => $shipmentType, ':cargo_description' => $cargo, ':weight_tons' => $weight, ':pickup_date' => $pickupDate, ':delivery_date' => $deliveryDate, ':payment_method' => $payment, ':status' => $initialStatus, ':target_price' => $targetPrice, ]); $newShipmentId = db()->lastInsertId(); $_SESSION['shipper_name'] = $shipperName; $_SESSION['shipper_company_session'] = $shipperCompany; // for rudimentary filtering // Notify Shipper (Confirmation) if ($shipperId) { $user = db()->query("SELECT * FROM users WHERE id = $shipperId")->fetch(); if ($user) { NotificationService::send('shipment_created', $user, [ 'shipment_id' => $newShipmentId, 'origin' => $origin, 'destination' => $destination ]); } } $msg = ($initialStatus === 'pending_approval') ? t('pending_approval_msg') : t('success_shipment'); set_flash('success', $msg); header('Location: ' . url_with_lang('shipper_dashboard.php')); exit; } } $shipments = []; $filterCompany = $_SESSION['shipper_company_session'] ?? $prefillCompany; try { if ($filterCompany) { $stmt = db()->prepare("SELECT * FROM shipments WHERE shipper_company = ? ORDER BY created_at DESC LIMIT 20"); $stmt->execute([$filterCompany]); } else { $stmt = db()->query("SELECT * FROM shipments ORDER BY created_at DESC LIMIT 20"); } $shipments = $stmt->fetchAll(); } catch (Throwable $e) { $shipments = []; } $stats = [ 'total' => 0, 'active' => 0, 'delivered' => 0, ]; try { if ($filterCompany) { $stmt = db()->prepare("SELECT COUNT(*) FROM shipments WHERE shipper_company = ?"); $stmt->execute([$filterCompany]); $stats['total'] = (int)$stmt->fetchColumn(); $stmt = db()->prepare("SELECT COUNT(*) FROM shipments WHERE shipper_company = ? AND status != 'delivered'"); $stmt->execute([$filterCompany]); $stats['active'] = (int)$stmt->fetchColumn(); $stmt = db()->prepare("SELECT COUNT(*) FROM shipments WHERE shipper_company = ? AND status = 'delivered'"); $stmt->execute([$filterCompany]); $stats['delivered'] = (int)$stmt->fetchColumn(); } else { $stats['total'] = (int)db()->query("SELECT COUNT(*) FROM shipments")->fetchColumn(); $stats['active'] = (int)db()->query("SELECT COUNT(*) FROM shipments WHERE status != 'delivered'")->fetchColumn(); $stats['delivered'] = (int)db()->query("SELECT COUNT(*) FROM shipments WHERE status = 'delivered'")->fetchColumn(); } } catch (Throwable $e) {} // Fetch countries for dropdowns $countries = []; try { $countries = db()->query("SELECT * FROM countries ORDER BY name_en ASC")->fetchAll(); } catch (Throwable $e) {} render_header(t('shipper_dashboard'), 'shipper'); $flash = get_flash(); ?>
= e(t('welcome_back') ?? 'Welcome to your dashboard. Manage your cargo shipments here.') ?>
= e(t('total_shipments_posted')) ?>
= e(t('active_shipments')) ?>
= e(t('delivered_shipments')) ?>
= e(t('no_shipments')) ?>