Add machines with day-by-day work lists for job tasks

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:43:55 +02:00
co-authored by Claude Sonnet 5.5
parent 0de0e332b2
commit aac579257a
59 changed files with 3803 additions and 1 deletions
@@ -0,0 +1,176 @@
<?php
namespace App\Modules\Machines\Infrastructure\Persistence\Repositories;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Machines\Application\Contracts\MachineQueries;
use App\Modules\Machines\Application\Contracts\MachineTasks;
use App\Modules\Machines\Application\DTOs\CounterSummary;
use App\Modules\Machines\Application\DTOs\DayLoad;
use App\Modules\Machines\Application\DTOs\MachineSummary;
use App\Modules\Machines\Application\DTOs\QueueRow;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineQueueEntryModel;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use Carbon\CarbonImmutable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class EloquentMachineQueries implements MachineQueries
{
public function __construct(
private CatalogQueries $catalog,
private UserAccountQueries $users,
private MachineTasks $tasks,
) {}
public function machines(bool $includeInactive = false): array
{
return $this->summaries(MachineModel::query()
->with('counters.latestReading')
->when(! $includeInactive, fn ($query) => $query->where('is_active', true))
->orderBy('name')
->get());
}
public function findMachine(int $id): ?MachineSummary
{
$model = MachineModel::query()->with('counters.latestReading')->find($id);
return $model === null ? null : $this->summaries(collect([$model]))[0];
}
public function machinesFor(array $serviceUnitIds): array
{
if ($serviceUnitIds === []) {
return [];
}
return $this->summaries(MachineModel::query()
->with('counters.latestReading')
->where('is_active', true)
->whereIn('id', DB::table('machine_service_units')->select('machine_id')->whereIn('service_unit_id', $serviceUnitIds))
->orderBy('name')
->get());
}
public function rowForTask(int $taskId): ?QueueRow
{
$model = MachineQueueEntryModel::query()->where('task_id', $taskId)->whereNull('completed_at')->first();
return $model === null ? null : $this->toRow($model, null, false);
}
public function rowsOn(int $machineId, string $day, string $today): array
{
$models = MachineQueueEntryModel::query()
->where('machine_id', $machineId)
->whereNull('completed_at')
->when(
$day === $today,
fn ($query) => $query->where('planned_on', '<=', $day),
fn ($query) => $query->where('planned_on', $day),
)
->get();
$infos = $this->tasks->infos($models->pluck('task_id')->filter()->unique()->values()->all());
return $models
->sortBy(fn (MachineQueueEntryModel $model): string => sprintf(
'%d-%d-%s-%05d-%010d',
$model->quick_sale_order_line_id === null ? 1 : 0,
substr($model->planned_on, 0, 10) < $day ? 0 : 1,
substr($model->planned_on, 0, 10),
$model->position,
$model->id,
))
->map(fn (MachineQueueEntryModel $model): QueueRow => $this->toRow(
$model,
$model->task_id === null ? null : ($infos[$model->task_id] ?? null),
substr($model->planned_on, 0, 10) < $day,
))
->values()
->all();
}
public function load(int $machineId, string $from, int $days): array
{
$to = CarbonImmutable::parse($from)->addDays($days - 1)->format('Y-m-d');
$models = MachineQueueEntryModel::query()
->where('machine_id', $machineId)
->whereNull('completed_at')
->whereBetween('planned_on', [$from, $to])
->get();
$infos = $this->tasks->infos($models->pluck('task_id')->filter()->unique()->values()->all());
$loads = [];
for ($i = 0; $i < $days; $i++) {
$day = CarbonImmutable::parse($from)->addDays($i)->format('Y-m-d');
$rows = $models->filter(fn (MachineQueueEntryModel $model): bool => substr($model->planned_on, 0, 10) === $day);
$quarters = $rows->sum(function (MachineQueueEntryModel $model) use ($infos): int {
$hours = $model->task_id === null ? null : ($infos[$model->task_id]->estimatedHours ?? null);
return $hours === null ? 0 : (int) round((float) $hours * 4);
});
$loads[] = new DayLoad($day, $rows->count(), $quarters);
}
return $loads;
}
private function toRow(MachineQueueEntryModel $model, mixed $task, bool $isLate): QueueRow
{
return new QueueRow(
entryId: $model->id,
machineId: $model->machine_id,
plannedOn: substr($model->planned_on, 0, 10),
position: $model->position,
isLate: $isLate,
taskId: $model->task_id,
orderLineId: $model->quick_sale_order_line_id,
stepPosition: $model->production_step_position,
task: $task,
);
}
/**
* @param Collection<int, MachineModel> $models
* @return list<MachineSummary>
*/
private function summaries(Collection $models): array
{
$unitIdsByMachine = DB::table('machine_service_units')
->whereIn('machine_id', $models->pluck('id'))
->get()
->groupBy('machine_id')
->map(fn ($rows) => $rows->pluck('service_unit_id')->map(fn ($id): int => (int) $id)->values()->all());
$unitNames = $this->catalog->serviceUnitNames($unitIdsByMachine->flatten()->unique()->values()->all());
$userNames = $this->users->namesByIds($models->pluck('responsible_user_id')->filter()->unique()->values()->all());
return $models->map(function (MachineModel $model) use ($unitIdsByMachine, $unitNames, $userNames): MachineSummary {
$unitIds = $unitIdsByMachine->get($model->id, []);
return new MachineSummary(
id: $model->id,
name: $model->name,
type: $model->type,
description: $model->description,
serviceUnitIds: $unitIds,
serviceUnitNames: array_values(array_intersect_key($unitNames, array_flip($unitIds))),
responsibleUserId: $model->responsible_user_id,
responsibleName: $model->responsible_user_id === null ? null : ($userNames[$model->responsible_user_id] ?? ''),
counters: $model->counters->map(fn (MachineCounterModel $counter): CounterSummary => new CounterSummary(
$counter->id,
$counter->name,
$counter->unit,
$counter->latestReading?->reading_milli,
$counter->latestReading === null ? null : Carbon::instance($counter->latestReading->read_at)->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'),
))->values()->all(),
isActive: $model->is_active,
);
})->values()->all();
}
}
@@ -0,0 +1,77 @@
<?php
namespace App\Modules\Machines\Infrastructure\Persistence\Repositories;
use App\Modules\Machines\Domain\Contracts\MachineRepository;
use App\Modules\Machines\Domain\Entities\Machine;
use App\Modules\Machines\Domain\ValueObjects\MachineCounter;
use App\Modules\Machines\Domain\ValueObjects\MachineName;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
use Illuminate\Support\Facades\DB;
class EloquentMachineRepository implements MachineRepository
{
public function findById(int $id): ?Machine
{
$model = MachineModel::query()->with('counters')->find($id);
return $model === null ? null : $this->toEntity($model);
}
public function save(Machine $machine): Machine
{
$model = $machine->id() === null ? new MachineModel : MachineModel::query()->findOrFail($machine->id());
$model->fill([
'name' => $machine->name()->value,
'type' => $machine->type(),
'description' => $machine->description(),
'responsible_user_id' => $machine->responsibleUserId(),
'is_active' => $machine->isActive(),
])->save();
DB::table('machine_service_units')->where('machine_id', $model->id)->delete();
DB::table('machine_service_units')->insert(array_map(
fn (int $unitId): array => ['machine_id' => $model->id, 'service_unit_id' => $unitId],
$machine->serviceUnitIds(),
));
$keptIds = [];
foreach ($machine->counters() as $index => $counter) {
$counterModel = $counter->id === null
? new MachineCounterModel(['machine_id' => $model->id])
: MachineCounterModel::query()->where('machine_id', $model->id)->findOrFail($counter->id);
$counterModel->fill(['name' => $counter->name, 'unit' => $counter->unit, 'sort_order' => $index + 1])->save();
$keptIds[] = $counterModel->id;
}
MachineCounterModel::query()->where('machine_id', $model->id)->whereNotIn('id', $keptIds)->delete();
return $this->toEntity($model->load('counters'));
}
public function isNameTaken(string $name, ?int $exceptId = null): bool
{
return MachineModel::query()
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
->exists();
}
private function toEntity(MachineModel $model): Machine
{
return Machine::reconstitute(
$model->id,
new MachineName($model->name),
$model->type,
$model->description,
DB::table('machine_service_units')->where('machine_id', $model->id)->orderBy('service_unit_id')->pluck('service_unit_id')->map(fn ($id): int => (int) $id)->values()->all(),
$model->responsible_user_id,
$model->counters->map(fn (MachineCounterModel $counter): MachineCounter => new MachineCounter($counter->id, $counter->name, $counter->unit))->values()->all(),
$model->is_active,
);
}
}
@@ -0,0 +1,86 @@
<?php
namespace App\Modules\Machines\Infrastructure\Persistence\Repositories;
use App\Modules\Machines\Domain\Contracts\QueueRepository;
use App\Modules\Machines\Domain\Entities\QueueEntry;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineQueueEntryModel;
use DateTimeImmutable;
class EloquentQueueRepository implements QueueRepository
{
public function findById(int $id): ?QueueEntry
{
$model = MachineQueueEntryModel::query()->find($id);
return $model === null ? null : $this->toEntity($model);
}
public function openEntryForTask(int $taskId): ?QueueEntry
{
$model = MachineQueueEntryModel::query()->where('task_id', $taskId)->whereNull('completed_at')->first();
return $model === null ? null : $this->toEntity($model);
}
public function openEntryForOrderLine(int $orderLineId): ?QueueEntry
{
$model = MachineQueueEntryModel::query()->where('quick_sale_order_line_id', $orderLineId)->whereNull('completed_at')->first();
return $model === null ? null : $this->toEntity($model);
}
public function openEntriesOn(int $machineId, DateTimeImmutable $day): array
{
return MachineQueueEntryModel::query()
->where('machine_id', $machineId)
->where('planned_on', $day->format('Y-m-d'))
->whereNull('completed_at')
->orderBy('position')
->orderBy('id')
->get()
->map(fn (MachineQueueEntryModel $model): QueueEntry => $this->toEntity($model))
->values()
->all();
}
public function save(QueueEntry $entry): QueueEntry
{
$model = $entry->id() === null ? new MachineQueueEntryModel : MachineQueueEntryModel::query()->findOrFail($entry->id());
$model->fill([
'machine_id' => $entry->machineId(),
'planned_on' => $entry->plannedOn()->format('Y-m-d'),
'position' => $entry->position(),
'task_id' => $entry->taskId(),
'quick_sale_order_line_id' => $entry->orderLineId(),
'production_step_position' => $entry->stepPosition(),
'completed_at' => $entry->completedAt()?->format('Y-m-d H:i:s'),
'completed_by' => $entry->completedBy(),
])->save();
return $this->toEntity($model);
}
public function delete(QueueEntry $entry): void
{
if ($entry->id() !== null) {
MachineQueueEntryModel::query()->whereKey($entry->id())->delete();
}
}
private function toEntity(MachineQueueEntryModel $model): QueueEntry
{
return QueueEntry::reconstitute(
$model->id,
$model->machine_id,
new DateTimeImmutable(substr($model->planned_on, 0, 10)),
$model->position,
$model->task_id,
$model->quick_sale_order_line_id,
$model->production_step_position,
$model->completed_at === null ? null : DateTimeImmutable::createFromInterface($model->completed_at),
$model->completed_by,
);
}
}