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
+5 -4
View File
@@ -1,6 +1,7 @@
<?php
namespace App\Http\Livewire;
use Livewire\WithFileUploads;
use Livewire\Component;
use App\Imports\UsersImport;
@@ -10,10 +11,10 @@ use App\Http\Controllers\Controller;
class Import extends Component
{
use WithFileUploads;
public $photo;
public $importado;
public function render()
{
return view('livewire.import');
@@ -21,8 +22,8 @@ class Import extends Component
public function import()
{
$data=Excel::import(new UsersImport, $this->photo);
$this->photo=null;
$data = Excel::import(new UsersImport, $this->photo);
$this->photo = null;
$this->importado = 'true';
// aquí se puede procesar los datos obtenidos
}
-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) {
+41 -20
View File
@@ -1,53 +1,74 @@
<?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
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
//dd($row);
return 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 = 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
{
return 1;
}
public function batchSize(): int
{
return 1000;
}
public function chunkSize(): int
{
return 1000;
}
public function getDateFormats(): array
{
return [
'Y-m-d',
];
}
}
+35 -22
View File
@@ -1,27 +1,40 @@
<div>
<div class="text-center ">
<div class="my-4 text-gray-400 font-semibold">
Añadir cuentas por volumen
</div>
<form wire:submit.prevent="import">
<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>
<label for="archivo">Selecciona un archivo XLS:</label> <br>
<input type="file" id="archivo" name="archivo" accept=".xlsx" wire:model="photo">
<br>
@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>
@endif
<div class="text-center ">
<div class="my-4 text-gray-400 font-semibold">
Añadir cuentas por volumen
</div>
<form wire:submit.prevent="import">
@if ($importado== 'true')
<div class="text-green-600 fonto-semibold my-4">Cuentas importadas correctamente!!</div>
@endif
</form>
</div>
<label for="archivo">Selecciona un archivo XLS:</label> <br>
<input type="file" id="archivo" name="archivo" accept=".xlsx" wire:model="photo">
<br>
@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>
@endif
@if ($importado == 'true')
<div class="text-green-600 fonto-semibold my-4">Cuentas importadas correctamente!!</div>
@endif
</form>
</div>
</div>