add stickers logic

This commit is contained in:
root
2026-03-10 16:40:11 -04:00
parent 3bf4c687da
commit 2e9a391280
17 changed files with 1047 additions and 390 deletions
@@ -0,0 +1,80 @@
<?php
namespace App\Http\Controllers\Api\Reports;
use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Reports\Stickers\StickerFormDataRequest;
use App\Http\Requests\Reports\Stickers\StickerPrintRequest;
use App\Http\Requests\Reports\Stickers\StickerStudentsByClassRequest;
use App\Http\Resources\Reports\Stickers\StickerClassSectionResource;
use App\Http\Resources\Reports\Stickers\StickerPresetResource;
use App\Http\Resources\Reports\Stickers\StickerStudentResource;
use App\Services\Reports\Stickers\StickerPresetService;
use App\Services\Reports\Stickers\StickerPrintService;
use App\Services\Reports\Stickers\StickerQueryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;
class StickersController extends BaseApiController
{
public function __construct(
private StickerQueryService $queryService,
private StickerPresetService $presetService,
private StickerPrintService $printService
) {
parent::__construct();
}
public function formData(StickerFormDataRequest $request): JsonResponse
{
$payload = $request->validated();
$schoolYear = $this->queryService->resolveSchoolYear($payload['school_year'] ?? null);
$classSectionId = (int) ($payload['class_section_id'] ?? 0);
$classes = $this->queryService->listClassesWithStudents($schoolYear);
$students = $classSectionId > 0
? $this->queryService->listStudentsByClass($classSectionId, $schoolYear)
: $this->queryService->listStudentsForYear($schoolYear);
$presets = $this->presetService->presets();
return $this->success([
'classes' => StickerClassSectionResource::collection($classes),
'students' => StickerStudentResource::collection($students),
'presets' => StickerPresetResource::collection($presets),
]);
}
public function studentsByClass(StickerStudentsByClassRequest $request): JsonResponse
{
$payload = $request->validated();
$schoolYear = $this->queryService->resolveSchoolYear($payload['school_year'] ?? null);
$classSectionId = (int) ($payload['class_section_id'] ?? 0);
$students = $this->queryService->listStudentsByClass($classSectionId, $schoolYear);
return $this->success([
'students' => StickerStudentResource::collection($students),
]);
}
public function print(StickerPrintRequest $request)
{
try {
$result = $this->printService->generate($request->validated());
} catch (\Throwable $e) {
Log::error('Sticker print failed: ' . $e->getMessage());
return $this->error('Failed to generate stickers.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
if (!($result['ok'] ?? false)) {
return $this->error($result['message'] ?? 'Unable to generate stickers.', Response::HTTP_UNPROCESSABLE_ENTITY);
}
return response($result['pdf'], 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="' . ($result['filename'] ?? 'Student_Stickers.pdf') . '"',
]);
}
}