87 lines
3.0 KiB
PHP
87 lines
3.0 KiB
PHP
<?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,
|
|
);
|
|
}
|
|
}
|