Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/rentpix.corals.io/Corals/modules/RentPix/Services/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/rentpix.corals.io/Corals/modules/RentPix/Services/DriversFixer.php
<?php

namespace Corals\Modules\RentPix\Services;

use Carbon\Carbon;
use Corals\Modules\RentPix\Classes\Integration\RentalAPIService;
use Corals\Modules\RentPix\Models\AltDriver as Driver;
use Corals\Modules\RentPix\Models\Inspection;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;

class DriversFixer
{

    protected function createTables()
    {
        Schema::create('pix_alt_drivers', function (Blueprint $table) {
            $table->increments('id');

            $table->string('name');
            $table->date('dob')->nullable();
            $table->string('email')->nullable();
            $table->string('phone');
            $table->string('address');
            $table->string('license');
            $table->date('license_expiration_date');
            $table->string('integration_id')->nullable()->unique();

            $table->text('properties')->nullable();
            $table->auditable();
            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('pix_alt_driver_customer', function (Blueprint $table) {
            $table->increments('id');

            $table->unsignedInteger('driver_id');
            $table->unsignedInteger('customer_id');
            $table->text('properties')->nullable();
            $table->auditable();
            $table->softDeletes();
            $table->timestamps();

            $table->foreign('driver_id')
                ->references('id')
                ->on('pix_alt_drivers')
                ->onUpdate('cascade');

            $table->foreign('customer_id')
                ->references('id')
                ->on('pix_customers')
                ->onUpdate('cascade');
        });

        Schema::create('pix_alt_driver_reservation', function (Blueprint $table) {
            $table->increments('id');

            $table->unsignedInteger('driver_id');
            $table->unsignedInteger('reservation_id');
            $table->boolean('is_primary')->default(false);
            $table->text('properties')->nullable();
            $table->auditable();
            $table->softDeletes();
            $table->timestamps();

            $table->foreign('driver_id')
                ->references('id')
                ->on('pix_alt_drivers')
                ->onUpdate('cascade');

            $table->foreign('reservation_id')
                ->references('id')
                ->on('pix_reservations')
                ->onUpdate('cascade');
        });

        Schema::create('pix_alt_driver_inspection', function (Blueprint $table) {
            $table->increments('id');

            $table->unsignedInteger('driver_id');
            $table->unsignedInteger('inspection_id');
            $table->boolean('is_primary')->default(false);
            $table->text('properties')->nullable();
            $table->auditable();
            $table->softDeletes();
            $table->timestamps();

            $table->foreign('driver_id')
                ->references('id')
                ->on('pix_alt_drivers')
                ->onUpdate('cascade');

            $table->foreign('inspection_id')
                ->references('id')
                ->on('pix_inspections')
                ->onUpdate('cascade');
        });
    }

    public function handle()
    {
        Auth::loginUsingId(4);

        if (!Schema::hasTable('pix_alt_drivers')) {
            $this->createTables();
        }

        Inspection::query()->eachById(function (Inspection $inspection) {
            logger()->channel('custom')->info('INS:' . $inspection->code);
            $rentalAPIService = new RentalAPIService();
            try {
                $reservation = $rentalAPIService->getReservationDetails($inspection->reservation->code, null, true);

                $drivers = data_get($reservation, 'data.drivers', []);
            } catch (\Exception $exception) {
                logger()->channel('custom')->error('INS:' . $inspection->code . ' | ' . $exception->getMessage());
            }

            foreach ($drivers ?? [] as $index => $driver) {
                $driverData = [
                    "name" => sprintf("%s %s", data_get($driver, 'first_name'), data_get($driver, 'last_name')),
                    "phone" => data_get($driver, 'phone'),
                    "email" => data_get($driver, 'email'),
                    "address" => data_get($driver, 'state'),
                    "license" => data_get($driver, 'license'),
                    "license_expiration_date" => data_get($driver, 'license_expiration_date'),
                    "integration_id" => data_get($driver, 'id'),
                    "properties" => ['dump' => $driver],
                ];

                if ($index == 0) {
                    $driverData['is_primary'] = 1;
                }

                $this->handelDriver($driverData, $inspection);
            }
        });
    }

    private function handelDriver($driverData, Inspection $inspection)
    {
        try {
            $integration_id = data_get($driverData, 'integration_id');
            $phone = data_get($driverData, 'phone');

            if (!$integration_id && !$phone) {
                throw new \Exception('No integration id or phone number was found', 400);
            }

            if ($integration_id && $driver = Driver::query()->where('integration_id', '=', $integration_id)->first()) {
                $driver->update($driverData);
            } elseif ($phone) {
                $driver = Driver::query()->updateOrCreate(['phone' => $phone], $driverData);
            } else {
                $driver = Driver::query()->updateOrCreate(['integration_id' => $integration_id], $driverData);
            }

            $relationProperties = [
                'messages' => []
            ];

            $reservation = $inspection->reservation;

            $dob = data_get($driverData, 'dob');
            if ($dob) {
                $reservationStart = Carbon::parse($reservation->starts_at);

                if ($reservationStart->diffInYears($dob) < 25) {
                    $relationProperties['messages'][] = [
                        'level' => 'warning',
                        'text' => 'The driver under 25 and requires an HQ-approved MVR on file'
                    ];
                }
            }

            $license_expiration = Carbon::parse($driverData['license_expiration_date']);

            if ($license_expiration->between($reservation->starts_at, $reservation->ends_at)) {
                $relationProperties['messages'][] = [
                    'level' => 'danger',
                    'text' => 'The license will expire in ' . format_date($driverData['license_expiration_date'], 'Y-m-d')
                ];
            }

            $driver->inspections()->syncWithoutDetaching([
                $inspection->id => [
                    'is_primary' => data_get($driverData, 'is_primary') ?? 0,
                    'properties' => json_encode($relationProperties),
                ],
            ]);

            $driver->reservations()->syncWithoutDetaching([
                $reservation->id => [
                    'is_primary' => data_get($driverData, 'is_primary') ?? 0,
                ],
            ]);

            $driver->customers()->syncWithoutDetaching($reservation->customer->id);
        } catch (\Exception $exception) {
            return apiExceptionResponse($exception);
        }
    }
}

Spamworldpro Mini