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.
This commit is contained in:
Juan Felipe Duarte
2025-06-25 16:36:32 -07:00
parent e8050ea19f
commit 85d18dc4ff
7 changed files with 430 additions and 86 deletions
+81 -5
View File
@@ -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;