importar usuarios desde excel

This commit is contained in:
Felipe
2023-10-21 16:42:20 -05:00
parent 7db131cd67
commit ff00f91eed
6 changed files with 82 additions and 57 deletions
+1
View File
@@ -1,6 +1,7 @@
<?php
namespace App\Http\Livewire;
use Livewire\WithFileUploads;
use Livewire\Component;
use App\Imports\UsersImport;
-11
View File
@@ -222,17 +222,6 @@ class ShowPlanes extends Component
/* ------------- CREAR ------------- */
public function createNewCuenta()
{
// $this->validate([
// 'servicio_newCuenta' => 'required',
// 'usuario_newCuenta' => 'required',
// 'password_newCuenta' => 'required',
// 'fecha_inicial' => 'required',
// 'fecha_final' => 'required',
// 'estado' => 'required',
// ]);
if ($consulta_cuenta = Cuentas::where('correo', $this->usuario_newCuenta)->exists()) {
$this->alert('warning', 'La cuenta ya existe', [
'position' => 'top'
+1
View File
@@ -289,6 +289,7 @@ class ShowPromociones extends Component
]);
}
$cuenta = null;
$this->modalComprarPromo = false;
}
}
if (count($this->f) > 0) {
+27 -6
View File
@@ -1,8 +1,12 @@
<?php
namespace App\Imports;
use Illuminate\Support\Facades\Hash;
use App\Models\Cuentas;
use App\Models\Historial_cuenta;
use App\Models\Pendientes;
use App\Models\Servicio;
use Maatwebsite\Excel\Concerns\ToModel;
class UsersImport implements ToModel
@@ -14,18 +18,37 @@ class UsersImport implements ToModel
*/
public function model(array $row)
{
//dd($row);
return new Cuentas([
$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) {
$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']);
}
}
return $cuenta;
}
public function headingRow(): int
{
@@ -45,9 +68,7 @@ class UsersImport implements ToModel
public function getDateFormats(): array
{
return [
'Y-m-d',
];
}
}
+16 -3
View File
@@ -1,4 +1,14 @@
<div>
<div class=" fixed backdrop-blur-sm bg-white/70 left-0 bottom-0 top-0 right-0 h-full z-40" wire:loading
wire:target="import">
<div class="grid h-full place-items-center ">
<div class="max-w-xs">
<img src="./img/loading.gif" alt="" class="w-full text-center">
<p class="text-gray-500 text-center">Cargando</p>
</div>
</div>
</div>
<div class="text-center ">
<div class="my-4 text-gray-400 font-semibold">
Añadir cuentas por volumen
@@ -9,13 +19,16 @@
<input type="file" id="archivo" name="archivo" accept=".xlsx" wire:model="photo">
<br>
@error('photo') <span class="error">{{ $message }}</span> @enderror
@error('photo')
<span class="error">{{ $message }}</span>
@enderror
@if ($photo)
<img src="/img/xls.png" class="w-24 mx-auto">
<p class="text-green-500">Archivo cargado</p>
<button type="submit"class=" my-4 inline-flex justify-center rounded-md border border-transparent px-4 py-2 sm:mb-[0] mb-2 text-white bg-[#B221FD] hover:bg-[#7a02b8] focus:shadow-outline-green ease-in-out sm-text-sm sm-leading-5">Importar datos</button>
<button
type="submit"class=" my-4 inline-flex justify-center rounded-md border border-transparent px-4 py-2 sm:mb-[0] mb-2 text-white bg-[#B221FD] hover:bg-[#7a02b8] focus:shadow-outline-green ease-in-out sm-text-sm sm-leading-5">Importar
datos</button>
@endif
@if ($importado == 'true')