97 lines
3.4 KiB
PHP
97 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* 테넌트 목록 조회 API
|
|
* barobill_companies 테이블에서 테넌트 목록을 가져옵니다.
|
|
*/
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
require_once __DIR__ . '/../../lib/DotEnv.php';
|
|
(new DotEnv(__DIR__ . '/../../.env'))->load();
|
|
|
|
require_once(getenv('DOCUMENT_ROOT') . '/session.php');
|
|
require_once(getenv('DOCUMENT_ROOT') . '/lib/mydb.php');
|
|
|
|
try {
|
|
$pdo = db_connect();
|
|
|
|
if (!$pdo) {
|
|
throw new Exception("Database connection failed.");
|
|
}
|
|
|
|
// barobill_companies 테이블에서 모든 회사 가져오기
|
|
$sql = "SELECT c.*, p.company_name as parent_name, p.barobill_user_id as parent_user_id
|
|
FROM {$DB}.barobill_companies c
|
|
LEFT JOIN {$DB}.barobill_companies p ON c.parent_id = p.id
|
|
ORDER BY c.parent_id ASC, c.id ASC";
|
|
$stmt = $pdo->query($sql);
|
|
$companies = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// 계좌 정보 확인 (company_accounts 테이블에서)
|
|
$tenants = [];
|
|
foreach ($companies as $company) {
|
|
// 계좌 정보 확인
|
|
$accountSql = "SELECT COUNT(*) as count FROM {$DB}.company_accounts WHERE company_id = ?";
|
|
$accountStmt = $pdo->prepare($accountSql);
|
|
$accountStmt->execute([$company['id']]);
|
|
$accountResult = $accountStmt->fetch(PDO::FETCH_ASSOC);
|
|
$hasAccount = ($accountResult['count'] > 0);
|
|
|
|
$tenants[] = [
|
|
'id' => $company['id'],
|
|
'name' => $company['company_name'],
|
|
'corp_num' => $company['corp_num'],
|
|
'user_id' => $company['barobill_user_id'],
|
|
'parent_id' => $company['parent_id'],
|
|
'parent_name' => $company['parent_name'] ?? null,
|
|
'has_account' => $hasAccount,
|
|
'memo' => $company['memo'] ?? ''
|
|
];
|
|
}
|
|
|
|
// 현재 세션의 회사 정보
|
|
$currentCompany = $mycompany ?? '';
|
|
|
|
// 현재 선택된 테넌트 (세션에서 가져오거나 기본값)
|
|
$selectedTenantId = $_SESSION['eaccount_tenant_id'] ?? null;
|
|
|
|
// 세션에 저장된 tenant_id가 없으면 '(주)주일기업'을 기본값으로 설정
|
|
if ($selectedTenantId === null) {
|
|
// '(주)주일기업' 찾기
|
|
$defaultTenant = null;
|
|
foreach ($tenants as $tenant) {
|
|
if (strpos($tenant['name'], '주일기업') !== false ||
|
|
strpos($tenant['name'], '주일') !== false ||
|
|
$tenant['user_id'] === 'juil5130') {
|
|
$defaultTenant = $tenant;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// '(주)주일기업'을 찾지 못하면 첫 번째 테넌트 사용
|
|
if ($defaultTenant) {
|
|
$selectedTenantId = $defaultTenant['id'];
|
|
} elseif (count($tenants) > 0) {
|
|
$selectedTenantId = $tenants[0]['id'];
|
|
}
|
|
|
|
if ($selectedTenantId) {
|
|
$_SESSION['eaccount_tenant_id'] = $selectedTenantId;
|
|
}
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'tenants' => $tenants,
|
|
'current_tenant_id' => $selectedTenantId,
|
|
'current_company' => $currentCompany
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
} catch (Exception $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => '테넌트 목록 조회 실패: ' . $e->getMessage(),
|
|
'tenants' => [],
|
|
'current_tenant_id' => null
|
|
], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
?>
|