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/demo.sports-crowd.com/app/Http/Controllers/Exports/ClientsExport.php
<?php

namespace App\Http\Controllers\Exports;

use App\Http\Controllers\UtilController;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
//use Illuminate\Contracts\Queue\ShouldQueue; // exporta en background si lo deseas (seguro con grandes datos)
use Maatwebsite\Excel\Concerns\Exportable; // necesario para streaming eficiente

class ClientsExport implements FromQuery, WithHeadings, WithMapping, WithCustomChunkSize
{
    use Exportable; // <- habilita streaming real

    private $util;
    private $query;

    public function __construct()
    {
        $this->util = new UtilController();
    }

    public function headings(): array
    {
        return [
            __('messages.screen_clients_tag3'),
            __('messages.screen_clients_tag4'),
            __('messages.screen_clients_tag5'),
            __('messages.screen_clients_tag6'),
            __('messages.screen_clients_tag11'),
            __('messages.screen_clients_tag14'),
            __('messages.screen_clients_tag13'),
            __('messages.screen_clients_tag19'),
            __('messages.screen_clients_tag15'),
            __('messages.screen_clients_tag20'),
            __('messages.screen_add_clients_tag11'),
            __('messages.screen_clients_tag16'),
            __('messages.screen_clients_tag17'),
            __('messages.screen_clients_tag18'),
            __('messages.screen_clients_tag21'),
            __('messages.screen_clients_tag22'),
            __('messages.screen_clients_accept_terms'),
            __('messages.screen_clients_terms_conditions'),
            'Id',
            'QR',
            
            
        ];
    }

    public function setQuery($query)
    {
        $this->query = $query;
        return $this; // permite encadenar llamadas (fluidez)
    }

    public function query()
    {
        return $this->query;
    }

    public function chunkSize(): int
    {
        // Exportará en lotes de 1000 registros por chunk
        return 1000;
    }

    public function map($client): array
    {
        return [
            $client->last_name,
            $client->first_name,
            $client->email,
            $client->active === 1 ? 'SI' : 'NO',
            $client->is_subscriber === 1 ? 'SI' : 'NO',
            $client->documentType,
            $client->document,
            $client->dob,
            $client->phone,
            $this->util->getFullSexText($client->sex),
            $this->formatAddresses($client->addresses),
            Carbon::parse($client->created_at)->format('Y-m-d'),
            Carbon::parse($client->last_session)->format('Y-m-d'),
            Carbon::parse($client->last_session)->format('h:i:s A'),
            $client->model,
            $client->segmentation,
            $client->accepted ? 'SI' : 'NO',
            $client->terms_conditions,
            $client->id,
            $this->getQRCode($client->id, $client->cards),
            
        ];
    }

    private function getQRCode($id, $cards)
    {
        if (!$cards) {
            return '';
        }

        $itemCards = array_filter(explode(',', $cards));
        foreach ($itemCards as $item) {
            if ($item) {
                return '{"u":' . $id . ',"c":' . $item . '}';
            }
        }

        return '';
    }

    private function formatAddresses($addresses)
{
    if (!$addresses) {
        return '';
    }
    
    $addressList = explode('||', $addresses);
    $formatted = [];
    
    foreach ($addressList as $address) {
        $parts = explode('|', $address);
        if (count($parts) >= 2) {
            $formatted[] = $parts[1]; // La dirección
        }
    }
    
    return implode('; ', $formatted);
}


}