isRestaurant()) { return $this->lookupLocal($ownerRef, $code); } return $this->lookupRetail($ownerRef, $code) ?? $this->lookupLocal($ownerRef, $code); } /** @return array{id: int|string|null, name: string, price_minor: int, sku: string|null}|null */ private function lookupLocal(string $ownerRef, string $code): ?array { $product = PosProduct::owned($ownerRef) ->active() ->where('sku', $code) ->first(); if (! $product) { return null; } return [ 'id' => $product->id, 'name' => $product->name, 'price_minor' => $product->price_minor, 'sku' => $product->sku, ]; } /** @return array{id: int|string|null, name: string, price_minor: int, sku: string|null}|null */ private function lookupRetail(string $ownerRef, string $code): ?array { try { $rows = (array) (CrmClient::for($ownerRef)->products([ 'search' => $code, 'type' => 'product', 'active' => 1, 'per_page' => 25, ])['data'] ?? []); } catch (\Throwable) { return null; } foreach ($rows as $row) { $sku = isset($row['sku']) ? (string) $row['sku'] : ''; if ($sku !== '' && strcasecmp($sku, $code) === 0) { return [ 'id' => $row['id'] ?? null, 'name' => (string) ($row['name'] ?? ''), 'price_minor' => (int) ($row['unit_price_minor'] ?? 0), 'sku' => $sku, ]; } } return null; } }