products(['active' => 1, 'per_page' => 500]); $rows = (array) ($response['data'] ?? []); if ($rows === []) { throw new RuntimeException('No active products found in CRM.'); } $imported = 0; $updated = 0; foreach ($rows as $row) { $name = trim((string) ($row['name'] ?? '')); if ($name === '') { continue; } $sku = isset($row['sku']) && $row['sku'] !== '' ? (string) $row['sku'] : null; $matchQuery = PosProduct::owned($ownerRef)->where('name', $name); if ($sku) { $matchQuery = PosProduct::owned($ownerRef)->where(fn ($q) => $q->where('sku', $sku)->orWhere('name', $name)); } $existing = $matchQuery->first(); $attrs = [ 'name' => $name, 'sku' => $sku, 'price_minor' => max(0, (int) ($row['unit_price_minor'] ?? 0)), 'currency' => strtoupper((string) ($row['currency'] ?? config('pos.default_currency', 'GHS'))), 'is_active' => (bool) ($row['active'] ?? true), ]; if ($existing) { $existing->update($attrs); $updated++; } else { PosProduct::create([...$attrs, 'owner_ref' => $ownerRef]); $imported++; } } return compact('imported', 'updated'); } }