Commit 43259f2c authored by Mahmoud Aglan's avatar Mahmoud Aglan

Rename training_groups.program_id to training_program_id (naming convention)

Per project rule 01-migration-first.md, FK columns must follow
{singular_table}_id convention. Updates migration, models,
services, and all Livewire queries/views that reference this column.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 0cc5b982
...@@ -18,7 +18,7 @@ class TrainingGroup extends Model ...@@ -18,7 +18,7 @@ class TrainingGroup extends Model
use BelongsToAcademy, HasUuid, SoftDeletes, Auditable; use BelongsToAcademy, HasUuid, SoftDeletes, Auditable;
protected $fillable = [ protected $fillable = [
'academy_id', 'program_id', 'branch_id', 'academy_id', 'training_program_id', 'branch_id',
'name', 'name_ar', 'code', 'name', 'name_ar', 'code',
'head_trainer_id', 'max_capacity', 'current_count', 'waitlist_count', 'head_trainer_id', 'max_capacity', 'current_count', 'waitlist_count',
'season', 'start_date', 'end_date', 'season', 'start_date', 'end_date',
...@@ -38,7 +38,7 @@ class TrainingGroup extends Model ...@@ -38,7 +38,7 @@ class TrainingGroup extends Model
public function program(): BelongsTo public function program(): BelongsTo
{ {
return $this->belongsTo(TrainingProgram::class, 'program_id'); return $this->belongsTo(TrainingProgram::class, 'training_program_id');
} }
public function branch(): BelongsTo public function branch(): BelongsTo
......
...@@ -75,7 +75,7 @@ public function creator(): BelongsTo ...@@ -75,7 +75,7 @@ public function creator(): BelongsTo
public function groups(): HasMany public function groups(): HasMany
{ {
return $this->hasMany(TrainingGroup::class, 'program_id'); return $this->hasMany(TrainingGroup::class, 'training_program_id');
} }
public function enrollments(): HasMany public function enrollments(): HasMany
......
...@@ -41,7 +41,7 @@ public function enroll(Participant $participant, TrainingGroup $group, User $act ...@@ -41,7 +41,7 @@ public function enroll(Participant $participant, TrainingGroup $group, User $act
// Guard: one group per program rule // Guard: one group per program rule
$existingInProgram = Enrollment::where('participant_id', $participant->id) $existingInProgram = Enrollment::where('participant_id', $participant->id)
->where('training_program_id', $group->program_id) ->where('training_program_id', $group->training_program_id)
->whereIn('status', ['pending', 'active']) ->whereIn('status', ['pending', 'active'])
->exists(); ->exists();
if ($existingInProgram) { if ($existingInProgram) {
...@@ -69,7 +69,7 @@ public function enroll(Participant $participant, TrainingGroup $group, User $act ...@@ -69,7 +69,7 @@ public function enroll(Participant $participant, TrainingGroup $group, User $act
$enrollment = Enrollment::create([ $enrollment = Enrollment::create([
'participant_id' => $participant->id, 'participant_id' => $participant->id,
'training_group_id' => $group->id, 'training_group_id' => $group->id,
'training_program_id' => $group->program_id, 'training_program_id' => $group->training_program_id,
'enrollment_date' => now()->toDateString(), 'enrollment_date' => now()->toDateString(),
'start_date' => $options['start_date'] ?? $group->start_date ?? now()->toDateString(), 'start_date' => $options['start_date'] ?? $group->start_date ?? now()->toDateString(),
'end_date' => $options['end_date'] ?? $group->end_date, 'end_date' => $options['end_date'] ?? $group->end_date,
...@@ -124,7 +124,7 @@ public function enrollInProgram(Participant $participant, TrainingProgram $progr ...@@ -124,7 +124,7 @@ public function enrollInProgram(Participant $participant, TrainingProgram $progr
private function findOrCreateGroup(TrainingProgram $program, User $actor, bool $allowOverload): TrainingGroup private function findOrCreateGroup(TrainingProgram $program, User $actor, bool $allowOverload): TrainingGroup
{ {
// Find groups accepting enrollments (forming or active) with available capacity // Find groups accepting enrollments (forming or active) with available capacity
$availableGroup = TrainingGroup::where('program_id', $program->id) $availableGroup = TrainingGroup::where('training_program_id', $program->id)
->whereIn('status', ['forming', 'active']) ->whereIn('status', ['forming', 'active'])
->lockForUpdate() ->lockForUpdate()
->get() ->get()
...@@ -139,7 +139,7 @@ private function findOrCreateGroup(TrainingProgram $program, User $actor, bool $ ...@@ -139,7 +139,7 @@ private function findOrCreateGroup(TrainingProgram $program, User $actor, bool $
// No group with capacity found — handle overload or auto-create // No group with capacity found — handle overload or auto-create
if ($allowOverload) { if ($allowOverload) {
// Overload: pick the least-full group (even though all are full) // Overload: pick the least-full group (even though all are full)
$leastFullGroup = TrainingGroup::where('program_id', $program->id) $leastFullGroup = TrainingGroup::where('training_program_id', $program->id)
->whereIn('status', ['forming', 'active', 'full']) ->whereIn('status', ['forming', 'active', 'full'])
->lockForUpdate() ->lockForUpdate()
->orderByRaw('(max_capacity - current_count) DESC') ->orderByRaw('(max_capacity - current_count) DESC')
...@@ -161,7 +161,7 @@ private function findOrCreateGroup(TrainingProgram $program, User $actor, bool $ ...@@ -161,7 +161,7 @@ private function findOrCreateGroup(TrainingProgram $program, User $actor, bool $
*/ */
private function autoCreateGroup(TrainingProgram $program, User $actor): TrainingGroup private function autoCreateGroup(TrainingProgram $program, User $actor): TrainingGroup
{ {
$existingCount = TrainingGroup::where('program_id', $program->id)->count(); $existingCount = TrainingGroup::where('training_program_id', $program->id)->count();
$sequenceNumber = $existingCount + 1; $sequenceNumber = $existingCount + 1;
$programName = $program->name_ar ?: $program->name; $programName = $program->name_ar ?: $program->name;
...@@ -170,7 +170,7 @@ private function autoCreateGroup(TrainingProgram $program, User $actor): Trainin ...@@ -170,7 +170,7 @@ private function autoCreateGroup(TrainingProgram $program, User $actor): Trainin
return $this->groupService->create([ return $this->groupService->create([
'academy_id' => $program->academy_id, 'academy_id' => $program->academy_id,
'program_id' => $program->id, 'training_program_id' => $program->id,
'branch_id' => $program->branch_id, 'branch_id' => $program->branch_id,
'name' => $groupName, 'name' => $groupName,
'name_ar' => $groupNameAr, 'name_ar' => $groupNameAr,
...@@ -260,7 +260,7 @@ public function transfer(Enrollment $enrollment, TrainingGroup $toGroup, User $a ...@@ -260,7 +260,7 @@ public function transfer(Enrollment $enrollment, TrainingGroup $toGroup, User $a
$newEnrollment = Enrollment::create([ $newEnrollment = Enrollment::create([
'participant_id' => $enrollment->participant_id, 'participant_id' => $enrollment->participant_id,
'training_group_id' => $toGroup->id, 'training_group_id' => $toGroup->id,
'training_program_id' => $toGroup->program_id, 'training_program_id' => $toGroup->training_program_id,
'enrollment_date' => now()->toDateString(), 'enrollment_date' => now()->toDateString(),
'start_date' => now()->toDateString(), 'start_date' => now()->toDateString(),
'end_date' => $toGroup->end_date, 'end_date' => $toGroup->end_date,
......
...@@ -22,7 +22,7 @@ class TrainingGroupService ...@@ -22,7 +22,7 @@ class TrainingGroupService
public function create(array $data, User $actor): TrainingGroup public function create(array $data, User $actor): TrainingGroup
{ {
return DB::transaction(function () use ($data, $actor) { return DB::transaction(function () use ($data, $actor) {
$program = TrainingProgram::findOrFail($data['program_id']); $program = TrainingProgram::findOrFail($data['training_program_id']);
return TrainingGroup::create(array_merge($data, [ return TrainingGroup::create(array_merge($data, [
'max_capacity' => $data['max_capacity'] ?? $program->max_participants, 'max_capacity' => $data['max_capacity'] ?? $program->max_participants,
......
...@@ -36,7 +36,7 @@ public function mount(?TrainingGroup $group = null): void ...@@ -36,7 +36,7 @@ public function mount(?TrainingGroup $group = null): void
if ($group && $group->exists) { if ($group && $group->exists) {
$this->group = $group; $this->group = $group;
$this->editing = true; $this->editing = true;
$this->program_id = $group->program_id; $this->program_id = $group->training_program_id;
$this->branch_id = $group->branch_id; $this->branch_id = $group->branch_id;
$this->name = $group->name ?? ''; $this->name = $group->name ?? '';
$this->name_ar = $group->name_ar; $this->name_ar = $group->name_ar;
...@@ -104,7 +104,7 @@ public function save(TrainingGroupService $service): void ...@@ -104,7 +104,7 @@ public function save(TrainingGroupService $service): void
try { try {
$data = [ $data = [
'program_id' => $this->program_id, 'training_program_id' => $this->program_id,
'branch_id' => $this->branch_id, 'branch_id' => $this->branch_id,
'name' => $this->name, 'name' => $this->name,
'name_ar' => $this->name_ar, 'name_ar' => $this->name_ar,
......
...@@ -84,7 +84,7 @@ public function render() ...@@ -84,7 +84,7 @@ public function render()
->orWhere('code', 'ilike', "%{$this->search}%"); ->orWhere('code', 'ilike', "%{$this->search}%");
})) }))
->when($this->status, fn ($q) => $q->where('status', $this->status)) ->when($this->status, fn ($q) => $q->where('status', $this->status))
->when($this->programFilter, fn ($q) => $q->where('program_id', $this->programFilter)) ->when($this->programFilter, fn ($q) => $q->where('training_program_id', $this->programFilter))
->orderBy($this->sortBy, $this->sortDir); ->orderBy($this->sortBy, $this->sortDir);
return view('livewire.groups.group-list', [ return view('livewire.groups.group-list', [
......
...@@ -237,7 +237,7 @@ public function render() ...@@ -237,7 +237,7 @@ public function render()
$groups = TrainingGroup::query() $groups = TrainingGroup::query()
->orderBy('name_ar') ->orderBy('name_ar')
->when($branchId, fn ($q) => $q->where('branch_id', $branchId)) ->when($branchId, fn ($q) => $q->where('branch_id', $branchId))
->when($this->programFilter, fn ($q) => $q->where('program_id', $this->programFilter)) ->when($this->programFilter, fn ($q) => $q->where('training_program_id', $this->programFilter))
->get(); ->get();
$programs = TrainingProgram::query() $programs = TrainingProgram::query()
...@@ -261,7 +261,7 @@ private function getFilteredSessionIds(mixed $branchId): Collection ...@@ -261,7 +261,7 @@ private function getFilteredSessionIds(mixed $branchId): Collection
return TrainingSession::query() return TrainingSession::query()
->when($this->groupFilter, fn ($q) => $q->where('training_group_id', $this->groupFilter)) ->when($this->groupFilter, fn ($q) => $q->where('training_group_id', $this->groupFilter))
->when(! $this->groupFilter && $this->programFilter, function ($q) { ->when(! $this->groupFilter && $this->programFilter, function ($q) {
$q->whereHas('group', fn ($gq) => $gq->where('program_id', $this->programFilter)); $q->whereHas('group', fn ($gq) => $gq->where('training_program_id', $this->programFilter));
}) })
->when($branchId, fn ($q) => $q->whereHas('group', fn ($gq) => $gq->where('branch_id', $branchId))) ->when($branchId, fn ($q) => $q->whereHas('group', fn ($gq) => $gq->where('branch_id', $branchId)))
->when($this->dateFrom, fn ($q) => $q->where('session_date', '>=', $this->dateFrom)) ->when($this->dateFrom, fn ($q) => $q->where('session_date', '>=', $this->dateFrom))
......
...@@ -100,7 +100,7 @@ public function render() ...@@ -100,7 +100,7 @@ public function render()
->where('session_date', '<=', $weekEnd) ->where('session_date', '<=', $weekEnd)
->whereNotIn('status', ['cancelled', 'rescheduled']) ->whereNotIn('status', ['cancelled', 'rescheduled'])
->when($branchId, fn ($q) => $q->whereHas('group', fn ($gq) => $gq->where('branch_id', $branchId))) ->when($branchId, fn ($q) => $q->whereHas('group', fn ($gq) => $gq->where('branch_id', $branchId)))
->when($this->programFilter, fn ($q) => $q->whereHas('group', fn ($gq) => $gq->where('program_id', $this->programFilter))) ->when($this->programFilter, fn ($q) => $q->whereHas('group', fn ($gq) => $gq->where('training_program_id', $this->programFilter)))
->when($this->trainerFilter, fn ($q) => $q->where('trainer_id', $this->trainerFilter)) ->when($this->trainerFilter, fn ($q) => $q->where('trainer_id', $this->trainerFilter))
->orderBy('start_time') ->orderBy('start_time')
->get(); ->get();
...@@ -148,7 +148,7 @@ private function getProgramColors($sessions): array ...@@ -148,7 +148,7 @@ private function getProgramColors($sessions): array
'bg-pink-100 border-pink-400 text-pink-800', 'bg-pink-100 border-pink-400 text-pink-800',
]; ];
$programIds = $sessions->pluck('group.program_id')->unique()->values(); $programIds = $sessions->pluck('group.training_program_id')->unique()->values();
$map = []; $map = [];
foreach ($programIds as $index => $programId) { foreach ($programIds as $index => $programId) {
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('training_groups', function (Blueprint $table) {
$table->renameColumn('program_id', 'training_program_id');
});
}
public function down(): void
{
Schema::table('training_groups', function (Blueprint $table) {
$table->renameColumn('training_program_id', 'program_id');
});
}
};
This diff is collapsed.
...@@ -129,7 +129,7 @@ class="w-full rounded-lg border-gray-300 text-sm shadow-sm focus:border-blue-500 ...@@ -129,7 +129,7 @@ class="w-full rounded-lg border-gray-300 text-sm shadow-sm focus:border-blue-500
$endMin = (int) substr($session->end_time, 3, 2); $endMin = (int) substr($session->end_time, 3, 2);
$durationMinutes = ($endHour * 60 + $endMin) - ($startHour * 60 + $startMin); $durationMinutes = ($endHour * 60 + $endMin) - ($startHour * 60 + $startMin);
$heightRem = max(3.5, ($durationMinutes / 60) * 3.75); $heightRem = max(3.5, ($durationMinutes / 60) * 3.75);
$colorClass = $programColors[$session->group?->program_id] ?? 'bg-gray-100 border-gray-400 text-gray-800'; $colorClass = $programColors[$session->group?->training_program_id] ?? 'bg-gray-100 border-gray-400 text-gray-800';
@endphp @endphp
<div <div
class="mb-1 overflow-hidden rounded-md border-s-4 p-1.5 text-xs {{ $colorClass }}" class="mb-1 overflow-hidden rounded-md border-s-4 p-1.5 text-xs {{ $colorClass }}"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment