91 lines
2.9 KiB
PHP
Executable File
91 lines
2.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
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]),
|
|
'vencimiento' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[4]),
|
|
'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 = "Licencia asignada para $servicioName";
|
|
$consulta->descripcion = 'La licencia 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();
|
|
}
|
|
}
|
|
|
|
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',
|
|
];
|
|
}
|
|
}
|