113 lines
3.8 KiB
PHP
113 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Payments;
|
|
|
|
use App\Models\Configuration;
|
|
use App\Models\Enrollment;
|
|
use App\Models\StudentClass;
|
|
use App\Models\User;
|
|
use App\Services\Billing\ChargeService;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class PaymentEventChargesService
|
|
{
|
|
public function __construct(private ChargeService $chargeService) {}
|
|
|
|
public function listCharges(?string $schoolYear, ?string $semester): array
|
|
{
|
|
$schoolYear = $schoolYear ?: Configuration::getConfig('school_year');
|
|
$semester = $semester ?: Configuration::getConfig('semester');
|
|
|
|
$charges = DB::table('event_charges')
|
|
->select(
|
|
'event_charges.*',
|
|
'users.firstname AS parent_firstname',
|
|
'users.lastname AS parent_lastname',
|
|
'students.firstname AS student_firstname',
|
|
'students.lastname AS student_lastname'
|
|
)
|
|
->leftJoin('users', 'users.id', '=', 'event_charges.parent_id')
|
|
->leftJoin('students', 'students.id', '=', 'event_charges.student_id')
|
|
->where('event_charges.school_year', $schoolYear)
|
|
->where('event_charges.semester', $semester)
|
|
->orderByDesc('event_charges.created_at')
|
|
->get()
|
|
->map(fn ($row) => (array) $row)
|
|
->all();
|
|
|
|
return [
|
|
'charges' => $charges,
|
|
'parents' => User::getParents(),
|
|
'school_year' => $schoolYear,
|
|
'semester' => $semester,
|
|
];
|
|
}
|
|
|
|
public function addCharges(array $payload, int $userId): int
|
|
{
|
|
$studentIds = $payload['student_ids'] ?? [];
|
|
$parentId = (int) $payload['parent_id'];
|
|
$schoolYear = (string) $payload['school_year'];
|
|
$semester = (string) $payload['semester'];
|
|
$eventName = (string) $payload['event_name'];
|
|
$amount = (float) $payload['amount'];
|
|
|
|
$count = 0;
|
|
|
|
if (! empty($studentIds)) {
|
|
foreach ($studentIds as $studentId) {
|
|
$result = $this->chargeService->createEventCharge([
|
|
'parent_id' => $parentId,
|
|
'student_id' => (int) $studentId,
|
|
'event_name' => $eventName,
|
|
'amount' => $amount,
|
|
'description' => $payload['description'] ?? null,
|
|
'school_year' => $schoolYear,
|
|
'semester' => $semester,
|
|
'created_by' => $userId,
|
|
]);
|
|
|
|
if ($result['ok'] ?? false) {
|
|
$count++;
|
|
}
|
|
}
|
|
} elseif (! empty($payload['student_id'])) {
|
|
$result = $this->chargeService->createEventCharge([
|
|
'parent_id' => $parentId,
|
|
'student_id' => (int) $payload['student_id'],
|
|
'event_name' => $eventName,
|
|
'amount' => $amount,
|
|
'description' => $payload['description'] ?? null,
|
|
'school_year' => $schoolYear,
|
|
'semester' => $semester,
|
|
'created_by' => $userId,
|
|
]);
|
|
|
|
if ($result['ok'] ?? false) {
|
|
$count++;
|
|
}
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
public function getEnrolledStudents(int $parentId, ?string $schoolYear): array
|
|
{
|
|
$schoolYear = $schoolYear ?: Configuration::getConfig('school_year');
|
|
|
|
$enrollments = Enrollment::getEnrolledStudents($parentId, $schoolYear);
|
|
|
|
$students = [];
|
|
foreach ($enrollments as $row) {
|
|
$students[] = [
|
|
'id' => (int) ($row->id ?? 0),
|
|
'firstname' => (string) ($row->firstname ?? ''),
|
|
'lastname' => (string) ($row->lastname ?? ''),
|
|
'grade' => StudentClass::getStudentGrade((int) ($row->id ?? 0)) ?: 'N/A',
|
|
];
|
|
}
|
|
|
|
return $students;
|
|
}
|
|
}
|