File "fare_actions.php"

Full path: /home/itsevak/public_html/prepaiddev.itsevak.com/admin/fare_actions.php
File size: 3.86 B (3.86 KB bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php
require_once '../includes/functions.php';

// Require admin access
requireAdminAccess();

// Set JSON response header
header('Content-Type: application/json');

// Get current user
$user = getCurrentUser();

// Check if request is POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    jsonResponse(['success' => false, 'message' => 'Invalid request method'], 405);
}

// Get JSON input
$input = json_decode(file_get_contents('php://input'), true);

if (!$input) {
    jsonResponse(['success' => false, 'message' => 'Invalid JSON input'], 400);
}

$action = $input['action'] ?? '';

try {
    switch ($action) {
        case 'create':
            $fareData = [
                'from_location_id' => $input['from_location_id'] ?? '',
                'to_location_id' => $input['to_location_id'] ?? '',
                'base_fare' => $input['base_fare'] ?? null,
                'per_km_rate' => $input['per_km_rate'] ?? null,
                'minimum_fare' => $input['minimum_fare'] ?? null,
                'fix_rate' => $input['fix_rate'] ?? null,
                'distance_km' => $input['distance_km'] ?? null,
                'estimated_duration_minutes' => $input['estimated_duration_minutes'] ?? null
            ];
            
            $result = createFareDetail($fareData, $user['id']);
            jsonResponse($result);
            break;
            
        case 'get_details':
            $fareId = $input['fare_id'] ?? null;
            if (!$fareId) {
                jsonResponse(['success' => false, 'message' => 'Fare ID is required'], 400);
            }
            
            $fare = getFareDetailById($fareId);
            if (!$fare) {
                jsonResponse(['success' => false, 'message' => 'Fare not found'], 404);
            }
            
            jsonResponse(['success' => true, 'fare' => $fare]);
            break;
            
        case 'update':
            $fareId = $input['fare_id'] ?? null;
            if (!$fareId) {
                jsonResponse(['success' => false, 'message' => 'Fare ID is required'], 400);
            }
            
            $fareData = [
                'from_location_id' => $input['from_location_id'] ?? '',
                'to_location_id' => $input['to_location_id'] ?? '',
                'base_fare' => $input['base_fare'] ?? null,
                'per_km_rate' => $input['per_km_rate'] ?? null,
                'minimum_fare' => $input['minimum_fare'] ?? null,
                'fix_rate' => $input['fix_rate'] ?? null,
                'distance_km' => $input['distance_km'] ?? null,
                'estimated_duration_minutes' => $input['estimated_duration_minutes'] ?? null
            ];
            
            $result = updateFareDetail($fareId, $fareData, $user['id']);
            jsonResponse($result);
            break;
            
        case 'toggle_status':
            $fareId = $input['fare_id'] ?? null;
            if (!$fareId) {
                jsonResponse(['success' => false, 'message' => 'Fare ID is required'], 400);
            }
            
            $result = toggleFareDetailStatus($fareId, $user['id']);
            jsonResponse($result);
            break;
            
        case 'delete':
            $fareId = $input['fare_id'] ?? null;
            if (!$fareId) {
                jsonResponse(['success' => false, 'message' => 'Fare ID is required'], 400);
            }
            
            $result = deleteFareDetail($fareId, $user['id']);
            jsonResponse($result);
            break;
            
        default:
            jsonResponse(['success' => false, 'message' => 'Invalid action'], 400);
    }
} catch (Exception $e) {
    logError('Fare action error: ' . $e->getMessage());
    jsonResponse(['success' => false, 'message' => 'An error occurred while processing the request'], 500);
}
?>