fix tests
This commit is contained in:
@@ -9,15 +9,13 @@ 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
|
||||
{
|
||||
$selectedYear = trim((string) ($filters['school_year'] ?? $this->context->schoolYear()));
|
||||
$selectedSem = trim((string) ($filters['semester'] ?? $this->context->semester()));
|
||||
$includeVoided = !empty($filters['include_voided']);
|
||||
$includeVoided = ! empty($filters['include_voided']);
|
||||
|
||||
$builder = DB::table('inventory_movements as m')
|
||||
->select([
|
||||
@@ -36,7 +34,7 @@ class InventoryMovementService
|
||||
->orderBy('m.id', 'DESC');
|
||||
|
||||
// Exclude voided movements by default
|
||||
if (!$includeVoided) {
|
||||
if (! $includeVoided) {
|
||||
$builder->whereNull('m.voided_at');
|
||||
}
|
||||
|
||||
@@ -61,7 +59,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,7 +74,8 @@ 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.'];
|
||||
}
|
||||
|
||||
@@ -90,7 +89,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.'];
|
||||
}
|
||||
|
||||
@@ -125,7 +124,8 @@ 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.'];
|
||||
}
|
||||
|
||||
@@ -139,7 +139,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.'];
|
||||
}
|
||||
|
||||
@@ -158,7 +158,8 @@ 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.'];
|
||||
}
|
||||
|
||||
@@ -200,7 +201,8 @@ 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.'];
|
||||
}
|
||||
|
||||
@@ -223,7 +225,7 @@ class InventoryMovementService
|
||||
): bool {
|
||||
$isInitial = ($type === 'initial');
|
||||
|
||||
if (!$isInitial) {
|
||||
if (! $isInitial) {
|
||||
$this->ensureInitialMovement($itemId);
|
||||
$this->ensureInitialMovementForYear($itemId, $this->context->schoolYear());
|
||||
$this->recalcQuantity($itemId);
|
||||
@@ -252,6 +254,7 @@ class InventoryMovementService
|
||||
]);
|
||||
|
||||
$this->recalcQuantity($itemId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -262,7 +265,7 @@ class InventoryMovementService
|
||||
public function voidMovement(int $movementId, string $reason, int $voidedBy): array
|
||||
{
|
||||
$movement = InventoryMovement::query()->find($movementId);
|
||||
if (!$movement) {
|
||||
if (! $movement) {
|
||||
return ['ok' => false, 'message' => 'Movement not found.'];
|
||||
}
|
||||
|
||||
@@ -288,7 +291,7 @@ class InventoryMovementService
|
||||
'item_id' => $itemId,
|
||||
'qty_change' => $reverseQty,
|
||||
'movement_type' => 'adjust',
|
||||
'reason' => 'Correction: voided movement #' . $movement->id,
|
||||
'reason' => 'Correction: voided movement #'.$movement->id,
|
||||
'note' => $reason,
|
||||
'semester' => $this->context->semester(),
|
||||
'school_year' => $this->context->schoolYear(),
|
||||
@@ -300,7 +303,8 @@ class InventoryMovementService
|
||||
$this->recalcQuantity($itemId);
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Inventory movement void failed: ' . $e->getMessage());
|
||||
Log::error('Inventory movement void failed: '.$e->getMessage());
|
||||
|
||||
return ['ok' => false, 'message' => 'Could not void movement.'];
|
||||
}
|
||||
|
||||
@@ -314,7 +318,7 @@ class InventoryMovementService
|
||||
public function correctMovement(int $movementId, int $correctedQtyChange, string $reason, int $performedBy): array
|
||||
{
|
||||
$movement = InventoryMovement::query()->find($movementId);
|
||||
if (!$movement) {
|
||||
if (! $movement) {
|
||||
return ['ok' => false, 'message' => 'Movement not found.'];
|
||||
}
|
||||
|
||||
@@ -333,7 +337,7 @@ class InventoryMovementService
|
||||
'item_id' => $itemId,
|
||||
'qty_change' => $correctedQtyChange,
|
||||
'movement_type' => 'adjust',
|
||||
'reason' => 'Correction for movement #' . $movement->id,
|
||||
'reason' => 'Correction for movement #'.$movement->id,
|
||||
'note' => $reason,
|
||||
'semester' => $this->context->semester(),
|
||||
'school_year' => $this->context->schoolYear(),
|
||||
@@ -343,7 +347,8 @@ class InventoryMovementService
|
||||
|
||||
$this->recalcQuantity($itemId);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Inventory movement correction failed: ' . $e->getMessage());
|
||||
Log::error('Inventory movement correction failed: '.$e->getMessage());
|
||||
|
||||
return ['ok' => false, 'message' => 'Could not correct movement.'];
|
||||
}
|
||||
|
||||
@@ -364,7 +369,7 @@ class InventoryMovementService
|
||||
}
|
||||
|
||||
$item = InventoryItem::query()->select('id', 'type', 'quantity', 'needs_repair_qty', 'good_qty')->find($itemId);
|
||||
if (!$item) {
|
||||
if (! $item) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -381,7 +386,7 @@ class InventoryMovementService
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($updates)) {
|
||||
if (! empty($updates)) {
|
||||
$item->update($updates);
|
||||
}
|
||||
}
|
||||
@@ -453,7 +458,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,
|
||||
@@ -462,6 +467,7 @@ class InventoryMovementService
|
||||
'school_year' => $schoolYear,
|
||||
'semester' => $this->context->semester() ?: null,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -471,7 +477,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)
|
||||
@@ -494,12 +500,14 @@ 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;
|
||||
}
|
||||
|
||||
@@ -508,6 +516,7 @@ class InventoryMovementService
|
||||
if ($val === null || $val === '' || $val === '0') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int) $val;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user