54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\ReporteventasResource\Pages;
|
|
|
|
use Filament\Pages\Page; // ✅ CORRECTO
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\Mail\ReporteVentasMail;
|
|
use App\Models\Venta;
|
|
|
|
class ReporteVentas extends Page
|
|
{
|
|
protected static string $view = 'filament.pages.reporte-ventas';
|
|
|
|
protected static ?string $navigationGroup = 'Reportes'; //
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-cube';
|
|
|
|
public static function canViewAny(): bool
|
|
{
|
|
return auth()->user()->can('ver reportes');
|
|
}
|
|
|
|
public ?string $fromDate = null;
|
|
public ?string $toDate = null;
|
|
public ?string $email = null;
|
|
|
|
public function generateReport()
|
|
{
|
|
$this->validate([
|
|
'fromDate' => 'required|date',
|
|
'toDate' => 'required|date|after_or_equal:fromDate',
|
|
'email' => 'required|email', // Validar que el campo 'email' sea una dirección de correo válida
|
|
]);
|
|
|
|
$email = $this->email; // El correo electrónico del formulario
|
|
|
|
// Obtener las ventas dentro del rango de fechas
|
|
$ventas = Venta::with(['cliente', 'detalles.producto', 'detalles.variante'])
|
|
->whereBetween('created_at', [$this->fromDate, $this->toDate])
|
|
->get();
|
|
|
|
// Enviar el correo con el reporte de ventas
|
|
Mail::to($email)->send(new ReporteVentasMail($ventas, $this->fromDate, $this->toDate, $email));
|
|
|
|
// Restablecer los campos a null después de enviar el correo
|
|
$this->fromDate = null;
|
|
$this->toDate = null;
|
|
$this->email = null;
|
|
|
|
// Mostrar mensaje de éxito
|
|
session()->flash('success', 'Reporte enviado al correo.');
|
|
}
|
|
}
|