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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user