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:
2026-11-26 16:28:16 +02:00
co-authored by Claude Sonnet 5.5
parent 30132deb0e
commit bca1eed166
41 changed files with 1521 additions and 19 deletions
@@ -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) !== [];
}
}
@@ -0,0 +1,32 @@
<?php
namespace App\Modules\Inventory\Infrastructure\Persistence\Eloquent;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
/**
* Persistence model, used only inside the Inventory Infrastructure layer.
*
* @property int $id
* @property int $task_id
* @property int $stock_item_id
* @property int $quantity_milli
*/
#[Table('task_material_suggestions')]
#[Fillable(['task_id', 'stock_item_id', 'quantity_milli'])]
class TaskMaterialSuggestionModel extends Model
{
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'task_id' => 'integer',
'stock_item_id' => 'integer',
'quantity_milli' => 'integer',
];
}
}
@@ -6,11 +6,14 @@ use App\Modules\Inventory\Application\Contracts\StockQueries;
use App\Modules\Inventory\Application\DTOs\StockGroupSummary;
use App\Modules\Inventory\Application\DTOs\StockItemSummary;
use App\Modules\Inventory\Application\DTOs\StockMovementSummary;
use App\Modules\Inventory\Application\DTOs\TaskMaterialSuggestion;
use App\Modules\Inventory\Domain\Enums\StockUnit;
use App\Modules\Inventory\Domain\Services\StockSearch;
use App\Modules\Inventory\Domain\Services\StockValuation;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemGroupModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\TaskMaterialSuggestionModel;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
@@ -97,6 +100,36 @@ class EloquentStockQueries implements StockQueries
return $this->summaries(collect([$model]), $balance)[0];
}
public function taskMovements(int $taskId): array
{
$models = StockMovementModel::query()
->with('reversal:id,reverses_movement_id')
->where('task_id', $taskId)
->orderByDesc('id')
->get();
return $this->summaries($models, 0, trackBalance: false);
}
public function taskSuggestions(int $taskId): array
{
return TaskMaterialSuggestionModel::query()
->join('stock_items', 'stock_items.id', '=', 'task_material_suggestions.stock_item_id')
->where('task_material_suggestions.task_id', $taskId)
->orderBy('task_material_suggestions.id')
->get(['task_material_suggestions.*', 'stock_items.name as item_name', 'stock_items.unit as item_unit'])
->map(fn (TaskMaterialSuggestionModel $model): TaskMaterialSuggestion => new TaskMaterialSuggestion(
$model->id,
$model->task_id,
$model->stock_item_id,
(string) $model->getAttribute('item_name'),
StockUnit::from((string) $model->getAttribute('item_unit')),
$model->quantity_milli,
))
->values()
->all();
}
public function imagePathOf(int $itemId, bool $thumbnail): ?string
{
$model = StockItemModel::query()->find($itemId, ['id', 'image_path', 'thumbnail_path']);
@@ -147,9 +180,10 @@ class EloquentStockQueries implements StockQueries
* @param int $balance The balance after the newest of them.
* @return list<StockMovementSummary>
*/
private function summaries(Collection $models, int $balance): array
private function summaries(Collection $models, int $balance, bool $trackBalance = true): array
{
$names = $this->users->namesByIds($models->pluck('user_id')->unique()->values()->all());
$items = StockItemModel::query()->whereIn('id', $models->pluck('stock_item_id')->unique())->get(['id', 'name', 'unit'])->keyBy('id');
$summaries = [];
foreach ($models as $model) {
@@ -175,9 +209,13 @@ class EloquentStockQueries implements StockQueries
documentNumber: $model->document_number,
reversesMovementId: $model->reverses_movement_id,
reversedByMovementId: $model->reversal?->id,
itemName: (string) $items->get($model->stock_item_id)?->name,
unit: $items->get($model->stock_item_id)?->unit,
);
$balance -= $model->quantity_milli;
if ($trackBalance) {
$balance -= $model->quantity_milli;
}
}
return $summaries;
@@ -0,0 +1,35 @@
<?php
namespace App\Modules\Inventory\Infrastructure\Persistence\Repositories;
use App\Modules\Inventory\Domain\Contracts\TaskMaterialSuggestionRepository;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\TaskMaterialSuggestionModel;
class EloquentTaskMaterialSuggestionRepository implements TaskMaterialSuggestionRepository
{
public function add(int $taskId, int $stockItemId, int $quantityMilli): void
{
TaskMaterialSuggestionModel::query()->create([
'task_id' => $taskId,
'stock_item_id' => $stockItemId,
'quantity_milli' => $quantityMilli,
]);
}
public function find(int $id): ?array
{
$model = TaskMaterialSuggestionModel::query()->find($id);
return $model === null ? null : [
'id' => $model->id,
'taskId' => $model->task_id,
'stockItemId' => $model->stock_item_id,
'quantityMilli' => $model->quantity_milli,
];
}
public function remove(int $id): void
{
TaskMaterialSuggestionModel::query()->whereKey($id)->delete();
}
}
@@ -6,15 +6,24 @@ use App\Models\User;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Inventory\Application\Contracts\StockItemImages;
use App\Modules\Inventory\Application\Contracts\StockQueries;
use App\Modules\Inventory\Application\Contracts\StockTasks;
use App\Modules\Inventory\Application\UseCases\TaskMaterials;
use App\Modules\Inventory\Domain\Contracts\StockItemGroupRepository;
use App\Modules\Inventory\Domain\Contracts\StockItemRepository;
use App\Modules\Inventory\Domain\Contracts\StockMovementRepository;
use App\Modules\Inventory\Domain\Contracts\TaskMaterialSuggestionRepository;
use App\Modules\Inventory\Infrastructure\Adapters\TasksForStock;
use App\Modules\Inventory\Infrastructure\Adapters\TaskUsageWithStock;
use App\Modules\Inventory\Infrastructure\Files\StockItemImageFiles;
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockItemGroupRepository;
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockItemRepository;
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockMovementRepository;
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentStockQueries;
use App\Modules\Inventory\Infrastructure\Persistence\Repositories\EloquentTaskMaterialSuggestionRepository;
use App\Modules\Jobs\Application\Contracts\TaskUsage;
use App\Modules\Jobs\Domain\Events\TaskMaterialsSuggested;
use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Livewire\Livewire;
@@ -35,10 +44,20 @@ class InventoryServiceProvider extends ServiceProvider
StockMovementRepository::class => EloquentStockMovementRepository::class,
StockQueries::class => EloquentStockQueries::class,
StockItemImages::class => StockItemImageFiles::class,
TaskMaterialSuggestionRepository::class => EloquentTaskMaterialSuggestionRepository::class,
StockTasks::class => TasksForStock::class,
];
public function register(): void
{
// Registered after TimeTracking: a task with material is kept like one with hours.
$this->app->extend(TaskUsage::class, fn (TaskUsage $usage): TaskUsage => new TaskUsageWithStock($usage));
}
public function boot(): void
{
Event::listen(TaskMaterialsSuggested::class, fn (TaskMaterialsSuggested $event) => app(TaskMaterials::class)->suggest($event->taskId, $event->materials));
$presentationPath = dirname(__DIR__, 2).'/Presentation';
Livewire::addNamespace('inventory', viewPath: $presentationPath.'/Livewire');