HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/app/Http/Controllers/Imports/MatchEventSeatsImport.php
<?php

namespace App\Http\Controllers\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use DB;
use App\Seat;
use App\Ticket;
use App\Zone;
use App\Http\Controllers\TicketsController;

class MatchEventSeatsImport implements ToCollection, WithHeadingRow
{
    private $match_event_id;
    public function __construct($match_event_id)
    {
        $this->match_event_id = $match_event_id;
    }
    public $answer = [];
    public $edit = [
        'actualizados' => 0,
    ];
    public $is_error;
    public $request;

    /**
     * @param Collection $collection
     */
    public function collection(Collection $rows)
    {
        $zones = Zone::select('id', 'name', 'zone_id')->where('active', true)->get();
        $seats = Seat::select('seats.id', 'seats.code', 'seats.zone_id', 'l.name as letter',)
            ->join('letters as l', 'seats.letter_id', '=', 'l.id')
            ->where('active', true)
            ->get();
        $is_error = false;
        DB::beginTransaction();
        $cont = 1;

        foreach ($rows as $row) {
            $code = trim($row["codigo_boleta"]);
            if ($code) {
                $cont++;
                $targetTribune = null;
                $targetZones = null;
                $targetSeat = null;

                // VALIDACIONES DE LOS DATOS
                $ticket = Ticket::where([['code_ticket', $code], ['match_event_id', $this->match_event_id]])->with('seat')->with('user')->first();
                if (!$ticket) {
                    $this->answer[] = [
                        'message' => 'Código de boleta ' . $code . ' no encontrado para el evento, línea: ' . $cont,
                        'state' => false,
                        'line' => $cont
                    ];
                    $is_error = true;
                    continue;
                }
                $tribune = trim($row["tribuna_destino"]);
                if ($tribune) {
                    $targetTribune = $zones->first(function ($item) use ($tribune) {
                        return $item->name == $tribune;
                    });
                    if (!$targetTribune) {
                        $this->answer[] = [
                            'message' => 'Información de la tribuna ' . $tribune . ' no encontrada, línea: ' . $cont,
                            'state' => false,
                            'line' => $cont
                        ];
                        $is_error = true;
                        continue;
                    }
                }
                $sectors = trim($row["sectores_destino"]);
                if ($sectors && $targetTribune) {
                    $sectors = explode(",", $sectors);
                    $targetZones = $zones->filter(function ($item) use ($sectors, $targetTribune) {
                        return $item->zone_id === $targetTribune->id && in_array($item->name, $sectors);
                    })->values()->pluck('id')->toArray();
                    if (!$targetZones) {
                        $this->answer[] = [
                            'message' => 'Información sectores destino ' . $row["sectores_destino"] . ' no encontrado(s) dentro de la tribuna ' . $tribune . ', línea: ' . $cont,
                            'state' => false,
                            'line' => $cont
                        ];
                        $is_error = true;
                        continue;
                    }
                }
                $seat = trim($row["silla_destino"]);
                if ($seat && $targetZones) {
                    $match;
                    preg_match_all('/^([^\d]+)(\d+)/', $seat, $match);
                    $text = $match[1][0];
                    $num = $match[2][0];
                    $targetSeat = $seats->first(function ($item) use ($text, $num, $targetZones) {
                        return $item->letter == $text && $item->code === intval($num) && in_array($item->zone_id, $targetZones);
                    });
                    if (!$targetSeat) {
                        $this->answer[] = [
                            'message' => 'Información de la silla ' . $seat . ' no encontrada dentro del/los sector(es) destino ' . $zone . ', línea: ' . $cont,
                            'state' => false,
                            'line' => $cont
                        ];
                        $is_error = true;
                        continue;
                    }
                }

                if ($this->match_event_id && $ticket) {
                    $tickets = new TicketsController();
                    $freeSeat = $tickets->getSeatFreeByZonesOrSeat($this->match_event_id, $targetZones, $targetSeat ? $targetSeat->id : null);
                    if (!$freeSeat) {
                        $this->answer[] = [
                            'message' => 'No existe una silla disponible para el/los sector(es) destino o silla destino para la boleta ' . $code . ', línea: ' . $cont,
                            'state' => false,
                            'line' => $cont
                        ];
                        $is_error = true;
                        continue;
                    }

                    $specialTicket = trim($row["texto_boleta_especial"]);
                    $response = $tickets->changeFreeSeatToTicket($ticket->id, $freeSeat->id, $specialTicket);
                    if (!$response['r']) {
                        $this->answer[] = [
                            'message' => $response['m'] . ', línea: ' . $cont,
                            'state' => false,
                            'line' => $cont
                        ];
                        $is_error = true;
                        continue;
                    }
                    $this->edit['actualizados'] += 1;
                }
            }
        }
        if ($is_error || sizeof($rows) <= 0) {
            DB::rollback();
            return;
        } else {
            DB::commit();
        }
    }
}