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,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;
}
}