Commit 772a86ba authored by Fares's avatar Fares

fix(sa): fix group auto-creation and old() array conversion errors

- Remove non-existent facility_unit_id from sa_groups insert in
  enrollByCoach, add required code column with auto-generated value
- Guard schedule copy when no source group exists
- Fix old() helper usage for array fields (additional_coaches) by
  reading directly from session _old_input instead of old() which
  only returns strings
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 140ee3da
...@@ -455,37 +455,33 @@ final class EnrollmentService ...@@ -455,37 +455,33 @@ final class EnrollmentService
} }
// No available group — auto-create one // No available group — auto-create one
$coach = $db->selectOne("SELECT full_name_ar FROM sa_coaches WHERE id = ?", [$coachId]);
$coachName = $coach['full_name_ar'] ?? 'مدرب';
$newName = $program['name_ar'] . ' - ' . $coachName;
$newCode = 'GRP-' . strtoupper(substr(uniqid(), -6));
$sourceGroup = $db->selectOne( $sourceGroup = $db->selectOne(
"SELECT * FROM sa_groups WHERE program_id = ? AND is_archived = 0 ORDER BY id DESC LIMIT 1", "SELECT * FROM sa_groups WHERE program_id = ? AND is_archived = 0 ORDER BY id DESC LIMIT 1",
[$programId] [$programId]
); );
if (!$sourceGroup) {
return ['success' => false, 'error' => 'لا توجد مجموعات مرجعية لإنشاء مجموعة جديدة'];
}
$coach = $db->selectOne("SELECT full_name_ar FROM sa_coaches WHERE id = ?", [$coachId]);
$coachName = $coach['full_name_ar'] ?? 'مدرب';
$newName = $program['name_ar'] . ' - ' . $coachName;
$employeeId = (int) (App::getInstance()->session()->get('employee_id') ?? 0); $employeeId = (int) (App::getInstance()->session()->get('employee_id') ?? 0);
$db->beginTransaction(); $db->beginTransaction();
try { try {
$newGroupId = $db->insert('sa_groups', [ $newGroupId = $db->insert('sa_groups', [
'code' => $newCode,
'name_ar' => $newName, 'name_ar' => $newName,
'program_id' => $programId, 'program_id' => $programId,
'facility_unit_id' => $sourceGroup['facility_unit_id'],
'coach_id' => $coachId, 'coach_id' => $coachId,
'monthly_fee_member' => 0, 'monthly_fee_member' => 0,
'monthly_fee_nonmember' => 0, 'monthly_fee_nonmember' => 0,
'min_age' => $sourceGroup['min_age'], 'min_age' => $sourceGroup['min_age'] ?? null,
'max_age' => $sourceGroup['max_age'], 'max_age' => $sourceGroup['max_age'] ?? null,
'max_capacity' => (int) ($program['max_capacity'] ?? 20), 'max_capacity' => (int) ($program['max_capacity'] ?? 20),
'current_count' => 0, 'current_count' => 0,
'is_full' => 0, 'is_full' => 0,
'status' => 'active', 'status' => 'active',
'branch_id' => $sourceGroup['branch_id'] ?? null,
'created_by' => $employeeId, 'created_by' => $employeeId,
'created_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'),
...@@ -499,7 +495,8 @@ final class EnrollmentService ...@@ -499,7 +495,8 @@ final class EnrollmentService
'created_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s'),
]); ]);
// Copy schedule from source // Copy schedule from source if available
if ($sourceGroup) {
$schedules = $db->select( $schedules = $db->select(
"SELECT * FROM sa_group_schedule WHERE group_id = ? AND is_active = 1", "SELECT * FROM sa_group_schedule WHERE group_id = ? AND is_active = 1",
[(int) $sourceGroup['id']] [(int) $sourceGroup['id']]
...@@ -516,6 +513,7 @@ final class EnrollmentService ...@@ -516,6 +513,7 @@ final class EnrollmentService
'updated_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'),
]); ]);
} }
}
$db->commit(); $db->commit();
} catch (\Throwable $e) { } catch (\Throwable $e) {
......
...@@ -66,7 +66,8 @@ ...@@ -66,7 +66,8 @@
<label class="form-label">مدربين إضافيين</label> <label class="form-label">مدربين إضافيين</label>
<div id="additional-coaches" style="display:flex;flex-wrap:wrap;gap:8px;margin-top:6px;"> <div id="additional-coaches" style="display:flex;flex-wrap:wrap;gap:8px;margin-top:6px;">
<?php <?php
$oldAdditional = old('additional_coaches', []); $oldInput = \App\Core\App::getInstance()->session()->get('_old_input', []);
$oldAdditional = $oldInput['additional_coaches'] ?? [];
if (!is_array($oldAdditional)) $oldAdditional = []; if (!is_array($oldAdditional)) $oldAdditional = [];
?> ?>
<?php foreach ($coaches as $c): ?> <?php foreach ($coaches as $c): ?>
......
...@@ -66,7 +66,8 @@ ...@@ -66,7 +66,8 @@
<label class="form-label">مدربين إضافيين</label> <label class="form-label">مدربين إضافيين</label>
<div id="additional-coaches" style="display:flex;flex-wrap:wrap;gap:8px;margin-top:6px;"> <div id="additional-coaches" style="display:flex;flex-wrap:wrap;gap:8px;margin-top:6px;">
<?php <?php
$oldAdditional = old('additional_coaches', $groupCoachIds ?? []); $oldInput = \App\Core\App::getInstance()->session()->get('_old_input', []);
$oldAdditional = $oldInput['additional_coaches'] ?? ($groupCoachIds ?? []);
if (!is_array($oldAdditional)) $oldAdditional = []; if (!is_array($oldAdditional)) $oldAdditional = [];
?> ?>
<?php foreach ($coaches as $c): ?> <?php foreach ($coaches as $c): ?>
......
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