up
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Mail\ReporteVentasMail;
|
||||
use App\Models\User;
|
||||
use App\Models\Venta;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class EnviarReporteVentas extends Command
|
||||
{
|
||||
protected $signature = 'reporte:ventas';
|
||||
|
||||
protected $description = 'Enviar reporte de ventas semanal por correo';
|
||||
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$fromDate = Carbon::now()->subWeek()->startOfWeek(); // Lunes anterior
|
||||
$toDate = Carbon::now()->subWeek()->endOfWeek(); // Domingo anterior
|
||||
|
||||
$ventas = Venta::with(['cliente', 'detalles.producto', 'detalles.variante'])
|
||||
->whereBetween('created_at', [$fromDate, $toDate])
|
||||
->get();
|
||||
|
||||
// Obtener todos los usuarios con el rol 'Administrador'
|
||||
$administradores = User::role('Administrador')->get();
|
||||
|
||||
if ($administradores->isEmpty()) {
|
||||
$this->warn('No se encontraron usuarios con el rol Administrador.');
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($administradores as $admin) {
|
||||
Mail::to($admin->email)->send(new ReporteVentasMail($ventas, $fromDate, $toDate, $admin->email));
|
||||
$this->info("Reporte enviado a: {$admin->email}");
|
||||
}
|
||||
|
||||
$this->info('Todos los reportes han sido enviados a los administradores.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user