60 lines
2.2 KiB
PHP
60 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\Tuition;
|
|
|
|
use App\Interfaces\TuitionCalculatorInterface;
|
|
|
|
final class OldTuitionCalculatorService implements TuitionCalculatorInterface
|
|
{
|
|
public function calculateFamilyTuition(array $students, array $config): array
|
|
{
|
|
$gradeFee = (int) ($config['grade_fee'] ?? 9);
|
|
$firstStudentFee = $this->toCents($config['first_student_fee'] ?? $config['new_tuition_full_amount'] ?? 380);
|
|
$secondStudentFee = isset($config['second_student_fee']) && $config['second_student_fee'] !== '' && $config['second_student_fee'] !== null
|
|
? $this->toCents($config['second_student_fee'])
|
|
: max(0, $firstStudentFee - 10000);
|
|
|
|
usort($students, function (array $left, array $right) use ($gradeFee): int {
|
|
$leftLevel = GradeLevelParser::parse($left['grade_level'] ?? null, $gradeFee);
|
|
$rightLevel = GradeLevelParser::parse($right['grade_level'] ?? null, $gradeFee);
|
|
|
|
return [$leftLevel, (int) ($left['student_id'] ?? 0)] <=> [$rightLevel, (int) ($right['student_id'] ?? 0)];
|
|
});
|
|
|
|
$familyPosition = 0;
|
|
$details = [];
|
|
|
|
foreach ($students as $student) {
|
|
$familyPosition++;
|
|
$amountCents = $familyPosition === 1 ? $firstStudentFee : $secondStudentFee;
|
|
$rule = $familyPosition === 1 ? 'old_first_student_fee' : 'old_additional_student_fee';
|
|
|
|
$details[] = [
|
|
'student_id' => (int) ($student['student_id'] ?? 0),
|
|
'student_name' => (string) ($student['student_name'] ?? ''),
|
|
'grade_level' => $student['grade_level'] ?? null,
|
|
'rule' => $rule,
|
|
'amount' => $this->fromCents($amountCents),
|
|
];
|
|
}
|
|
|
|
$total = array_sum(array_map(fn (array $detail): int => $this->toCents($detail['amount']), $details));
|
|
|
|
return [
|
|
'calculator' => 'old',
|
|
'total' => $this->fromCents($total),
|
|
'details' => $details,
|
|
];
|
|
}
|
|
|
|
private function toCents($amount): int
|
|
{
|
|
return (int) round(((float) $amount) * 100);
|
|
}
|
|
|
|
private function fromCents(int $cents): string
|
|
{
|
|
return number_format($cents / 100, 2, '.', '');
|
|
}
|
|
}
|