99 lines
3.6 KiB
PHP
99 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Parents;
|
|
|
|
use App\Controllers\View\InvoiceController;
|
|
use App\Models\EnrollmentModel;
|
|
use App\Models\EventChargesModel;
|
|
use App\Models\EventModel;
|
|
|
|
class ParentEventParticipationService
|
|
{
|
|
public function __construct(
|
|
private readonly EventChargesModel $chargesModel,
|
|
private readonly EventModel $eventModel,
|
|
private readonly EnrollmentModel $enrollmentModel,
|
|
private readonly InvoiceController $invoiceController,
|
|
) {
|
|
}
|
|
|
|
public function pageData(int $parentId, string $schoolYear, string $semester): array
|
|
{
|
|
$activeEvents = $this->eventModel->getActiveEvents($schoolYear, $semester);
|
|
$chargesList = $this->chargesModel->getChargesWithEventInfo($parentId, $schoolYear, $semester);
|
|
|
|
$charges = [];
|
|
$externalParticipantsByEvent = [];
|
|
foreach ($chargesList as $charge) {
|
|
$studentId = $charge['student_id'] ?? null;
|
|
$eventId = (int) ($charge['event_id'] ?? 0);
|
|
|
|
if (! empty($studentId)) {
|
|
$charges[$studentId . ':' . $eventId] = [
|
|
'participation' => $charge['participation'],
|
|
'date' => $charge['updated_at'] ?? $charge['created_at'],
|
|
];
|
|
continue;
|
|
}
|
|
|
|
$externalName = trim((string) ($charge['external_firstname'] ?? '') . ' ' . (string) ($charge['external_lastname'] ?? ''));
|
|
if ($eventId > 0 && $externalName !== '') {
|
|
$externalParticipantsByEvent[$eventId][] = [
|
|
'name' => $externalName,
|
|
'note' => (string) ($charge['external_note'] ?? ''),
|
|
'participation' => (string) ($charge['participation'] ?? ''),
|
|
'event_paid' => ! empty($charge['event_paid']),
|
|
'charged' => (float) ($charge['charged'] ?? ($charge['event_amount'] ?? 0)),
|
|
];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'activeEvents' => $activeEvents,
|
|
'charges' => $charges,
|
|
'externalParticipantsByEvent' => $externalParticipantsByEvent,
|
|
'yourStudents' => $this->enrollmentModel->getEnrolledStudents($parentId, $schoolYear),
|
|
'activeEventCount' => is_array($activeEvents) ? count($activeEvents) : 0,
|
|
];
|
|
}
|
|
|
|
public function updateParticipation(array $participations, int $parentId, string $schoolYear, string $semester): void
|
|
{
|
|
foreach ($participations as $key => $value) {
|
|
[$studentId, $eventId] = explode(':', (string) $key);
|
|
|
|
$existing = $this->chargesModel->where([
|
|
'parent_id' => $parentId,
|
|
'student_id' => $studentId,
|
|
'event_id' => $eventId,
|
|
])->first();
|
|
|
|
if ($value === 'no') {
|
|
if ($existing) {
|
|
$this->chargesModel->delete($existing['id']);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if ($existing) {
|
|
$this->chargesModel->update($existing['id'], ['participation' => $value]);
|
|
continue;
|
|
}
|
|
|
|
$event = $this->eventModel->getEvent($eventId, $schoolYear);
|
|
$this->chargesModel->insert([
|
|
'parent_id' => $parentId,
|
|
'student_id' => $studentId,
|
|
'event_id' => $eventId,
|
|
'participation' => $value,
|
|
'charged' => $event['amount'],
|
|
'school_year' => $schoolYear,
|
|
'semester' => $semester,
|
|
'updated_by' => $parentId,
|
|
]);
|
|
}
|
|
|
|
$this->invoiceController->generateInvoice($parentId);
|
|
}
|
|
}
|