Files
sirpremiumv2/app/Http/Livewire/ShowPlanes.php
T
2023-07-26 08:28:13 -05:00

544 lines
16 KiB
PHP

<?php
namespace App\Http\Livewire;
use App\Models\Cuentas;
use App\Models\Historiale;
use App\Models\Historial_cuenta;
use App\Models\Role;
use App\Models\Servicio;
use App\Models\Tarifas;
use App\Models\user;
use Carbon\Carbon;
use Livewire\Component;
use Jantinnerezo\LivewireAlert\LivewireAlert;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
class ShowPlanes extends Component
{
use LivewireAlert;
use WithFileUploads;
use WithPagination;
public $delete_cuenta;
public $informacionTarifaRol;
public $newCuenta = false;
public $renovar = false;
public $serviciosModal = false;
public $modTarifas = false;
public $roles_modTarifas;
public $pantallas_modTarifas;
public $precio_modTarifas = 0;
public $estadoTarifa = true;
public $diasTarifa = '30';
public $servicio_modTarifas;
public $buscar;
public $servicios_id;
public $fechaActual;
public $id_cuentaVer;
public $consulta_verUsuarios;
public $nombre_servicios = '';
public $nombre_servicio;
public $descripcion_servicio;
public $estado_servicio;
public $color_servicio;
public $servicio_newCuenta;
public $usuario_newCuenta;
public $password_newCuenta;
public $fecha_inicial;
public $fecha_final;
public $observacionesm = false;
public $observaciones;
public $id_observacion;
public $observacionesm1 = false;
public $observaciones1;
public $id_observacion1;
public $usuario_renovar;
public $password_renovar;
public $fecha_inicial_renovar;
public $fecha_final_renovar;
public $servicio;
public $photo;
public $photo_inicio;
public $estado='activo';
public $completa;
public $pantallas;
public $venceHoy;
public $activoHoy;
public $libres = null;
public $delete_1=['1'];
public $tarifas;
public $addUser;
public $usuarios;
public $selectedOption;
public $searchTerm = '';
public $tarifa;
public $perfil;
public $repetidas;
public $editarTarifaRol = false;
public function render()
{
$this->fechaActual = date('Y-m-d');
$consulta_tarifas = Tarifas::take(1);
if(empty($this->fecha_inicial)){
$this->fecha_inicial=Carbon::now()->format('Y-m-d');
$this->fecha_final=Carbon::now()->addDay(30)->format('Y-m-d');
}
if ($this->servicio_modTarifas) {
$this->tarifas = Tarifas::with('rol')
->where('servicio_id', $this->servicio_modTarifas)
->where('estado', 'activo')
->get();
}
// Empezamos definiendo la consulta básica
$consulta_cuentas = Cuentas::where('correo', 'LIKE', '%'.$this->buscar.'%')
->withCount('historiales')
->with('historiales')
->orderBy('id','DESC');
$consulta_cuentas = $consulta_cuentas->when(!empty($this->servicios_id), function ($query) {
return $query->where('servicio_id', $this->servicios_id);
})
->when(!empty($this->completa), function ($query) {
return $query->where('estado', 'pendiente');
})
->when(!empty($this->pantallas), function ($query) {
return $query->where('estado', 'activo');
})
->when(!empty($this->activoHoy), function ($query) {
$fechaHoy = Carbon::now()->subDay(1);
return $query->whereDate('vencimiento', '>', $fechaHoy);
})
->when($this->venceHoy, function ($query) {
$fechaHoy = Carbon::now()->addDay(1);
return $query->whereDate('vencimiento', '<', $fechaHoy);
})
->when($this->libres, function ($query) {
return $query->whereDoesntHave('historiales');
});
if (!empty($this->repetidas)) {
$consulta_cuentas = $consulta_cuentas->whereIn('correo', function ($subquery) {
$subquery->select('correo')
->from('cuentas')
->groupBy('correo')
->havingRaw('COUNT(*) > 1');
});
}
$consulta_cuentas = $consulta_cuentas->paginate(30);
if ( $this->searchTerm) {
$this->usuarios = User::where('email', 'LIKE', '%' . $this->searchTerm . '%')->get();
if ($this->selectedOption) {
$this->searchTerm = $this->selectedOption;
}
}else{
$this->selectedOption = null;
}
return view('livewire.show-planes', [
'cuentas' => $consulta_cuentas,
'servicios' => Servicio::where('estado','activo')->get(),
'roles' => Role::get()
]);
}
//modal Ver
public function ver($id, $nom)
{
$this->id_cuentaVer = $id;
$this->consulta_verUsuarios = Cuentas::with('historiales','servicio')->where('id', $id)->first();
//dd($this->consulta_verUsuarios);
$this->nombre_servicios = $nom;
}
public function openRenovar()
{
$this->renovar = true;
$id = $this->id_cuentaVer;
$renovarCuenta = Cuentas::findOrFail($id);
$this->usuario_renovar = $renovarCuenta->correo;
$this->password_renovar = $renovarCuenta->password;
$this->fecha_inicial_renovar = $renovarCuenta->inicio;
$this->fecha_final_renovar = $renovarCuenta->vencimiento;
$this->estado = $renovarCuenta->estado;
}
public function saveRenovar()
{
$id = $this->id_cuentaVer;
Cuentas::where('id', $id)->update([
'correo' => $this->usuario_renovar,
'password' => $this->password_renovar,
'inicio' => date('Y-m-d', strtotime(strval($this->fecha_inicial_renovar))),
'vencimiento' => date('Y-m-d', strtotime(strval($this->fecha_final_renovar))),
'estado' => $this->estado,
]);
$this->alert('success', 'Cuenta renovada correctamente!', [
'position' => 'top'
]);
$this->renovar = false;
}
/* ------------- CREAR ------------- */
public function createNewCuenta()
{
$this->validate([
'servicio_newCuenta' => 'required',
'usuario_newCuenta' => 'required',
'password_newCuenta' => 'required',
'fecha_inicial' => 'required',
'fecha_final' => 'required',
'estado' => 'required',
]);
if($consulta_cuenta = Cuentas::where('correo',$this->usuario_newCuenta)->exists()){
$this->alert('warning', 'La cuenta ya existe', [
'position' => 'top'
]);
}else{
$cuenta = new Cuentas;
$cuenta->servicio_id = $this->servicio_newCuenta;
$cuenta->correo = $this->usuario_newCuenta;
$cuenta->password = $this->password_newCuenta;
$cuenta->inicio = date('Y-m-d', strtotime(strval($this->fecha_inicial)));
$cuenta->vencimiento = date('Y-m-d', strtotime(strval($this->fecha_final)));
$cuenta->estado = $this->estado;
$cuenta->save();
$this->alert('success', 'Cuenta creada correctamente!', [
'position' => 'top'
]);
$this->limpiarInputNewCuenta();
$this->newCuenta = false;
}
}
public function createServicio()
{
$url_foto = $this->photo->store('photos');
$url_foto_inicio = $this->photo_inicio->store('photos');
$servicio = new Servicio;
$servicio->nombre = $this->nombre_servicio;
$servicio->descripcion = $this->descripcion_servicio;
$servicio->estado = $this->estado_servicio;
$servicio->color_fondo = $this->color_servicio;
$servicio->pantallas = '6';
$servicio->img = $url_foto;
$servicio->img_inicio = $url_foto_inicio;
$servicio->save();
$this->alert('success', 'Servicio creado correctamente!', [
'position' => 'top'
]);
$this->serviciosModal = false;
}
public function verObservacion($id)
{
$verObservacion = Historiale::findOrFail($id);
$this->id_observacion = $id;
$this->observaciones = $verObservacion->observaciones;
}
public function update()
{
Historiale::where('id', $this->id_observacion)->update(['observaciones' => $this->observaciones,]);
$this->alert('success', 'Observacion añadida correctamente!', [
'position' => 'top'
]);
$this->observacionesm = false;
}
public function verObservacion1($id)
{
$verObservacion = Cuentas::findOrFail($id);
$this->id_observacion1 = $id;
$this->observaciones1 = $verObservacion->observaciones;
}
public function update_observaciones1()
{
Cuentas::where('id', $this->id_observacion1)->update(['observaciones' => $this->observaciones1,]);
$this->alert('success', 'Observacion añadida correctamente!', [
'position' => 'top'
]);
$this->observacionesm1 = false;
}
public function delete_cuenta($id)
{
$this->delete_cuenta = $id;
$this->alert('warning', '¿Eliminar cuenta?', [
'position' => 'center',
'timer' => '10000',
'toast' => false,
'text' => 'Esta seguro',
'showConfirmButton' => true,
'onConfirmed' => 'confirmed',
'showDenyButton' => false,
'onDenied' => '',
'showCancelButton' => true,
'onDismissed' => '',
'cancelButtonText' => 'Salir',
'confirmButtonText' => 'Si',
]);
}
public function getListeners()
{
return [
'confirmed'
];
}
public function confirmed()
{
Cuentas::where('id', $this->delete_cuenta)->delete();
$this->alert('success', 'Cuenta eliminada correctamente!', [
'position' => 'top'
]);
}
/* ------------- ACTUALIZAR ------------- */
public function updateTarifas()
{
$consulta_existe = Tarifas::where('pantallas', $this->pantallas_modTarifas)
->where('servicio_id', $this->servicio_modTarifas)
->where('rol_id', $this->roles_modTarifas)
->where('dias', $this->diasTarifa)
->exists();
if ($consulta_existe) {
$this->alert('warning', '¡Esta tarifa ya existe!', [
'position' => 'top'
]);
} elseif (empty($this->servicio_modTarifas)) {
$this->alert('warning', '¡Seleccione un servicio!', [
'position' => 'top'
]);
} elseif (empty($this->precio_modTarifas)) {
$this->alert('warning', '¡Coloque un precio!', [
'position' => 'top'
]);
} else {
$tarifa = new Tarifas;
$tarifa->pantallas = $this->pantallas_modTarifas;
$tarifa->dias = $this->diasTarifa;
$tarifa->valor = $this->precio_modTarifas;
$tarifa->estado = $this->estadoTarifa ? 'activo' : 'inactivo';
$tarifa->servicio_id = $this->servicio_modTarifas;
$tarifa->rol_id = $this->roles_modTarifas;
$tarifa->save();
$this->alert('success', '¡Tarifas actualizadas correctamente!', [
'position' => 'top'
]);
}
}
/* ------------- ELIMINAR ------------- */
public function eliminarModTarifa($id)
{
Tarifas::where('id',$id)->delete();
$this->alert('success', 'Tarifas eliminada correctamente!', [
'position' => 'top'
]);
}
/* ------------- LIMPIAR CAMPOS ------------- */
public function limpiarInputObservaciones()
{
$this->observaciones = '';
}
public function limpiarInputObservaciones1()
{
$this->observaciones1 = '';
$this->observacionesm1 = false;
}
public function limpiarInputNewCuenta()
{
$this->servicio_newCuenta = null;
$this->usuario_newCuenta = '';
$this->password_newCuenta = '';
$this->fecha_inicial = null;
$this->fecha_final = null;
$this->newCuenta = false;
}
public function limpiarInputServiciosModal()
{
$this->nombre_servicio = '';
$this->descripcion_servicio = '';
$this->estado_servicio = null;
$this->color_servicio = null;
}
public function eliminarCuentas(){
$consulta_cuentas = Cuentas::whereIn('id',$this->delete_1)->delete();
}
public function openAdd(){
$this->addUser = true;
}
public function saveUser(){
$this->validate([
'tarifa' => 'required',
]);
$tarifa = json_decode($this->tarifa, true);
//dd($this->tarifa['dias']);
$usuario_existe = User::firstWhere('email', $this->searchTerm)->id ?? null;
$usuario_id = $usuario_existe;
if (is_null($usuario_existe)) {
$rol_id=Role::where('nombre','cliente')->value('id');
$usuario = new User;
$usuario->name = 'AssignedUser';
$usuario->email = $this->searchTerm;
$usuario->password = '123456';
$usuario->celular = '+57';
$usuario->rol_id = $rol_id;
$usuario->save();
$usuario_id = $usuario->id;
}
$historial = new Historiale;
$historial->fecha_inicio = Carbon::now();
$historial->fecha_final = Carbon::now()->addDay($tarifa['dias']);
$historial->valor = '0';
$historial->tipo_pago = 'manual';
$historial->estado = 'activo';
$historial->vendedor_id = Auth()->user()->id;
$historial->tarifa_id = $tarifa['id'];
$historial->cliente_id = $usuario_id;
$historial->cantidad = $tarifa['pantallas'];
$historial->save();
$consultaHC = Historial_cuenta::where('cuenta_id',$this->consulta_verUsuarios->id)->where('perfil',$this->perfil)->exists();
if (!$consultaHC) {
$historial_cuenta = new Historial_cuenta;
$historial_cuenta->cuenta_id = $this->consulta_verUsuarios->id;
$historial_cuenta->historial_id = $historial->id;
$historial_cuenta->perfil = $this->perfil;
$historial_cuenta->save();
$this->alert('success', 'Usuario añadido correctamente!', [
'position' => 'top'
]);
$this->addUser = false;
$this->consulta_verUsuarios = Cuentas::with('historiales','servicio')->where('id', $this->id_cuentaVer)->first();
}else{
$historial->delete();
$this->alert('warning', 'El perfil ya se encuentra en uso!', [
'position' => 'top'
]);
}
}
public function editar($tarifa){
//dd($tarifa);
$this->editarTarifaRol=true;
$this->informacionTarifaRol = $tarifa;
$this->roles_modTarifas = $tarifa['rol_id'];
$this->pantallas_modTarifas = $tarifa['pantallas'];
$this->precio_modTarifas = $tarifa['valor'];
$this->diasTarifa = $tarifa['dias'];
$this->estadoTarifa = $tarifa['estado'];
$this->servicio_modTarifas = $tarifa['servicio_id'];
}
public function actualizarTarifaRol($id){
//dd($id);
$validateData = $this->validate([
'roles_modTarifas'=> 'required',
'pantallas_modTarifas' => 'required',
'precio_modTarifas'=> 'required',
'diasTarifa' => 'required',
'estadoTarifa'=> 'required'
]);
$consulta = Tarifas::find($id)->update([
'pantallas' => $this->pantallas_modTarifas,
'dias' => $this->diasTarifa,
'valor'=> $this->precio_modTarifas,
'estado'=> $this->estadoTarifa,
'rol_id' => $this->roles_modTarifas,
'servicio_id' => $this->servicio_modTarifas
]);
$this->alert('success', 'Tarifa actualizada correctamente!', [
'position' => 'top'
]);
$this->editarTarifaRol = false;
}
}