영업관리자 전체 누적 실적 집계 로직 업데이트 및 신규 테넌트 데이터 연동
This commit is contained in:
@@ -44,9 +44,20 @@ try {
|
||||
|
||||
if (!$member) return null;
|
||||
|
||||
// 이 멤버의 직접 실적 가져오기
|
||||
$stmt = $pdo->prepare("SELECT id, customer_name as customer, contract_date as contractDate, amount FROM sales_record WHERE member_id = ? AND status = 'completed' AND contract_date BETWEEN ? AND ?");
|
||||
$stmt->execute([$parentId, $startDate, $endDate]);
|
||||
// 이 멤버의 직접 실적 가져오기 (기존 sales_record + 신규 sales_tenant_products)
|
||||
$sql = "
|
||||
SELECT id, customer_name as customer, contract_date as contractDate, amount
|
||||
FROM sales_record
|
||||
WHERE member_id = ? AND status = 'completed' AND contract_date BETWEEN ? AND ?
|
||||
UNION ALL
|
||||
SELECT p.id, t.name as customer, p.contract_date as contractDate, p.contract_amount as amount
|
||||
FROM sales_tenant_products p
|
||||
JOIN sales_tenants t ON p.tenant_id = t.id
|
||||
WHERE (t.sales_manager_id = ? OR (t.sales_manager_id IS NULL AND t.manager_id = ?))
|
||||
AND p.contract_date BETWEEN ? AND ?
|
||||
";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$parentId, $startDate, $endDate, $parentId, $parentId, $startDate, $endDate]);
|
||||
$directContracts = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$directSales = 0;
|
||||
@@ -148,8 +159,16 @@ try {
|
||||
|
||||
// 전체 누적 실적 계산 (전체 기간)
|
||||
function calculateTotalStats($pdo, $parentId, $targetUserId, $depth) {
|
||||
$stmt = $pdo->prepare("SELECT amount FROM sales_record WHERE member_id = ? AND status = 'completed'");
|
||||
$stmt->execute([$parentId]);
|
||||
$sql = "
|
||||
SELECT amount FROM sales_record WHERE member_id = ? AND status = 'completed'
|
||||
UNION ALL
|
||||
SELECT p.contract_amount as amount
|
||||
FROM sales_tenant_products p
|
||||
JOIN sales_tenants t ON p.tenant_id = t.id
|
||||
WHERE (t.sales_manager_id = ? OR (t.sales_manager_id IS NULL AND t.manager_id = ?))
|
||||
";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$parentId, $parentId, $parentId]);
|
||||
$amounts = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
||||
|
||||
$directSales = array_sum($amounts);
|
||||
|
||||
@@ -3824,6 +3824,7 @@
|
||||
const [isHelpOpen, setIsHelpOpen] = useState(false);
|
||||
const [selectedRole, setSelectedRole] = useState('영업관리'); // 기본값: 영업관리
|
||||
const [organizationData, setOrganizationData] = useState(null);
|
||||
const [systemTotalStats, setSystemTotalStats] = useState(null);
|
||||
const [isOrgLoading, setIsOrgLoading] = useState(false);
|
||||
|
||||
// Session states
|
||||
@@ -3903,9 +3904,11 @@
|
||||
const result = await res.json();
|
||||
if (result.success) {
|
||||
setOrganizationData(result.org_tree);
|
||||
setSystemTotalStats(result.total_stats);
|
||||
} else {
|
||||
console.error('Performance fetch failed:', result.error);
|
||||
setOrganizationData(null);
|
||||
setSystemTotalStats(null);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Fetch error:', err);
|
||||
@@ -3954,7 +3957,12 @@
|
||||
|
||||
{/* Dashbaord & Org Tree */}
|
||||
{selectedRole === '영업관리' && (
|
||||
<SalesManagementDashboard organizationData={organizationData} onRefresh={fetchPerformanceData} isLoading={isOrgLoading} />
|
||||
<SalesManagementDashboard
|
||||
organizationData={organizationData}
|
||||
systemTotalStats={systemTotalStats}
|
||||
onRefresh={fetchPerformanceData}
|
||||
isLoading={isOrgLoading}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* NEW: Profit & Tenant Management Section */}
|
||||
@@ -4026,7 +4034,7 @@
|
||||
// ... (SimulatorSection, SalesList, CommissionDetailModal remain same) ...
|
||||
|
||||
// 6. Sales Management Dashboard Component
|
||||
const SalesManagementDashboard = ({ organizationData, onRefresh, isLoading }) => {
|
||||
const SalesManagementDashboard = ({ organizationData, systemTotalStats, onRefresh, isLoading }) => {
|
||||
const [periodType, setPeriodType] = useState('current_month'); // current_month, custom
|
||||
const [startYear, setStartYear] = useState(new Date().getFullYear());
|
||||
const [startMonth, setStartMonth] = useState(new Date().getMonth() + 1);
|
||||
@@ -4161,7 +4169,13 @@
|
||||
setPeriodOrgData(filtered);
|
||||
}, [periodType, startYear, startMonth, endYear, endMonth, organizationData]);
|
||||
|
||||
const totalStats = calculateTotalStats(organizationData);
|
||||
const totalStats = systemTotalStats ? {
|
||||
totalRevenue: systemTotalStats.totalSales,
|
||||
totalCommission: systemTotalStats.totalCommission,
|
||||
totalCount: systemTotalStats.totalCount,
|
||||
commissionRate: systemTotalStats.totalSales > 0 ? ((systemTotalStats.totalCommission / systemTotalStats.totalSales) * 100).toFixed(1) : 0
|
||||
} : calculateTotalStats(organizationData);
|
||||
|
||||
const periodStats = calculatePeriodStats(periodOrgData);
|
||||
|
||||
const years = Array.from({ length: 5 }, (_, i) => new Date().getFullYear() - i);
|
||||
|
||||
Reference in New Issue
Block a user