db->get_row($this->db->prepare('SELECT * FROM %i WHERE id = %d', Schema::claimsTable(), $id), ARRAY_A); return $row ? Claim::fromRow($row) : null; } public function findActiveByOrderItem(int $orderItemId): array { $rows = $this->db->get_results($this->db->prepare("SELECT * FROM %i WHERE order_item_id = %d AND state <> 'closed' ORDER BY id", Schema::claimsTable(), $orderItemId), ARRAY_A) ?: []; return array_map([Claim::class, 'fromRow'], $rows); } /** @return list */ public function findByOrder(int $orderId): array { $rows = $this->db->get_results($this->db->prepare('SELECT * FROM %i WHERE order_id = %d ORDER BY id', Schema::claimsTable(), $orderId), ARRAY_A) ?: []; return array_map([Claim::class, 'fromRow'], $rows); } public function insert(Claim $claim): void { $data = self::columns($claim->toRow()); unset($data['id']); $data['created_at'] = EventWriter::now(); $data['updated_at'] = $data['created_at']; if (false === $this->db->insert(Schema::claimsTable(), $data)) { throw new \RuntimeException('Could not insert claim: ' . esc_html($this->db->last_error)); } $claim->assignId((int) $this->db->insert_id); $this->events->append(WarrantyEvent::SUBJECT_CLAIM, (int) $claim->id(), $claim->pullEvents()); } public function save(Claim $claim): void { if ($claim->version() === $claim->loadedVersion()) { return; } $id = (int) $claim->id(); $data = self::columns($claim->toRow()); unset($data['id']); $data['updated_at'] = EventWriter::now(); $affected = $this->db->update(Schema::claimsTable(), $data, ['id' => $id, 'version' => $claim->loadedVersion()]); if (false === $affected) { throw new \RuntimeException('Could not update claim: ' . esc_html($this->db->last_error)); } if (0 === $affected) { $current = (int) $this->db->get_var($this->db->prepare('SELECT version FROM %i WHERE id = %d', Schema::claimsTable(), $id)); throw DomainError::versionConflict((int) $claim->loadedVersion(), (int) $current); } $this->events->append(WarrantyEvent::SUBJECT_CLAIM, $id, $claim->pullEvents()); } /** * List for the admin queue: claims in review first (they need a person), * then the oldest reports. * * @param array{state?:string, order_id?:int, page?:int, per_page?:int} $filters * @return array{items:list, total:int} */ public function search(array $filters): array { $where = ['1=1']; $params = [Schema::claimsTable()]; $state = $filters['state'] ?? 'open'; if ('open' === $state) { $where[] = "state <> 'closed'"; } elseif (in_array($state, ['needs_review', 'closed'], true)) { $where[] = 'state = %s'; $params[] = $state; } if (!empty($filters['order_id'])) { $where[] = 'order_id = %d'; $params[] = (int) $filters['order_id']; } $perPage = max(1, min(100, (int) ($filters['per_page'] ?? 50))); $page = max(1, (int) ($filters['page'] ?? 1)); $whereSql = implode(' AND ', $where); $total = (int) $this->db->get_var($this->db->prepare("SELECT COUNT(*) FROM %i WHERE {$whereSql}", ...$params)); $rows = $this->db->get_results($this->db->prepare( "SELECT * FROM %i WHERE {$whereSql} ORDER BY FIELD(state, 'needs_review', 'open', 'closed'), reported_on, id LIMIT %d OFFSET %d", ...array_merge($params, [$perPage, ($page - 1) * $perPage]), ), ARRAY_A) ?: []; return ['items' => array_map([Claim::class, 'fromRow'], $rows), 'total' => $total]; } /** @return array{open:int, needs_review:int} */ public function counts(): array { $rows = $this->db->get_results($this->db->prepare("SELECT state, COUNT(*) AS n FROM %i WHERE state <> 'closed' GROUP BY state", Schema::claimsTable()), ARRAY_A) ?: []; $out = ['open' => 0, 'needs_review' => 0]; foreach ($rows as $row) { $out[(string) $row['state']] = (int) $row['n']; } return $out; } /** * History of a claim and of its units, oldest first. * * @param list $unitIds * @return list> */ public function history(int $claimId, array $unitIds): array { $unitIds = array_values(array_filter(array_map('intval', $unitIds))); $sql = "SELECT id, subject, subject_id, event_type, actor_id, reason, before_json, after_json, external_ref, occurred_at FROM %i WHERE (subject = 'claim' AND subject_id = %d)"; $params = [Schema::eventsTable(), $claimId]; if ([] !== $unitIds) { $sql .= " OR (subject = 'unit' AND subject_id IN (" . implode(',', array_fill(0, count($unitIds), '%d')) . '))'; $params = array_merge($params, $unitIds); } $rows = $this->db->get_results($this->db->prepare($sql . ' ORDER BY id', ...$params), ARRAY_A) ?: []; return array_map(static function (array $row): array { $row['id'] = (int) $row['id']; $row['subject_id'] = (int) $row['subject_id']; $row['actor_id'] = null === $row['actor_id'] ? null : (int) $row['actor_id']; $row['before'] = json_decode((string) $row['before_json'], true) ?: []; $row['after'] = json_decode((string) $row['after_json'], true) ?: []; unset($row['before_json'], $row['after_json']); return $row; }, $rows); } /** * Every claim with its units, for the CSV export (G16). * * @return \Generator> */ public function exportRows(): \Generator { $lastId = 0; do { $rows = $this->db->get_results($this->db->prepare('SELECT * FROM %i WHERE id > %d ORDER BY id LIMIT 500', Schema::claimsTable(), $lastId), ARRAY_A) ?: []; foreach ($rows as $row) { $lastId = (int) $row['id']; yield $row; } } while (count($rows) === 500); } /** * @param array $row * @return array */ private static function columns(array $row): array { foreach ($row as $key => $value) { if (is_bool($value)) { $row[$key] = $value ? 1 : 0; } } return $row; } }