add tests batch 20

This commit is contained in:
root
2026-06-09 01:03:53 -04:00
parent 95efb9652e
commit 6be4875c5e
1502 changed files with 13797 additions and 11313 deletions
@@ -9,18 +9,20 @@ use App\Models\StudentClass;
use App\Models\TeacherClass;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class InventoryTeacherDistributionService
{
public function __construct(
private InventoryContextService $context,
private InventoryMovementService $movementService
) {}
) {
}
public function formData(int $userId, ?int $classSectionId, ?int $itemId): array
{
$ctx = $this->buildTeacherClassContext($userId);
if (! $ctx['ok']) {
if (!$ctx['ok']) {
return $ctx;
}
@@ -31,7 +33,7 @@ class InventoryTeacherDistributionService
: (int) ($classSectionId ?? ($ctx['defaultClassSectionId'] ?? 0));
$classID = null;
if (! empty($selectedClassId)) {
if (!empty($selectedClassId)) {
$classID = ClassSection::getClassId($selectedClassId);
$classID = $classID !== null ? (int) $classID : null;
}
@@ -41,7 +43,7 @@ class InventoryTeacherDistributionService
->leftJoin('inventory_categories as c', 'c.id', '=', 'i.category_id')
->where('i.type', 'book');
if (! empty($classID)) {
if (!empty($classID)) {
$builder->where(function ($q) use ($classID) {
$q->where(function ($inner) use ($classID) {
$inner->whereNotNull('c.grade_min')
@@ -66,34 +68,27 @@ class InventoryTeacherDistributionService
$name = $r['category_name'] ?? 'Uncategorized';
$gmin = $r['grade_min'] ?? null;
$gmax = $r['grade_max'] ?? null;
if ($gmin === null && $gmax === null) {
return $name;
}
if ($gmin !== null && $gmax !== null) {
return $name.' (G'.(int) $gmin.'G'.(int) $gmax.')';
}
if ($gmin !== null) {
return $name.' (G'.(int) $gmin.'+)';
}
return $name.' (≤G'.(int) $gmax.')';
if ($gmin === null && $gmax === null) return $name;
if ($gmin !== null && $gmax !== null) return $name . ' (G' . (int) $gmin . 'G' . (int) $gmax . ')';
if ($gmin !== null) return $name . ' (G' . (int) $gmin . '+)';
return $name . ' (≤G' . (int) $gmax . ')';
};
$booksGrouped = [];
foreach ($rows as $r) {
$catId = (int) ($r['category_id'] ?? 0);
if (! isset($booksGrouped[$catId])) {
if (!isset($booksGrouped[$catId])) {
$booksGrouped[$catId] = [
'label' => $labelFor($r),
'items' => [],
];
}
$display = $r['name'];
if (! empty($r['isbn'])) {
$display .= ' — ISBN '.$r['isbn'];
if (!empty($r['isbn'])) {
$display .= ' — ISBN ' . $r['isbn'];
}
if (! empty($r['edition'])) {
$display .= ' ('.$r['edition'].')';
if (!empty($r['edition'])) {
$display .= ' (' . $r['edition'] . ')';
}
$booksGrouped[$catId]['items'][] = [
@@ -128,9 +123,7 @@ class InventoryTeacherDistributionService
->all();
foreach ($rowsHist as $r) {
$sid = (int) ($r['student_id'] ?? 0);
if ($sid) {
$already[$sid] = (int) ($r['qty'] ?? 0);
}
if ($sid) $already[$sid] = (int) ($r['qty'] ?? 0);
}
}
@@ -161,7 +154,7 @@ class InventoryTeacherDistributionService
public function distribute(int $userId, array $payload): array
{
$ctx = $this->buildTeacherClassContext($userId);
if (! $ctx['ok']) {
if (!$ctx['ok']) {
return $ctx;
}
@@ -175,12 +168,12 @@ class InventoryTeacherDistributionService
? (int) $classIds[0]
: (in_array($postedClassId, $classIds, true) ? $postedClassId : 0);
if (! $classSectionId) {
if (!$classSectionId) {
return ['ok' => false, 'message' => 'Invalid class selection.'];
}
$book = InventoryItem::query()->find($itemId);
if (! $book || $book->type !== 'book') {
if (!$book || $book->type !== 'book') {
return ['ok' => false, 'message' => 'Invalid book.'];
}
@@ -188,9 +181,7 @@ class InventoryTeacherDistributionService
$validIds = [];
foreach ($students as $st) {
$sid = $st['student_id'] ?? ($st['id'] ?? null);
if ($sid) {
$validIds[(int) $sid] = true;
}
if ($sid) $validIds[(int) $sid] = true;
}
$selectedIds = array_values(array_filter($selectedIds, fn ($sid) => isset($validIds[$sid])));
@@ -209,9 +200,7 @@ class InventoryTeacherDistributionService
->all();
foreach ($rows as $r) {
$sid = (int) ($r['student_id'] ?? 0);
if ($sid) {
$already[$sid] = (int) ($r['qty'] ?? 0);
}
if ($sid) $already[$sid] = (int) ($r['qty'] ?? 0);
}
$toGive = [];
@@ -254,7 +243,7 @@ class InventoryTeacherDistributionService
}
$user = User::query()->find($userId);
if (! $user) {
if (!$user) {
return ['ok' => false, 'message' => 'User not found.'];
}
@@ -264,13 +253,13 @@ class InventoryTeacherDistributionService
->pluck('r.name')
->toArray();
if (! in_array('teacher', $roles, true) && ! in_array('teacher_assistant', $roles, true)) {
if (!in_array('teacher', $roles, true) && !in_array('teacher_assistant', $roles, true)) {
return ['ok' => false, 'message' => 'Access denied.'];
}
$classSectionIds = array_column($classes, 'class_section_id');
$studentsData = [];
if (! empty($classSectionIds)) {
if (!empty($classSectionIds)) {
$students = StudentClass::getStudentsByClassSectionIds($classSectionIds);
foreach ($students as $student) {
$studentsData[$student['class_section_id']][] = $student;
@@ -289,7 +278,7 @@ class InventoryTeacherDistributionService
$taUser = User::query()->find($ta['teacher_id'] ?? 0);
if ($taUser) {
$csid = $ta['class_section_id'];
$taNames[$csid][] = trim(($taUser->firstname ?? '').' '.($taUser->lastname ?? ''));
$taNames[$csid][] = trim(($taUser->firstname ?? '') . ' ' . ($taUser->lastname ?? ''));
}
}