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);
}
}