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 $models * @return list */ 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(); } }