File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/AcademyCategoryController.php
<?php
namespace App\Http\Controllers;
use DataTables;
use App\AcademyCategory;
use App\AcademyCategoryErp;
use App\AcademyLocation;
use App\Parameter;
use App\Services\AcademyLocationsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AcademyCategoryController extends Controller
{
public function index()
{
$syncERP = Parameter::select('sync_siesa')->first()->sync_siesa;
return view('academy.categories.list', compact('syncERP'));
}
public function indexAdd()
{
$academyLocationsService = new AcademyLocationsService;
if (!$academyLocationsService->validateAuthorizedLocations()) {
$authorizedLocations = $academyLocationsService->getAuthorizedLocations();
$locations = AcademyLocation::select('id', 'name')->where('active', 1)->whereIn('id', $authorizedLocations)->orderBy('name', 'ASC')->get();
} else {
$locations = AcademyLocation::select('id', 'name')->where('active', 1)->orderBy('name', 'ASC')->get();
}
$syncERP = Parameter::select('sync_siesa')->first()->sync_siesa;
return view('academy.categories.create', compact('locations', 'syncERP'));
}
public function indexEdit($id)
{
$object = AcademyCategory::with('academy_category_erp')->findOrFail($id);
$academyLocationsService = new AcademyLocationsService;
if (!$academyLocationsService->validateAuthorizedLocations()) {
$authorizedLocations = $academyLocationsService->getAuthorizedLocations();
$locations = AcademyLocation::select('id', 'name')->where('active', 1)->whereIn('id', $authorizedLocations)->orderBy('name', 'ASC')->get();
} else {
$locations = AcademyLocation::select('id', 'name')->where('active', 1)->orderBy('name', 'ASC')->get();
}
$syncERP = Parameter::select('sync_siesa')->first()->sync_siesa;
return view('academy.categories.edit', compact('object', 'locations', 'syncERP'));
}
public function tableFilter()
{
$obj = AcademyCategory::select(
'academy_categories.id',
'academy_categories.name',
'academy_categories.inscription_value',
'academy_categories.monthly_payment',
'academy_categories.academy_location_id',
'academy_categories.created_at',
'academy_categories.active',
'academy_category_erps.registration_service_item_code',
'academy_category_erps.pension_service_item_code',
'academy_category_erps.cost_center',
'academy_categories.show_in',
)
->with('academy_location')
->leftjoin('academy_locations', 'academy_locations.id', '=', 'academy_categories.academy_location_id')
->leftjoin('academy_category_erps', 'academy_category_erps.id', '=', 'academy_categories.academy_category_erp_id');
$academyLocationsService = new AcademyLocationsService;
if (!$academyLocationsService->validateAuthorizedLocations()) {
$authorizedLocations = $academyLocationsService->getAuthorizedLocations();
$obj->whereIn('academy_locations.id', $authorizedLocations);
}
return DataTables::of($obj)
->addColumn('actions', function ($obj) {
return '
<i class="fa fa-pencil iconMini" onClick="clickEdit(' . $obj->id . ')" data-id="' . $obj->id . '" data-toggle="tooltip" data-placement="bottom" title="Editar" style="cursor:pointer;"></i>
<i class="fa fa-trash iconMini" onClick="clickDelete(' . $obj->id . ')" data-id="' . $obj->id . '" data-toggle="tooltip" data-placement="bottom" title="Eliminar" style="cursor:pointer;"></i>
';
})
->editColumn('created_at', function ($obj) {
return \Carbon\Carbon::parse($obj->created_at)->format('Y-m-d h:i:s A');
})
->editColumn('active', function ($obj) {
if ($obj->active == 0) {
return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" /> <span></span>' . __('messages.yes') . ' </label></div> </label> </div>';
} else {
return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" checked="" />
<span></span> ' . __('messages.yes') . ' </label> </div> </label> </div>';
}
})
->rawColumns(['actions', 'active'])
->make(true);
}
public function create(Request $request)
{
try {
if (AcademyCategory::where([['name', $request->input('name')], ['active', true], ['academy_location_id', $request->input('academy_location_id')]])->first()) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
}
$data = $request->except(['registration_service_item_code', 'pension_service_item_code', 'cost_center']);
if ($model = AcademyCategory::create($data)) {
if ($request->input('registration_service_item_code') || $request->input('pension_service_item_code') || $request->input('cost_center')) {
$erp = AcademyCategoryErp::updateOrCreate([
'registration_service_item_code' => $request->input('registration_service_item_code'),
'pension_service_item_code' => $request->input('pension_service_item_code'),
'cost_center' => $request->input('cost_center'),
]);
$model->update(['academy_category_erp_id' => $erp->id]);
}
$this->registerLog(Auth::user()->id, 'Crear categoría academia', json_encode($request->all()), "Create", $this->getModule($request));
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => $model->id));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
}
} catch (\Exception $e) {
return response(
array(
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __($e->getMessage()),
"data" => null
)
);
}
}
public function update(Request $request, $id)
{
try {
$request['id'] = $id;
if (AcademyCategory::where([['id', '!=', $id], ['name', $request->input('name')], ['active', true], ['academy_location_id', $request->input('academy_location_id')]])->first()) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
}
$data = $request->except(['registration_service_item_code', 'pension_service_item_code', 'cost_center']);
if (AcademyCategory::where('id', $id)->update($data)) {
if ($request->input('registration_service_item_code') || $request->input('pension_service_item_code') || $request->input('cost_center')) {
$erp = AcademyCategoryErp::select('academy_category_erps.*')->join('academy_categories', 'academy_categories.academy_category_erp_id', '=', 'academy_category_erps.id')
->where('academy_categories.id', $id)->first();
if (!$erp) {
$erp = AcademyCategoryErp::updateOrCreate([
'registration_service_item_code' => $request->input('registration_service_item_code'),
'pension_service_item_code' => $request->input('pension_service_item_code'),
'cost_center' => $request->input('cost_center'),
]);
AcademyCategory::where('id', $id)->update(['academy_category_erp_id' => $erp->id]);
} else {
$erp->update([
'registration_service_item_code' => $request->input('registration_service_item_code'),
'pension_service_item_code' => $request->input('pension_service_item_code'),
'cost_center' => $request->input('cost_center'),
]);
}
}
$this->registerLog(Auth::user()->id, 'Editar categoría academia', json_encode($request->all()), "Update", $this->getModule($request));
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $id));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
}
} catch (\Exception $e) {
return response(
array(
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __($e->getMessage()),
"data" => null
)
);
}
}
public function delete(Request $request, $id)
{
try {
$category = AcademyCategory::find($id);
if (!$category) {
return response([
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __('messages.not_found'),
"data" => null
]);
}
// Definir las relaciones a verificar
$relations = [
'academy_tournament_categories' => 'Torneos asociados',
'academy_categories_schedules' => 'Horarios de categoría',
'academy_users' => 'Usuarios de academia',
];
// Comprobar si existen relaciones
$related = [];
$hasUsers = false;
foreach ($relations as $method => $relationName) {
if ($category->$method()->exists()) {
$related[] = $method;
if ($method === 'academy_users') {
$hasUsers = true;
}
}
}
// Si existen relaciones, bloquear la eliminación
if (!empty($related)) {
// Caso 1: Hay usuarios asignados
if ($hasUsers) {
return response([
"r" => false,
"type" => "error",
"title" => "No se puede eliminar",
"m" => "La categoría no puede ser eliminada porque está asociada a usuarios (alumnos o profesores).\n\n" .
"Siga los siguientes pasos para poder hacer la eliminación:\n" .
"1. Reasigne a los usuarios según sea su nueva sede, categoría u horario.\n" .
"2. Realice la eliminación de la categoría.",
"data" => null
]);
}
// Caso 2: No hay usuarios, pero sí horarios asociados
if (in_array('academy_categories_schedules', $related)) {
return response([
"r" => false,
"type" => "error",
"title" => "No se puede eliminar",
"m" => "La categoría no puede ser eliminada porque está asociada a horarios.\n\n" .
"Siga los siguientes pasos para poder hacer la eliminación:\n" .
"1. Desvincule los horarios asociados a la categoría que desea eliminar.\n" .
"2. Realice la eliminación de la categoría.",
"data" => null
]);
}
// Caso 3: No hay usuarios ni horarios asociados
return response([
"r" => false,
"type" => "error",
"title" => "No se puede eliminar",
"m" => "La categoría no puede ser eliminada porque está asociada a otros elementos.\n\n" .
"Desvincule todos los elementos relacionados antes de intentar eliminar la categoría.",
"data" => null
]);
}
// Eliminar la categoría si no tiene relaciones
if ($category->delete()) {
$this->registerLog(Auth::user()->id, 'Eliminó categoría academia', json_encode($category), "Delete", $this->getModule($request));
return response([
"r" => true,
"type" => "success",
"title" => "",
"m" => __('messages.deleted_successfully'),
"data" => null
]);
}
return response([
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __('messages.error_removing'),
"data" => null
]);
} catch (\Exception $e) {
return response([
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __('messages.delete_relation_data') . " Detalles: " . $e->getMessage(),
"data" => null
]);
}
}
public function activate(Request $request)
{
try {
$id = $request['id'];
$state = $request['state'];
$category = AcademyCategory::find($id);
$category->active = $state;
$category->update();
$this->registerLog(Auth::user()->id, 'Activar/Desactivar categoría academia', json_encode($category), "Update", $this->getModule($request));
return array('r' => true, 'd' => null, 'm' => __('messages.updated_successfully'));
} catch (\Throwable $th) {
return array('r' => false, 'd' => null, 'm' => __('messages.error_updating'));
}
}
public function get(Request $request)
{
$academyLocationsService = new AcademyLocationsService;
if (!$academyLocationsService->validateAuthorizedLocations()) {
$authorizedLocations = $academyLocationsService->getAuthorizedLocations();
$categories = AcademyCategory::select('academy_categories.id', 'academy_categories.name')
->with('academy_location')
->join('academy_locations', 'academy_locations.id', '=', 'academy_categories.academy_location_id')
->where('academy_categories.active', 1)->whereIn('academy_locations.id', $authorizedLocations)
->orderBy('academy_categories.name', 'ASC');
} else {
$categories = AcademyCategory::select('id', 'name')->with('academy_location')->where('active', 1)->orderBy('name', 'ASC');
}
if ($request['locations']) {
$categories->whereIn('academy_location_id', $request['locations']);
}
return $categories->get();
}
}