Initial commit
This commit is contained in:
@@ -0,0 +1,505 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Historiale;
|
||||
use App\Models\Promociones;
|
||||
use App\Models\recarga;
|
||||
use App\Models\Role;
|
||||
use App\Models\Saldo;
|
||||
use App\Models\Tarifas;
|
||||
use App\Models\User;
|
||||
use App\Models\Usuario_promo;
|
||||
use App\Models\Usuario_tarifa;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Livewire\Component;
|
||||
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
||||
use App\Models\Servicio;
|
||||
use App\Models\TarifaPromo;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
|
||||
class ShowUsuarios extends Component
|
||||
{
|
||||
use LivewireAlert;
|
||||
use WithPagination;
|
||||
|
||||
public $roles;
|
||||
public $nombre;
|
||||
public $email;
|
||||
public $password;
|
||||
public $rol;
|
||||
public $buscar;
|
||||
public $newPassword;
|
||||
public $pass_id;
|
||||
|
||||
public $modalPassword = false;
|
||||
|
||||
public $id_edit;
|
||||
public $nombre_edit;
|
||||
public $email_edit;
|
||||
public $password_edit;
|
||||
public $rol_edit;
|
||||
|
||||
public $editar = false;
|
||||
public $add = false;
|
||||
public $gestion = false;
|
||||
public $gestionPromo = false;
|
||||
public $delete_user;
|
||||
|
||||
public $modalSaldo = false;
|
||||
public $saldo_actual;
|
||||
public $nuevo_saldo;
|
||||
public $valor_recargar;
|
||||
public $user_saldo;
|
||||
|
||||
public $consulta_tarifas;
|
||||
public $consulta_usuarioTarifas;
|
||||
public $usuario_nombre;
|
||||
public $precioTarifa;
|
||||
public $tarifasGestion;
|
||||
public $estadoTarifa;
|
||||
public $usuarioGestion_id;
|
||||
public $precioTarifasGestion;
|
||||
public $buscar_servicio;
|
||||
public $rol_usuario;
|
||||
|
||||
public $consulta_promociones;
|
||||
public $consulta_usuarioPromo;
|
||||
public $precioPromoGestion;
|
||||
public $promoGestion;
|
||||
public $estadoPromo;
|
||||
public $buscar_promo;
|
||||
|
||||
public $servicio_modTarifas;
|
||||
public $roles_modTarifas;
|
||||
public $precio_modTarifas = 0;
|
||||
public $consulta_saldo;
|
||||
public $modTarifas;
|
||||
|
||||
|
||||
|
||||
protected $rules = [
|
||||
'nombre' => 'required',
|
||||
'email' => 'required',
|
||||
'password' => 'required',
|
||||
'rol' => 'required',
|
||||
];
|
||||
|
||||
public function render()
|
||||
{
|
||||
$consulta_tarifas = TarifaPromo::take(1);
|
||||
|
||||
$consulta_usuarios = User::where('name', 'LIKE', '%' . $this->buscar . '%')->orWhere('email', 'LIKE', '%' . $this->buscar . '%')->with('rol', 'saldo')->orderBy('id','DESC')->withCount('historiales_venta')->paginate(15);
|
||||
|
||||
if (!empty($this->buscar_servicio)) {
|
||||
$this->consulta_tarifas = Tarifas::where('rol_id', $this->rol_usuario)->with([
|
||||
'servicio' => function ($query) {
|
||||
$query->Where('nombre', 'LIKE', '%' . $this->buscar_servicio . '%'); }
|
||||
])->get();
|
||||
|
||||
}
|
||||
|
||||
if (!empty($this->buscar_promo)) {
|
||||
$this->consulta_promo = Promociones::where('visible', '!=', 'false')->where('nombre', 'like', '%' . $this->buscar_promo . '%')->get();
|
||||
}
|
||||
|
||||
if ($this->gestionPromo == true) {
|
||||
$this->consulta_usuarioPromo = Usuario_promo::where('usuario_id', $this->usuarioGestion_id)->with('usuario', 'promocion')->get();
|
||||
}
|
||||
if (!empty($this->servicio_modTarifas)) {
|
||||
$consulta_tarifas = TarifaPromo::with('rol')->where('promocion_id', $this->servicio_modTarifas)->get();
|
||||
}
|
||||
|
||||
$this->roles = Role::get();
|
||||
|
||||
return view('livewire.show-usuarios', [
|
||||
'usuarios' => $consulta_usuarios,
|
||||
'tarifas' => $consulta_tarifas,
|
||||
'promo' => Promociones::where('visible', 'true')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
/// Crear usuario ///
|
||||
public function crear()
|
||||
{
|
||||
|
||||
$this->validate();
|
||||
|
||||
$consulta_user = User::where('email', $this->email)->exists();
|
||||
|
||||
if (!$consulta_user) {
|
||||
$crear = new User;
|
||||
$crear->name = $this->nombre;
|
||||
$crear->email = $this->email;
|
||||
$crear->password = Hash::make($this->password);
|
||||
$crear->rol_id = $this->rol;
|
||||
$crear->save();
|
||||
|
||||
$saldo = new saldo;
|
||||
$saldo->valor = '0';
|
||||
$saldo->usuario_id = $crear->id;
|
||||
$saldo->save();
|
||||
|
||||
if ($crear) {
|
||||
$this->alert('success', 'Usuario creado con èxito!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
$this->add = false;
|
||||
} else {
|
||||
$this->alert('warning', 'Este correo ya está registrado!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//CONSULTAR SALDO//
|
||||
public function saldo($user_saldo)
|
||||
{
|
||||
$this->user_saldo = $user_saldo;
|
||||
$this->consulta_saldo = Saldo::where('usuario_id', $this->user_saldo)->first();
|
||||
|
||||
if (is_null($this->consulta_saldo)) {
|
||||
$saldo = new Saldo;
|
||||
$saldo->valor = 0;
|
||||
$saldo->usuario_id = $this->user_saldo;
|
||||
$saldo->save();
|
||||
$this->consulta_saldo = $saldo;
|
||||
}
|
||||
|
||||
$this->saldo_actual = $this->consulta_saldo->valor;
|
||||
$this->modalSaldo = true;
|
||||
|
||||
}
|
||||
|
||||
public function addSaldo()
|
||||
{
|
||||
$recarga = new recarga;
|
||||
$recarga->payment_method_id = 'manual';
|
||||
$recarga->status = 'Aprovada';
|
||||
$recarga->usuario_id = $this->user_saldo;
|
||||
$recarga->saldo_id = $this->consulta_saldo->id;
|
||||
$recarga->monto = $this->valor_recargar;
|
||||
$recarga->valor_recarga = $this->valor_recargar;
|
||||
$recarga->save();
|
||||
|
||||
|
||||
$this->consulta_saldo->update([
|
||||
'valor' => $this->saldo_actual + $this->valor_recargar
|
||||
]);
|
||||
|
||||
$this->alert('success', 'Saldo actualizado con exito!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
|
||||
|
||||
$this->limpiarSaldo();
|
||||
|
||||
}
|
||||
|
||||
public function editar($id)
|
||||
{
|
||||
$this->id_edit = $id;
|
||||
|
||||
$consulta = User::FindOrFail($this->id_edit);
|
||||
$this->nombre_edit = $consulta->name;
|
||||
$this->email_edit = $consulta->email;
|
||||
$this->rol_edit = $consulta->rol_id;
|
||||
|
||||
$this->editar = true;
|
||||
|
||||
}
|
||||
public function actualizar()
|
||||
{
|
||||
$this->validate(
|
||||
[
|
||||
'nombre_edit' => 'required',
|
||||
'email_edit' => 'required',
|
||||
'rol_edit' => 'required',
|
||||
]
|
||||
);
|
||||
User::where('id', $this->id_edit)->update([
|
||||
'name' => $this->nombre_edit,
|
||||
'email' => $this->email_edit,
|
||||
'rol_id' => $this->rol_edit
|
||||
]);
|
||||
|
||||
$this->editar = false;
|
||||
|
||||
$this->alert('success', 'Usuario actualizado con exito!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
|
||||
public function gestionar($id, $rid, $usuNom)
|
||||
{
|
||||
|
||||
$this->usuarioGestion_id = $id;
|
||||
$this->rol_usuario = $rid;
|
||||
$this->usuario_nombre = $usuNom;
|
||||
|
||||
$this->consulta_tarifas = Tarifas::where('rol_id', $rid)->with('servicio')->get();
|
||||
|
||||
$this->consulta_usuarioTarifas = Usuario_tarifa::where('usuario_id', $this->usuarioGestion_id)->with('tarifa')->get();
|
||||
//dd($this->consulta_usuarioTarifas);
|
||||
|
||||
$this->gestion = true;
|
||||
}
|
||||
|
||||
public function addtarifas()
|
||||
{
|
||||
$consulta_existe = Usuario_tarifa::where('tarifa_id', $this->tarifasGestion)
|
||||
->where('usuario_id', $this->usuarioGestion_id)
|
||||
->exists();
|
||||
|
||||
if ($consulta_existe) {
|
||||
$this->alert('warning', '¡Este usuario ya tiene esta tarifa!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
} elseif (empty($this->tarifasGestion)) {
|
||||
$this->alert('warning', '¡Seleccione una tarifa!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
} elseif (empty($this->precioTarifasGestion)) {
|
||||
$this->alert('warning', '¡Coloque un precio!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
} else {
|
||||
$usuarioTarifa = new Usuario_tarifa;
|
||||
$usuarioTarifa->precio = $this->precioTarifasGestion;
|
||||
$usuarioTarifa->tarifa_id = $this->tarifasGestion;
|
||||
$usuarioTarifa->usuario_id = $this->usuarioGestion_id;
|
||||
$usuarioTarifa->visible = $this->estadoTarifa ? 'false' : 'true';
|
||||
$usuarioTarifa->save();
|
||||
|
||||
$this->alert('success', '¡Tarifas del usuario actualizadas correctamente!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
|
||||
$this->consulta_usuarioTarifas = Usuario_tarifa::where('usuario_id', $this->usuarioGestion_id)
|
||||
->with('tarifa')
|
||||
->get();
|
||||
|
||||
|
||||
$this->precioTarifasGestion = null;
|
||||
}
|
||||
|
||||
public function eliminarUsuarioTarifa($id)
|
||||
{
|
||||
Usuario_tarifa::where('id', $id)->delete();
|
||||
|
||||
$this->alert('success', 'Tarifa de usuario eliminada correctamente!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
|
||||
$this->consulta_usuarioTarifas = Usuario_tarifa::where('usuario_id', $this->usuarioGestion_id)->with('tarifa')->get();
|
||||
}
|
||||
|
||||
public function gestionarPromociones($id, $usuNom)
|
||||
{
|
||||
$this->usuarioGestion_id = $id;
|
||||
$this->usuario_nombre = $usuNom;
|
||||
|
||||
$this->consulta_promo = Promociones::where('visible', '!=', 'false')->get();
|
||||
|
||||
//$this->consulta_usuarioPromo = Usuario_promo::join('promociones', 'promocion_id', '=', 'promociones.id')->where('usuario_id',$this->usuarioGestion_id)->get(['usuario_promos.id','nombre','usuario_promos.visible','usuario_promos.precio']);
|
||||
$this->consulta_usuarioPromo = Usuario_promo::where('usuario_id', $this->usuarioGestion_id)->with('usuario', 'promocion')->get();
|
||||
$this->gestionPromo = true;
|
||||
}
|
||||
|
||||
public function addpromos()
|
||||
{
|
||||
$consulta_existe = Usuario_promo::where('promocion_id', $this->promoGestion)->where('usuario_id', $this->usuarioGestion_id)->get();
|
||||
|
||||
|
||||
if (empty($this->promoGestion)) {
|
||||
$this->alert('warning', 'Seleccione una promocion!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
} else {
|
||||
|
||||
if ($consulta_existe->count() > 0) {
|
||||
|
||||
$this->alert('warning', 'Este usuario ya tiene esta promocion!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
|
||||
} else {
|
||||
$usuarioPromo = new Usuario_promo();
|
||||
$usuarioPromo->precio = $this->precioPromoGestion;
|
||||
$usuarioPromo->promocion_id = $this->promoGestion;
|
||||
$usuarioPromo->usuario_id = $this->usuarioGestion_id;
|
||||
if ($this->estadoPromo) {
|
||||
$usuarioPromo->visible = 'false';
|
||||
} else {
|
||||
$usuarioPromo->visible = 'true';
|
||||
}
|
||||
$usuarioPromo->save();
|
||||
|
||||
$this->alert('success', 'promociones del usuario actualizadas correctamente!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->consulta_usuarioPromo = Usuario_promo::join('promociones', 'promocion_id', '=', 'promociones.id')->where('usuario_id', $this->usuarioGestion_id)->get(['usuario_promos.id', 'nombre', 'usuario_promos.visible', 'usuario_promos.precio']);
|
||||
|
||||
}
|
||||
|
||||
public function eliminarUsuarioPromo($id)
|
||||
{
|
||||
Usuario_promo::where('id', $id)->delete();
|
||||
|
||||
$this->alert('success', 'Promocion de usuario eliminada correctamente!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
|
||||
$this->consulta_usuarioPromo = Usuario_promo::join('promociones', 'promocion_id', '=', 'promociones.id')->where('usuario_id', $this->usuarioGestion_id)->get(['usuario_promos.id', 'nombre', 'usuario_promos.visible', 'usuario_promos.precio']);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function eliminar($id)
|
||||
{
|
||||
$this->delete_user = $id;
|
||||
$this->alert('warning', '¿Eliminar usuario?', [
|
||||
'position' => 'center',
|
||||
'timer' => '10000',
|
||||
'toast' => false,
|
||||
'text' => 'Esta seguro',
|
||||
'showConfirmButton' => true,
|
||||
'onConfirmed' => 'confirmed',
|
||||
'showDenyButton' => false,
|
||||
'onDenied' => '',
|
||||
'showCancelButton' => true,
|
||||
'onDismissed' => '',
|
||||
'cancelButtonText' => 'Salir',
|
||||
'confirmButtonText' => 'Si',
|
||||
]);
|
||||
}
|
||||
|
||||
public function getListeners()
|
||||
{
|
||||
return [
|
||||
'confirmed'
|
||||
];
|
||||
}
|
||||
|
||||
public function confirmed()
|
||||
{
|
||||
// Paso 1: Obtener el id del usuario eliminado
|
||||
$usuarioEliminado = User::where('name', 'usuarioEliminado')->value('id');
|
||||
|
||||
// Paso 2: Actualizar los registros en la base de datos
|
||||
$consultaHistorial=Historiale::where('vendedor_id', $this->delete_user)
|
||||
->update(['vendedor_id' => $usuarioEliminado,'cliente_id'=>$usuarioEliminado]);
|
||||
|
||||
|
||||
$user = User::find($this->delete_user);
|
||||
$user->delete();
|
||||
|
||||
}
|
||||
|
||||
public function editarPassword($id)
|
||||
{
|
||||
$this->pass_id = $id;
|
||||
|
||||
$this->modalPassword = true;
|
||||
}
|
||||
|
||||
public function confirmarNewPassword()
|
||||
{
|
||||
|
||||
$validatedData = $this->validate([
|
||||
'newPassword' => 'required'
|
||||
]);
|
||||
|
||||
User::where('id', $this->pass_id)->update([
|
||||
'password' => Hash::make($this->newPassword),
|
||||
]);
|
||||
|
||||
$this->alert('success', 'Contraseña actualizada correctamente!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
|
||||
$this->limpiarContra();
|
||||
}
|
||||
|
||||
public function limpiarInputAdd()
|
||||
{
|
||||
$this->nombre = '';
|
||||
$this->email = '';
|
||||
$this->rol = null;
|
||||
|
||||
$this->add = false;
|
||||
}
|
||||
|
||||
public function limpiarInputEdit()
|
||||
{
|
||||
$this->nombre_edit = '';
|
||||
$this->email_edit = '';
|
||||
$this->rol_edit = null;
|
||||
|
||||
$this->editar = false;
|
||||
}
|
||||
public function limpiarContra()
|
||||
{
|
||||
$this->newPassword = '';
|
||||
$this->modalPassword = false;
|
||||
}
|
||||
public function limpiarSaldo()
|
||||
{
|
||||
$this->valor_recargar = null;
|
||||
$this->modalSaldo = false;
|
||||
}
|
||||
|
||||
/* ------------- ACTUALIZAR ------------- */
|
||||
public function updateTarifas()
|
||||
{
|
||||
|
||||
$consulta_existe = TarifaPromo::where('promocion_id', $this->servicio_modTarifas)->where('rol_id', $this->roles_modTarifas)->get();
|
||||
//dd($consulta_existe);
|
||||
|
||||
if (empty($this->servicio_modTarifas)) {
|
||||
$this->alert('warning', 'Seleccione una promocion!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
} else {
|
||||
|
||||
if ($consulta_existe->count() > 0) {
|
||||
|
||||
$this->alert('warning', 'Esta tarifa ya existe!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
|
||||
} else {
|
||||
$tarifa = new TarifaPromo;
|
||||
$tarifa->precio = $this->precio_modTarifas;
|
||||
$tarifa->promocion_id = $this->servicio_modTarifas;
|
||||
$tarifa->rol_id = $this->roles_modTarifas;
|
||||
$tarifa->save();
|
||||
|
||||
$this->alert('success', 'Tarifas actualizadas correctamente!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------- ELIMINAR ------------- */
|
||||
|
||||
public function eliminarModTarifa($id)
|
||||
{
|
||||
TarifaPromo::where('id', $id)->delete();
|
||||
|
||||
$this->alert('success', 'Tarifas eliminada correctamente!', [
|
||||
'position' => 'top'
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user