177 lines
7.2 KiB
PHP
177 lines
7.2 KiB
PHP
<?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();
|
|
}
|
|
}
|