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 how much of a counter one job used (spec 19.5): counters are * cumulative, so the usage is added to the counter's latest reading. * * @return int The counter's new reading. * * @throws CounterNotFound * @throws InvalidReading */ public function recordUsage(int $counterId, string $usage, int $actorUserId): int { $usageMilli = Reading::parse($usage); if ($usageMilli === null || $usageMilli <= 0) { throw new InvalidReading; } return $this->transactions->run(function () use ($counterId, $usageMilli, $actorUserId): int { if (! $this->maintenance->lockCounter($counterId)) { throw new CounterNotFound; } $reading = ($this->maintenance->latestReading($counterId) ?? 0) + $usageMilli; $this->maintenance->addReading($counterId, $reading, $actorUserId, $this->clock->now()); return $reading; }); } /** * Records maintenance done (Huolto tehty) at the counter's latest * reading and uses up the item's stock item. This resets the item: the * whole counter interval is left again, counted from this reading, and * the time interval starts again from today. * * @return array{recordId: int, stockRecorded: bool} * * @throws MaintenanceItemNotFound */ public function recordMaintenance(int $itemId, int $actorUserId, ?string $note = null): array { $note = $note === null ? '' : trim($note); return $this->transactions->run(function () use ($itemId, $actorUserId, $note): array { $item = $this->maintenance->findItem($itemId) ?? throw new MaintenanceItemNotFound; $readingMilli = $item->counterId() === null ? null : ($this->maintenance->latestReading($item->counterId()) ?? $item->baselineMilli()); $recordId = $this->maintenance->addRecord($itemId, $this->clock->now(), $actorUserId, $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()); $this->materials->returnUseOf($recordId, $actorUserId); }); } }