From 85d18dc4ff6925fb02a5a9c6d7510ca751096a48 Mon Sep 17 00:00:00 2001 From: Juan Felipe Duarte <70235683+DuarteJFelipe@users.noreply.github.com> Date: Wed, 25 Jun 2025 16:36:32 -0700 Subject: [PATCH] Tarifas y promos por usuario (falta el editar en la vista de tarifas) Introduces the subvendedor_id field to the tarifa_promos table and updates the TarifaPromo model accordingly. Refactors tarifas and promociones management in ShowTarifas and ShowUsuarios Livewire components, including utility calculation, validation, and UI improvements for managing both regular and promo tarifas. --- app/Http/Livewire/ShowTarifas.php | 217 +++++++++++++++--- app/Http/Livewire/ShowUsuarios.php | 86 ++++++- app/Models/TarifaPromo.php | 11 +- ..._subvendedor_id_to_tarifa_promos_table.php | 22 ++ .../views/livewire/show-tarifas.blade.php | 130 +++++++++-- .../views/livewire/show-usuarios.blade.php | 48 ++-- routes/web.php | 2 +- 7 files changed, 430 insertions(+), 86 deletions(-) create mode 100644 database/migrations/2025_06_25_122857_add_subvendedor_id_to_tarifa_promos_table.php diff --git a/app/Http/Livewire/ShowTarifas.php b/app/Http/Livewire/ShowTarifas.php index 49bc96c..53972de 100644 --- a/app/Http/Livewire/ShowTarifas.php +++ b/app/Http/Livewire/ShowTarifas.php @@ -5,6 +5,7 @@ namespace App\Http\Livewire; use App\Models\Promociones; use App\Models\Role; use App\Models\Servicio; +use App\Models\TarifaPromo; use App\Models\Tarifas; use Livewire\Component; use Livewire\WithPagination; @@ -16,23 +17,37 @@ class ShowTarifas extends Component use LivewireAlert; use WithPagination; + // Modales + public $modTarifas = false; + public $modTarifasPromo = false; + public $tarifas; public $planes; + public $promociones; - // Tarifa datos - public $pantallas; - public $dias = 30; + public $rol_id; public $precio; public $utilidad; - public $estado; + public $estado = true; + public $valorBase; + + // Tarifa datos public $servicio_id; public $plan_id; - public $rol_id; + + // Promo datos + public $promocion_id; + public $promo_id; + + + // Todo: QUE PRIMERO vea si el usuario tiene alguna promo en usuario_tarifas pues esta tiene prioridad public function render() { - $this->tarifas = Tarifas::where('servicio_id', $this->servicio_id)->where('rol_id', Auth::user()->rol->id)->where('estado', 'activo')->get(); - $this->planes = Tarifas::where('servicio_id', $this->servicio_id)->where('rol_id', Auth::user()->rol->id)->where('estado', 'activo')->get(); + $this->planes = Tarifas::where('servicio_id', $this->servicio_id)->where('rol_id', Auth::user()->rol->id)->where('estado', 'activo')->get(); + // $this->tarifas = Tarifas::where('servicio_id', $this->servicio_id)->where('rol_id', Auth::user()->rol->id)->where('estado', 'activo')->get(); + // $this->tarifas = TarifaPromo::where('promocion_id', $this->promocion_id)->where('rol_id', Auth::user()->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(), @@ -41,39 +56,167 @@ class ShowTarifas extends Component ]); } + 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() { - $this->validate([ - 'pantallas' => 'required', - 'dias' => 'required', - 'precio' => 'required', - 'utilidad' => 'required', - 'estado' => 'required', - 'servicio_id' => 'required', - 'rol_id' => 'required', - ], [ - 'pantallas.required' => 'El campo pantallas es obligatorio', - 'dias.required' => 'El campo días es obligatorio', - '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', - ]); + $planSeleccionado = collect($this->planes)->firstWhere('id', $this->plan_id); - Tarifas::create([ - 'pantallas' => $this->pantallas, - 'dias' => $this->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, - ]); + if ($planSeleccionado) { - $this->alert('success', '¡Tarifas creada correctamente!', [ - 'position' => 'top' - ]); + $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; } } diff --git a/app/Http/Livewire/ShowUsuarios.php b/app/Http/Livewire/ShowUsuarios.php index 31c299b..f2c1cfd 100755 --- a/app/Http/Livewire/ShowUsuarios.php +++ b/app/Http/Livewire/ShowUsuarios.php @@ -103,8 +103,8 @@ class ShowUsuarios extends Component $consulta_tarifas = TarifaPromo::take(1); $consulta_usuarios = User::withCount(['usuario_tarifas as active_tarifas' => function ($query) { - $query->where('visible', 'true'); - }]) + $query->where('visible', 'true'); + }]) ->withCount(['usuario_promos as active_promos' => function ($query) { $query->where('visible', 'true'); }]) @@ -118,9 +118,9 @@ class ShowUsuarios extends Component $query->whereHas('usuario_tarifas', function ($q) { $q->where('visible', true); }) - ->orWhereHas('usuario_promos', function ($q) { - $q->where('visible', true); - }); + ->orWhereHas('usuario_promos', function ($q) { + $q->where('visible', true); + }); }); } @@ -168,6 +168,71 @@ class ShowUsuarios extends Component ]); } + 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() { @@ -421,6 +486,7 @@ class ShowUsuarios extends Component public function gestionar($id, $rid, $usuNom) { + $this->utilidad = 0; $this->usuarioGestion_id = $id; $this->rol_usuario = $rid; @@ -440,6 +506,14 @@ class ShowUsuarios extends Component ->where('usuario_id', $this->usuarioGestion_id) ->exists(); + + if ($this->precio < $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' @@ -486,6 +560,8 @@ class ShowUsuarios extends Component public function gestionarPromociones($id, $usuNom) { + $this->utilidad = 0; + $this->usuarioGestion_id = $id; $this->usuario_nombre = $usuNom; diff --git a/app/Models/TarifaPromo.php b/app/Models/TarifaPromo.php index 1958734..d18b664 100755 --- a/app/Models/TarifaPromo.php +++ b/app/Models/TarifaPromo.php @@ -15,13 +15,16 @@ class TarifaPromo extends Model 'rol_id', 'precio', 'visible', + 'subvendedor_id', ]; - public function rol(){ - return $this ->belongsTo(Role::class,'rol_id'); + public function rol() + { + return $this->belongsTo(Role::class, 'rol_id'); } - public function promocion(){ - return $this ->belongsTo(Promociones::class,'promocion_id'); + public function promocion() + { + return $this->belongsTo(Promociones::class, 'promocion_id'); } } diff --git a/database/migrations/2025_06_25_122857_add_subvendedor_id_to_tarifa_promos_table.php b/database/migrations/2025_06_25_122857_add_subvendedor_id_to_tarifa_promos_table.php new file mode 100644 index 0000000..2990f4a --- /dev/null +++ b/database/migrations/2025_06_25_122857_add_subvendedor_id_to_tarifa_promos_table.php @@ -0,0 +1,22 @@ +string('subvendedor_id')->nullable()->after('utilidad'); + }); + } + + public function down() + { + Schema::table('tarifa_promos', function (Blueprint $table) { + $table->dropColumn('subvendedor_id'); + }); + } +}; diff --git a/resources/views/livewire/show-tarifas.blade.php b/resources/views/livewire/show-tarifas.blade.php index e894692..0768d95 100644 --- a/resources/views/livewire/show-tarifas.blade.php +++ b/resources/views/livewire/show-tarifas.blade.php @@ -1,7 +1,8 @@
-
+
@@ -20,7 +21,7 @@ @if (auth()->user()->subvendedor) - Modificar tarifas + Tarifas servicios @@ -29,8 +30,8 @@ @endif @if (auth()->user()->subvendedor) - Mod. - tarifas promos. + + Tarifas promociones @@ -45,10 +46,9 @@
- -
+
- @foreach ($servicios as $servicio) @@ -73,18 +73,19 @@
- +
- +
@@ -127,8 +128,7 @@ {{ $tarifa->dias ?? '' }} {{ number_format($tarifa->utilidad ?? 0) }} - + {{-- --}} @endforeach @@ -139,12 +139,112 @@
- +
+ +
+
+
+ +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+ + +
+ + + + + + + + + + + + + @if (!empty($tarifas)) + @foreach ($tarifas as $tarifa) + + + + + + + + @endforeach + @endif + +
EstadoRolValorUtilidad
+ @if ($tarifa->visible == 'activo') +
+ @else +
+ @endif +
{{ $tarifa->rol->nombre ?? '' }}{{ number_format($tarifa->precio) ?? 0 }}{{ number_format($tarifa->utilidad) ?? 0 }} + {{-- --}} +
+
+ +
+ + + +
+ +
+
+
+
diff --git a/resources/views/livewire/show-usuarios.blade.php b/resources/views/livewire/show-usuarios.blade.php index 37439b9..406753d 100755 --- a/resources/views/livewire/show-usuarios.blade.php +++ b/resources/views/livewire/show-usuarios.blade.php @@ -37,7 +37,7 @@ @if (auth()->user()->rol->nombre === 'super' || auth()->user()->rol->nombre === 'administrador')
Mod. - tarifas promos. + tarifas promos @@ -184,29 +184,22 @@ @if (!empty($consulta_tarifas)) @foreach ($consulta_tarifas as $consulta_tarifa) - @if ($consulta_tarifa->servicio && $consulta_tarifa->servicio->por_tiempo) - - @elseif ($consulta_tarifa->servicio) - - @endif - + @if ($consulta_tarifa->servicio && $consulta_tarifa->servicio->por_tiempo) + + @elseif ($consulta_tarifa->servicio) + + @endif @endforeach @endif
@@ -223,9 +216,14 @@

Utilidad

- + + + @if (auth()->user()->rol->nombre === 'super' || auth()->user()->rol->nombre === 'administrador') + + @else + + @endif + @@ -366,9 +364,11 @@

Utilidad

- + @if (auth()->user()->rol->nombre === 'super' || auth()->user()->rol->nombre === 'administrador') + + @else + + @endif diff --git a/routes/web.php b/routes/web.php index 03f942f..c9366c8 100755 --- a/routes/web.php +++ b/routes/web.php @@ -44,12 +44,12 @@ Route::middleware(["auth"])->group(function () { Route::any('/usuarios', [MenuController::class, 'showusuarios'])->name('usuarios'); Route::any('/roles', [MenuController::class, 'showroles'])->name('roles'); Route::any('/tarifas', [MenuController::class, 'showtarifas'])->name('tarifas'); + Route::any('/planes', [MenuController::class, 'showplanes'])->name('planes'); }); // Editores Route::middleware(["auth", "solo_usuario_editor"])->group(function () { Route::get('/import', [MenuController::class, 'showimport'])->name('import'); - Route::any('/planes', [MenuController::class, 'showplanes'])->name('planes'); }); //Administradores