141 lines
4.2 KiB
PHP
141 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
/**
|
|
* @copyright Copyright (c) 2023 Notsoweb (https://notsoweb.com) - All rights reserved.
|
|
*/
|
|
|
|
use App\Http\Requests\StoreMainRoleSkills;
|
|
use App\Http\Requests\UpdateMainRoleSkills;
|
|
use App\Models\department;
|
|
use App\Models\mainRole;
|
|
use App\Models\MainRoleSkills;
|
|
use App\Models\Score;
|
|
use App\Models\Skill;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Notsoweb\Core\Http\Controllers\VueController;
|
|
|
|
/**
|
|
* Descripción
|
|
*
|
|
* @author Moisés de Jesús Cortés Castellanos <ing.moisesdejesuscortesc@notsoweb.com>
|
|
*
|
|
* @version 1.0.0
|
|
*/
|
|
class MainRoleSkillsController extends VueController
|
|
{
|
|
public function __construct()
|
|
{
|
|
return $this->vueRoot('admin.mainRoleSkills');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$q = request()->get('q');
|
|
|
|
$mainRole = mainRole::where('name', 'LIKE', "%{$q}%")->pluck('id');
|
|
$skills = Skill::where('name', 'LIKE', "%{$q}%")->pluck('id');
|
|
$scores = Score::where('alias', 'LIKE', "%{$q}%")->pluck('id');
|
|
|
|
$mainRoleSkills = MainRoleSkills::whereIn('main_role_id', $mainRole)
|
|
->orWhereIn('skill_id', $skills)
|
|
->orWhereIn('scored_id', $scores)
|
|
->with([
|
|
'mainRole:id,name,department_id',
|
|
'skill:id,name',
|
|
'score:id,alias'
|
|
])
|
|
->paginate(config('app.pagination'));
|
|
|
|
$departments = department::select(['id', 'name', 'description'])->orderBy('name', 'ASC')->get();
|
|
$mainRoles = mainRole::with(['department:id,name'])->orderBy('name', 'ASC')->get();
|
|
$skills = Skill::with(['department:id,name'])->orderBy('name', 'ASC')->get();
|
|
$scores = Score::orderBy('alias', 'ASC')->get();
|
|
|
|
|
|
return $this->vuew('index', [
|
|
'mainRoleSkills' => $mainRoleSkills,
|
|
'departments' => $departments,
|
|
'mainRoles' => $mainRoles,
|
|
'skills' => $skills,
|
|
'scores' => $scores,
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$roleId = request()->get('role_id');
|
|
$departmentId = request()->get('department_id');
|
|
|
|
// Obtener el rol seleccionado y su departamento
|
|
$selectedRole = null;
|
|
$selectedDepartment = null;
|
|
|
|
if ($roleId) {
|
|
$selectedRole = mainRole::with('department:id,name')->find($roleId);
|
|
if ($selectedRole && $selectedRole->department) {
|
|
$selectedDepartment = $selectedRole->department;
|
|
}
|
|
} elseif ($departmentId) {
|
|
$selectedDepartment = department::find($departmentId);
|
|
}
|
|
$mainRoles = mainRole::with('department:id,name')->orderBy('name', 'ASC')->get();
|
|
$skills = Skill::with('department:id,name')->orderBy('name', 'ASC')->get();
|
|
$scores = Score::orderBy('alias', 'ASC')->get();
|
|
|
|
return $this->vuew('create', [
|
|
'mainRoles' => $mainRoles,
|
|
'skills' => $skills,
|
|
'scores' => $scores,
|
|
'selectedRole' => $selectedRole,
|
|
'selectedDepartment' => $selectedDepartment
|
|
]);
|
|
}
|
|
|
|
public function store(StoreMainRoleSkills $request)
|
|
{
|
|
try {
|
|
$create = [];
|
|
foreach ($request['skills'] as $skill) {
|
|
$create[] = [
|
|
'main_role_id' => $request['main_role_id'],
|
|
'skill_id' => $skill['skill_id'],
|
|
'scored_id' => $skill['scored_id'],
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
];
|
|
}
|
|
|
|
MainRoleSkills::insert($create);
|
|
|
|
return $this->index();
|
|
|
|
} catch (\Illuminate\Database\QueryException $e) {
|
|
// Si hay error de restricción única
|
|
if ($e->getCode() === '23000') {
|
|
return back()->withErrors(['skills' => 'Una o más habilidades ya están asignadas a este rol.']);
|
|
}
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function update(UpdateMainRoleSkills $request, MainRoleSkills $mainRoleSkills)
|
|
{
|
|
$mainRoleSkills->update($request->all());
|
|
}
|
|
|
|
public function destroy($mainRoleId)
|
|
{
|
|
try {
|
|
// Eliminar todas las habilidades asociadas a este rol principal
|
|
MainRoleSkills::where('main_role_id', $mainRoleId)->delete();
|
|
|
|
return $this->index();
|
|
} catch (\Throwable $th) {
|
|
Log::channel('mainRoleSkills')->error($th->getMessage());
|
|
return response()->json(['error' => 'Error al eliminar las habilidades'], 500);
|
|
}
|
|
}
|
|
}
|