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/agile-selling-wpb/app/Http/Controllers/PrintTicketController.php
<?php

namespace App\Http\Controllers;

use QrCode;
use App\Team;
use App\Ticket;
use Carbon\Carbon;
use App\TokenUserTicket;
use Codedge\Fpdf\Fpdf\Fpdf;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PrintTicketController extends Controller
{
    private $ticket;
    private $main_team;
    private $rival_team;

    public function printTicket($code,$user_id = null,$token = null){
        if($this->validateUserWhoPrint($code,$user_id)){
            return $this->buildTicket($token);
        }else{
            return ['status' => 'Unauthorized'];
        }
    }

    public function getTicketInfo($code, $user_id = null){
        $this->ticket = Ticket::where('code_ticket', $code)
                        ->with('seat')
                        ->with('match_event')
                        ->with('user');
        if($user_id != null) {
            $this->ticket->where('user_id', $user_id);
        }
        $this->ticket = $this->ticket->first();

        if(!$this->ticket){
            return false;
        }
        $this->rival_team = $this->ticket->match_event->team;
        $this->main_team = Team::where('is_main', true)->first();
        return true;
    }

    public function validateUserWhoPrint($code,$user_id)
    {
        try {
            if($user_id){
                return $this->getTicketInfo($code, $user_id);
            }else{
                $role_user = Auth::user()->rol->id;
                if( ($role_user == 1 || $role_user == 2 || $role_user == 7 ) ){ // 1 Superadmin ;  2 Admin ;  7 Gana
                    return $this->getTicketInfo($code); // De lo contrario es un admin y lo dejará imprimir.
                }
            }
            return false;
        } catch (\Throwable $th) {
            return false;
        }
    }

    public function buildTicket($token)
    {
        // Rutas donde se guardan las fuentes
        define('FPDF_FONTPATH',public_path('fonts'));

        // Declaracion papael de 11cm X 16 cm.
        $fpdf = new Fpdf('P','mm',array(110,160));

        /// Inicio variables
        $tournament = $this->ticket->match_event->tournament->name;
        $date_name = $this->ticket->match_event->date_name;
        $date_event = Carbon::parse($this->ticket->match_event->event_start)->format('d-m-Y');
        $abbr_local = $this->main_team->abbreviation;
        $abbr_visit = $this->rival_team->abbreviation;
        $time = substr($this->ticket->match_event->event_start,11,5);
        $tribuna = strtoupper(substr($this->ticket->seat->zone->zone->name,0,3));
        $sector = explode("_", $this->ticket->zone)[1];
        $fila = $this->ticket->seat->letter->name;
        $silla = $this->ticket->code_seat;
        $code = $this->ticket->code_ticket;

        // DYNAMIC IMAGES
        $url_main_team = env('AWS_URL') . '/teams/' . $this->main_team->logo;
        $url_tournament = env('AWS_URL') . '/tournaments/' . $this->ticket->match_event->tournament->logo;
        $qr_base64_png = $this->generateQRCode($code);
        /// Fin variables

        // Formato Ticket
        $fpdf->AddPage();
        $fpdf->SetFillColor(130,130,130);
        $fpdf->Rect(0, 0, 210, 297, 'F');

        $fpdf = $this->buildImage($fpdf, $url_main_team, $url_tournament, $qr_base64_png);
        $fpdf = $this->addFonts($fpdf);

        $fpdf->SetFont('organetto-ultraboldsemiext', '', 11);
        $fpdf->Text(20,27, $tournament);
        $fpdf->SetFont('organetto-lightsemiext', '', 11);
        $fpdf->Text(45,32, $date_name);      // FECHA

        $fpdf->SetFont('organetto-regularsemiext', '', 11);
        $fpdf->Text(16.5,45,'FECHA DEL PARTIDO:');
        $fpdf->SetFont('organetto-ultraboldsemiext', '', 11);
        $fpdf->Text(68.5,45,$date_event);

        $fpdf->SetFont('organetto-boldsemiext', '', 30);
        $fpdf->Text(5,58, $abbr_local);     // EQUIPO LOCAL
        $fpdf->Text(80,58,$abbr_visit);     // EQUIPO VISITANTE
        $fpdf->SetFont('organetto-regularsemiext', '', 21);
        $fpdf->Text(48,57,'VS.');

        $fpdf->SetFont('organetto-regularsemiext', '', 9);
        $fpdf->Text(4,68,'HORA:');
        $fpdf->Text(25,68,'TRIBUNA:');
        $fpdf->Text(50,68,'SECTOR:');
        $fpdf->Text(75,68,'FILA:');
        $fpdf->Text(93,68,'SILLA:');

        $fpdf->SetFont('organetto-ultraboldsemiext', '', 14);
        $fpdf->Text(4,77,$time);      // HORA DEL PARTIDO
        $fpdf->Text(27,77,$tribuna);  // TRIBUNA
        $fpdf->Text(52,77, $sector);  // SECTOR
        $fpdf->Text(75,77,$fila);     // FILA
        $fpdf->Text(94,77,$silla);    // SILLA


        if(!$token){
            $pdf = $fpdf->Output('I', 'ticket.pdf');
            return response($pdf, 200, ['Content-Type' => 'application/pdf']);
        }else{
            $pdf = $fpdf->Output('F', 'tickets_tmp/'.$token.'.pdf');
            $image = new \Spatie\PdfToImage\Pdf(public_path('/tickets_tmp/'.$token.'.pdf'));
            $image->setOutputFormat('png')->saveImage(public_path('/tickets_tmp/'.$token . ".png"));
            return true;
        }
    }

    public function buildImage($fpdf, $url_main_team, $url_tournament, $qr_base64_png)
    {
        // Background
        $fpdf->Image(public_path('/img/ticket/bg_ticket.png'),1,1,107.8,157.8);

        // MAIN_TEAM_IMAGE
        $fpdf->Image($url_main_team,39,5,15,16);

        // TOURNAMENT_IMAGE
        $fpdf->Image($url_tournament ,56,5,15,16);

        // Point line
        $fpdf->Image(public_path('/img/ticket/point-line.png'),2,36.3,105,0.5);

        // QR
        $pic = 'data://text/plain;base64,'. $qr_base64_png;
        $fpdf->Image($pic,32,87,47,47, 'png');

        // Footer
        $fpdf->Image(public_path('/img/ticket/footer.png'),4,146,102,5);

        return $fpdf;
    }

    public function addFonts($fpdf){
        $fpdf->AddFont('organetto-lightsemiext','');
        $fpdf->AddFont('organetto-boldsemiext','');
        $fpdf->AddFont('organetto-regularsemiext','');
        $fpdf->AddFont('organetto-ultraboldsemiext','');
        return $fpdf;
    }

    public function generateQRCode($code){
        $qr = QrCode::format('png')->size(200)->generate($code);
        return base64_encode($qr);
    }

    public function printTicketValidate($token)
    {
        $validate = TokenUserTicket::where('token', $token)->first();
        if($validate){
            $code = $validate->code;
            $user_id = $validate->user_id;
            $validate->delete();
            return $this->printTicket($code,$user_id);
        }
        return ":(";
    }
}