This commit is contained in:
Juan Felipe Duarte
2025-06-26 11:05:53 -07:00
parent b8d741dddd
commit 83ba4f71da
4 changed files with 250 additions and 22 deletions
+152 -4
View File
@@ -20,6 +20,8 @@ class ShowTarifas extends Component
// Modales
public $modTarifas = false;
public $modTarifasPromo = false;
public $editarTarifa = false;
public $editarTarifaPromo = false;
public $tarifas;
public $planes;
@@ -31,6 +33,8 @@ class ShowTarifas extends Component
public $estado = true;
public $valorBase;
public $tarifa_id;
// Tarifa datos
public $servicio_id;
public $plan_id;
@@ -45,6 +49,7 @@ class ShowTarifas extends Component
public function render()
{
$this->planes = Tarifas::where('servicio_id', $this->servicio_id)->where('rol_id', Auth::user()->rol->id)->where('estado', 'activo')->get();
$this->promociones = TarifaPromo::where('promocion_id', $this->promocion_id)->where('rol_id', Auth::user()->rol->id)->where('visible', 'activo')->get();
if ($this->modTarifas) {
$this->tarifas = Tarifas::where('servicio_id', $this->servicio_id)->where('rol_id', $this->rol_id)->where('estado', 'activo')->get();
@@ -52,7 +57,6 @@ class ShowTarifas extends Component
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(),
@@ -151,7 +155,6 @@ class ShowTarifas extends Component
$this->alert('success', '¡Tarifas creada correctamente!', [
'position' => 'top'
]);
} else {
$this->alert('error', 'Error inesperado', [
'position' => 'top'
@@ -202,7 +205,6 @@ class ShowTarifas extends Component
$this->alert('success', '¡Tarifas creada correctamente!', [
'position' => 'top'
]);
} else {
$this->alert('error', 'Error inesperado', [
'position' => 'top'
@@ -212,16 +214,162 @@ class ShowTarifas extends Component
}
}
public function limpiarInputs(){
public function editarTarifa($id)
{
$tarifa = Tarifas::find($id);
if ($tarifa) {
$this->editarTarifa = true;
$this->servicio_id = $tarifa->servicio_id;
$this->precio = $tarifa->valor;
$this->utilidad = $tarifa->utilidad;
$this->estado = $tarifa->estado == 'activo';
$this->plan_id = $tarifa->plan_id;
$this->rol_id = $tarifa->rol_id;
$this->valorBase = $tarifa->valor - $tarifa->utilidad;
$this->tarifa_id = $tarifa->id;
} else {
$this->tarifa_id = null;
$this->alert('error', 'Tarifa no encontrada', [
'position' => 'top'
]);
}
}
public function editarTarifaPromo($id)
{
$tarifa = TarifaPromo::find($id);
if ($tarifa) {
$this->editarTarifaPromo = true;
$this->promocion_id = $tarifa->promocion_id;
$this->precio = $tarifa->precio;
$this->utilidad = $tarifa->utilidad;
$this->estado = $tarifa->visible == 'activo';
$this->plan_id = $tarifa->plan_id;
$this->rol_id = $tarifa->rol_id;
$this->valorBase = $tarifa->precio - $tarifa->utilidad;
$this->tarifa_id = $tarifa->id;
} else {
$this->tarifa_id = null;
$this->alert('error', 'Tarifa no encontrada', [
'position' => 'top'
]);
}
}
public function actualizarTarifa()
{
$tarifa = Tarifas::find($this->tarifa_id);
if (!$tarifa) {
$this->alert('error', 'Tarifa no encontrada', [
'position' => 'top'
]);
return;
}
$this->validate([
'precio' => 'required|numeric',
'utilidad' => 'required',
'estado' => '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',
'rol_id.required' => 'El campo rol es obligatorio',
]);
if ($this->precio < $this->valorBase) {
$this->alert('error', 'El precio no puede ser menor al valor base', [
'position' => 'top'
]);
return;
}
$tarifa->update([
'valor' => $this->precio,
'utilidad' => $this->utilidad,
'estado' => $this->estado ? 'activo' : 'inactivo',
'rol_id' => $this->rol_id,
]);
$this->limpiarEditarInputs();
$this->alert('success', '¡Tarifa actualizada correctamente!', [
'position' => 'top'
]);
}
public function actualizarTarifaPromo()
{
$tarifa = TarifaPromo::find($this->tarifa_id);
if (!$tarifa) {
$this->alert('error', 'Tarifa no encontrada', [
'position' => 'top'
]);
return;
}
$this->validate([
'precio' => 'required|numeric',
'utilidad' => 'required',
'estado' => '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',
'rol_id.required' => 'El campo rol es obligatorio',
]);
if ($this->precio < $this->valorBase) {
$this->alert('error', 'El precio no puede ser menor al valor base', [
'position' => 'top'
]);
return;
}
$tarifa->update([
'precio' => $this->precio,
'utilidad' => $this->utilidad,
'visible' => $this->estado ? 'activo' : 'inactivo',
'rol_id' => $this->rol_id,
]);
$this->limpiarEditarInputs();
$this->alert('success', '¡Tarifa actualizada correctamente!', [
'position' => 'top'
]);
}
public function limpiarEditarInputs()
{
$this->editarTarifa = false;
$this->editarTarifaPromo = false;
$this->valorBase = null;
$this->estado = true;
$this->tarifa_id = null;
}
public function limpiarInputs()
{
$this->modTarifas = false;
$this->modTarifasPromo = false;
$this->editarTarifa = false;
$this->editarTarifaPromo = 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;
$this->tarifa_id = null;
}
}