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
+3 -2
View File
@@ -1,6 +1,7 @@
<?php <?php
namespace App\Http\Livewire; namespace App\Http\Livewire;
use Livewire\WithFileUploads; use Livewire\WithFileUploads;
use Livewire\Component; use Livewire\Component;
use App\Imports\UsersImport; use App\Imports\UsersImport;
@@ -21,8 +22,8 @@ class Import extends Component
public function import() public function import()
{ {
$data=Excel::import(new UsersImport, $this->photo); $data = Excel::import(new UsersImport, $this->photo);
$this->photo=null; $this->photo = null;
$this->importado = 'true'; $this->importado = 'true';
// aquí se puede procesar los datos obtenidos // aquí se puede procesar los datos obtenidos
} }
-11
View File
@@ -222,17 +222,6 @@ class ShowPlanes extends Component
/* ------------- CREAR ------------- */ /* ------------- CREAR ------------- */
public function createNewCuenta() 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()) { if ($consulta_cuenta = Cuentas::where('correo', $this->usuario_newCuenta)->exists()) {
$this->alert('warning', 'La cuenta ya existe', [ $this->alert('warning', 'La cuenta ya existe', [
'position' => 'top' 'position' => 'top'
+1
View File
@@ -289,6 +289,7 @@ class ShowPromociones extends Component
]); ]);
} }
$cuenta = null; $cuenta = null;
$this->modalComprarPromo = false;
} }
} }
if (count($this->f) > 0) { if (count($this->f) > 0) {
+37 -16
View File
@@ -1,31 +1,54 @@
<?php <?php
namespace App\Imports; namespace App\Imports;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use App\Models\Cuentas; use App\Models\Cuentas;
use App\Models\Historial_cuenta;
use App\Models\Pendientes;
use App\Models\Servicio;
use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Concerns\ToModel;
class UsersImport implements ToModel class UsersImport implements ToModel
{ {
/** /**
* @param array $row * @param array $row
* *
* @return \Illuminate\Database\Eloquent\Model|null * @return \Illuminate\Database\Eloquent\Model|null
*/ */
public function model(array $row) public function model(array $row)
{ {
//dd($row); $cuenta = new Cuentas([
return new Cuentas([ 'correo' => $row[0],
'password' => $row[1],
'correo'=> $row[0], 'estado' => $row[2],
'password'=> $row[1], 'inicio' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[3]),
'estado'=> $row[2], 'vencimiento' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[4]),
'inicio'=> \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[3]), 'servicio_id' => $row[5],
'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 public function headingRow(): int
{ {
@@ -45,9 +68,7 @@ class UsersImport implements ToModel
public function getDateFormats(): array public function getDateFormats(): array
{ {
return [ return [
'Y-m-d', 'Y-m-d',
]; ];
} }
} }
+32 -19
View File
@@ -1,27 +1,40 @@
<div> <div>
<div class="text-center "> <div class=" fixed backdrop-blur-sm bg-white/70 left-0 bottom-0 top-0 right-0 h-full z-40" wire:loading
<div class="my-4 text-gray-400 font-semibold"> wire:target="import">
Añadir cuentas por volumen <div class="grid h-full place-items-center ">
</div> <div class="max-w-xs">
<form wire:submit.prevent="import"> <img src="./img/loading.gif" alt="" class="w-full text-center">
<p class="text-gray-500 text-center">Cargando</p>
</div>
</div>
</div>
<label for="archivo">Selecciona un archivo XLS:</label> <br> <div class="text-center ">
<input type="file" id="archivo" name="archivo" accept=".xlsx" wire:model="photo"> <div class="my-4 text-gray-400 font-semibold">
<br> Añadir cuentas por volumen
</div>
<form wire:submit.prevent="import">
@error('photo') <span class="error">{{ $message }}</span> @enderror <label for="archivo">Selecciona un archivo XLS:</label> <br>
<input type="file" id="archivo" name="archivo" accept=".xlsx" wire:model="photo">
<br>
@if ($photo) @error('photo')
<span class="error">{{ $message }}</span>
@enderror
<img src="/img/xls.png" class="w-24 mx-auto"> @if ($photo)
<p class="text-green-500">Archivo cargado</p> <img src="/img/xls.png" class="w-24 mx-auto">
<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> <p class="text-green-500">Archivo cargado</p>
@endif <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') @if ($importado == 'true')
<div class="text-green-600 fonto-semibold my-4">Cuentas importadas correctamente!!</div> <div class="text-green-600 fonto-semibold my-4">Cuentas importadas correctamente!!</div>
@endif @endif
</form> </form>
</div> </div>
</div> </div>