planes = Tarifas::where('servicio_id', $this->servicio_id)->where('rol_id', Auth::user()->rol->id)->where('estado', 'activo')->get(); if ($this->modTarifas) { $this->tarifas = Tarifas::where('servicio_id', $this->servicio_id)->where('rol_id', $this->rol_id)->where('estado', 'activo')->get(); } if ($this->modTarifasPromo) { $this->tarifas = TarifaPromo::where('promocion_id', $this->promocion_id)->where('rol_id', $this->rol_id)->where('visible', 'activo')->get(); } $this->promociones = TarifaPromo::where('promocion_id', $this->promocion_id)->where('rol_id', Auth::user()->rol->id)->where('visible', 'activo')->get(); return view('livewire.show-tarifas', [ 'roles' => Role::where('subvendedor_id', auth()->user()->id)->get(), 'promo' => Promociones::where('visible', 'true')->get(), 'servicios' => Servicio::where('estado', 'activo')->get(), ]); } public function updatedPlanId($value) { if (!empty($value)) { $planSeleccionado = collect($this->planes)->firstWhere('id', $value); if ($planSeleccionado) { $this->valorBase = $planSeleccionado['valor']; $this->precio = $planSeleccionado['valor']; $this->calcularUtilidad(); } } else { $this->valorBase = null; $this->precio = null; $this->utilidad = null; } } public function updatedPromoId($value) { if (!empty($value)) { $promocionSeleccionada = collect($this->promociones)->firstWhere('id', $value); if ($promocionSeleccionada) { $this->valorBase = $promocionSeleccionada['precio']; $this->precio = $promocionSeleccionada['precio']; $this->calcularUtilidad(); } } else { $this->valorBase = null; $this->precio = null; $this->utilidad = null; } } public function updatedPrecio($value) { $this->calcularUtilidad(); } private function calcularUtilidad() { if (!is_null($this->precio) && !is_null($this->valorBase)) { $this->utilidad = $this->precio - $this->valorBase; } else { $this->utilidad = null; } } public function guardarTarifa() { $planSeleccionado = collect($this->planes)->firstWhere('id', $this->plan_id); if ($planSeleccionado) { $this->validate([ 'precio' => 'required|numeric', 'utilidad' => 'required', 'estado' => 'required', 'servicio_id' => 'required', 'rol_id' => 'required', ], [ 'precio.required' => 'El campo precio es obligatorio', 'utilidad.required' => 'El campo utilidad es obligatorio', 'estado.required' => 'El campo estado es obligatorio', 'servicio_id.required' => 'El campo servicio es obligatorio', 'rol_id.required' => 'El campo rol es obligatorio', ]); if ($this->precio < $this->valorBase) { $this->alert('error', 'El precio no puede ser menor al costo del plan', [ 'position' => 'top' ]); return; } Tarifas::create([ 'pantallas' => $planSeleccionado['pantallas'], 'dias' => $planSeleccionado['dias'], 'valor' => $this->precio, 'utilidad' => $this->utilidad, 'estado' => $this->estado ? 'activo' : 'inactivo', 'servicio_id' => $this->servicio_id, 'rol_id' => $this->rol_id, 'subvendedor_id' => Auth::user()->id, ]); $this->limpiarInputs(); $this->alert('success', '¡Tarifas creada correctamente!', [ 'position' => 'top' ]); } else { $this->alert('error', 'Error inesperado', [ 'position' => 'top' ]); return; } } public function guardarTarifaPromo() { $promocionSeleccionada = collect($this->promociones)->firstWhere('id', $this->promo_id); if ($promocionSeleccionada) { $this->validate([ 'precio' => 'required|numeric', 'utilidad' => 'required', 'estado' => 'required', 'promocion_id' => 'required', 'rol_id' => 'required', ], [ 'precio.required' => 'El campo precio es obligatorio', 'utilidad.required' => 'El campo utilidad es obligatorio', 'estado.required' => 'El campo estado es obligatorio', 'promocion_id.required' => 'El campo servicio es obligatorio', 'rol_id.required' => 'El campo rol es obligatorio', ]); if ($this->precio < $this->valorBase) { $this->alert('error', 'El precio no puede ser menor al costo del plan', [ 'position' => 'top' ]); return; } TarifaPromo::create([ 'utilidad' => $this->utilidad, 'promocion_id' => $this->promocion_id, 'precio' => $this->precio, 'visible' => $this->estado ? 'activo' : 'inactivo', 'rol_id' => $this->rol_id, 'subvendedor_id' => Auth::user()->id, ]); $this->limpiarInputs(); $this->alert('success', '¡Tarifas creada correctamente!', [ 'position' => 'top' ]); } else { $this->alert('error', 'Error inesperado', [ 'position' => 'top' ]); return; } } public function limpiarInputs(){ $this->modTarifas = false; $this->servicio_id = null; $this->promocion_id = null; $this->precio = null; $this->utilidad = null; $this->estado = true; $this->plan_id = null; $this->rol_id = null; $this->valorBase = null; } }