[], 'tenant_info' => [], 'local_db' => [], 'barobill_api' => [], 'final_result' => [] ]; try { // Step 1: 세션에서 테넌트 ID 확인 $selectedTenantId = $_SESSION['eaccount_tenant_id'] ?? null; $debug['step'][] = '1. 세션에서 테넌트 ID 확인'; $debug['tenant_info']['session_tenant_id'] = $selectedTenantId; // Step 2: DB에서 테넌트 정보 확인 $pdo = db_connect(); if (!$pdo) { throw new Exception("Database connection failed."); } $debug['step'][] = '2. DB 연결 성공'; // 테넌트 정보 조회 if ($selectedTenantId) { $sql = "SELECT id, company_name, corp_num, barobill_user_id FROM {$DB}.barobill_companies WHERE id = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([$selectedTenantId]); $tenant = $stmt->fetch(PDO::FETCH_ASSOC); $debug['tenant_info']['db_query'] = $sql; $debug['tenant_info']['db_params'] = [$selectedTenantId]; $debug['tenant_info']['tenant_found'] = $tenant ? true : false; $debug['tenant_info']['tenant_data'] = $tenant; if ($tenant) { $debug['step'][] = '3. 테넌트 정보 조회 성공: ' . $tenant['company_name']; } else { $debug['step'][] = '3. 테넌트 정보 조회 실패: ID ' . $selectedTenantId . '를 찾을 수 없음'; } } else { // 기본값으로 주일기업 찾기 $sql = "SELECT id, company_name, corp_num, barobill_user_id FROM {$DB}.barobill_companies WHERE company_name LIKE '%주일기업%' OR company_name LIKE '%주일%' OR barobill_user_id = 'juil5130' ORDER BY id ASC LIMIT 1"; $stmt = $pdo->query($sql); $tenant = $stmt->fetch(PDO::FETCH_ASSOC); $debug['tenant_info']['default_search_query'] = $sql; $debug['tenant_info']['tenant_found'] = $tenant ? true : false; $debug['tenant_info']['tenant_data'] = $tenant; if ($tenant) { $selectedTenantId = $tenant['id']; $debug['step'][] = '3. 기본 테넌트(주일기업) 찾기 성공: ' . $tenant['company_name']; } else { $debug['step'][] = '3. 기본 테넌트(주일기업) 찾기 실패'; } } // Step 3: 로컬 DB에서 계좌 정보 조회 if ($selectedTenantId) { $debug['step'][] = '4. 로컬 DB 계좌 정보 조회 시작'; $accountSql = "SELECT id, company_id, bank_code, account_num, account_pwd FROM {$DB}.company_accounts WHERE company_id = ? ORDER BY id DESC"; $accountStmt = $pdo->prepare($accountSql); $accountStmt->execute([$selectedTenantId]); $localAccounts = $accountStmt->fetchAll(PDO::FETCH_ASSOC); $debug['local_db']['query'] = $accountSql; $debug['local_db']['params'] = [$selectedTenantId]; $debug['local_db']['count'] = count($localAccounts); $debug['local_db']['accounts'] = $localAccounts; $debug['step'][] = '4. 로컬 DB 계좌 정보 조회 완료: ' . count($localAccounts) . '개'; // 모든 회사의 계좌 정보도 확인 (디버깅용) $allAccountsSql = "SELECT ca.*, c.company_name, c.barobill_user_id FROM {$DB}.company_accounts ca LEFT JOIN {$DB}.barobill_companies c ON ca.company_id = c.id ORDER BY ca.id DESC LIMIT 20"; $allAccountsStmt = $pdo->query($allAccountsSql); $allAccounts = $allAccountsStmt->fetchAll(PDO::FETCH_ASSOC); $debug['local_db']['all_companies_accounts'] = $allAccounts; $debug['local_db']['all_companies_accounts_count'] = count($allAccounts); } else { $debug['local_db']['error'] = '테넌트 ID가 없어서 로컬 DB 조회 불가'; } // Step 4: 바로빌 API 설정 확인 global $barobillUserId, $barobillCorpNum, $barobillCertKey, $isTestMode; $debug['barobill_api']['config'] = [ 'user_id' => $barobillUserId, 'corp_num' => $barobillCorpNum, 'cert_key_length' => strlen($barobillCertKey), 'cert_key_preview' => !empty($barobillCertKey) ? substr($barobillCertKey, 0, 8) . '...' . substr($barobillCertKey, -4) : 'NOT SET', 'test_mode' => $isTestMode ]; $debug['step'][] = '5. 바로빌 API 설정 확인 완료'; // Step 5: 바로빌 API 호출 시도 $debug['step'][] = '6. 바로빌 API 호출 시작'; $result = callBarobillAccountSOAP('GetBankAccountEx', [ 'AvailOnly' => 0 ]); $debug['barobill_api']['success'] = $result['success']; $debug['barobill_api']['error'] = $result['error'] ?? null; $debug['barobill_api']['error_code'] = $result['error_code'] ?? null; if ($result['success']) { $data = $result['data']; $accountList = []; if (isset($data->BankAccountEx)) { if (is_array($data->BankAccountEx)) { $accountList = $data->BankAccountEx; } else { $accountList = [$data->BankAccountEx]; } } $barobillAccounts = []; foreach ($accountList as $acc) { if (isset($acc->BankAccountNum) && is_numeric($acc->BankAccountNum) && $acc->BankAccountNum < 0) { continue; } $barobillAccounts[] = [ 'bankAccountNum' => $acc->BankAccountNum ?? '', 'bankCode' => $acc->BankCode ?? '', 'bankName' => getBankName($acc->BankCode ?? ''), 'accountName' => $acc->AccountName ?? '', ]; } $debug['barobill_api']['accounts'] = $barobillAccounts; $debug['barobill_api']['count'] = count($barobillAccounts); $debug['step'][] = '6. 바로빌 API 호출 성공: ' . count($barobillAccounts) . '개 계좌'; } else { $debug['barobill_api']['accounts'] = []; $debug['barobill_api']['count'] = 0; $debug['step'][] = '6. 바로빌 API 호출 실패: ' . ($result['error'] ?? '알 수 없는 오류'); } // Step 6: 최종 결과 통합 $allAccounts = []; // 로컬 DB 계좌 추가 if (isset($localAccounts)) { foreach ($localAccounts as $localAcc) { $allAccounts[] = [ 'source' => 'local_db', 'bankAccountNum' => $localAcc['account_num'], 'bankCode' => $localAcc['bank_code'], 'bankName' => getBankName($localAcc['bank_code']), ]; } } // 바로빌 API 계좌 추가 if (isset($barobillAccounts)) { foreach ($barobillAccounts as $barobillAcc) { $exists = false; foreach ($allAccounts as &$existingAcc) { if ($existingAcc['bankAccountNum'] === $barobillAcc['bankAccountNum'] && $existingAcc['source'] === 'local_db') { $existingAcc['source'] = 'both'; $exists = true; break; } } if (!$exists) { $allAccounts[] = array_merge($barobillAcc, ['source' => 'barobill_api']); } } } $debug['final_result']['total_count'] = count($allAccounts); $debug['final_result']['accounts'] = $allAccounts; $debug['step'][] = '7. 최종 통합 완료: ' . count($allAccounts) . '개 계좌'; // 모든 barobill_companies 목록도 확인 $allCompaniesSql = "SELECT id, company_name, corp_num, barobill_user_id, parent_id FROM {$DB}.barobill_companies ORDER BY id ASC"; $allCompaniesStmt = $pdo->query($allCompaniesSql); $allCompanies = $allCompaniesStmt->fetchAll(PDO::FETCH_ASSOC); $debug['tenant_info']['all_companies'] = $allCompanies; $debug['tenant_info']['all_companies_count'] = count($allCompanies); echo json_encode([ 'success' => true, 'debug' => $debug, 'summary' => [ 'tenant_id' => $selectedTenantId, 'tenant_name' => $tenant['company_name'] ?? '알 수 없음', 'local_accounts_count' => count($localAccounts ?? []), 'barobill_accounts_count' => count($barobillAccounts ?? []), 'total_accounts_count' => count($allAccounts) ] ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); } catch (Exception $e) { $debug['error'] = $e->getMessage(); $debug['error_trace'] = $e->getTraceAsString(); echo json_encode([ 'success' => false, 'error' => $e->getMessage(), 'debug' => $debug ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); } /** * 은행 코드 -> 은행명 변환 */ function getBankName($code) { $banks = [ '002' => 'KDB산업은행', '003' => 'IBK기업은행', '004' => 'KB국민은행', '007' => '수협은행', '011' => 'NH농협은행', '012' => '지역농축협', '020' => '우리은행', '023' => 'SC제일은행', '027' => '한국씨티은행', '031' => '대구은행', '032' => '부산은행', '034' => '광주은행', '035' => '제주은행', '037' => '전북은행', '039' => '경남은행', '045' => '새마을금고', '048' => '신협', '050' => '저축은행', '064' => '산림조합', '071' => '우체국', '081' => '하나은행', '088' => '신한은행', '089' => 'K뱅크', '090' => '카카오뱅크', '092' => '토스뱅크' ]; return $banks[$code] ?? $code; } ?>