376 lines
12 KiB
PHP
376 lines
12 KiB
PHP
<?php
|
|
|
|
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;
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ShowTarifas extends Component
|
|
{
|
|
use LivewireAlert;
|
|
use WithPagination;
|
|
|
|
// Modales
|
|
public $modTarifas = false;
|
|
public $modTarifasPromo = false;
|
|
public $editarTarifa = false;
|
|
public $editarTarifaPromo = false;
|
|
|
|
public $tarifas;
|
|
public $planes;
|
|
public $promociones;
|
|
|
|
public $rol_id;
|
|
public $precio;
|
|
public $utilidad;
|
|
public $estado = true;
|
|
public $valorBase;
|
|
|
|
public $tarifa_id;
|
|
|
|
// Tarifa datos
|
|
public $servicio_id;
|
|
public $plan_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->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();
|
|
}
|
|
if ($this->modTarifasPromo) {
|
|
$this->tarifas = TarifaPromo::where('promocion_id', $this->promocion_id)->where('rol_id', $this->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 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;
|
|
}
|
|
}
|