HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/rms.naniguide.com/app/Http/Controllers/DashboardController.php
<?php

namespace App\Http\Controllers;

use App\Models\Forecast;
use App\Models\Hotel;
use App\Models\RoomType;
use App\Services\RevenueManagementService;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function __construct(private RevenueManagementService $rms)
    {
    }

    public function index(Request $request)
    {
        $hotel = $this->hotel($request);
        $from = $request->input('from', now()->startOfMonth()->toDateString());
        $to = $request->input('to', now()->endOfMonth()->toDateString());
        $kpis = $this->rms->kpis($hotel, $from, $to);
        $chart = $this->rms->chartData($hotel, $from, $to);
        $forecast = $this->rms->forecast($hotel, $request->input('granularity', 'daily'));
        $ai = $this->rms->aiRecommendations($hotel);

        return view('rms.dashboard', compact('hotel', 'from', 'to', 'kpis', 'chart', 'forecast', 'ai'));
    }

    public function forecast(Request $request)
    {
        $hotel = $this->hotel($request);
        $forecast = $this->rms->forecast($hotel, $request->input('granularity', 'daily'));

        Forecast::create([
            'hotel_id' => $hotel->id,
            'period_start' => now()->toDateString(),
            'period_end' => now()->addDays($forecast['granularity'] === 'monthly' ? 30 : ($forecast['granularity'] === 'weekly' ? 7 : 1))->toDateString(),
            'granularity' => $forecast['granularity'],
            'occupancy_forecast' => $forecast['occupancy_forecast'],
            'adr_forecast' => $forecast['adr_forecast'],
            'revenue_forecast' => $forecast['revenue_forecast'],
            'accuracy' => 86.5,
        ]);

        return back()->with('status', 'Forecast đã được tạo.');
    }

    public function pricing(Request $request)
    {
        $hotel = $this->hotel($request);
        $roomTypes = RoomType::where('hotel_id', $hotel->id)->get();
        $recommendation = null;

        if ($request->isMethod('post')) {
            $data = $request->validate([
                'room_type_id' => ['required', 'exists:room_types,id'],
                'occupancy' => ['required', 'numeric'],
                'pickup' => ['required', 'integer'],
                'booking_window' => ['required', 'integer'],
                'day_of_week' => ['nullable', 'string'],
                'season' => ['nullable', 'string'],
            ]);
            $roomType = RoomType::where('hotel_id', $hotel->id)->findOrFail($data['room_type_id']);
            $recommendation = $this->rms->recommendedRate($roomType, $data);
        }

        return view('rms.pricing', compact('hotel', 'roomTypes', 'recommendation'));
    }

    private function hotel(Request $request): Hotel
    {
        $user = $request->user();
        return $user->hotel_id ? Hotel::findOrFail($user->hotel_id) : Hotel::firstOrFail();
    }
}