Files
2025-02-27 15:19:52 -05:00

99 lines
3.5 KiB
PHP
Executable File

<?php
namespace App\Imports;
use App\Models\Log_general;
use Illuminate\Support\Facades\Hash;
use App\Models\Cuentas;
use App\Models\Historial_cuenta;
use App\Models\Notificacion;
use App\Models\Pendientes;
use App\Models\Servicio;
use App\Notifications\NotificacionPendiente;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\ToModel;
class UsersImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
$cuenta = new Cuentas([
'correo' => $row[0],
'password' => $row[1],
'estado' => $row[2],
'inicio' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[3])->format('Y-m-d H:i:s'),
'vencimiento' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[4])->format('Y-m-d H:i:s'),
'servicio_id' => $row[5],
]);
$cuenta->save();
// Aquí realizamos operaciones similares a las cuentas manuales
if ($row[2] === 'activo') {
$servicioPantallas = Servicio::find($row[5])->pantallas;
$cuentasPendientes = Pendientes::where('estado', 'pendiente')
->where('servicio_id', $row[5])
->take($servicioPantallas)
->get();
foreach ($cuentasPendientes as $i => $cuentaPendiente) {
$servicioName = $cuentaPendiente->servicio->nombre;
$historial_cuenta = new Historial_cuenta();
$historial_cuenta->historial_id = $cuentaPendiente->historial_id;
$historial_cuenta->cuenta_id = $cuenta->id;
$historial_cuenta->perfil = $i + 1;
$historial_cuenta->save();
$cuentaPendiente->update(['estado' => 'cerrado']);
$historial_cuenta->historial->vendedor->notify(new NotificacionPendiente($servicioName));
$consulta = new Notificacion();
$consulta->remitente_id = Auth::user()->id;
$consulta->destinatario_id = $historial_cuenta->historial->vendedor_id;
$consulta->titulo = "Cuenta asignada para $servicioName";
$consulta->descripcion = 'La cuenta de ' . $servicioName . ' ha sido activada y ahora puedes utilizar tu cuenta sin problemas. Tus credenciales son: correo: ' . $historial_cuenta->cuenta->correo . ' contraseña: ' . $historial_cuenta->cuenta->password;
$consulta->estado = true;
$consulta->todos = false;
$consulta->save();
$logsGeneral = new Log_general();
$logsGeneral->user_id = $historial_cuenta->historial->vendedor_id;
$logsGeneral->detalle = 'Se asigno una cuenta '. $servicioName .', con credenciales: '. $historial_cuenta->cuenta->correo . ' contraseña: ' . $historial_cuenta->cuenta->password.' Que se encontraba en estado pendiente';
$logsGeneral->tipo = 'asigno_cuenta';
$logsGeneral->historial_id = $cuentaPendiente->historial_id;
$logsGeneral->save();
}
}
return $cuenta;
}
public function headingRow(): int
{
return 1;
}
public function batchSize(): int
{
return 1000;
}
public function chunkSize(): int
{
return 1000;
}
public function getDateFormats(): array
{
return [
'Y-m-d',
];
}
}