fix:예상 수당 계산 로직 개선

- 개발 진행 중인 건 (hq_status가 review~int_test)의 예상 수당 포함
- 인계 완료 중 지급 미완료 건의 예상 수당 포함
- 지급 완료된 금액은 예상 수당에서 제외
- calculateExpectedCommissionSummary() 메서드 추가

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
김보곤
2026-02-03 16:17:39 +09:00
parent 8761e8628d
commit 4c5a80b456

View File

@@ -240,8 +240,45 @@ private function getDashboardData(Request $request): array
$stats['total_membership_fee'] = $totalMembershipFee;
$stats['total_commission'] = $totalCommission;
// 1차/2차 수당 지급 현황 계산 (영업파트너 수당 기준)
$commissionSummary = $this->calculateCommissionSummaryFromCollection($myCommissionsAsPartner);
// === 예상 수당 계산 (개발 진행 중 + 인계 완료 중 지급 미완료) ===
// 1) 내가 등록한 가망고객 중 개발 진행 중인 건 (hq_status가 review 이상, handover 미만)
$myProspectIds = TenantProspect::whereIn('registered_by', $partnerIds)->pluck('id')->toArray();
$devInProgressStatuses = [
SalesTenantManagement::HQ_STATUS_REVIEW,
SalesTenantManagement::HQ_STATUS_PLANNING,
SalesTenantManagement::HQ_STATUS_CODING,
SalesTenantManagement::HQ_STATUS_DEV_TEST,
SalesTenantManagement::HQ_STATUS_DEV_DONE,
SalesTenantManagement::HQ_STATUS_INT_TEST,
];
$devInProgressManagementIds = SalesTenantManagement::whereIn('tenant_prospect_id', $myProspectIds)
->whereIn('hq_status', $devInProgressStatuses)
->pluck('id')
->toArray();
$devInProgressRegFee = SalesContractProduct::whereIn('management_id', $devInProgressManagementIds)
->sum('registration_fee');
$expectedFromDevInProgress = (int)($devInProgressRegFee * 0.10); // 가입비 × 10%
// 2) 인계 완료 중 지급 미완료 건
$handoverUnpaidRegFee = SalesContractProduct::whereIn('management_id', $handoverManagementIds)
->sum('registration_fee');
// 지급 완료된 금액 차감
$paidCommissionFromHandover = $myCommissionsAsPartner
->whereIn('management_id', $handoverManagementIds)
->where('status', SalesCommission::STATUS_PAID)
->sum('partner_commission');
$expectedFromHandover = (int)($handoverUnpaidRegFee * 0.10) - $paidCommissionFromHandover;
$expectedFromHandover = max(0, $expectedFromHandover);
// 총 예상 수당 (지급 완료 제외)
$totalExpectedCommission = $expectedFromDevInProgress + $expectedFromHandover;
// 1차/2차 수당 지급 현황 계산 (예상 수당 기반)
$commissionSummary = $this->calculateExpectedCommissionSummary(
$myCommissionsAsPartner,
$totalExpectedCommission,
$paidCommission
);
// 전환된 테넌트만 조회 (최신순, 페이지네이션)
$tenants = Tenant::whereIn('id', $convertedTenantIds)
@@ -841,7 +878,56 @@ public function helpGuide(): View
}
/**
* 수당 컬렉션에서 1차/2차 수당 요약 계산
* 예상 수당 요약 계산 (개발 진행중 + 인계완료 미지급)
*/
private function calculateExpectedCommissionSummary($commissions, int $totalExpectedCommission, int $paidCommission): array
{
$thisMonth = now()->format('Y-m');
$thisMonthStart = now()->startOfMonth()->format('Y-m-d');
$thisMonthEnd = now()->endOfMonth()->format('Y-m-d');
// 지급예정 금액 (입금 완료, 지급 대기)
$scheduledAmount = $commissions
->whereIn('status', [SalesCommission::STATUS_PENDING, SalesCommission::STATUS_APPROVED])
->sum('partner_commission');
// 납입대기 금액 = 총 예상 수당 - 지급예정 - 지급완료
$pendingAmount = max(0, $totalExpectedCommission - $scheduledAmount - $paidCommission);
// 1차/2차 분할 (각 50%)
$halfExpected = $totalExpectedCommission / 2;
$halfPending = $pendingAmount / 2;
$halfScheduled = $scheduledAmount / 2;
$halfPaid = $paidCommission / 2;
return [
'scheduled_this_month' => $commissions
->where('status', SalesCommission::STATUS_APPROVED)
->filter(fn($c) => $c->scheduled_payment_date?->format('Y-m') === $thisMonth)
->sum('partner_commission'),
'total_received' => $paidCommission,
'pending_amount' => $pendingAmount,
'contracts_this_month' => $commissions
->filter(fn($c) => $c->payment_date >= $thisMonthStart && $c->payment_date <= $thisMonthEnd)
->count(),
'first_commission' => [
'total' => (int)$halfExpected,
'pending' => (int)$halfPending,
'scheduled' => (int)$halfScheduled,
'paid' => (int)$halfPaid,
],
'second_commission' => [
'total' => (int)$halfExpected,
'pending' => (int)$halfPending,
'scheduled' => (int)$halfScheduled,
'paid' => (int)$halfPaid,
],
'total_commission' => $totalExpectedCommission,
];
}
/**
* 수당 컬렉션에서 1차/2차 수당 요약 계산 (기존 방식 - 백업)
*/
private function calculateCommissionSummaryFromCollection($commissions): array
{