add test batches

This commit is contained in:
root
2026-06-08 23:45:55 -04:00
parent c792b8be05
commit 8d4d610b82
1480 changed files with 22587 additions and 10762 deletions
@@ -3,6 +3,7 @@
namespace App\Services\Inventory;
use App\Models\InventoryCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class InventoryCategoryService
@@ -15,7 +16,6 @@ class InventoryCategoryService
if ($type) {
$query->where('type', $this->normalizeType($type));
}
return $query->get()->toArray();
}
@@ -37,8 +37,7 @@ class InventoryCategoryService
try {
$category = InventoryCategory::query()->create($payload);
} catch (\Throwable $e) {
Log::error('Inventory category create failed: '.$e->getMessage());
Log::error('Inventory category create failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to save category.'];
}
@@ -48,7 +47,7 @@ class InventoryCategoryService
public function update(int $id, array $data): array
{
$category = InventoryCategory::query()->find($id);
if (! $category) {
if (!$category) {
return ['ok' => false, 'message' => 'Category not found.'];
}
@@ -69,8 +68,7 @@ class InventoryCategoryService
try {
$category->update($payload);
} catch (\Throwable $e) {
Log::error('Inventory category update failed: '.$e->getMessage());
Log::error('Inventory category update failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to save category.'];
}
@@ -80,15 +78,14 @@ class InventoryCategoryService
public function delete(int $id): array
{
$category = InventoryCategory::query()->find($id);
if (! $category) {
if (!$category) {
return ['ok' => false, 'message' => 'Category not found.'];
}
try {
$category->delete();
} catch (\Throwable $e) {
Log::error('Inventory category delete failed: '.$e->getMessage());
Log::error('Inventory category delete failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to delete category.'];
}
@@ -132,7 +129,6 @@ class InventoryCategoryService
private function normalizeType(?string $type): string
{
$type = strtolower((string) $type);
return in_array($type, $this->types, true) ? $type : 'office';
}
}
+16 -23
View File
@@ -2,8 +2,8 @@
namespace App\Services\Inventory;
use App\Models\InventoryCategory;
use App\Models\InventoryItem;
use App\Models\InventoryCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
@@ -14,7 +14,8 @@ class InventoryItemService
public function __construct(
private InventoryContextService $context,
private InventoryMovementService $movementService
) {}
) {
}
public function list(array $filters = []): array
{
@@ -31,7 +32,7 @@ class InventoryItemService
$builder->where('semester', $semester);
}
if ($q !== '') {
$builder->where('name', 'like', '%'.$q.'%');
$builder->where('name', 'like', '%' . $q . '%');
}
$items = $builder->orderBy('name')->get()->toArray();
@@ -61,7 +62,6 @@ class InventoryItemService
public function find(int $id): ?array
{
$item = InventoryItem::query()->find($id);
return $item ? $item->toArray() : null;
}
@@ -78,12 +78,10 @@ class InventoryItemService
} else {
$this->movementService->recalcQuantity((int) $item->id);
}
return ['ok' => true, 'item' => $item->toArray()];
});
} catch (\Throwable $e) {
Log::error('Inventory item create failed: '.$e->getMessage());
Log::error('Inventory item create failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to create item.'];
}
}
@@ -91,7 +89,7 @@ class InventoryItemService
public function update(int $id, array $data, ?int $userId = null): array
{
$item = InventoryItem::query()->find($id);
if (! $item) {
if (!$item) {
return ['ok' => false, 'message' => 'Item not found.'];
}
@@ -101,8 +99,7 @@ class InventoryItemService
try {
$item->update($payload);
} catch (\Throwable $e) {
Log::error('Inventory item update failed: '.$e->getMessage());
Log::error('Inventory item update failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to update item.'];
}
@@ -112,15 +109,14 @@ class InventoryItemService
public function delete(int $id): array
{
$item = InventoryItem::query()->find($id);
if (! $item) {
if (!$item) {
return ['ok' => false, 'message' => 'Item not found.'];
}
try {
$item->delete();
} catch (\Throwable $e) {
Log::error('Inventory item delete failed: '.$e->getMessage());
Log::error('Inventory item delete failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to delete item.'];
}
@@ -130,7 +126,7 @@ class InventoryItemService
public function auditClassroom(int $itemId, array $data, ?int $userId = null): array
{
$item = InventoryItem::query()->find($itemId);
if (! $item || $item->type !== 'classroom') {
if (!$item || $item->type !== 'classroom') {
return ['ok' => false, 'message' => 'Invalid classroom item.'];
}
@@ -163,7 +159,7 @@ class InventoryItemService
$totalIn += -$deltaMiss;
}
if ($totalOut > 0 && ! $this->movementService->recordMovement($itemId, -$totalOut, 'out', 'Audit: loss/missing increased')) {
if ($totalOut > 0 && !$this->movementService->recordMovement($itemId, -$totalOut, 'out', 'Audit: loss/missing increased')) {
return ['ok' => false, 'message' => 'Not enough stock to apply audit.'];
}
if ($totalIn > 0) {
@@ -186,7 +182,7 @@ class InventoryItemService
public function adjustStock(int $itemId, array $data, ?int $userId = null): array
{
$item = InventoryItem::query()->find($itemId);
if (! $item) {
if (!$item) {
return ['ok' => false, 'message' => 'Item not found.'];
}
@@ -203,7 +199,7 @@ class InventoryItemService
$type = ($mode === 'out') ? 'out' : (($mode === 'in') ? 'in' : 'adjust');
$ok = $this->movementService->recordMovement($itemId, $delta, $type, $reason, $note, null, null, $userId);
if (! $ok) {
if (!$ok) {
return ['ok' => false, 'message' => 'Not enough stock to apply adjustment.'];
}
@@ -246,7 +242,6 @@ class InventoryItemService
private function normalizeType(?string $type): string
{
$type = strtolower((string) $type);
return in_array($type, $this->types, true) ? $type : 'classroom';
}
@@ -272,9 +267,8 @@ class InventoryItemService
} else {
$out = [];
foreach ($ids as $id) {
$out[$id] = 'User #'.$id;
$out[$id] = 'User #' . $id;
}
return $out;
}
@@ -282,9 +276,8 @@ class InventoryItemService
$map = [];
foreach ($rows as $r) {
$disp = trim((string) ($r->display_name ?? ''));
$map[(int) $r->id] = $disp !== '' ? $disp : ('User #'.$r->id);
$map[(int) $r->id] = $disp !== '' ? $disp : ('User #' . $r->id);
}
return $map;
}
@@ -304,7 +297,7 @@ class InventoryItemService
->all();
$currentYear = $this->context->schoolYear();
if ($currentYear && ! in_array($currentYear, $years, true)) {
if ($currentYear && !in_array($currentYear, $years, true)) {
array_unshift($years, $currentYear);
}
@@ -9,7 +9,9 @@ use Illuminate\Support\Facades\Log;
class InventoryMovementService
{
public function __construct(private InventoryContextService $context) {}
public function __construct(private InventoryContextService $context)
{
}
public function list(array $filters = []): array
{
@@ -49,7 +51,7 @@ class InventoryMovementService
$qtyChange = (int) ($data['qty_change'] ?? 0);
$item = InventoryItem::query()->find($itemId);
if (! $item) {
if (!$item) {
return ['ok' => false, 'message' => 'Selected item not found.'];
}
@@ -76,8 +78,7 @@ class InventoryMovementService
InventoryMovement::query()->create($payload);
$this->recalcQuantity($itemId);
} catch (\Throwable $e) {
Log::error('Inventory movement create failed: '.$e->getMessage());
Log::error('Inventory movement create failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Could not save movement.'];
}
@@ -87,7 +88,7 @@ class InventoryMovementService
public function update(int $id, array $data): array
{
$movement = InventoryMovement::query()->find($id);
if (! $movement) {
if (!$movement) {
return ['ok' => false, 'message' => 'Movement not found.'];
}
@@ -117,8 +118,7 @@ class InventoryMovementService
$movement->update($payload);
$this->recalcQuantity($itemId);
} catch (\Throwable $e) {
Log::error('Inventory movement update failed: '.$e->getMessage());
Log::error('Inventory movement update failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Could not update movement.'];
}
@@ -128,7 +128,7 @@ class InventoryMovementService
public function delete(int $id): array
{
$movement = InventoryMovement::query()->find($id);
if (! $movement) {
if (!$movement) {
return ['ok' => false, 'message' => 'Movement not found.'];
}
@@ -139,8 +139,7 @@ class InventoryMovementService
$this->recalcQuantity($itemId);
}
} catch (\Throwable $e) {
Log::error('Inventory movement delete failed: '.$e->getMessage());
Log::error('Inventory movement delete failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Could not delete movement.'];
}
@@ -170,8 +169,7 @@ class InventoryMovementService
$this->recalcQuantity($itemId);
}
} catch (\Throwable $e) {
Log::error('Inventory movement bulk delete failed: '.$e->getMessage());
Log::error('Inventory movement bulk delete failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Bulk delete failed.'];
}
@@ -190,7 +188,7 @@ class InventoryMovementService
): bool {
$isInitial = ($type === 'initial');
if (! $isInitial) {
if (!$isInitial) {
$this->ensureInitialMovement($itemId);
$this->ensureInitialMovementForYear($itemId, $this->context->schoolYear());
$this->recalcQuantity($itemId);
@@ -219,7 +217,6 @@ class InventoryMovementService
]);
$this->recalcQuantity($itemId);
return true;
}
@@ -236,7 +233,7 @@ class InventoryMovementService
}
$item = InventoryItem::query()->select('id', 'type', 'quantity', 'needs_repair_qty', 'good_qty')->find($itemId);
if (! $item) {
if (!$item) {
return;
}
@@ -253,7 +250,7 @@ class InventoryMovementService
}
}
if (! empty($updates)) {
if (!empty($updates)) {
$item->update($updates);
}
}
@@ -284,7 +281,7 @@ class InventoryMovementService
->where('school_year', $schoolYear)
->exists();
if (! $hasAnyThisYear) {
if (!$hasAnyThisYear) {
$onHand = (int) (InventoryItem::query()->where('id', $itemId)->value('quantity') ?? 0);
InventoryMovement::query()->create([
'item_id' => $itemId,
@@ -293,7 +290,6 @@ class InventoryMovementService
'school_year' => $schoolYear,
'semester' => $this->context->semester() ?: null,
]);
return;
}
@@ -303,7 +299,7 @@ class InventoryMovementService
->where('movement_type', 'initial')
->exists();
if (! $hasInitial) {
if (!$hasInitial) {
$onHand = (int) (InventoryItem::query()->where('id', $itemId)->value('quantity') ?? 0);
$netYear = (int) (InventoryMovement::query()
->where('item_id', $itemId)
@@ -326,14 +322,12 @@ class InventoryMovementService
if (in_array($movementType, ['out', 'distribution'], true) && $qtyChange > 0) {
return -abs($qtyChange);
}
return $qtyChange;
}
private function wouldGoNegative(int $itemId, int $delta): bool
{
$onHand = (int) (InventoryItem::query()->where('id', $itemId)->value('quantity') ?? 0);
return $onHand + $delta < 0;
}
@@ -342,7 +336,6 @@ class InventoryMovementService
if ($val === null || $val === '' || $val === '0') {
return null;
}
return (int) $val;
}
}
@@ -7,7 +7,9 @@ use Illuminate\Support\Facades\DB;
class InventorySummaryService
{
public function __construct(private InventoryContextService $context) {}
public function __construct(private InventoryContextService $context)
{
}
public function summary(string $type, ?string $schoolYear): array
{
@@ -22,7 +24,7 @@ class InventorySummaryService
$itemIds = array_map(static fn ($i) => (int) ($i['id'] ?? 0), $items);
$agg = [];
if (! empty($itemIds)) {
if (!empty($itemIds)) {
$qb = DB::table('inventory_movements')
->selectRaw('item_id,
SUM(CASE WHEN qty_change>0 THEN qty_change ELSE 0 END) AS received,
@@ -63,7 +65,7 @@ class InventorySummaryService
->leftJoin('users as u', 'u.id', '=', 'i.updated_by')
->orderBy('i.name');
if (! $isAllYears) {
if (!$isAllYears) {
$qbItems->where('i.school_year', $selectedYear);
}
@@ -85,7 +87,7 @@ class InventorySummaryService
'variance' => 0,
];
if (! empty($items)) {
if (!empty($items)) {
$itemIds = array_map('intval', array_column($items, 'id'));
$aggById = [];
$qb = DB::table('inventory_movements as m')
@@ -100,7 +102,7 @@ class InventorySummaryService
->whereIn('m.item_id', $itemIds)
->groupBy('m.item_id');
if (! $isAllYears) {
if (!$isAllYears) {
$qb->where('m.school_year', $selectedYear);
}
@@ -180,7 +182,7 @@ class InventorySummaryService
->all();
$currentYear = $this->context->schoolYear();
if ($currentYear && ! in_array($currentYear, $years, true)) {
if ($currentYear && !in_array($currentYear, $years, true)) {
array_unshift($years, $currentYear);
}
@@ -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 ?? ''));
}
}
+8 -11
View File
@@ -13,9 +13,9 @@ class SupplierService
$query = Supplier::query();
if ($q !== '') {
$query->where(function ($builder) use ($q) {
$builder->where('name', 'like', '%'.$q.'%')
->orWhere('email', 'like', '%'.$q.'%')
->orWhere('phone', 'like', '%'.$q.'%');
$builder->where('name', 'like', '%' . $q . '%')
->orWhere('email', 'like', '%' . $q . '%')
->orWhere('phone', 'like', '%' . $q . '%');
});
}
@@ -37,8 +37,7 @@ class SupplierService
try {
$supplier = Supplier::query()->create($this->payload($data));
} catch (\Throwable $e) {
Log::error('Supplier create failed: '.$e->getMessage());
Log::error('Supplier create failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to save supplier.'];
}
@@ -48,15 +47,14 @@ class SupplierService
public function update(int $id, array $data): array
{
$supplier = Supplier::query()->find($id);
if (! $supplier) {
if (!$supplier) {
return ['ok' => false, 'message' => 'Supplier not found.'];
}
try {
$supplier->update($this->payload($data));
} catch (\Throwable $e) {
Log::error('Supplier update failed: '.$e->getMessage());
Log::error('Supplier update failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to update supplier.'];
}
@@ -66,15 +64,14 @@ class SupplierService
public function delete(int $id): array
{
$supplier = Supplier::query()->find($id);
if (! $supplier) {
if (!$supplier) {
return ['ok' => false, 'message' => 'Supplier not found.'];
}
try {
$supplier->delete();
} catch (\Throwable $e) {
Log::error('Supplier delete failed: '.$e->getMessage());
Log::error('Supplier delete failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to delete supplier.'];
}
@@ -19,8 +19,7 @@ class SupplyCategoryService
'name' => (string) ($data['name'] ?? ''),
]);
} catch (\Throwable $e) {
Log::error('Supply category create failed: '.$e->getMessage());
Log::error('Supply category create failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to save category.'];
}
@@ -30,15 +29,14 @@ class SupplyCategoryService
public function update(int $id, array $data): array
{
$category = SupplyCategory::query()->find($id);
if (! $category) {
if (!$category) {
return ['ok' => false, 'message' => 'Category not found.'];
}
try {
$category->update(['name' => (string) ($data['name'] ?? '')]);
} catch (\Throwable $e) {
Log::error('Supply category update failed: '.$e->getMessage());
Log::error('Supply category update failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to update category.'];
}
@@ -48,15 +46,14 @@ class SupplyCategoryService
public function delete(int $id): array
{
$category = SupplyCategory::query()->find($id);
if (! $category) {
if (!$category) {
return ['ok' => false, 'message' => 'Category not found.'];
}
try {
$category->delete();
} catch (\Throwable $e) {
Log::error('Supply category delete failed: '.$e->getMessage());
Log::error('Supply category delete failed: ' . $e->getMessage());
return ['ok' => false, 'message' => 'Failed to delete category.'];
}