영업관리 및 운영자 대시보드 2단계 승인 프로세스 및 차등 수당 로직 최종 최적화 완료
This commit is contained in:
@@ -49,7 +49,7 @@ try {
|
||||
// 2) 매니저 수익 (sales_manager_id = 본인): 매니저 수당 대상 (상위 오버라이딩 X)
|
||||
$sql = "
|
||||
/* [Type: sales] 본인이 영업한 건 (가입비/계약금) -> 영업 수당 20%, 상위 5% */
|
||||
SELECT 'sales' as type, p.id, t.tenant_name as customer, p.contract_date as contractDate, p.contract_amount as amount, 0 as fixed_commission
|
||||
SELECT 'sales' as type, p.id, t.tenant_name as customer, p.contract_date as contractDate, p.contract_amount as amount, 0 as fixed_commission, p.join_approved, p.payment_approved
|
||||
FROM sales_tenant_products p
|
||||
JOIN sales_tenants t ON p.tenant_id = t.id
|
||||
WHERE t.manager_id = ?
|
||||
@@ -58,7 +58,7 @@ try {
|
||||
UNION ALL
|
||||
|
||||
/* [Type: manager] 본인이 매니징한 건 (구독료 수당) -> 매니저 수당 100%, 상위 오버라이딩 없음 */
|
||||
SELECT 'manager' as type, p.id, t.tenant_name as customer, p.contract_date as contractDate, p.contract_amount as amount, p.commission_amount as fixed_commission
|
||||
SELECT 'manager' as type, p.id, t.tenant_name as customer, p.contract_date as contractDate, p.contract_amount as amount, p.commission_amount as fixed_commission, p.join_approved, p.payment_approved
|
||||
FROM sales_tenant_products p
|
||||
JOIN sales_tenants t ON p.tenant_id = t.id
|
||||
WHERE t.sales_manager_id = ?
|
||||
@@ -67,7 +67,7 @@ try {
|
||||
UNION ALL
|
||||
|
||||
/* [Type: sales] 기타 영업 기록 (기존 호환) */
|
||||
SELECT 'sales' as type, id, customer_name as customer, contract_date as contractDate, amount, 0 as fixed_commission
|
||||
SELECT 'sales' as type, id, customer_name as customer, contract_date as contractDate, amount, 0 as fixed_commission, 1 as join_approved, 1 as payment_approved
|
||||
FROM sales_record
|
||||
WHERE member_id = ? AND status = 'completed' AND contract_date BETWEEN ? AND ?
|
||||
";
|
||||
@@ -84,18 +84,20 @@ try {
|
||||
|
||||
foreach ($directContracts as &$c) {
|
||||
$itemComm = 0;
|
||||
if ($c['type'] === 'manager') {
|
||||
// 매니저 수당: 본인(depth=0)인 경우에만 가져감 (오버라이딩 불가)
|
||||
if ($parentId == $targetUserId) {
|
||||
$itemComm = $c['fixed_commission'];
|
||||
if ($c['payment_approved'] == 1) {
|
||||
if ($c['type'] === 'manager') {
|
||||
// 매니저 수당: 본인(depth=0)인 경우에만 가져감 (오버라이딩 불가)
|
||||
if ($parentId == $targetUserId) {
|
||||
$itemComm = $c['fixed_commission'];
|
||||
} else {
|
||||
$itemComm = 0;
|
||||
}
|
||||
} else {
|
||||
$itemComm = 0;
|
||||
// 영업 수당: 본인 20%, 상위 5%, 상위2 3%
|
||||
if ($parentId == $targetUserId) $itemComm = $c['amount'] * 0.20;
|
||||
else if ($depth == 1) $itemComm = $c['amount'] * 0.05;
|
||||
else if ($depth == 2) $itemComm = $c['amount'] * 0.03;
|
||||
}
|
||||
} else {
|
||||
// 영업 수당: 본인 20%, 상위 5%, 상위2 3%
|
||||
if ($parentId == $targetUserId) $itemComm = $c['amount'] * 0.20;
|
||||
else if ($depth == 1) $itemComm = $c['amount'] * 0.05;
|
||||
else if ($depth == 2) $itemComm = $c['amount'] * 0.03;
|
||||
}
|
||||
|
||||
$c['commission'] = $itemComm; // 화면 표시용 (이 건으로 targetUserId가 번 돈)
|
||||
@@ -176,37 +178,43 @@ try {
|
||||
// 전체 누적 실적 계산 (재귀) - 로직 동일하게 수정
|
||||
function calculateTotalStats($pdo, $parentId, $targetUserId, $depth) {
|
||||
$sql = "
|
||||
SELECT 'sales' as type, p.contract_amount as amount, 0 as fixed_commission
|
||||
SELECT 'sales' as type, p.contract_amount as amount, 0 as fixed_commission, p.join_approved, p.payment_approved
|
||||
FROM sales_tenant_products p JOIN sales_tenants t ON p.tenant_id = t.id WHERE t.manager_id = ?
|
||||
UNION ALL
|
||||
SELECT 'manager' as type, p.contract_amount as amount, p.commission_amount as fixed_commission
|
||||
SELECT 'manager' as type, p.contract_amount as amount, p.commission_amount as fixed_commission, p.join_approved, p.payment_approved
|
||||
FROM sales_tenant_products p JOIN sales_tenants t ON p.tenant_id = t.id WHERE t.sales_manager_id = ?
|
||||
UNION ALL
|
||||
SELECT 'sales' as type, amount, 0 as fixed_commission FROM sales_record WHERE member_id = ? AND status = 'completed'
|
||||
SELECT 'sales' as type, amount, 0 as fixed_commission, 1 as join_approved, 1 as payment_approved FROM sales_record WHERE member_id = ? AND status = 'completed'
|
||||
";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$parentId, $parentId, $parentId]);
|
||||
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$directSales = 0;
|
||||
$count = count($rows);
|
||||
$commission = 0;
|
||||
$pendingJoin = 0;
|
||||
$pendingPayment = 0;
|
||||
|
||||
foreach ($rows as $r) {
|
||||
$directSales += $r['amount'];
|
||||
if ($r['type'] === 'manager') {
|
||||
if ($parentId == $targetUserId) $commission += $r['fixed_commission'];
|
||||
} else {
|
||||
if ($parentId == $targetUserId) $commission += $r['amount'] * 0.20;
|
||||
else if ($depth == 1) $commission += $r['amount'] * 0.05;
|
||||
else if ($depth == 2) $commission += $r['amount'] * 0.03;
|
||||
if ($r['join_approved'] == 0) $pendingJoin++;
|
||||
if ($r['join_approved'] == 1 && $r['payment_approved'] == 0) $pendingPayment++;
|
||||
|
||||
if ($r['payment_approved'] == 1) {
|
||||
if ($r['type'] === 'manager') {
|
||||
if ($parentId == $targetUserId) $commission += $r['fixed_commission'];
|
||||
} else {
|
||||
if ($parentId == $targetUserId) $commission += $r['amount'] * 0.20;
|
||||
else if ($depth == 1) $commission += $r['amount'] * 0.05;
|
||||
else if ($depth == 2) $commission += $r['amount'] * 0.03;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$stats = [
|
||||
'totalSales' => $directSales,
|
||||
'totalCommission' => $commission,
|
||||
'totalCount' => $count
|
||||
'totalCount' => $count,
|
||||
'pendingJoin' => $pendingJoin,
|
||||
'pendingPayment' => $pendingPayment
|
||||
];
|
||||
|
||||
$stmt = $pdo->prepare("SELECT id FROM sales_member WHERE parent_id = ? AND is_active = 1");
|
||||
@@ -221,11 +229,15 @@ try {
|
||||
$stats['totalSales'] += $childStats['totalSales'];
|
||||
$stats['totalCommission'] += $childStats['totalCommission'];
|
||||
$stats['totalCount'] += $childStats['totalCount'];
|
||||
$stats['pendingJoin'] += $childStats['pendingJoin'];
|
||||
$stats['pendingPayment'] += $childStats['pendingPayment'];
|
||||
} else {
|
||||
// 3단계 이하는 매출만 합산 (오버라이딩 X)
|
||||
$childStats = calculateTotalStats($pdo, $childId, $targetUserId, $depth + 1);
|
||||
$stats['totalSales'] += $childStats['totalSales'];
|
||||
$stats['totalCount'] += $childStats['totalCount'];
|
||||
$stats['pendingJoin'] += $childStats['pendingJoin'];
|
||||
$stats['pendingPayment'] += $childStats['pendingPayment'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user