Files
sam-api/app/Services/StatusBoardService.php
권혁성 f7850e43a7 feat: CEO 대시보드 API 구현 및 DB 컬럼 오류 수정
- StatusBoardService: 현황판 8개 항목 집계 API
- CalendarService: 캘린더 일정 조회 API (작업지시/계약/휴가)
- TodayIssueService: 오늘의 이슈 리스트 API
- VatService: 부가세 신고 현황 API
- EntertainmentService: 접대비 현황 API
- WelfareService: 복리후생 현황 API

버그 수정:
- orders 테이블 status → status_code 컬럼명 수정
- users 테이블 department 관계 → tenantProfile.department로 수정
- Swagger 문서 및 라우트 추가
2026-01-21 10:25:18 +09:00

231 lines
7.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\BadDebts\BadDebt;
use App\Models\Orders\Client;
use App\Models\Orders\Order;
use App\Models\Tenants\Approval;
use App\Models\Tenants\ApprovalStep;
use App\Models\Tenants\Leave;
use App\Models\Tenants\Purchase;
use App\Models\Tenants\Stock;
use Carbon\Carbon;
/**
* CEO 대시보드 현황판(StatusBoard) 서비스
*
* 각 카테고리별 건수를 집계하여 현황판 데이터 제공
*/
class StatusBoardService extends Service
{
/**
* 현황판 전체 데이터 조회
*/
public function summary(): array
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$today = Carbon::today();
return [
'items' => [
$this->getOrdersStatus($tenantId, $today),
$this->getBadDebtStatus($tenantId),
$this->getSafetyStockStatus($tenantId),
$this->getTaxDeadlineStatus($tenantId, $today),
$this->getNewClientStatus($tenantId, $today),
$this->getLeaveStatus($tenantId, $today),
$this->getPurchaseStatus($tenantId),
$this->getApprovalStatus($tenantId, $userId),
],
];
}
/**
* 수주 현황 (오늘 신규 수주 건수)
*/
private function getOrdersStatus(int $tenantId, Carbon $today): array
{
$count = Order::query()
->where('tenant_id', $tenantId)
->whereDate('created_at', $today)
->where('status_code', 'confirmed') // 확정된 수주만
->count();
return [
'id' => 'orders',
'label' => __('message.status_board.orders'),
'count' => $count,
'path' => '/sales/order-management-sales',
'isHighlighted' => false,
];
}
/**
* 채권 추심 현황 (추심 진행 중인 건수)
*/
private function getBadDebtStatus(int $tenantId): array
{
$count = BadDebt::query()
->where('tenant_id', $tenantId)
->where('status', 'in_progress') // 추심 진행 중
->count();
return [
'id' => 'bad_debts',
'label' => __('message.status_board.bad_debts'),
'count' => $count,
'path' => '/accounting/bad-debt-collection',
'isHighlighted' => false,
];
}
/**
* 안전 재고 현황 (안전재고 미달 품목 수)
*/
private function getSafetyStockStatus(int $tenantId): array
{
$count = Stock::query()
->where('tenant_id', $tenantId)
->where('safety_stock', '>', 0) // 안전재고 설정된 품목만
->whereColumn('stock_qty', '<', 'safety_stock')
->count();
$isHighlighted = $count > 0; // 미달 품목 있으면 강조
return [
'id' => 'safety_stock',
'label' => __('message.status_board.safety_stock'),
'count' => $count,
'path' => '/material/stock-status',
'isHighlighted' => $isHighlighted,
];
}
/**
* 세금 신고 현황 (부가세 신고 D-day)
*/
private function getTaxDeadlineStatus(int $tenantId, Carbon $today): array
{
// 부가세 신고 마감일 계산 (분기별: 1/25, 4/25, 7/25, 10/25)
$quarter = $today->quarter;
$deadlineMonth = match ($quarter) {
1 => 1, // 1분기 → 1월 25일
2 => 4, // 2분기 → 4월 25일
3 => 7, // 3분기 → 7월 25일
4 => 10, // 4분기 → 10월 25일
};
$deadlineYear = $today->year;
// 1분기 마감일이 지났으면 다음 분기 마감일
if ($today->month > $deadlineMonth || ($today->month == $deadlineMonth && $today->day > 25)) {
$deadlineMonth = match ($quarter) {
1 => 4,
2 => 7,
3 => 10,
4 => 1, // 다음 해
};
if ($deadlineMonth == 1) {
$deadlineYear++;
}
}
$deadline = Carbon::create($deadlineYear, $deadlineMonth, 25);
$daysUntil = $today->diffInDays($deadline, false);
$countText = $daysUntil >= 0
? __('message.status_board.tax_d_day', ['days' => $daysUntil])
: __('message.status_board.tax_overdue', ['days' => abs($daysUntil)]);
return [
'id' => 'tax_deadline',
'label' => __('message.status_board.tax_deadline'),
'count' => $countText,
'path' => '/accounting/tax',
'isHighlighted' => $daysUntil <= 7 && $daysUntil >= 0,
];
}
/**
* 신규 업체 등록 현황 (최근 7일 신규 거래처)
*/
private function getNewClientStatus(int $tenantId, Carbon $today): array
{
$count = Client::query()
->where('tenant_id', $tenantId)
->where('created_at', '>=', $today->copy()->subDays(7))
->count();
return [
'id' => 'new_clients',
'label' => __('message.status_board.new_clients'),
'count' => $count,
'path' => '/accounting/vendors',
'isHighlighted' => false,
];
}
/**
* 연차 현황 (오늘 휴가 중인 인원)
*/
private function getLeaveStatus(int $tenantId, Carbon $today): array
{
$count = Leave::query()
->where('tenant_id', $tenantId)
->where('status', 'approved')
->whereDate('start_date', '<=', $today)
->whereDate('end_date', '>=', $today)
->count();
return [
'id' => 'leaves',
'label' => __('message.status_board.leaves'),
'count' => $count,
'path' => '/hr/vacation-management',
'isHighlighted' => false,
];
}
/**
* 발주 현황 (발주 대기 건수)
*/
private function getPurchaseStatus(int $tenantId): array
{
$count = Purchase::query()
->where('tenant_id', $tenantId)
->where('status', 'pending') // 대기 중인 발주
->count();
return [
'id' => 'purchases',
'label' => __('message.status_board.purchases'),
'count' => $count,
'path' => '/construction/order/order-management',
'isHighlighted' => false,
];
}
/**
* 결재 요청 현황 (나의 결재 대기 건수)
*/
private function getApprovalStatus(int $tenantId, int $userId): array
{
$count = ApprovalStep::query()
->whereHas('approval', function ($query) use ($tenantId) {
$query->where('tenant_id', $tenantId)
->where('status', 'pending');
})
->where('approver_id', $userId)
->where('status', 'pending')
->count();
return [
'id' => 'approvals',
'label' => __('message.status_board.approvals'),
'count' => $count,
'path' => '/approval/inbox',
'isHighlighted' => $count > 0,
];
}
}