Record used material on tasks and suggest workflow steps' default materials
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Adapters;
|
||||
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
|
||||
use App\Modules\Jobs\Application\Contracts\TaskUsage;
|
||||
|
||||
/**
|
||||
* A task with recorded material is kept like one with hours: the stock
|
||||
* movements refer to it.
|
||||
*/
|
||||
class TaskUsageWithStock implements TaskUsage
|
||||
{
|
||||
public function __construct(private TaskUsage $inner) {}
|
||||
|
||||
public function hasTimeEntries(int $taskId): bool
|
||||
{
|
||||
return $this->inner->hasTimeEntries($taskId)
|
||||
|| StockMovementModel::query()->where('task_id', $taskId)->exists();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Infrastructure\Adapters;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Inventory\Application\Contracts\StockTasks;
|
||||
use App\Modules\Inventory\Application\DTOs\StockTaskContext;
|
||||
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
||||
use App\Modules\Jobs\Application\Contracts\TaskQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\BookableTask;
|
||||
|
||||
/**
|
||||
* Reads tasks from Jobs and the stock setting of their service units from
|
||||
* Catalog. A task's work type decides: its unit must use stock, and a
|
||||
* shared work type's task follows the job's units (spec 19).
|
||||
*/
|
||||
class TasksForStock implements StockTasks
|
||||
{
|
||||
public function __construct(
|
||||
private TaskQueries $tasks,
|
||||
private CustomerJobQueries $jobs,
|
||||
private CatalogQueries $catalog,
|
||||
) {}
|
||||
|
||||
public function context(int $taskId): ?StockTaskContext
|
||||
{
|
||||
$task = $this->tasks->bookableTask($taskId);
|
||||
|
||||
if ($task === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new StockTaskContext($task->taskId, $task->jobId, $task->label(), $this->usesStock($task), $task->isLocked());
|
||||
}
|
||||
|
||||
public function labels(array $taskIds): array
|
||||
{
|
||||
$labels = [];
|
||||
|
||||
foreach ($this->tasks->bookableTasks($taskIds) as $task) {
|
||||
$labels[$task->taskId] = $task->label();
|
||||
}
|
||||
|
||||
return $labels;
|
||||
}
|
||||
|
||||
private function usesStock(BookableTask $task): bool
|
||||
{
|
||||
$stockUnitIds = $this->catalog->stockServiceUnitIds();
|
||||
|
||||
if ($stockUnitIds === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$workType = $this->catalog->findWorkType($task->workTypeId);
|
||||
$unit = $workType === null ? null : $this->catalog->findServiceUnit($workType->serviceUnitId);
|
||||
|
||||
if ($unit === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $unit->isShared) {
|
||||
return in_array($unit->id, $stockUnitIds, true);
|
||||
}
|
||||
|
||||
$jobUnitIds = array_map(fn ($unit): int => $unit->id, $this->jobs->findDetails($task->jobId)->serviceUnits ?? []);
|
||||
|
||||
return array_intersect($jobUnitIds, $stockUnitIds) !== [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user