885 lines
28 KiB
PHP
Executable File
885 lines
28 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Configuracione;
|
|
use App\Models\Historiale;
|
|
use App\Models\Log_general;
|
|
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 $utilidad = 0;
|
|
|
|
public $consulta_tarifas;
|
|
public $consulta_usuarioTarifas;
|
|
public $usuario_nombre;
|
|
public $precioTarifa;
|
|
public $tarifasGestion;
|
|
|
|
|
|
public $usuarioGestion_id;
|
|
public $precioTarifasGestion;
|
|
public $buscar_servicio;
|
|
public $rol_usuario;
|
|
|
|
public $consulta_promociones;
|
|
public $consulta_usuarioPromo;
|
|
public $precioPromoGestion;
|
|
public $promoGestion;
|
|
public $buscar_promo;
|
|
|
|
public $servicio_modTarifas;
|
|
public $roles_modTarifas;
|
|
public $precio_modTarifas = 0;
|
|
public $consulta_saldo;
|
|
public $modTarifas;
|
|
public $modalSaldoDesc;
|
|
|
|
public $editarTarifaRol = false;
|
|
public $informacionTarifaRol;
|
|
|
|
public $showActionMenu = false;
|
|
public $actionMenuUser = null;
|
|
public $actionMenuSubvendedor = false;
|
|
|
|
public $estadoTarifa = false;
|
|
public $estadoPromo = false;
|
|
|
|
public $entradas = false;
|
|
|
|
public $verPreciosPersonalizados = false;
|
|
|
|
protected $rules = [
|
|
'nombre' => 'required',
|
|
'email' => 'required',
|
|
'password' => 'required',
|
|
'rol' => 'required',
|
|
];
|
|
|
|
public function render()
|
|
{
|
|
$consulta_tarifas = TarifaPromo::take(1);
|
|
|
|
$consulta_usuarios = User::withCount(['usuario_tarifas as active_tarifas' => function ($query) {
|
|
$query->where('visible', 'true');
|
|
}])
|
|
->withCount(['usuario_promos as active_promos' => function ($query) {
|
|
$query->where('visible', 'true');
|
|
}])
|
|
->where(function ($query) {
|
|
$query->where('name', 'ILIKE', '%' . $this->buscar . '%')
|
|
->orWhere('email', 'ILIKE', '%' . $this->buscar . '%');
|
|
});
|
|
|
|
if ($this->verPreciosPersonalizados) {
|
|
$consulta_usuarios = $consulta_usuarios->where(function ($query) {
|
|
$query->whereHas('usuario_tarifas', function ($q) {
|
|
$q->where('visible', 'true');
|
|
})
|
|
->orWhereHas('usuario_promos', function ($q) {
|
|
$q->where('visible', 'true');
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
|
|
if (!(Auth::user()->rol->nombre == "super")) {
|
|
$consulta_usuarios = $consulta_usuarios->where('subvendedor_id', Auth::user()->id);
|
|
}
|
|
|
|
$consulta_usuarios = $consulta_usuarios
|
|
->with('rol', 'saldo', 'subvendedor_relacion')
|
|
->orderBy('id', 'DESC')
|
|
->withCount('historiales_venta')
|
|
->paginate($this->entradas);
|
|
|
|
if (!empty($this->buscar_servicio)) {
|
|
$this->consulta_tarifas = Tarifas::where('rol_id', $this->rol_usuario)->with([
|
|
'servicio' => function ($query) {
|
|
$query->Where('nombre', 'ILIKE', '%' . $this->buscar_servicio . '%');
|
|
}
|
|
])->get();
|
|
}
|
|
|
|
if (!empty($this->buscar_promo)) {
|
|
$this->consulta_promo = Promociones::where('visible', '!=', 'false')->where('nombre', 'ILIKE', '%' . $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();
|
|
}
|
|
|
|
if (Auth::user()->rol->nombre == "super") {
|
|
$this->roles = Role::get();
|
|
} else if (Auth::user()->subvendedor) {
|
|
$this->roles = Role::where('subvendedor_id', Auth::user()->id)->get();
|
|
} else {
|
|
$this->roles = Role::where('nombre', '=', 'distribuidor')->get();
|
|
}
|
|
//dd($consulta_usuarios);
|
|
return view('livewire.show-usuarios', [
|
|
'usuarios' => $consulta_usuarios,
|
|
'tarifas' => $consulta_tarifas,
|
|
'promo' => Promociones::where('visible', 'true')->get(),
|
|
'config' => Configuracione::with('usuario')->orderby('id', 'desc')->first(),
|
|
]);
|
|
}
|
|
|
|
public $valorBase;
|
|
|
|
public function updatedTarifasGestion($value)
|
|
{
|
|
if (!empty($value)) {
|
|
$planSeleccionado = collect($this->consulta_tarifas)->firstWhere('id', $value);
|
|
|
|
if ($planSeleccionado) {
|
|
$this->valorBase = $planSeleccionado['valor'];
|
|
$this->precioTarifasGestion = $planSeleccionado['valor'];
|
|
$this->calcularUtilidadTarifasGestion();
|
|
}
|
|
} else {
|
|
$this->valorBase = null;
|
|
$this->precioTarifasGestion = null;
|
|
$this->utilidad = null;
|
|
}
|
|
}
|
|
|
|
public function updatedPrecioTarifasGestion($value)
|
|
{
|
|
$this->calcularUtilidadTarifasGestion();
|
|
}
|
|
|
|
private function calcularUtilidadTarifasGestion()
|
|
{
|
|
if (!is_null($this->precioTarifasGestion) && !is_null($this->valorBase)) {
|
|
$this->utilidad = $this->precioTarifasGestion - $this->valorBase;
|
|
} else {
|
|
$this->utilidad = null;
|
|
}
|
|
}
|
|
|
|
|
|
public function updatedPromoGestion($value)
|
|
{
|
|
if (!empty($value)) {
|
|
$planSeleccionado = collect($this->consulta_promo)->firstWhere('id', $value);
|
|
|
|
if ($planSeleccionado) {
|
|
$this->valorBase = $planSeleccionado['precio'];
|
|
$this->precioPromoGestion = $planSeleccionado['precio'];
|
|
$this->calcularUtilidadPromoGestion();
|
|
}
|
|
} else {
|
|
$this->valorBase = null;
|
|
$this->precioPromoGestion = null;
|
|
$this->utilidad = null;
|
|
}
|
|
}
|
|
|
|
public function updatedPrecioPromoGestion($value)
|
|
{
|
|
$this->calcularUtilidadPromoGestion();
|
|
}
|
|
|
|
private function calcularUtilidadPromoGestion()
|
|
{
|
|
if (!is_null($this->precioPromoGestion) && !is_null($this->valorBase)) {
|
|
$this->utilidad = $this->precioPromoGestion - $this->valorBase;
|
|
} else {
|
|
$this->utilidad = null;
|
|
}
|
|
}
|
|
|
|
/// Crear usuario ///
|
|
public function crear()
|
|
{
|
|
|
|
$this->validate();
|
|
|
|
$consulta_user = User::where('email', strtolower(trim($this->email)))->exists();
|
|
|
|
if (!$consulta_user) {
|
|
$crear = new User;
|
|
$crear->name = $this->nombre;
|
|
$crear->email = strtolower(trim($this->email));
|
|
$crear->password = Hash::make($this->password);
|
|
$crear->rol_id = $this->rol;
|
|
$crear->subvendedor_id = Auth::user()->rol->nombre == "super" ? null : Auth::user()->id;
|
|
$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 saldodesc($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->modalSaldoDesc = true;
|
|
}
|
|
|
|
public function addSaldoSubvendedor()
|
|
{
|
|
if (!$this->valor_recargar) {
|
|
$this->alert('warning', '¡Coloque un valor!', [
|
|
'position' => 'top'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$usuarioDestino = User::find($this->user_saldo);
|
|
if ($usuarioDestino && $usuarioDestino->estado !== null && $usuarioDestino->estado !== 'activo') {
|
|
$this->alert('error', 'No se puede recargar saldo a una cuenta cerrada o inactiva.', [
|
|
'position' => 'top'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$this->valor_recargar = str_replace([',', '.'], '', $this->valor_recargar);
|
|
$montoRecarga = (int) $this->valor_recargar;
|
|
|
|
$miSaldo = Saldo::where('usuario_id', Auth::user()->id)->first();
|
|
|
|
if (!$miSaldo || $miSaldo->valor < $montoRecarga) {
|
|
$this->alert('error', 'No tienes saldo suficiente para hacer esta recarga.', [
|
|
'position' => 'top'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$miSaldoAnterior = $miSaldo->valor;
|
|
$miSaldo->update([
|
|
'valor' => $miSaldo->valor - $montoRecarga,
|
|
]);
|
|
|
|
$recarga = new recarga;
|
|
$recarga->payment_method_id = 'manual';
|
|
$recarga->status = 'approved';
|
|
$recarga->usuario_id = $this->user_saldo;
|
|
$recarga->saldo_id = $this->consulta_saldo->id;
|
|
$recarga->monto = $montoRecarga;
|
|
$recarga->valor_recarga = $montoRecarga;
|
|
$recarga->save();
|
|
|
|
$nuevoSaldoDestino = $this->saldo_actual + $montoRecarga;
|
|
$this->consulta_saldo->update([
|
|
'valor' => $nuevoSaldoDestino,
|
|
]);
|
|
|
|
$usuarioDestino = User::find($this->user_saldo);
|
|
$nombre = $usuarioDestino->name;
|
|
$email = $usuarioDestino->email;
|
|
|
|
$logsGeneral = new Log_general();
|
|
$logsGeneral->user_id = Auth::user()->id;
|
|
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email .
|
|
' ha recargado a ' . $nombre . ' - ' . $email . ' la cantidad de $' . number_format($montoRecarga) .
|
|
', de su saldo. Su saldo anterior: $' . number_format($miSaldoAnterior) .
|
|
', saldo actual: $' . number_format($miSaldoAnterior - $montoRecarga) .
|
|
'. Saldo nuevo de ' . $nombre . ': $' . number_format($nuevoSaldoDestino);
|
|
$logsGeneral->tipo = 'recarga';
|
|
$logsGeneral->historial_id = null;
|
|
$logsGeneral->save();
|
|
|
|
$this->emit('actualizarSaldoHeader');
|
|
|
|
$this->alert('success', 'Saldo actualizado con éxito.', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->limpiarSaldo();
|
|
}
|
|
|
|
public function addSaldo()
|
|
{
|
|
if (!$this->valor_recargar) {
|
|
$this->alert('warning', '¡Coloque un valor!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
$usuarioDestino = User::find($this->user_saldo);
|
|
if ($usuarioDestino && $usuarioDestino->estado !== null && $usuarioDestino->estado !== 'activo') {
|
|
$this->alert('error', 'No se puede recargar saldo a una cuenta cerrada o inactiva.', [
|
|
'position' => 'top'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$this->valor_recargar = str_replace([',', '.'], '', $this->valor_recargar);
|
|
$recarga = new recarga;
|
|
$recarga->payment_method_id = 'manual';
|
|
$recarga->status = 'approved';
|
|
$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();
|
|
|
|
$nuevo_saldo = $this->saldo_actual + (int)$this->valor_recargar;
|
|
$this->consulta_saldo->update([
|
|
'valor' => $nuevo_saldo,
|
|
]);
|
|
|
|
$usuario_a_recargar = User::find($this->user_saldo);
|
|
$nombre = $usuario_a_recargar->name;
|
|
$email = $usuario_a_recargar->email;
|
|
//Log de recarga de saldo s
|
|
$logsGeneral = new Log_general();
|
|
$logsGeneral->user_id = Auth::user()->id;
|
|
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha recargado a ' . $nombre . ' - ' . $email . ' la cantidad de $' . $this->valor_recargar . ', Saldo anterior: $' . $this->saldo_actual . ', Nuevo saldo disponible: $' . $nuevo_saldo;
|
|
$logsGeneral->tipo = 'recarga';
|
|
$logsGeneral->historial_id = null;
|
|
$logsGeneral->save();
|
|
|
|
$this->alert('success', 'Saldo actualizado con exito!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
|
|
$this->limpiarSaldo();
|
|
}
|
|
|
|
public function descontar()
|
|
{
|
|
if (!$this->valor_recargar) {
|
|
$this->alert('warning', '¡Coloque un valor!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
$recarga = new recarga;
|
|
$recarga->payment_method_id = 'manual';
|
|
$recarga->status = 'Descuento';
|
|
$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();
|
|
|
|
$userSaldo = User::find($this->user_saldo);
|
|
|
|
$logsGeneral = new Log_general();
|
|
$logsGeneral->user_id = Auth::user()->id;
|
|
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha descontado la cantidad de $' . $this->valor_recargar . ' al usuario ' . $userSaldo->name . ' - ' . $userSaldo->email;
|
|
$logsGeneral->tipo = 'descuento_saldo';
|
|
$logsGeneral->historial_id = null;
|
|
$logsGeneral->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 quitarSubvendedor($id)
|
|
{
|
|
User::FindOrFail($id);
|
|
|
|
User::find($id)->update([
|
|
'subvendedor' => false
|
|
]);
|
|
}
|
|
public function ponerSubvendedor($id)
|
|
{
|
|
User::FindOrFail($id);
|
|
|
|
User::find($id)->update([
|
|
'subvendedor' => true
|
|
]);
|
|
}
|
|
|
|
public function abrirAcciones($id)
|
|
{
|
|
$usuario = User::findOrFail($id);
|
|
$this->actionMenuUser = $usuario;
|
|
$this->actionMenuSubvendedor = (bool) $usuario->subvendedor;
|
|
$this->showActionMenu = true;
|
|
}
|
|
|
|
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 editarTarifa($tarifa)
|
|
{
|
|
$this->editarTarifaRol = true;
|
|
|
|
$this->informacionTarifaRol = $tarifa;
|
|
|
|
$this->roles_modTarifas = $tarifa['rol_id'];
|
|
$this->precio_modTarifas = $tarifa['precio'];
|
|
$this->utilidad = $tarifa['utilidad'];
|
|
$this->estadoTarifa = $tarifa['visible'] == 'activo' ? 1 : 0;
|
|
$this->servicio_modTarifas = $tarifa['promocion_id'];
|
|
}
|
|
|
|
public function actualizarTarifaRol($id)
|
|
{
|
|
if (empty($this->roles_modTarifas)) {
|
|
$this->alert('warning', '¡Seleccione un rol!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
if (empty($this->precio_modTarifas)) {
|
|
$this->alert('warning', '¡Coloque un precio!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
if (empty($this->utilidad)) {
|
|
$this->alert('warning', '¡Coloque una utilidad!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
|
|
TarifaPromo::find($id)->update([
|
|
'precio' => $this->precio_modTarifas,
|
|
'utilidad' => $this->utilidad,
|
|
'visible' => $this->estadoTarifa == 1 ? 'activo' : 'inactivo',
|
|
'rol_id' => $this->roles_modTarifas,
|
|
'promocion_id' => $this->servicio_modTarifas
|
|
]);
|
|
|
|
$this->alert('success', 'Tarifa actualizada con exito!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->editarTarifaRol = false;
|
|
}
|
|
|
|
|
|
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' => strtolower(trim($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->utilidad = 0;
|
|
|
|
$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 (auth()->user()->rol->nombre !== 'super') {
|
|
if ($this->precioTarifasGestion < $this->valorBase) {
|
|
$this->alert('error', 'El precio no puede ser menor al costo del plan', [
|
|
'position' => 'top'
|
|
]);
|
|
return;
|
|
}
|
|
}
|
|
|
|
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'
|
|
]);
|
|
} elseif (empty($this->utilidad)) {
|
|
$this->alert('warning', '¡Coloque una utilidad!', [
|
|
'position' => 'top'
|
|
]);
|
|
} else {
|
|
$usuarioTarifa = new Usuario_tarifa;
|
|
$usuarioTarifa->precio = $this->precioTarifasGestion;
|
|
$usuarioTarifa->utilidad = $this->utilidad;
|
|
$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();
|
|
}
|
|
|
|
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->utilidad = 0;
|
|
|
|
$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->utilidad = $this->utilidad;
|
|
$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',
|
|
'customClass' => [
|
|
'confirmButton' => 'bg-green-500',
|
|
'denyButton' => 'bg-red-500',
|
|
'cancelButton' => 'bg-gray-500',
|
|
]
|
|
]);
|
|
}
|
|
|
|
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);
|
|
|
|
$logsGeneral = new Log_general();
|
|
$logsGeneral->user_id = Auth::user()->id;
|
|
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha eliminado al usuario ' . $user->name . ' - ' . $user->email;
|
|
$logsGeneral->tipo = 'eliminar_usuario';
|
|
$logsGeneral->historial_id = null;
|
|
$logsGeneral->save();
|
|
|
|
$user->delete();
|
|
}
|
|
|
|
public function editarPassword($id)
|
|
{
|
|
$this->pass_id = $id;
|
|
|
|
$this->modalPassword = true;
|
|
}
|
|
|
|
public function confirmarNewPassword()
|
|
{
|
|
$validatedData = $this->validate([
|
|
'newPassword' => 'required'
|
|
]);
|
|
|
|
if (!$this->pass_id) {
|
|
$this->alert('error', 'Error: no se encontró el usuario.', [
|
|
'position' => 'top'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$user = User::findOrFail($this->pass_id);
|
|
$user->password = Hash::make($this->newPassword);
|
|
$user->save();
|
|
|
|
$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;
|
|
$this->modalSaldoDesc = false;
|
|
}
|
|
|
|
/* ------------- ACTUALIZAR ------------- */
|
|
public function updateTarifas()
|
|
{
|
|
$consulta_existe = TarifaPromo::where('promocion_id', $this->servicio_modTarifas)
|
|
->where('rol_id', $this->roles_modTarifas)
|
|
->exists();
|
|
|
|
if ($consulta_existe) {
|
|
$this->alert('warning', '¡Esta tarifa ya existe!', [
|
|
'position' => 'top'
|
|
]);
|
|
} elseif (empty($this->servicio_modTarifas)) {
|
|
$this->alert('warning', '¡Seleccione una promocion!', [
|
|
'position' => 'top'
|
|
]);
|
|
} elseif (empty($this->precio_modTarifas)) {
|
|
$this->alert('warning', '¡Coloque un precio!', [
|
|
'position' => 'top'
|
|
]);
|
|
} elseif (empty($this->utilidad)) {
|
|
$this->alert('warning', '¡Coloque la utilidad!', [
|
|
'position' => 'top'
|
|
]);
|
|
} else {
|
|
$tarifa = new TarifaPromo;
|
|
$tarifa->utilidad = $this->utilidad;
|
|
$tarifa->promocion_id = $this->servicio_modTarifas;
|
|
$tarifa->rol_id = $this->roles_modTarifas;
|
|
$tarifa->precio = $this->precio_modTarifas;
|
|
$tarifa->visible = $this->estadoTarifa ? 'false' : 'true';
|
|
$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'
|
|
]);
|
|
}
|
|
}
|