From 94b16f95688769b4a85f49da86d83849ab047304 Mon Sep 17 00:00:00 2001 From: lizandrogd <77708265+lizandrogd@users.noreply.github.com> Date: Sun, 5 May 2024 02:17:54 -0500 Subject: [PATCH] Update ShowServicios.php --- app/Http/Livewire/ShowServicios.php | 219 +++++++++++++++------------- 1 file changed, 119 insertions(+), 100 deletions(-) diff --git a/app/Http/Livewire/ShowServicios.php b/app/Http/Livewire/ShowServicios.php index 4b7555f..5e2008d 100755 --- a/app/Http/Livewire/ShowServicios.php +++ b/app/Http/Livewire/ShowServicios.php @@ -56,13 +56,6 @@ class ShowServicios extends Component public $fecha; public $msj; public $mensaje; - //// - public $tarifas; - public $servicio_t; - public $cuentas_completas; - public $cuentas_completas_tomadas; - public $cuentas_creadas; - public $cuentas_tomadas; public $tiempo_tarjetaPlan; public $verServ; public $fecha_final_1; @@ -81,9 +74,9 @@ class ShowServicios extends Component { $this->fechaHoy = Carbon::now(); $this->fechaActual = Carbon::parse($this->fechaHoy); - $date = $this->fechaHoy->subDay(30); + $date = $this->fechaHoy->subDay(15); $this->fecha = $date->format('Y-m-d'); - + //dd($this->fecha); $this->usuario_actual = Auth::user(); if (empty($this->nombre_tarjetaPlan) && empty($this->email_tarjetaPlan)) { @@ -93,10 +86,9 @@ class ShowServicios extends Component $this->celular_tarjetaPlan = $this->usuario_actual->celular; } if (!empty($this->tarifa_id)) { - $this->paquetesDisponibles(); + $this->cargartarjetaPlan($this->tarifa_id); } - $this->msj = Configuracione::orderby('id', 'desc')->select('msj_compra', 'msj_wp', 'clave_1', 'clave_2', 'clave_3', 'clave_4', 'clave_5', 'clave_6', 'iptv')->first(); $consulta_servicios = Servicio::where('estado', 'activo') @@ -119,92 +111,91 @@ class ShowServicios extends Component ]); } + //Al abrir la tarjeta del servicio public function cargartarjetaPlan($id) { - $this->tarifas = Tarifas::with('servicio') - ->where('id', $id) - ->first(); - $this->servicio_t = $this->tarifas->servicio; + //Buscar tarifas por el id + $tarifa_seleccionada = Tarifas::find($id); + //dd($tarifa_seleccionada); + //Traer el servicio relacionado con esa tarifa + $servicio_tarifa = $tarifa_seleccionada->servicio; + //INFORMACIÓN DEL SERVICIO + $this->servicio_id = $servicio_tarifa->id; //Id + $this->servicio_tarjetaPlan = $servicio_tarifa->nombre; //Nombre + $this->tiempo_tarjetaPlan = $servicio_tarifa->por_tiempo; //Si es por tiempo o por pantalla + $this->mensaje = $servicio_tarifa->mensaje; //Mensaje + $this->colorFondo_tarjetaPlan = $servicio_tarifa->color_fondo; //Color de fondo - //Servicio por pantallas - $this->cuentas_creadas = Cuentas::whereDate('inicio', '>=', $this->fecha) - ->where('servicio_id', $this->servicio_t->id) - ->where('estado', 'activo') - ->get(); + //INFORMACIÓN DE LA TARJETA + $this->tarifa_id = $id; //El id de la tarifa + $this->pantallas_tarjetaPlan = $tarifa_seleccionada->pantallas; //Numero de pantallas de la tarifa + $this->dias_tarjetaPlan = $tarifa_seleccionada->dias; // Numero de dias de la tarifa + //Se añade a hoy la cantidad de dias de la tarifa ejem: 30 - 15. Da 15 despues de hoy. + $this->fecha_final_1 = Carbon::now()->addDays($tarifa_seleccionada->dias - 15); + //Se añade a hoy la cantidad de dias de la tarifa ejem: 30 + 15. Da 45 despues de hoy. + $this->fecha_final_2 = Carbon::now()->addDays($tarifa_seleccionada->dias + 15); - $this->cuentas_tomadas = Historial_cuenta::whereIn('cuenta_id', $this->cuentas_creadas - ->pluck('id')) - ->count(); - - $this->servicio_id = $this->servicio_t->id; - $this->tarifa_id = $id; - $this->servicio_tarjetaPlan = $this->servicio_t->nombre; - $this->tiempo_tarjetaPlan = $this->servicio_t->por_tiempo; - $this->pantallas_tarjetaPlan = $this->tarifas->pantallas; - $this->dias_tarjetaPlan = $this->tarifas->dias; - $this->fecha_final_1 = Carbon::now()->addDays($this->tarifas->dias - 15); - $this->fecha_final_2 = Carbon::now()->addDays($this->tarifas->dias + 15); - $this->mensaje = $this->servicio_t->mensaje; - - - //Servicio por cuenta completa - $this->cuentas_completas = Cuentas::whereDate('inicio', '>=', $this->fecha) - ->whereDate('vencimiento', '>=', $this->fecha_final_1) - ->whereDate('vencimiento', '<=', $this->fecha_final_2) - ->where('servicio_id', $this->tarifas->servicio->id) - ->where('estado', 'pendiente') - ->get(); - $this->cuentas_completas_tomadas = Historial_cuenta::whereIn('cuenta_id', $this->cuentas_completas->pluck('id')) - ->count(); + //Traer la tarifa personalizada del usuario para esta tarifa. $usuario_tarifa = Usuario_tarifa::where('usuario_id', auth()->user()->id) ->where('tarifa_id', $id) ->first(); - $this->valor_tarjetaPlan = ($usuario_tarifa) ? $usuario_tarifa->precio : $this->tarifas->valor; - $this->utilidad = ($usuario_tarifa) ? $usuario_tarifa->utilidad : $this->tarifas->utilidad; + //Si tiene tarifa personalizada toma el precio, si no tiene da el valor de la tarifa. + $this->valor_tarjetaPlan = ($usuario_tarifa) ? $usuario_tarifa->precio : $tarifa_seleccionada->valor; + //Si tiene tarifa personalizada toma la utilidad, si no tiene toma la utilidad de la tarifa. + $this->utilidad = ($usuario_tarifa) ? $usuario_tarifa->utilidad : $tarifa_seleccionada->utilidad; + //Fecha de vencimiento del servicio. + $this->fechaFinal = $this->fechaActual->addDay($tarifa_seleccionada->dias); - $this->colorFondo_tarjetaPlan = $this->servicio_t->color_fondo; - $this->fechaFinal = $this->fechaActual->addDay($this->tarifas->dias); + //Cantidad de pantallas solicitadas es igual a la completa del servicio. + if ($tarifa_seleccionada->pantallas == $servicio_tarifa->completa) { + //Traer todas las cuentas completas 'pendiente' de los ultimos 15 dias. solo disponibles + $this->disponibles = Cuentas::whereDate('inicio', '>=', $this->fecha) + ->whereDate('vencimiento', '>=', $this->fecha_final_1) + ->whereDate('vencimiento', '<=', $this->fecha_final_2) + ->where('servicio_id', $servicio_tarifa->id) + ->where('estado', 'pendiente') + ->doesntHave('perfil') + ->count(); - //Cantidad de paquetes disponibles - $this->paquetesDisponibles(); + } else { + $cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan; + $cantidad_restante = $servicio_tarifa->pantallas - $cantidad_solicitada; + //Traer todas las cuentas completas 'pendiente' de los ultimos 15 dias. solo disponibles + $cuentas_pantallas = Cuentas::whereDate('inicio', '>=', $this->fecha) + ->whereDate('vencimiento', '>=', $this->fecha_final_1) + ->whereDate('vencimiento', '<=', $this->fecha_final_2) + ->where('servicio_id', $servicio_tarifa->id) + ->where('estado', 'activo') + ->with('perfil') + ->whereDoesntHave('perfil', function ($query) use ($cantidad_restante) { + $query->select('cuenta_id') + ->groupBy('cuenta_id') + ->havingRaw("COUNT(*) >= $cantidad_restante"); + }) + ->get(); + //dd($cuentas_pantallas); + $cuentas_pantallas_tomadas = Historial_cuenta::whereIn('cuenta_id', $cuentas_pantallas + ->pluck('id')) + ->count(); + //Validar si el numero pantallas del servicio es igual o mayor que lo solicitado + if ($servicio_tarifa->pantallas >= $cantidad_solicitada) { + //Cantidad de cuentas restantes * cantidad de pantallas del servicio / en las pantalla de la tarifa. + $this->disponibles = (($cuentas_pantallas->count() * $servicio_tarifa->pantallas) - $cuentas_pantallas_tomadas) / $tarifa_seleccionada->pantallas; + } else { + $this->disponibles = $cuentas_pantallas; + } + + } $this->tarjetaPlan = true; } - public function paquetesDisponibles() - { - if ($this->tarifas->pantallas == $this->servicio_t->completa) { - - $this->disponibles = (($this->cuentas_completas->count() * $this->servicio_t->completa) - $this->cuentas_completas_tomadas) / $this->tarifas->pantallas; - $restantes_completas = $this->cuentas_completas->count() - ($this->cuentas_completas_tomadas / $this->servicio_t->completa); - - if ($restantes_completas <= 10) { - //$this->notificacionPocas($this->servicio_t,$restantes_completas); - } - } else { - $cantidad_solicitada = (int) $this->cantidad_tarjetaPlan * (int) $this->pantallas_tarjetaPlan; - - if ($this->servicio_t->pantallas >= $cantidad_solicitada) { - $this->disponibles = (($this->cuentas_creadas->count() * $this->servicio_t->pantallas) - $this->cuentas_tomadas) / $this->tarifas->pantallas; - $restantes = $this->cuentas_creadas->count() - ($this->cuentas_tomadas / $this->servicio_t->pantallas); - } else { - $this->disponibles = ($this->cuentas_creadas->count() - $this->cuentas_tomadas); - - $restantes = $this->cuentas_creadas->count() - ($this->cuentas_tomadas); - } - - - if ($restantes <= 10) { - //$this->notificacionPocas($this->servicio_t,$restantes); - } - } - } public function notificacionPocas($servicio, $restantes) { @@ -292,9 +283,15 @@ class ShowServicios extends Component ->whereDate('vencimiento', '<=', $this->fecha_final_2) // ->whereDate('vencimiento','>=', $this->fechaFinal) ->Orderby('created_at', 'ASC') - ->withCount('historiales') + //->withCount('historiales') ->where('estado', 'activo') - ->having('historiales_count', '<=', $suma) + //->having('historiales_count', '<=', $suma) + ->with('perfil') + ->whereDoesntHave('perfil', function ($query) use ($suma) { + $query->select('cuenta_id') + ->groupBy('cuenta_id') + ->havingRaw("COUNT(*) >= $suma"); + }) ->get(); // dd($cuenta); } elseif ((int) $pantalla_servicio_completa == (int) $this->pantallas_tarjetaPlan) { @@ -305,9 +302,9 @@ class ShowServicios extends Component ->whereDate('vencimiento', '>=', $this->fecha_final_1) ->whereDate('vencimiento', '<=', $this->fecha_final_2) ->Orderby('created_at', 'ASC') - ->withCount('historiales') + //->withCount('historiales') ->where('estado', 'pendiente') - ->having('historiales_count', '==', 0) + ->doesntHave('perfil') ->get(); } elseif ((int) $pantalla_servicio < (int) $cantidad_solicitada) { $suma2 = $pantalla_servicio - $pantallas; @@ -315,9 +312,15 @@ class ShowServicios extends Component $cuenta = Cuentas::where('servicio_id', $this->servicio_id) ->whereDate('inicio', '>=', $this->fecha) ->Orderby('created_at', 'ASC') - ->withCount('historiales') + //->withCount('historiales') ->where('estado', 'activo') - ->having('historiales_count', '<=', $suma2) + //->having('historiales_count', '<=', $suma2) + ->with('perfil') + ->whereDoesntHave('perfil', function ($query) use ($suma2) { + $query->select('cuenta_id') + ->groupBy('cuenta_id') + ->havingRaw("COUNT(*) >= $suma2"); + }) ->get(); } else { $cuenta = null; @@ -326,7 +329,7 @@ class ShowServicios extends Component for ($h = 0; $h < (int) $cantidad_necesaria; $h++) { if (!empty($cuenta) && isset($cuenta[$h])) { - + $esta_error = false; $cuentasMostar = ''; $perfil = $cuenta[$h]->historiales_count; @@ -344,6 +347,7 @@ class ShowServicios extends Component } } } else { + $esta_error = true; $eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete(); $logsGeneral = new Log_general(); $logsGeneral->user_id = Auth::user()->id; @@ -354,27 +358,42 @@ class ShowServicios extends Component ]); } } - $descuento = Saldo::where('usuario_id', Auth::user()->id)->first(); + if (!$esta_error && !empty($historial->cuentas)) { + $descuento = Saldo::where('usuario_id', Auth::user()->id)->first(); - $saldo_anterior = $descuento->valor ?? 0; - $nuevo_saldo = $saldo_anterior - $total_valor; - $cobro = $descuento->update(['valor' => $nuevo_saldo]); - if ($cobro) { - $historial->update(['estado' => 'activo']); + $saldo_anterior = $descuento->valor ?? 0; + $nuevo_saldo = $saldo_anterior - $total_valor; + $cobro = $descuento->update(['valor' => $nuevo_saldo]); + if ($cobro) { - $logsGeneral = new Log_general(); - $logsGeneral->user_id = Auth::user()->id; - $logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha comprado el servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor; - $logsGeneral->save(); + $historial->update(['estado' => 'activo']); + + $logsGeneral = new Log_general(); + $logsGeneral->user_id = Auth::user()->id; + $logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' ha comprado el servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor; + $logsGeneral->save(); + + + $this->credenciales($historial); + } else { + $logsGeneral = new Log_general(); + $logsGeneral->user_id = Auth::user()->id; + $logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo un error con el cobro del servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor; + $logsGeneral->save(); + $eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete(); + $this->alert('warning', 'Error en el cobro, comuniquese con soporte!', [ + 'position' => 'top' + ]); + } - $this->credenciales($historial); } else { + + $eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete(); $logsGeneral = new Log_general(); $logsGeneral->user_id = Auth::user()->id; - $logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo un error con el cobro del servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor; + $logsGeneral->detalle = 'El usuario ' . Auth::user()->name . ' - ' . Auth::user()->email . ' Tuvo error de "No hay cuentas disponibles" Comprando el servicio ' . $servicioNombre . ' - ' . $cuentasMostar . ' cantidad de ' . $this->cantidad_tarjetaPlan . ' por un valor de $' . $total_valor; $logsGeneral->save(); - $eliminar = Historial_cuenta::where('historial_id', $historial->id)->delete(); - $this->alert('warning', 'Error en el cobro, comuniquese con soporte!', [ + $this->alert('warning', 'No hay cuentas disponibles!', [ 'position' => 'top' ]); } @@ -418,7 +437,7 @@ class ShowServicios extends Component $message = $historial->tarifa->servicio->nombre . '-' . $historial->tarifa->pantallas . 'P. Ver credenciales: ' . $shortURL; $recipients = '+57' . $this->celular_tarjetaPlan; - $this->sendMessage($message, $recipients); + //$this->sendMessage($message, $recipients); } public function sendMessage($message, $recipients)