// 1. Ambil receipts $receipts = json_decode(file_get_contents('https://api.loyverse.com/v1.0/receipts?created_at_min='.$tarikh.'&created_at_max='.$tarikh), true); // 2. Ambil semua items $items = json_decode(file_get_contents('https://api.loyverse.com/v1.0/items'), true); // 3. Ambil semua categories $categories = json_decode(file_get_contents('https://api.loyverse.com/v1.0/categories'), true); // Buat map senang lookup $itemMap = []; foreach ($items['items'] as $item) { $itemMap[$item['id']] = [ 'name' => $item['name'], 'price' => $item['price'], 'category_id' => $item['category_id'] ?? null ]; } $catMap = []; foreach ($categories['categories'] as $cat) { $catMap[$cat['id']] = $cat['name']; } // Combine data $result = []; foreach ($receipts['receipts'] as $receipt) { foreach ($receipt['line_items'] as $line) { $item = $itemMap[$line['item_id']] ?? []; $categoryName = $catMap[$item['category_id']] ?? '-'; $result[] = [ 'receipt_no' => $receipt['receipt_number'], 'item_name' => $item['name'] ?? '-', 'qty' => $line['quantity'], 'category' => $categoryName, 'price' => $item['price'] ?? 0, 'total' => ($item['price'] ?? 0) * $line['quantity'] ]; } }