881 lines
32 KiB
PHP
Executable File
881 lines
32 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Cuentas;
|
|
use App\Models\Historiale;
|
|
use App\Models\Historial_cuenta;
|
|
use App\Models\Log_general;
|
|
use App\Models\Log_historial;
|
|
use App\Models\Notificacion;
|
|
use App\Models\Pendientes;
|
|
use App\Models\Role;
|
|
use App\Models\Servicio;
|
|
use App\Models\Tarifas;
|
|
use App\Models\user;
|
|
use App\Notifications\NotificacionPendiente;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Component;
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
|
use Livewire\WithFileUploads;
|
|
use Livewire\WithPagination;
|
|
|
|
|
|
class ShowPlanes extends Component
|
|
{
|
|
use LivewireAlert;
|
|
use WithFileUploads;
|
|
use WithPagination;
|
|
|
|
public $delete_cuenta;
|
|
public $informacionTarifaRol;
|
|
|
|
public $newCuenta = false;
|
|
public $renovar = false;
|
|
public $serviciosModal = false;
|
|
public $modTarifas = false;
|
|
public $ver = false;
|
|
|
|
public $roles_modTarifas;
|
|
public $pantallas_modTarifas;
|
|
public $precio_modTarifas = 0;
|
|
public $utilidad = 0;
|
|
|
|
public $estadoTarifa = true;
|
|
public $diasTarifa = '30';
|
|
public $servicio_modTarifas;
|
|
|
|
|
|
public $buscar;
|
|
public $servicios_id;
|
|
public $fechaActual;
|
|
public $id_cuentaVer;
|
|
public $consulta_verUsuarios;
|
|
public $nombre_servicios = '';
|
|
|
|
|
|
public $nombre_servicio;
|
|
public $descripcion_servicio;
|
|
public $estado_servicio;
|
|
public $color_servicio;
|
|
|
|
|
|
public $servicio_newCuenta;
|
|
public $usuario_newCuenta;
|
|
public $password_newCuenta;
|
|
public $fecha_inicial;
|
|
public $fecha_final;
|
|
|
|
|
|
public $observacionesm = false;
|
|
public $observaciones;
|
|
public $id_observacion;
|
|
|
|
public $observacionesm1 = false;
|
|
public $observaciones1;
|
|
public $id_observacion1;
|
|
|
|
|
|
public $usuario_renovar;
|
|
public $usuario_renovar_old;
|
|
public $password_renovar;
|
|
public $fecha_inicial_renovar;
|
|
public $fecha_inicial_renovar_old;
|
|
public $fecha_final_renovar;
|
|
public $fecha_final_renovar_old;
|
|
|
|
public $servicio;
|
|
|
|
public $photo;
|
|
public $photo_inicio;
|
|
|
|
public $estado = 'activo';
|
|
public $completa;
|
|
public $pantallas;
|
|
public $venceHoy;
|
|
public $activoHoy;
|
|
public $libres = null;
|
|
|
|
public $delete_1 = [];
|
|
public $tarifas;
|
|
public $addUser;
|
|
public $usuarios;
|
|
|
|
public $selectedOption;
|
|
public $searchTerm = '';
|
|
public $tarifa;
|
|
public $perfil;
|
|
public $repetidas;
|
|
|
|
public $entradas = 10;
|
|
|
|
public $editarTarifaRol = false;
|
|
|
|
public $notaEliminarModal = false;
|
|
public $notaEliminarVariosModal = false;
|
|
public $nota_eliminacion;
|
|
public $cont;
|
|
public $tiempo_usuario;
|
|
|
|
|
|
|
|
|
|
public function render()
|
|
{
|
|
$this->fechaActual = date('Y-m-d');
|
|
|
|
$consulta_tarifas = Tarifas::take(1);
|
|
|
|
if (empty($this->fecha_inicial)) {
|
|
$this->fecha_inicial = Carbon::now()->toDateTimeString();
|
|
$this->fecha_final = Carbon::now()->addDay(30)->toDateTimeString();
|
|
}
|
|
if ($this->servicio_modTarifas) {
|
|
$this->tarifas = Tarifas::with('rol')
|
|
->where('servicio_id', $this->servicio_modTarifas)
|
|
// ->where('estado', 'activo')
|
|
->get();
|
|
}
|
|
|
|
// Empezamos definiendo la consulta básica
|
|
$consulta_cuentas = Cuentas::where('correo', 'LIKE', '%' . $this->buscar . '%')
|
|
->withCount('historiales')
|
|
->with('historiales')
|
|
->orderBy('id', 'DESC');
|
|
|
|
$consulta_cuentas = $consulta_cuentas->when(!empty($this->servicios_id), function ($query) {
|
|
return $query->where('servicio_id', $this->servicios_id);
|
|
})
|
|
->when(!empty($this->completa), function ($query) {
|
|
return $query->where('estado', 'pendiente');
|
|
})
|
|
->when(!empty($this->pantallas), function ($query) {
|
|
return $query->where('estado', 'activo');
|
|
})
|
|
->when(!empty($this->activoHoy), function ($query) {
|
|
$fechaHoy = Carbon::now()->subDay(1);
|
|
return $query->whereDate('vencimiento', '>', $fechaHoy);
|
|
})
|
|
->when($this->venceHoy, function ($query) {
|
|
$fechaHoy = Carbon::now()->addDay(1);
|
|
return $query->whereDate('vencimiento', '<', $fechaHoy);
|
|
})
|
|
->when($this->libres, function ($query) {
|
|
return $query->whereDoesntHave('historiales');
|
|
});
|
|
|
|
if (!empty($this->repetidas)) {
|
|
$consulta_cuentas = $consulta_cuentas->whereIn('correo', function ($subquery) {
|
|
$subquery->select('correo')
|
|
->from('cuentas')
|
|
->groupBy('correo')
|
|
->havingRaw('COUNT(*) > 1');
|
|
});
|
|
}
|
|
|
|
|
|
$consulta_cuentas = $consulta_cuentas->paginate($this->entradas);
|
|
|
|
if ($this->searchTerm) {
|
|
$this->usuarios = User::where('email', 'LIKE', '%' . $this->searchTerm . '%')->get();
|
|
if ($this->selectedOption) {
|
|
$this->searchTerm = $this->selectedOption;
|
|
}
|
|
} else {
|
|
$this->selectedOption = null;
|
|
}
|
|
|
|
$this->cont = $consulta_cuentas->toArray();
|
|
return view('livewire.show-planes', [
|
|
'cuentas' => $consulta_cuentas,
|
|
'servicios' => Servicio::where('estado', 'activo')->get(),
|
|
'roles' => Role::get()
|
|
]);
|
|
}
|
|
|
|
//modal Ver
|
|
public function ver(Cuentas $cuenta)
|
|
{
|
|
$this->id_cuentaVer = $cuenta->id;
|
|
$this->consulta_verUsuarios = $cuenta;
|
|
$this->nombre_servicios = $this->consulta_verUsuarios->servicio->nombre;
|
|
|
|
$this->ver = true;
|
|
}
|
|
public function openRenovar()
|
|
{
|
|
$id = $this->id_cuentaVer;
|
|
|
|
$renovarCuenta = Cuentas::findOrFail($id);
|
|
$this->usuario_renovar_old = $renovarCuenta->correo;
|
|
$this->usuario_renovar = $renovarCuenta->correo;
|
|
$this->password_renovar = $renovarCuenta->password;
|
|
$this->fecha_inicial_renovar_old = $renovarCuenta->inicio;
|
|
$this->fecha_inicial_renovar = $renovarCuenta->inicio;
|
|
$this->fecha_final_renovar_old = $renovarCuenta->vencimiento;
|
|
$this->fecha_final_renovar = $renovarCuenta->vencimiento;
|
|
$this->estado = $renovarCuenta->estado;
|
|
|
|
$this->renovar = true;
|
|
}
|
|
|
|
public function calcularFechas()
|
|
{
|
|
$this->fecha_inicial_renovar = Carbon::now()->format('Y-m-d');
|
|
$this->fecha_final_renovar = Carbon::now()->addDays(30)->format('Y-m-d');
|
|
}
|
|
|
|
|
|
public function getListeners()
|
|
{
|
|
return [
|
|
'siRenovar',
|
|
'noRenovar',
|
|
];
|
|
}
|
|
public function saveRenovar()
|
|
{
|
|
$correo_existe = Cuentas::where('correo', $this->usuario_renovar)->exists();
|
|
if (!$correo_existe || $this->usuario_renovar_old == $this->usuario_renovar) {
|
|
if ($this->fecha_inicial_renovar_old !== $this->fecha_inicial_renovar || $this->fecha_final_renovar_old !== $this->fecha_final_renovar) {
|
|
$this->alert('warning', '¿Desea también desvincular todos los usuarios de esta cuenta? ', [
|
|
'position' => 'center',
|
|
'timer' => '60000',
|
|
'toast' => false,
|
|
'text' => 'Esta seguro',
|
|
'timerProgressBar' => true,
|
|
'showDenyButton' => true,
|
|
'onDenied' => 'noRenovar',
|
|
'denyButtonText' => 'No, solo renovar',
|
|
'showConfirmButton' => true,
|
|
'onConfirmed' => 'siRenovar',
|
|
'confirmButtonText' => 'Si, liberar cuenta',
|
|
'customClass' => [
|
|
'confirmButton' => 'bg-green-500',
|
|
'denyButton' => 'bg-red-500',
|
|
]
|
|
]);
|
|
} else {
|
|
$cuenta_renovar = Cuentas::find($this->id_cuentaVer);
|
|
|
|
Cuentas::where('id', $this->id_cuentaVer)->update([
|
|
'correo' => $this->usuario_renovar,
|
|
'password' => $this->password_renovar,
|
|
'estado' => $this->estado,
|
|
]);
|
|
|
|
if ($this->usuario_renovar_old !== $this->usuario_renovar) {
|
|
Log_historial::create([
|
|
'user_id' => Auth::user()->id,
|
|
'detalle' => 'El usuario ha editado el usuario de la cuenta ' . $cuenta_renovar->correo
|
|
]);
|
|
}
|
|
$this->alert('success', 'Cuenta actualizada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$this->renovar = false;
|
|
$this->ver = false;
|
|
}
|
|
} else {
|
|
$this->alert('warning', 'Esta cuenta ya existe!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
}
|
|
public function siRenovar()
|
|
{
|
|
$id = $this->id_cuentaVer;
|
|
|
|
$historiales = Historial_cuenta::where('cuenta_id', $id)->get();
|
|
|
|
foreach ($historiales as $historial) {
|
|
$historial->delete();
|
|
}
|
|
|
|
|
|
$cuenta_renovar = Cuentas::find($id);
|
|
|
|
Cuentas::where('id', $id)->update([
|
|
'correo' => $this->usuario_renovar,
|
|
'password' => $this->password_renovar,
|
|
'inicio' => date('Y-m-d', strtotime(strval($this->fecha_inicial_renovar))),
|
|
'vencimiento' => date('Y-m-d', strtotime(strval($this->fecha_final_renovar))),
|
|
'estado' => $this->estado,
|
|
]);
|
|
|
|
|
|
Log_historial::create([
|
|
'user_id' => Auth::user()->id,
|
|
'detalle' => 'El usuario ha renovado y liberado la cuenta ' . $cuenta_renovar->correo
|
|
]);
|
|
|
|
$servicio_id = $cuenta_renovar->servicio->id;
|
|
$servicioPantallas = Servicio::find($servicio_id)->pantallas;
|
|
|
|
if ($this->estado == 'activo') {
|
|
$cuentasPendientes = Pendientes::where('estado', 'pendiente')->where('servicio_id', $servicio_id)->take($servicioPantallas)->get();
|
|
|
|
foreach ($cuentasPendientes as $i => $cuentaPendiente) {
|
|
$servicioName = $cuentaPendiente->servicio->nombre;
|
|
|
|
$historial_cuenta = new Historial_cuenta();
|
|
$historial_cuenta->historial_id = $cuentaPendiente->historial_id;
|
|
$historial_cuenta->cuenta_id = $id;
|
|
$historial_cuenta->perfil = $i + 1;
|
|
$historial_cuenta->save();
|
|
$cuentaPendiente->update(['estado' => 'cerrado']);
|
|
|
|
if ($cuentaPendiente->historial?->vendedor !== null) {
|
|
$cuentaPendiente->historial->vendedor->notify(new NotificacionPendiente($servicioName));
|
|
}
|
|
|
|
$consulta = new Notificacion();
|
|
$consulta->remitente_id = Auth::user()->id;
|
|
$consulta->destinatario_id = $historial_cuenta->historial->vendedor_id;
|
|
$consulta->titulo = "Licencia asignada para $servicioName";
|
|
$consulta->descripcion = 'La licencia de ' . $servicioName . ' ha sido activada y ahora puedes utilizar tu cuenta sin problemas. Tus credenciales son: correo: ' . $historial_cuenta->cuenta->correo . ' contraseña: ' . $historial_cuenta->cuenta->password;
|
|
$consulta->estado = true;
|
|
$consulta->todos = false;
|
|
$consulta->save();
|
|
|
|
$logsGeneral = new Log_general();
|
|
$logsGeneral->user_id = $historial_cuenta->historial->vendedor_id;
|
|
$logsGeneral->detalle = 'Se asigno una cuenta ' . $servicioName . ', con credenciales: ' . $historial_cuenta->cuenta->correo . ' contraseña: ' . $historial_cuenta->cuenta->password . ' Que se encontraba en estado pendiente';
|
|
$logsGeneral->save();
|
|
}
|
|
}
|
|
|
|
$this->alert('success', 'Cuenta renovada y liberada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->renovar = false;
|
|
$this->ver = false;
|
|
}
|
|
public function noRenovar()
|
|
{
|
|
if (is_string($this->fecha_inicial_renovar)) {
|
|
$this->fecha_inicial_renovar = \Carbon\Carbon::createFromFormat('Y-m-d', $this->fecha_inicial_renovar);
|
|
}
|
|
if (is_string($this->fecha_final_renovar)) {
|
|
$this->fecha_final_renovar = \Carbon\Carbon::createFromFormat('Y-m-d', $this->fecha_final_renovar);
|
|
}
|
|
// Ahora puedes calcular la diferencia en días
|
|
$this->tiempo_usuario = $this->fecha_final_renovar->diffInDays($this->fecha_inicial_renovar);
|
|
|
|
$id = $this->id_cuentaVer;
|
|
//dd( date('Y-m-d', strtotime(strval($this->fecha_final_renovar))));
|
|
// Corrección 2: Verifica que la cuenta exista antes de intentar actualizarla
|
|
$cuenta = Cuentas::find($id);
|
|
if ($cuenta) {
|
|
$cuenta->update([
|
|
'correo' => $this->usuario_renovar,
|
|
'password' => $this->password_renovar,
|
|
'inicio' => date('Y-m-d', strtotime(strval($this->fecha_inicial_renovar))),
|
|
'vencimiento' => date('Y-m-d', strtotime(strval($this->fecha_final_renovar))),
|
|
'estado' => $this->estado,
|
|
]);
|
|
|
|
$id_historiales = Historial_cuenta::where('cuenta_id', $id)->pluck('historial_id')->toArray();
|
|
|
|
$historiales = Historiale::whereIn('id', $id_historiales)->get();
|
|
|
|
foreach ($historiales as $historial) {
|
|
|
|
// Corrección 3: Usa la fecha final original del historial para calcular la nueva fecha final
|
|
$nueva_fecha_final = \Carbon\Carbon::parse($historial->fecha_final)->addDays($this->tiempo_usuario)->format('Y-m-d');
|
|
|
|
$historial_guardar = new Historiale;
|
|
$historial_guardar->fecha_inicio = $historial->fecha_final;
|
|
$historial_guardar->fecha_final = $nueva_fecha_final;
|
|
$historial_guardar->valor = 0;
|
|
$historial_guardar->tipo_pago = 'manual';
|
|
$historial_guardar->estado = 'activo';
|
|
$historial_guardar->vendedor_id = $historial->vendedor_id;
|
|
$historial_guardar->tarifa_id = $historial->tarifa_id;
|
|
$historial_guardar->cliente_id = $historial->cliente_id;
|
|
$historial_guardar->cantidad = 1;
|
|
$historial_guardar->nombre_cliente = $historial->nombre_cliente;
|
|
$historial_guardar->utilidad = 0;
|
|
$historial_guardar->save();
|
|
|
|
// Corrección 4: Verifica si el historial se guardó correctamente antes de actualizar Historial_cuenta
|
|
if ($historial_guardar->id) {
|
|
Historial_cuenta::where('cuenta_id', $id)->update(['historial_id' => $historial_guardar->id]);
|
|
}
|
|
|
|
$servicioNombre = $historial_guardar->tarifa->servicio->nombre;
|
|
|
|
// Corrección 5: Verifica si el usuario está autenticado antes de acceder a Auth::user()
|
|
if (Auth::check()) {
|
|
$logsGeneral = new Log_general();
|
|
$logsGeneral->user_id = Auth::user()->id;
|
|
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha renovado el servicio:' . $servicioNombre . ' con cuenta ' . $cuenta->correo . ' para el cliente: ' . $historial->cliente->name . ' - ' . $historial->cliente->email;
|
|
$logsGeneral->save();
|
|
}
|
|
}
|
|
|
|
// Corrección 6: Verifica si el usuario está autenticado antes de acceder a Auth::user()
|
|
if (Auth::check()) {
|
|
Log_historial::create([
|
|
'user_id' => Auth::user()->id,
|
|
'detalle' => 'El usuario ha renovado la cuenta ' . $cuenta->correo
|
|
]);
|
|
}
|
|
|
|
// Corrección 7: Asumiendo que el método alert() y las variables de estado están correctamente implementadas
|
|
$this->alert('success', 'Cuenta renovada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->renovar = false;
|
|
$this->ver = false;
|
|
}
|
|
}
|
|
|
|
/* ------------- CREAR ------------- */
|
|
public function createNewCuenta()
|
|
{
|
|
if ($consulta_cuenta = Cuentas::where('correo', $this->usuario_newCuenta)->exists()) {
|
|
$this->alert('warning', 'La cuenta ya existe', [
|
|
'position' => 'top'
|
|
]);
|
|
} else {
|
|
$cuenta = new Cuentas;
|
|
$cuenta->servicio_id = $this->servicio_newCuenta;
|
|
$cuenta->correo = $this->usuario_newCuenta;
|
|
$cuenta->password = $this->password_newCuenta;
|
|
$cuenta->inicio = date('Y-m-d', strtotime(strval($this->fecha_inicial)));
|
|
$cuenta->vencimiento = date('Y-m-d', strtotime(strval($this->fecha_final)));
|
|
$cuenta->estado = $this->estado;
|
|
$cuenta->save();
|
|
|
|
Log_historial::create([
|
|
'user_id' => Auth::user()->id,
|
|
'detalle' => 'El usuario ha creado la cuenta ' . $cuenta->correo
|
|
]);
|
|
|
|
if ($this->estado == 'activo') {
|
|
$servicioPantallas = Servicio::find($this->servicio_newCuenta)->pantallas;
|
|
|
|
$cuentasPendientes = Pendientes::where('estado', 'pendiente')->where('servicio_id', $this->servicio_newCuenta)->take($servicioPantallas)->get();
|
|
|
|
foreach ($cuentasPendientes as $i => $cuentaPendiente) {
|
|
$servicioName = $cuentaPendiente->servicio->nombre;
|
|
|
|
$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']);
|
|
|
|
if ($cuentaPendiente->historial?->vendedor !== null) {
|
|
$cuentaPendiente->historial->vendedor->notify(new NotificacionPendiente($servicioName));
|
|
}
|
|
|
|
$consulta = new Notificacion();
|
|
$consulta->remitente_id = Auth::user()->id;
|
|
$consulta->destinatario_id = $historial_cuenta->historial?->vendedor_id ?? null;
|
|
$consulta->titulo = "Licencia asignada para $servicioName";
|
|
$consulta->descripcion = 'La licencia de ' . $servicioName . ' ha sido activada y ahora puedes utilizar tu cuenta sin problemas. Tus credenciales son: correo: ' . $historial_cuenta->cuenta->correo . ' contraseña: ' . $historial_cuenta->cuenta->password;
|
|
$consulta->estado = true;
|
|
$consulta->todos = false;
|
|
$consulta->save();
|
|
|
|
if ($cuentaPendiente->historial?->vendedor !== null) {
|
|
$logsGeneral = new Log_general();
|
|
$logsGeneral->user_id = $historial_cuenta->historial->vendedor_id;
|
|
$logsGeneral->detalle = 'Se asigno una cuenta ' . $servicioName . ', con credenciales: ' . $historial_cuenta->cuenta->correo . ' contraseña: ' . $historial_cuenta->cuenta->password . ' Que se encontraba en estado pendiente';
|
|
$logsGeneral->save();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
$this->alert('success', 'Cuenta creada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->limpiarInputNewCuenta();
|
|
|
|
$this->newCuenta = false;
|
|
}
|
|
}
|
|
public function createServicio()
|
|
{
|
|
$url_foto = $this->photo->store('photos');
|
|
$url_foto_inicio = $this->photo_inicio->store('photos');
|
|
|
|
$servicio = new Servicio;
|
|
$servicio->nombre = $this->nombre_servicio;
|
|
$servicio->descripcion = $this->descripcion_servicio;
|
|
$servicio->estado = $this->estado_servicio;
|
|
$servicio->color_fondo = $this->color_servicio;
|
|
$servicio->pantallas = '6';
|
|
$servicio->img = $url_foto;
|
|
$servicio->img_inicio = $url_foto_inicio;
|
|
$servicio->save();
|
|
|
|
$this->alert('success', 'Servicio creado correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->serviciosModal = false;
|
|
}
|
|
public function verObservacion($id)
|
|
{
|
|
$verObservacion = Historiale::findOrFail($id);
|
|
|
|
$this->id_observacion = $id;
|
|
$this->observaciones = $verObservacion->observaciones;
|
|
}
|
|
public function update()
|
|
{
|
|
Historiale::where('id', $this->id_observacion)->update(['observaciones' => $this->observaciones,]);
|
|
|
|
$this->alert('success', 'Observacion añadida correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->observacionesm = false;
|
|
}
|
|
|
|
public function verObservacion1($id)
|
|
{
|
|
$verObservacion = Cuentas::findOrFail($id);
|
|
|
|
$this->id_observacion1 = $id;
|
|
|
|
$this->observaciones1 = $verObservacion->observaciones;
|
|
}
|
|
public function update_observaciones1()
|
|
{
|
|
Cuentas::where('id', $this->id_observacion1)->update(['observaciones' => $this->observaciones1,]);
|
|
|
|
$this->alert('success', 'Observacion añadida correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->observacionesm1 = false;
|
|
}
|
|
|
|
public function delete_cuenta($id)
|
|
{
|
|
$this->delete_cuenta = $id;
|
|
|
|
$this->notaEliminarModal = true;
|
|
}
|
|
|
|
public function confirm_delete_cuenta()
|
|
{
|
|
$this->validate([
|
|
'nota_eliminacion' => 'required',
|
|
], [
|
|
'nota_eliminacion.required' => 'La nota es obligatoria',
|
|
]);
|
|
|
|
$cuenta = Cuentas::findOrFail($this->delete_cuenta);
|
|
|
|
Log_historial::create([
|
|
'user_id' => Auth::user()->id,
|
|
'detalle' => 'El usuario ha eliminado la cuenta ' . $cuenta->correo . ' nota de eliminacion: ' . $this->nota_eliminacion
|
|
]);
|
|
|
|
$cuenta->delete();
|
|
$this->notaEliminarModal = false;
|
|
|
|
$this->alert('success', 'Cuenta eliminada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
|
|
public function eliminarCuentas()
|
|
{
|
|
$this->notaEliminarVariosModal = true;
|
|
}
|
|
|
|
public function confirm_delete_cuentas()
|
|
{
|
|
$this->validate([
|
|
'nota_eliminacion' => 'required',
|
|
], [
|
|
'nota_eliminacion.required' => 'La nota es obligatoria',
|
|
]);
|
|
|
|
$cuentas = Cuentas::whereIn('id', $this->delete_1)->get();
|
|
if ($cuentas) {
|
|
foreach ($cuentas as $cuenta) {
|
|
Log_historial::create([
|
|
'user_id' => Auth::user()->id,
|
|
'detalle' => 'El usuario ha eliminado la cuenta ' . $cuenta->correo . ' nota de eliminacion: ' . $this->nota_eliminacion
|
|
]);
|
|
}
|
|
}
|
|
$consulta_cuentas = Cuentas::whereIn('id', $this->delete_1)->delete();
|
|
|
|
$this->delete_1 = [];
|
|
$this->notaEliminarVariosModal = false;
|
|
|
|
$this->alert('success', 'Cuentas eliminadas correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
/* ------------- ACTUALIZAR ------------- */
|
|
public function updateTarifas()
|
|
{
|
|
$consulta_existe = Tarifas::where('pantallas', $this->pantallas_modTarifas)
|
|
->where('servicio_id', $this->servicio_modTarifas)
|
|
->where('rol_id', $this->roles_modTarifas)
|
|
->where('dias', $this->diasTarifa)
|
|
->exists();
|
|
|
|
if ($consulta_existe) {
|
|
$this->alert('warning', '¡Esta tarifa ya existe!', [
|
|
'position' => 'top'
|
|
]);
|
|
} elseif (empty($this->servicio_modTarifas)) {
|
|
$this->alert('warning', '¡Seleccione un servicio!', [
|
|
'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 Tarifas;
|
|
$tarifa->pantallas = $this->pantallas_modTarifas;
|
|
$tarifa->dias = $this->diasTarifa;
|
|
$tarifa->valor = $this->precio_modTarifas;
|
|
$tarifa->utilidad = $this->utilidad;
|
|
$tarifa->estado = $this->estadoTarifa ? 'activo' : 'inactivo';
|
|
$tarifa->servicio_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)
|
|
{
|
|
Tarifas::where('id', $id)->delete();
|
|
|
|
$this->alert('success', 'Tarifas eliminada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
|
|
/* ------------- LIMPIAR CAMPOS ------------- */
|
|
public function limpiarInputObservaciones()
|
|
{
|
|
$this->observaciones = '';
|
|
}
|
|
public function limpiarInputObservaciones1()
|
|
{
|
|
$this->observaciones1 = '';
|
|
$this->observacionesm1 = false;
|
|
}
|
|
public function limpiarInputNewCuenta()
|
|
{
|
|
$this->servicio_newCuenta = null;
|
|
$this->usuario_newCuenta = '';
|
|
$this->password_newCuenta = '';
|
|
$this->fecha_inicial = null;
|
|
$this->fecha_final = null;
|
|
|
|
$this->newCuenta = false;
|
|
}
|
|
public function limpiarInputServiciosModal()
|
|
{
|
|
$this->nombre_servicio = '';
|
|
$this->descripcion_servicio = '';
|
|
$this->estado_servicio = null;
|
|
$this->color_servicio = null;
|
|
}
|
|
|
|
public function openAdd()
|
|
{
|
|
$this->addUser = true;
|
|
}
|
|
public function saveUser()
|
|
{
|
|
|
|
$this->validate([
|
|
'tarifa' => 'required',
|
|
|
|
]);
|
|
|
|
|
|
$tarifa = json_decode($this->tarifa, true);
|
|
|
|
|
|
// dd($tarifa)
|
|
|
|
//dd($this->tarifa['dias']);
|
|
$usuario_existe = User::firstWhere('email', $this->searchTerm)->id ?? null;
|
|
$usuario_id = $usuario_existe;
|
|
|
|
if (is_null($usuario_existe)) {
|
|
$rol_id = Role::where('nombre', 'cliente')->value('id');
|
|
$usuario = new User;
|
|
$usuario->name = 'AssignedUser';
|
|
$usuario->email = $this->searchTerm;
|
|
$usuario->password = '123456';
|
|
$usuario->celular = '+57';
|
|
$usuario->rol_id = $rol_id;
|
|
$usuario->save();
|
|
|
|
$usuario_id = $usuario->id;
|
|
}
|
|
|
|
// dd($this->tarifa);
|
|
|
|
$historial = new Historiale;
|
|
$historial->fecha_inicio = Carbon::now();
|
|
$historial->fecha_final = Carbon::now()->addDay($tarifa['dias']);
|
|
$historial->valor = '0';
|
|
$historial->tipo_pago = 'manual';
|
|
$historial->estado = 'activo';
|
|
$historial->vendedor_id = Auth()->user()->id;
|
|
$historial->tarifa_id = $tarifa['id'];
|
|
$historial->cliente_id = $usuario_id;
|
|
$historial->cantidad = $tarifa['pantallas'];
|
|
$historial->save();
|
|
|
|
|
|
$consultaHC = Historial_cuenta::where('cuenta_id', $this->consulta_verUsuarios->id)->orderBy('created_at', 'desc')->first()->perfil ?? 0;
|
|
|
|
if ($this->consulta_verUsuarios->estado == 'pendiente') { // completa
|
|
$perfiles = $this->consulta_verUsuarios->servicio->completa;
|
|
|
|
for ($i = 0; $i < $perfiles; $i++) {
|
|
$historial_cuenta = new Historial_cuenta;
|
|
$historial_cuenta->cuenta_id = $this->consulta_verUsuarios->id;
|
|
$historial_cuenta->historial_id = $historial->id;
|
|
$historial_cuenta->perfil = $i + 1;
|
|
$historial_cuenta->save();
|
|
}
|
|
|
|
$this->alert('success', 'Usuario añadido correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->addUser = false;
|
|
$this->consulta_verUsuarios = Cuentas::with('historiales', 'servicio')->where('id', $this->id_cuentaVer)->first();
|
|
}
|
|
|
|
if ($this->consulta_verUsuarios->estado == 'activo') { // pantallas
|
|
$perfiles = $this->consulta_verUsuarios->servicio->pantallas;
|
|
|
|
if ($consultaHC) {
|
|
if ($consultaHC < $perfiles) {
|
|
$historial_cuenta = new Historial_cuenta;
|
|
$historial_cuenta->cuenta_id = $this->consulta_verUsuarios->id;
|
|
$historial_cuenta->historial_id = $historial->id;
|
|
$historial_cuenta->perfil = $consultaHC + 1;
|
|
$historial_cuenta->save();
|
|
|
|
$this->alert('success', 'Usuario añadido correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->addUser = false;
|
|
$this->consulta_verUsuarios = Cuentas::with('historiales', 'servicio')->where('id', $this->id_cuentaVer)->first();
|
|
} else {
|
|
$historial->delete();
|
|
$this->alert('warning', 'Ya haz alcanzado el maximo de perfiles!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
} else {
|
|
$historial_cuenta = new Historial_cuenta;
|
|
$historial_cuenta->cuenta_id = $this->consulta_verUsuarios->id;
|
|
$historial_cuenta->historial_id = $historial->id;
|
|
$historial_cuenta->perfil = $consultaHC + 1;
|
|
$historial_cuenta->save();
|
|
|
|
$this->alert('success', 'Usuario añadido correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
|
|
$logsGeneral = new Log_general();
|
|
$logsGeneral->user_id = Auth::user()->id;
|
|
$logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha añadido el usuario ' . $this->searchTerm . ' a una cuenta ';
|
|
// . ' a la cuenta ' . $this->consulta_verUsuarios->name . ' - ' . $this->consulta_verUsuarios->email;
|
|
$logsGeneral->save();
|
|
|
|
$this->addUser = false;
|
|
$this->consulta_verUsuarios = Cuentas::with('historiales', 'servicio')->where('id', $this->id_cuentaVer)->first();
|
|
}
|
|
}
|
|
|
|
public function editar($tarifa)
|
|
{
|
|
//dd($tarifa);
|
|
$this->editarTarifaRol = true;
|
|
|
|
$this->informacionTarifaRol = $tarifa;
|
|
|
|
$this->roles_modTarifas = $tarifa['rol_id'];
|
|
$this->pantallas_modTarifas = $tarifa['pantallas'];
|
|
$this->precio_modTarifas = $tarifa['valor'];
|
|
$this->utilidad = $tarifa['utilidad'];
|
|
$this->diasTarifa = $tarifa['dias'];
|
|
$this->estadoTarifa = $tarifa['estado'] == 'activo' ? 1 : 0;
|
|
$this->servicio_modTarifas = $tarifa['servicio_id'];
|
|
}
|
|
|
|
public function actualizarTarifaRol($id)
|
|
{
|
|
//dd($id);
|
|
$validateData = $this->validate([
|
|
'roles_modTarifas' => 'required',
|
|
'pantallas_modTarifas' => 'required',
|
|
'precio_modTarifas' => 'required',
|
|
'utilidad' => 'required',
|
|
'diasTarifa' => 'required',
|
|
'estadoTarifa' => 'required'
|
|
]);
|
|
$consulta = Tarifas::find($id)->update([
|
|
'pantallas' => $this->pantallas_modTarifas,
|
|
'dias' => $this->diasTarifa,
|
|
'valor' => $this->precio_modTarifas,
|
|
'utilidad' => $this->utilidad,
|
|
'estado' => $this->estadoTarifa == 1 ? 'activo' : 'inactivo',
|
|
'rol_id' => $this->roles_modTarifas,
|
|
'servicio_id' => $this->servicio_modTarifas
|
|
|
|
]);
|
|
$this->alert('success', 'Tarifa actualizada correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->editarTarifaRol = false;
|
|
}
|
|
|
|
public function seleccionarTodos()
|
|
{
|
|
|
|
if (count($this->delete_1) == 0) {
|
|
$this->delete_1 = array_column($this->cont['data'], 'id');
|
|
} else {
|
|
$this->delete_1 = [];
|
|
}
|
|
}
|
|
}
|