Return stock of cancelled maintenance, throttle the PIN page's password
Cancelling a maintenance record now reverses the stock it used, through Inventory's new ReverseMaintenanceStockUse behind the MaintenanceMaterials port, in the same transaction as the cancellation. The PIN page allows 5 wrong current passwords a minute per user, as the login form does, so a signed-in session cannot be used to guess the password. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Inventory\Application\UseCases;
|
||||
|
||||
use App\Modules\Inventory\Domain\Contracts\StockMovementRepository;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockMovementAlreadyReversed;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockMovementNotFound;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockMovementNotReversible;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockReversalNotAllowed;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Returns the stock a cancelled maintenance record used (spec 19.5): each
|
||||
* of its use movements not yet reversed gets a reversal, recorded today.
|
||||
*/
|
||||
final class ReverseMaintenanceStockUse
|
||||
{
|
||||
public function __construct(
|
||||
private StockMovementRepository $movements,
|
||||
private ReverseStockMovement $reverse,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return int How many movements were reversed.
|
||||
*
|
||||
* @throws StockMovementNotFound
|
||||
* @throws StockMovementNotReversible
|
||||
* @throws StockMovementAlreadyReversed
|
||||
* @throws StockReversalNotAllowed
|
||||
*/
|
||||
public function execute(int $maintenanceRecordId, int $actorUserId): int
|
||||
{
|
||||
return $this->transactions->run(function () use ($maintenanceRecordId, $actorUserId): int {
|
||||
$movementIds = $this->movements->unreversedUseOfMaintenance($maintenanceRecordId);
|
||||
|
||||
foreach ($movementIds as $movementId) {
|
||||
$this->reverse->execute($movementId, $actorUserId, true);
|
||||
}
|
||||
|
||||
return count($movementIds);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,13 @@ interface StockMovementRepository
|
||||
|
||||
public function isReversed(int $movementId): bool;
|
||||
|
||||
/**
|
||||
* The ids of a maintenance record's use movements that are not reversed.
|
||||
*
|
||||
* @return list<int>
|
||||
*/
|
||||
public function unreversedUseOfMaintenance(int $maintenanceRecordId): array;
|
||||
|
||||
/**
|
||||
* The item's balance in thousandths: the sum of its movements.
|
||||
*/
|
||||
|
||||
+14
@@ -4,6 +4,7 @@ namespace App\Modules\Inventory\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Inventory\Domain\Contracts\StockMovementRepository;
|
||||
use App\Modules\Inventory\Domain\Entities\StockMovement;
|
||||
use App\Modules\Inventory\Domain\Enums\StockMovementKind;
|
||||
use App\Modules\Inventory\Domain\ValueObjects\StockUseTarget;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
|
||||
use DateTimeImmutable;
|
||||
@@ -50,6 +51,19 @@ class EloquentStockMovementRepository implements StockMovementRepository
|
||||
return StockMovementModel::query()->where('reverses_movement_id', $movementId)->exists();
|
||||
}
|
||||
|
||||
public function unreversedUseOfMaintenance(int $maintenanceRecordId): array
|
||||
{
|
||||
return StockMovementModel::query()
|
||||
->where('maintenance_record_id', $maintenanceRecordId)
|
||||
->where('kind', StockMovementKind::Consumption)
|
||||
->whereNotIn('id', StockMovementModel::query()->whereNotNull('reverses_movement_id')->select('reverses_movement_id'))
|
||||
->orderBy('id')
|
||||
->pluck('id')
|
||||
->map(fn (mixed $id): int => (int) $id)
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function balanceOf(int $itemId): int
|
||||
{
|
||||
return (int) StockMovementModel::query()->where('stock_item_id', $itemId)->sum('quantity_milli');
|
||||
|
||||
Reference in New Issue
Block a user