diff --git a/salesmanagement/api/sales_tenants.php b/salesmanagement/api/sales_tenants.php index 8f94236..ae28710 100644 --- a/salesmanagement/api/sales_tenants.php +++ b/salesmanagement/api/sales_tenants.php @@ -478,6 +478,7 @@ try { $stmt->execute([$manager_val, $tenant_id]); echo json_encode(['success' => true, 'message' => $manager_val ? '담당 매니저가 지정되었습니다.' : '담당 매니저 지정이 취소되었습니다.']); + } elseif ($action === 'update_product') { $product_id = $data['id'] ?? null; if (!$product_id) throw new Exception("ID가 누락되었습니다."); @@ -492,6 +493,10 @@ try { if ($p['operator_confirmed'] == 1) throw new Exception("이미 승인된 계약은 수정할 수 없습니다."); $product_name = $data['product_name'] ?? ''; + $contract_amount = $data['contract_amount'] ?? 0; + $commission_rate = $data['commission_rate'] ?? 0; + $contract_date = $data['contract_date'] ?? date('Y-m-d'); + $sub_models = isset($data['sub_models']) ? json_encode($data['sub_models']) : null; $commission_amount = ($contract_amount * $commission_rate) / 100; @@ -499,6 +504,37 @@ try { $stmt->execute([$product_name, $contract_amount, $commission_rate, $commission_amount, $contract_date, $sub_models, $product_id]); echo json_encode(['success' => true, 'message' => '계약 정보가 수정되었습니다.']); + + } elseif ($action === 'update_tenant') { + $tenant_id = $data['id'] ?? null; + if (!checkTenantPermission($pdo, $tenant_id, $currentUser)) throw new Exception("권한이 없습니다."); + + $tenant_name = $data['tenant_name'] ?? ''; + $representative = $data['representative'] ?? ''; + $business_no = $data['business_no'] ?? ''; + $contact_phone = $data['contact_phone'] ?? ''; + $email = $data['email'] ?? ''; + $address = $data['address'] ?? ''; + $sales_manager_id = $data['sales_manager_id'] ?? null; + + if (!$tenant_name) throw new Exception("업체명은 필수입니다."); + + $stmt = $pdo->prepare("UPDATE sales_tenants SET tenant_name = ?, representative = ?, business_no = ?, contact_phone = ?, email = ?, address = ?, sales_manager_id = ? WHERE id = ?"); + $stmt->execute([$tenant_name, $representative, $business_no, $contact_phone, $email, $address, $sales_manager_id, $tenant_id]); + + echo json_encode(['success' => true, 'message' => '테넌트 정보가 수정되었습니다.']); + + } elseif ($action === 'delete_tenant') { + $tenant_id = $data['id'] ?? null; + if (!checkTenantPermission($pdo, $tenant_id, $currentUser)) throw new Exception("권한이 없습니다."); + + // 관련 데이터 삭제 (계약, 시나리오, 상담기록) + $pdo->prepare("DELETE FROM sales_tenant_products WHERE tenant_id = ?")->execute([$tenant_id]); + $pdo->prepare("DELETE FROM sales_tenant_scenarios WHERE tenant_id = ?")->execute([$tenant_id]); + $pdo->prepare("DELETE FROM sales_tenant_consultations WHERE tenant_id = ?")->execute([$tenant_id]); + $pdo->prepare("DELETE FROM sales_tenants WHERE id = ?")->execute([$tenant_id]); + + echo json_encode(['success' => true, 'message' => '테넌트가 삭제되었습니다.']); } break; } diff --git a/salesmanagement/index.php b/salesmanagement/index.php index 83b81fe..5020026 100644 --- a/salesmanagement/index.php +++ b/salesmanagement/index.php @@ -2881,6 +2881,7 @@ const [isSaving, setIsSaving] = useState(false); const [potentialManagerList, setPotentialManagerList] = useState([]); const [activeManagerPopover, setActiveManagerPopover] = useState(null); + const [editingTenantId, setEditingTenantId] = useState(null); const popoverRef = useRef(null); // Filtered manager list based on role @@ -3067,16 +3068,19 @@ const handleCreateTenant = async (e) => { e.preventDefault(); try { - const res = await fetch('api/sales_tenants.php?action=create_tenant', { + const action = editingTenantId ? 'update_tenant' : 'create_tenant'; + const payload = editingTenantId ? { ...tenantFormData, id: editingTenantId } : tenantFormData; + + const res = await fetch(`api/sales_tenants.php?action=${action}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(tenantFormData) + body: JSON.stringify(payload) }); const result = await res.json(); if (result.success) { alert(result.message); setIsTenantModalOpen(false); - const newTenantId = result.id; + setEditingTenantId(null); await fetchData(); setTenantFormData({ tenant_name: '', representative: '', business_no: '', contact_phone: '', email: '', address: '', sales_manager_id: currentUser.id }); @@ -3085,7 +3089,41 @@ alert(result.error); } } catch (err) { - alert('등록 중 오류가 발생했습니다.'); + alert('처리 중 오류가 발생했습니다.'); + } + }; + + const handleOpenEditTenant = (t) => { + setEditingTenantId(t.id); + setTenantFormData({ + tenant_name: t.tenant_name, + representative: t.representative, + business_no: t.business_no, + contact_phone: t.contact_phone, + email: t.email, + address: t.address, + sales_manager_id: t.sales_manager_id + }); + setIsTenantModalOpen(true); + }; + + const handleDeleteTenant = async (tenantId) => { + if (!confirm('정말로 이 테넌트를 삭제하시겠습니까? 관련 계약 및 모든 기록이 삭제됩니다.')) return; + try { + const res = await fetch('api/sales_tenants.php?action=delete_tenant', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ id: tenantId }) + }); + const result = await res.json(); + if (result.success) { + alert('삭제되었습니다.'); + fetchData(); + } else { + alert(result.error); + } + } catch (err) { + alert('삭제 중 오류가 발생했습니다.'); } }; @@ -3226,7 +3264,14 @@ {currentRole === '영업관리' && ( - - - )} - {(currentRole === '매니저' || (currentRole === '영업관리' && t.sales_manager_id == currentUser.id)) && ( + +
+ {currentRole === '영업관리' && ( + + )} + {(currentRole === '매니저' || (currentRole === '영업관리' && t.sales_manager_id == currentUser.id)) && ( + + )} + +
- )} -
- + +
+ + {expandedTenantId === t.id && ( @@ -3455,8 +3509,8 @@

- - 신규 테넌트 등록 + + {editingTenantId ? '테넌트 정보 수정' : '신규 테넌트 등록'}

-
@@ -3516,9 +3570,30 @@ setTenantFormData({...tenantFormData, address: e.target.value})} className="w-full px-3 py-2 border border-slate-200 rounded-lg outline-none focus:ring-2 focus:ring-blue-500" placeholder="상세 주소를 입력하세요" /> -
- - +
+
+ {editingTenantId && ( + + )} +
+
+ + +