From 7db131cd672c5e8768ff34e0c8211b6b69ccb88a Mon Sep 17 00:00:00 2001 From: Felipe Date: Tue, 17 Oct 2023 18:02:03 -0500 Subject: [PATCH] update --- app/Http/Livewire/ShowPlanes.php | 207 ++++++++++++++++--------------- 1 file changed, 109 insertions(+), 98 deletions(-) diff --git a/app/Http/Livewire/ShowPlanes.php b/app/Http/Livewire/ShowPlanes.php index 10601bd..d3034b0 100644 --- a/app/Http/Livewire/ShowPlanes.php +++ b/app/Http/Livewire/ShowPlanes.php @@ -6,6 +6,7 @@ use App\Models\Cuentas; use App\Models\Historiale; use App\Models\Historial_cuenta; use App\Models\Log_general; +use App\Models\Pendientes; use App\Models\Role; use App\Models\Servicio; use App\Models\Tarifas; @@ -81,14 +82,14 @@ class ShowPlanes extends Component public $photo; public $photo_inicio; - public $estado='activo'; + public $estado = 'activo'; public $completa; public $pantallas; public $venceHoy; public $activoHoy; public $libres = null; - public $delete_1=['1']; + public $delete_1 = ['1']; public $tarifas; public $addUser; public $usuarios; @@ -103,51 +104,50 @@ class ShowPlanes extends Component public $editarTarifaRol = false; - + public function render() { $this->fechaActual = date('Y-m-d'); - + $consulta_tarifas = Tarifas::take(1); - if(empty($this->fecha_inicial)){ - $this->fecha_inicial=Carbon::now()->format('Y-m-d'); - $this->fecha_final=Carbon::now()->addDay(30)->format('Y-m-d'); + if (empty($this->fecha_inicial)) { + $this->fecha_inicial = Carbon::now()->format('Y-m-d'); + $this->fecha_final = Carbon::now()->addDay(30)->format('Y-m-d'); } if ($this->servicio_modTarifas) { $this->tarifas = Tarifas::with('rol') - ->where('servicio_id', $this->servicio_modTarifas) - ->where('estado', 'activo') - ->get(); - + ->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 = 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'); - }); + ->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) { @@ -157,23 +157,23 @@ class ShowPlanes extends Component ->havingRaw('COUNT(*) > 1'); }); } - + $consulta_cuentas = $consulta_cuentas->paginate($this->entradas); - - if ( $this->searchTerm) { + + if ($this->searchTerm) { $this->usuarios = User::where('email', 'LIKE', '%' . $this->searchTerm . '%')->get(); if ($this->selectedOption) { $this->searchTerm = $this->selectedOption; } - }else{ + } else { $this->selectedOption = null; } - + return view('livewire.show-planes', [ 'cuentas' => $consulta_cuentas, - 'servicios' => Servicio::where('estado','activo')->get(), + 'servicios' => Servicio::where('estado', 'activo')->get(), 'roles' => Role::get() ]); } @@ -182,7 +182,7 @@ class ShowPlanes extends Component public function ver($id, $nom) { $this->id_cuentaVer = $id; - $this->consulta_verUsuarios = Cuentas::with('historiales','servicio')->where('id', $id)->first(); + $this->consulta_verUsuarios = Cuentas::with('historiales', 'servicio')->where('id', $id)->first(); //dd($this->consulta_verUsuarios); $this->nombre_servicios = $nom; } @@ -222,21 +222,22 @@ 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', - ]); + // $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()){ + + + if ($consulta_cuenta = Cuentas::where('correo', $this->usuario_newCuenta)->exists()) { $this->alert('warning', 'La cuenta ya existe', [ 'position' => 'top' ]); - }else{ + } else { $cuenta = new Cuentas; $cuenta->servicio_id = $this->servicio_newCuenta; $cuenta->correo = $this->usuario_newCuenta; @@ -245,17 +246,30 @@ class ShowPlanes extends Component $cuenta->vencimiento = date('Y-m-d', strtotime(strval($this->fecha_final))); $cuenta->estado = $this->estado; $cuenta->save(); - + + 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) { + $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']); + } + } + $this->alert('success', 'Cuenta creada correctamente!', [ 'position' => 'top' ]); - + $this->limpiarInputNewCuenta(); - + $this->newCuenta = false; } - - } public function createServicio() { @@ -354,11 +368,11 @@ class ShowPlanes extends Component 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(); - + ->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' @@ -385,21 +399,19 @@ class ShowPlanes extends Component $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(); + Tarifas::where('id', $id)->delete(); $this->alert('success', 'Tarifas eliminada correctamente!', [ 'position' => 'top' @@ -434,23 +446,23 @@ class ShowPlanes extends Component $this->color_servicio = null; } - public function eliminarCuentas(){ - - $consulta_cuentas = Cuentas::whereIn('id',$this->delete_1)->delete(); + public function eliminarCuentas() + { + $consulta_cuentas = Cuentas::whereIn('id', $this->delete_1)->delete(); } - public function openAdd(){ - + public function openAdd() + { + $this->addUser = true; - - } - public function saveUser(){ + public function saveUser() + { $this->validate([ 'tarifa' => 'required', - + ]); @@ -458,9 +470,9 @@ class ShowPlanes extends Component //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'); + $rol_id = Role::where('nombre', 'cliente')->value('id'); $usuario = new User; $usuario->name = 'AssignedUser'; $usuario->email = $this->searchTerm; @@ -472,7 +484,7 @@ class ShowPlanes extends Component $usuario_id = $usuario->id; } - + $historial = new Historiale; $historial->fecha_inicio = Carbon::now(); $historial->fecha_final = Carbon::now()->addDay($tarifa['dias']); @@ -480,13 +492,13 @@ class ShowPlanes extends Component $historial->tipo_pago = 'manual'; $historial->estado = 'activo'; $historial->vendedor_id = Auth()->user()->id; - $historial->tarifa_id = $tarifa['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)->where('perfil',$this->perfil)->exists(); - + $consultaHC = Historial_cuenta::where('cuenta_id', $this->consulta_verUsuarios->id)->where('perfil', $this->perfil)->exists(); + if (!$consultaHC) { $historial_cuenta = new Historial_cuenta; $historial_cuenta->cuenta_id = $this->consulta_verUsuarios->id; @@ -499,20 +511,19 @@ class ShowPlanes extends Component ]); $this->addUser = false; - $this->consulta_verUsuarios = Cuentas::with('historiales','servicio')->where('id', $this->id_cuentaVer)->first(); - - }else{ + $this->consulta_verUsuarios = Cuentas::with('historiales', 'servicio')->where('id', $this->id_cuentaVer)->first(); + } else { $historial->delete(); $this->alert('warning', 'El perfil ya se encuentra en uso!', [ 'position' => 'top' ]); - } - + } } - public function editar($tarifa){ - //dd($tarifa); - $this->editarTarifaRol=true; + public function editar($tarifa) + { + //dd($tarifa); + $this->editarTarifaRol = true; $this->informacionTarifaRol = $tarifa; $this->roles_modTarifas = $tarifa['rol_id']; @@ -524,22 +535,23 @@ class ShowPlanes extends Component $this->servicio_modTarifas = $tarifa['servicio_id']; } - public function actualizarTarifaRol($id){ + public function actualizarTarifaRol($id) + { //dd($id); $validateData = $this->validate([ - 'roles_modTarifas'=> 'required', + 'roles_modTarifas' => 'required', 'pantallas_modTarifas' => 'required', - 'precio_modTarifas'=> 'required', - 'utilidad'=> 'required', + 'precio_modTarifas' => 'required', + 'utilidad' => 'required', 'diasTarifa' => 'required', - 'estadoTarifa'=> '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, + 'valor' => $this->precio_modTarifas, + 'utilidad' => $this->utilidad, + 'estado' => $this->estadoTarifa, 'rol_id' => $this->roles_modTarifas, 'servicio_id' => $this->servicio_modTarifas @@ -549,6 +561,5 @@ class ShowPlanes extends Component ]); $this->editarTarifaRol = false; - } }