File: /var/www/api_matriculas/app/Repositories/PostulationRepository.php
<?php
namespace App\Repositories;
use App\Models\Postulation;
use App\Models\StatusPostulation;
use Exception;
use Illuminate\Support\Facades\Schema;
class PostulationRepository
{
public function getByRut($rut, $getAll = true, $id = null, $userCreated = null)
{
$query = Postulation::where('student_rut', $rut)->where('deleted', false);
if (auth()->check() && auth()->user()->profile->code === 'parent') {
$userCreated = auth()->id();
}
if ($userCreated > 0) {
$query->where('user_created', $userCreated);
}
if (!$getAll || $id > 0) {
if ($id > 0) {
$query->where('id', '!=', $id);
}
return $query->first() ?: false;
}
return $query->get();
}
public function getAll($periodId = null, $userCreated = null, $limit = null)
{
$query = Postulation::where('deleted', false);
if ($periodId > 0) {
$query->where('period_id', $periodId);
}
if (auth()->check() && auth()->user()->profile->code === 'parent') {
$userCreated = auth()->id();
}
if ($userCreated > 0) {
$query->where('user_created', $userCreated);
}
if ($limit > 0) {
$query->limit($limit);
}
return $query->orderBy('id', 'desc')->get();
}
public function getById($id, $userCreated = null)
{
$query = Postulation::where('deleted', false)->where('id', $id)->first();
if (auth()->check() && auth()->user()->profile->code === 'parent') {
$userCreated = auth()->id();
}
if ($userCreated > 0) {
$query->where('user_created', $userCreated);
}
$register = $query->first();
if (!$register) {
throw new Exception("Postulaci贸n no existe o fue eliminada", 404);
}
return $register;
}
/**
* Crear una nueva postulaci贸n
*/
public function create($data)
{
$createData = [
// 馃敼 Estudiante
'student_first_name' => ($data->student_first_name),
'student_second_name' => ($data->student_second_name ?? ''),
'student_last_name' => ($data->student_last_name),
'student_second_last_name' => ($data->student_second_last_name ?? ''),
'student_rut' => formatterRut($data->student_rut, false),
'student_email' => strLower($data->student_email ?? null),
'student_mobile' => $data->student_mobile ?? null,
'student_gender_id' => $data->student_gender ?? null,
'student_country_id' => $data->student_country ?? null,
'student_region_id' => $data->student_region ?? null,
'student_commune_id' => $data->student_commune ?? null,
'student_address' => $data->student_address ?? null,
// 馃敼 Apoderado
'parent_relationship_id' => $data->parent_relationship ?? null,
'parent_first_name' => ($data->parent_first_name),
'parent_second_name' => ($data->parent_second_name ?? ''),
'parent_last_name' => ($data->parent_last_name),
'parent_second_last_name' => ($data->parent_second_last_name ?? ''),
'parent_rut' => !empty($data->parent_rut) ? formatterRut($data->parent_rut, false) : null,
'parent_email' => strLower($data->parent_email ?? null),
'parent_mobile' => $data->parent_mobile ?? null,
'parent_gender_id' => $data->parent_gender ?? null,
'parent_profession' => strUpper($data->parent_profession ?? ''),
'parent_country_id' => $data->parent_country ?? null,
'parent_region_id' => $data->parent_region ?? null,
'parent_commune_id' => $data->parent_commune ?? null,
'parent_address' => $data->parent_address ?? null,
// 馃敼 Acad茅mico
'course_id' => $data->course ?? null,
'previous_school' => $data->previous_school ?? null,
'siblings' => $data->siblings ?? 0,
'special_needs' => $data->special_needs ?? null,
'date_entry' => $data->date_entry ?? null,
// 馃敼 Estado
'status_postulation_id' => $this->getStatus('', true),
'period_id' => $data->period_id ?? null,
'status' => true,
'deleted' => false,
'created_at' => now(),
'updated_at' => now(),
];
if (auth()->check()) {
$createData['user_created'] = auth()->id();
$createData['user_updated'] = auth()->id();
}
$created = Postulation::create($createData);
// Generar c贸digo 煤nico de postulaci贸n si existe el campo
if ($created && Schema::hasColumn('postulations', 'code_postulation')) {
$created->code_postulation = 'PTL-' . date('Ymd') . '-' . str_pad($created->id, 4, '0', STR_PAD_LEFT);
$created->save();
}
return $created;
return $created;
}
/**
* Actualizar postulaci贸n existente
*/
public function update($registerData, $data)
{
$registerData->fill([
'student_first_name' => ($data->student_first_name),
'student_second_name' => ($data->student_second_name ?? ''),
'student_last_name' => ($data->student_last_name),
'student_second_last_name' => ($data->student_second_last_name ?? ''),
'student_rut' => formatterRut($data->student_rut, false),
'student_email' => strLower($data->student_email ?? null),
'student_mobile' => $data->student_mobile ?? null,
'student_gender_id' => $data->student_gender ?? null,
'student_country_id' => $data->student_country ?? null,
'student_region_id' => $data->student_region ?? null,
'student_commune_id' => $data->student_commune ?? null,
'student_address' => $data->student_address ?? null,
'parent_relationship_id' => $data->parent_relationship ?? null,
'parent_first_name' => ($data->parent_first_name),
'parent_second_name' => ($data->parent_second_name ?? ''),
'parent_last_name' => ($data->parent_last_name),
'parent_second_last_name' => ($data->parent_second_last_name ?? ''),
'parent_rut' => !empty($data->parent_rut) ? formatterRut($data->parent_rut, false) : null,
'parent_email' => strLower($data->parent_email ?? null),
'parent_mobile' => $data->parent_mobile ?? null,
'parent_gender_id' => $data->parent_gender ?? null,
'parent_profession' => strUpper($data->parent_profession ?? ''),
'parent_country_id' => $data->parent_country ?? null,
'parent_region_id' => $data->parent_region ?? null,
'parent_commune_id' => $data->parent_commune ?? null,
'parent_address' => $data->parent_address ?? null,
'course_id' => $data->course ?? null,
'previous_school' => $data->previous_school ?? null,
'siblings' => $data->siblings ?? 0,
'special_needs' => $data->special_needs ?? null,
'date_entry' => $data->date_entry ?? null,
'user_updated' => auth()->check() ? auth()->id() : null,
'updated_at' => now(),
]);
$registerData->save();
return $registerData;
}
/**
* Obtener ID del estado de postulaci贸n
*/
public function getStatus($code = 'in_curse', $getFirst = false)
{
$status = $getFirst
? StatusPostulation::first()
: StatusPostulation::where('code', $code)->first();
if (!$status) {
throw new Exception("Estado de postulaci贸n no existe o fue eliminado", 404);
}
return $status->id;
}
}