interval); $quantity = $data->stockItemId === null ? null : Reading::parse($data->stockQuantity); $period = $data->period === null || $data->period === '' ? null : (MaintenancePeriod::tryFrom($data->period) ?? throw new InvalidMaintenanceItem); return $this->transactions->run(function () use ($machineId, $itemId, $data, $interval, $quantity, $period): int { $this->machines->findById($machineId) ?? throw new MachineNotFound; if ($data->counterId !== null && $this->maintenance->machineOfCounter($data->counterId) !== $machineId) { throw new CounterNotFound; } $counterReading = $data->counterId === null ? 0 : ($this->maintenance->latestReading($data->counterId) ?? 0); if ($itemId === null) { $item = MaintenanceItem::create($machineId, $data->name, $data->counterId, $interval, $counterReading, $period, $data->periodCount, $this->clock->today(), $data->warningPercent, $data->instructions, $data->stockItemId, $quantity); } else { $item = $this->maintenance->findItem($itemId); if ($item === null || $item->machineId() !== $machineId) { throw new MaintenanceItemNotFound; } $counterChanged = $item->counterId() !== $data->counterId; $item->change($data->name, $data->counterId, $interval, $period, $data->periodCount, $data->warningPercent, $data->instructions, $data->stockItemId, $quantity); if ($counterChanged) { $item->rebaseCounter($counterReading); } } return (int) $this->maintenance->saveItem($item)->id(); }); } /** * @throws MaintenanceItemNotFound */ public function setItemActive(int $itemId, bool $active): void { $this->transactions->run(function () use ($itemId, $active): void { $item = $this->maintenance->findItem($itemId) ?? throw new MaintenanceItemNotFound; $active ? $item->activate() : $item->deactivate(); $this->maintenance->saveItem($item); }); } /** * Records a counter's reading, e.g. the machine's own total counter. * * @throws CounterNotFound * @throws InvalidReading */ public function recordReading(int $counterId, string $reading, int $actorUserId): int { $milli = Reading::parse($reading) ?? throw new InvalidReading; $this->transactions->run(function () use ($counterId, $milli, $actorUserId): void { $this->maintenance->machineOfCounter($counterId) ?? throw new CounterNotFound; $this->maintenance->addReading($counterId, $milli, $actorUserId, $this->clock->now()); }); return $milli; } /** * Records maintenance done (Huolto tehty) with the counter's reading * (the latest one when not given) and uses up the item's stock item. * * @return array{recordId: int, stockRecorded: bool} * * @throws MaintenanceItemNotFound * @throws InvalidReading */ public function recordMaintenance(int $itemId, int $actorUserId, string $reading = '', ?string $note = null): array { $readingMilli = Reading::parse($reading); $note = $note === null ? '' : trim($note); return $this->transactions->run(function () use ($itemId, $actorUserId, $readingMilli, $note): array { $item = $this->maintenance->findItem($itemId) ?? throw new MaintenanceItemNotFound; $now = $this->clock->now(); if ($item->counterId() !== null) { if ($readingMilli !== null) { $this->maintenance->addReading($item->counterId(), $readingMilli, $actorUserId, $now); } else { $readingMilli = $this->maintenance->latestReading($item->counterId()) ?? $item->baselineMilli(); } } $recordId = $this->maintenance->addRecord($itemId, $now, $actorUserId, $item->counterId() === null ? null : $readingMilli, $note === '' ? null : mb_substr($note, 0, 2000)); $stockRecorded = $item->stockItemId() === null || $this->materials->use($item->stockItemId(), (int) $item->stockQuantityMilli(), $item->machineId(), $recordId, $actorUserId); return ['recordId' => $recordId, 'stockRecorded' => $stockRecorded]; }); } /** * @throws MaintenanceRecordNotFound * @throws MaintenanceRecordCancelled */ public function cancelRecord(int $recordId, int $actorUserId): void { $this->transactions->run(function () use ($recordId, $actorUserId): void { $record = $this->maintenance->findRecord($recordId) ?? throw new MaintenanceRecordNotFound; if ($record['cancelled']) { throw new MaintenanceRecordCancelled; } $this->maintenance->cancelRecord($recordId, $actorUserId, $this->clock->now()); }); } }