diff --git a/app/Http/Livewire/Whatsapp/ShowCorreoConfig.php b/app/Http/Livewire/Whatsapp/ShowCorreoConfig.php index 46ff34a..58e5700 100644 --- a/app/Http/Livewire/Whatsapp/ShowCorreoConfig.php +++ b/app/Http/Livewire/Whatsapp/ShowCorreoConfig.php @@ -3,6 +3,7 @@ namespace App\Http\Livewire\Whatsapp; use App\Models\WhatsappSystemConfig; +use App\Services\BancolombiaParser; use App\Services\CorreoImapService; use Livewire\Component; use Jantinnerezo\LivewireAlert\LivewireAlert; @@ -82,7 +83,14 @@ class ShowCorreoConfig extends Component timeout: 20 ); - $this->emails = $service->fetchLatest(5); + $raw = $service->fetchLatest(5); + + // Añadir datos parseados de Bancolombia a cada correo + $this->emails = array_map(function (array $email) { + $email['bancolombia'] = BancolombiaParser::parse($email['body']); + return $email; + }, $raw); + $this->testStatus = 'ok'; } catch (\Throwable $e) { $this->testStatus = 'error'; diff --git a/app/Services/BancolombiaParser.php b/app/Services/BancolombiaParser.php new file mode 100644 index 0000000..06f619e --- /dev/null +++ b/app/Services/BancolombiaParser.php @@ -0,0 +1,119 @@ + 'Bancolombia', + 'tipo' => self::detectarTipo($text), + 'destinatario' => self::extraer($text, '/Bancolombia[:\s]+([A-ZÁÉÍÓÚÑÜ][A-Za-záéíóúñü\s]+?),\s+recibiste/u'), + 'remitente' => self::extraer($text, '/transferencia\s+de\s+(.+?)\s+por\s+\$/iu'), + 'valor' => self::extraerValor($text), + 'cuenta' => self::extraer($text, '/cuenta\s+\*(\d+)/iu'), + 'llave' => self::extraer($text, '/llave\s+([@\w.\-]+)/iu'), + 'fecha' => self::extraer($text, '/el\s+(\d{1,2}\/\d{1,2}\/\d{2,4})/iu'), + 'hora' => self::extraer($text, '/a\s+las\s+(\d{1,2}:\d{2})/iu'), + 'texto_original' => $text, + ]; + + return $result; + } + + // ───────────────────────────────────────────────────────── + // Helpers privados + // ───────────────────────────────────────────────────────── + + private static function esBancolombia(string $text): bool + { + return stripos($text, 'bancolombia') !== false + && ( + stripos($text, 'transferencia') !== false + || stripos($text, 'pago') !== false + || stripos($text, 'recibiste') !== false + || stripos($text, 'enviaste') !== false + ); + } + + private static function detectarTipo(string $text): string + { + if (stripos($text, 'recibiste') !== false) { + return 'transferencia_recibida'; + } + if (stripos($text, 'enviaste') !== false) { + return 'transferencia_enviada'; + } + if (stripos($text, 'pago') !== false) { + return 'pago'; + } + return 'notificacion'; + } + + /** + * Ejecuta un regex y devuelve el primer grupo capturado, trimmed. + */ + private static function extraer(string $text, string $pattern): string + { + if (preg_match($pattern, $text, $m)) { + return trim($m[1]); + } + return ''; + } + + /** + * Extrae el valor monetario como string limpio, ej: "1000.00" + * y también como float. + * Devuelve el string formateado con separadores originales. + */ + private static function extraerValor(string $text): string + { + // Captura números con puntos y comas: $1,000.00 | $1.000,00 | $500 + if (preg_match('/por\s+\$([0-9][0-9.,]*)/iu', $text, $m)) { + return trim($m[1]); + } + return ''; + } + + /** + * Elimina etiquetas HTML y decodifica entidades. + */ + private static function stripHtml(string $text): string + { + // Reemplazar
,

,

, ,
  • con salto de línea para preservar estructura + $text = preg_replace('/<(br\s*\/?|\/p|\/div|\/tr|\/li)>/i', "\n", $text); + $text = strip_tags($text); + $text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8'); + return $text; + } +} diff --git a/app/Services/CorreoImapService.php b/app/Services/CorreoImapService.php index fac1bca..138502f 100644 --- a/app/Services/CorreoImapService.php +++ b/app/Services/CorreoImapService.php @@ -273,6 +273,14 @@ class CorreoImapService // Eliminar líneas de cabecera de parte MIME (Content-Type, etc.) $body = preg_replace('/^(Content-[^\r\n]+\r?\n)+/mi', '', $body); + // Si el cuerpo contiene HTML, convertir bloques estructurales a saltos + // de línea y luego quitar todas las etiquetas para obtener texto plano + if (preg_match('/<[a-z][\s>]/i', $body)) { + $body = preg_replace('/<(br\s*\/?|\/p|\/div|\/tr|\/li)>/i', "\n", $body); + $body = strip_tags($body); + $body = html_entity_decode($body, ENT_QUOTES | ENT_HTML5, 'UTF-8'); + } + // Normalizar saltos de línea $body = str_replace(["\r\n", "\r"], "\n", $body); diff --git a/resources/views/livewire/whatsapp/show-correo-config.blade.php b/resources/views/livewire/whatsapp/show-correo-config.blade.php index c8250e2..6175488 100644 --- a/resources/views/livewire/whatsapp/show-correo-config.blade.php +++ b/resources/views/livewire/whatsapp/show-correo-config.blade.php @@ -190,8 +190,75 @@ {{-- Cuerpo del correo --}}
    + + {{-- Tarjeta de datos Bancolombia ─────────────────── --}} + @if(!empty($email['bancolombia'])) + @php $bc = $email['bancolombia']; @endphp +
    +
    + + + + + Bancolombia — datos extraídos + + @if($bc['tipo'] === 'transferencia_recibida') + Pago recibido + @elseif($bc['tipo'] === 'transferencia_enviada') + Transferencia enviada + @endif +
    +
    + @if($bc['valor']) +
    + Valor + ${{ $bc['valor'] }} +
    + @endif + @if($bc['llave']) +
    + Llave recibe + {{ $bc['llave'] }} +
    + @endif + @if($bc['fecha']) +
    + Fecha + {{ $bc['fecha'] }} +
    + @endif + @if($bc['hora']) +
    + Hora + {{ $bc['hora'] }} +
    + @endif + @if($bc['remitente']) +
    + Remitente + {{ $bc['remitente'] }} +
    + @endif + @if($bc['destinatario']) +
    + Destinatario + {{ $bc['destinatario'] }} +
    + @endif + @if($bc['cuenta']) +
    + Cuenta + *{{ $bc['cuenta'] }} +
    + @endif +
    +
    + @endif + + {{-- Texto completo del correo ────────────────────── --}} @if($email['body']) -
    {{ $email['body'] }}
    +

    Texto completo:

    +
    {{ $email['body'] }}
    @else

    (Sin contenido de texto)

    @endif