add all controllers logic
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ClassPreparation;
|
||||
|
||||
use App\Models\ClassPrepAdjustment;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ClassPreparationAdjustmentWriterService
|
||||
{
|
||||
public function saveAdjustments(string $classSectionId, string $schoolYear, array $adjustments, array $allowedItems): int
|
||||
{
|
||||
$allowed = array_fill_keys($allowedItems, true);
|
||||
|
||||
return DB::transaction(function () use ($classSectionId, $schoolYear, $adjustments, $allowed) {
|
||||
$count = 0;
|
||||
|
||||
foreach ($adjustments as $itemName => $adjustment) {
|
||||
$item = (string) $itemName;
|
||||
if ($item === '' || !isset($allowed[$item])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$row = ClassPrepAdjustment::query()
|
||||
->where('class_section_id', $classSectionId)
|
||||
->where('school_year', $schoolYear)
|
||||
->where('item_name', $item)
|
||||
->first();
|
||||
|
||||
if ($row) {
|
||||
$row->update([
|
||||
'adjustment' => (int) $adjustment,
|
||||
'adjustable' => 1,
|
||||
]);
|
||||
$count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
ClassPrepAdjustment::query()->create([
|
||||
'class_section_id' => $classSectionId,
|
||||
'item_name' => $item,
|
||||
'adjustment' => (int) $adjustment,
|
||||
'school_year' => $schoolYear,
|
||||
'adjustable' => 1,
|
||||
]);
|
||||
$count++;
|
||||
}
|
||||
|
||||
return $count;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ClassPreparation;
|
||||
|
||||
use App\Models\ClassSection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ClassPreparationPrintService
|
||||
{
|
||||
public function __construct(
|
||||
private ClassPreparationContextService $context,
|
||||
private ClassPreparationRosterService $roster,
|
||||
private ClassPreparationCalculatorService $calculator,
|
||||
private ClassPreparationAdjustmentService $adjustments,
|
||||
private ClassPreparationLogService $logs
|
||||
) {
|
||||
}
|
||||
|
||||
public function printPrep(string $classSectionId, string $schoolYear, ?string $semester = null): array
|
||||
{
|
||||
$semester = $semester ?: $this->context->getSemester();
|
||||
$limitToSemester = $this->context->hasRosterForSemester($schoolYear, $semester);
|
||||
|
||||
$studentCount = $this->roster->getStudentCountForSection($schoolYear, $semester, $limitToSemester, $classSectionId);
|
||||
$classLevel = $this->calculator->getClassLevelBySection($classSectionId);
|
||||
$className = ClassSection::getClassSectionNameBySectionId($classSectionId) ?? $classSectionId;
|
||||
|
||||
$items = $this->calculator->calculatePrepItems($studentCount, $classLevel, $classSectionId);
|
||||
[$items, $adjMap] = $this->adjustments->applyAdjustments($items, $classSectionId, $schoolYear);
|
||||
|
||||
$printedAt = $this->utcNow();
|
||||
$ok = $this->logs->createLog($classSectionId, $className, $schoolYear, $items, $printedAt);
|
||||
|
||||
if (!$ok) {
|
||||
Log::warning('Failed to store class preparation log.', [
|
||||
'class_section_id' => $classSectionId,
|
||||
'school_year' => $schoolYear,
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
'ok' => $ok,
|
||||
'class_section_id' => $classSectionId,
|
||||
'class_section' => $className,
|
||||
'school_year' => $schoolYear,
|
||||
'semester' => $semester,
|
||||
'prep_items' => $items,
|
||||
'adjustments' => $adjMap,
|
||||
'printed_at' => $ok ? $printedAt : null,
|
||||
];
|
||||
}
|
||||
|
||||
private function utcNow(): string
|
||||
{
|
||||
if (function_exists('utc_now')) {
|
||||
return (string) utc_now();
|
||||
}
|
||||
return now('UTC')->toDateTimeString();
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ class ClassPreparationService
|
||||
private ClassPreparationCalculatorService $calculator;
|
||||
private ClassPreparationAdjustmentService $adjustments;
|
||||
private ClassPreparationLogService $logs;
|
||||
private ClassPreparationAdjustmentWriterService $adjustmentWriter;
|
||||
private ClassPreparationPrintService $printService;
|
||||
|
||||
public function __construct(
|
||||
ClassPreparationContextService $context,
|
||||
@@ -19,7 +21,9 @@ class ClassPreparationService
|
||||
ClassPreparationInventoryService $inventory,
|
||||
ClassPreparationCalculatorService $calculator,
|
||||
ClassPreparationAdjustmentService $adjustments,
|
||||
ClassPreparationLogService $logs
|
||||
ClassPreparationLogService $logs,
|
||||
ClassPreparationAdjustmentWriterService $adjustmentWriter,
|
||||
ClassPreparationPrintService $printService
|
||||
) {
|
||||
$this->context = $context;
|
||||
$this->roster = $roster;
|
||||
@@ -27,6 +31,8 @@ class ClassPreparationService
|
||||
$this->calculator = $calculator;
|
||||
$this->adjustments = $adjustments;
|
||||
$this->logs = $logs;
|
||||
$this->adjustmentWriter = $adjustmentWriter;
|
||||
$this->printService = $printService;
|
||||
}
|
||||
|
||||
public function listPrep(?string $schoolYear = null, ?string $semester = null): array
|
||||
@@ -111,6 +117,23 @@ class ClassPreparationService
|
||||
return $count;
|
||||
}
|
||||
|
||||
public function saveAdjustments(string $classSectionId, string $schoolYear, array $adjustments): int
|
||||
{
|
||||
$schoolYear = $schoolYear !== '' ? $schoolYear : $this->context->getSchoolYear();
|
||||
|
||||
return $this->adjustmentWriter->saveAdjustments(
|
||||
$classSectionId,
|
||||
$schoolYear,
|
||||
$adjustments,
|
||||
$this->calculator->getAllowedCategories()
|
||||
);
|
||||
}
|
||||
|
||||
public function printPrep(string $classSectionId, string $schoolYear, ?string $semester = null): array
|
||||
{
|
||||
return $this->printService->printPrep($classSectionId, $schoolYear, $semester);
|
||||
}
|
||||
|
||||
private function utcNow(): string
|
||||
{
|
||||
if (function_exists('utc_now')) {
|
||||
|
||||
Reference in New Issue
Block a user