Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.0 kB
4
Indexable
Never
<?php

namespace App\Http\Controllers;

use App\Models\Reservation;
use App\Models\User;
use App\Models\Room;

class ReservationsController extends Controller
{
    public function importReservations()
    {
        $client = new \GuzzleHttp\Client();

        $response = $client->post('https://api.mews-demo.com/api/connector/v1/reservations/getAll', [
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'json' => [
                'ClientToken' => 'E0D439EE522F44368DC78E1BFB03710C-D24FB11DBE31D4621C4817E028D9E1D',
                'AccessToken' => '7059D2C25BF64EA681ACAB3A00B859CC-D91BFF2B1E3047A3E0DEC1D57BE1382',
                'Client' => 'Sample Client 1.0.0',
                'StartUtc' => '2021-01-01T00:00:00Z',
                'EndUtc' => '2021-01-05T00:00:00Z',
                'Limitation' => [
                    'Cursor' => null,
                    'Count' => 100,
                ],
            ],
        ]);

        $reservations = json_decode($response->getBody(), true);

        foreach ($reservations['Reservations'] as $reservation) {
            // Extract reservation data
            $state = $reservation['State'];
            $segment = $reservation['Segment'] ?? null;
            $source = $segment ? $reservation['Source'] : 'MEWS';
            $start = date('Y-m-d H:i:s', strtotime($reservation['StartUtc']));
            $end = date('Y-m-d H:i:s', strtotime($reservation['EndUtc']));
            $cancelled_at = $reservation['CancellationDateUtc'] ?? null;
            $created_at_in_pms = $reservation['CreatedDateUtc'] ?? null;

            if (isset($reservation['Customers'])) {
                // Extract customer data
                $customer = $reservation['Customers'][0];
                $first_name = $customer['FirstName'] ?? 'Unknown';
                $last_name = $customer['LastName'] ?? null;
                $phone = $customer['Phone'] ?? null;
                $email = $customer['Email'] ?? null;
                $locale = $customer['LanguageCode'] ?? null;
                $room_number = $customer['Number'] ?? null;

                // Create a new reservation instance
                $reservationModel = new Reservation;

                // Set the fields of the reservation
                $reservationModel->status = $state;
                $reservationModel->source = $source;
                $reservationModel->segment = $segment;
                $reservationModel->start = $start;
                $reservationModel->end = $end;
                $reservationModel->cancelled_at = $cancelled_at;
                $reservationModel->created_at_in_pms = $created_at_in_pms;

                // Create a new user instance
                $userModel = new User;

                // Set the fields of the user
                $userModel->firstName = $first_name ?? null;
                $userModel->lastName = $last_name ?? null;
                $userModel->phone = $phone ?? null;
                $userModel->email = $email ?? null;
                $userModel->locale = $locale ?? null;

                // Save the user to the database
                $userModel->save();

                // Associate the user with the reservation
                $reservationModel->user()->associate($userModel);

                // Create a new room instance
                $roomModel = new Room;

                // Set the fields of the room
                $roomModel->number = $room_number ?? null;

                // Save the room to the database
                $roomModel->save();

                // Associate the room with the reservation
                $reservationModel->room()->associate($roomModel);

                // Save the reservation to the database
                $reservationModel->save();
            }
        }
        return response()->json($reservations);
    }
}