2026-01-21 10:25:18 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use App\Models\BadDebts\BadDebt;
|
|
|
|
|
use App\Models\Orders\Client;
|
|
|
|
|
use App\Models\Orders\Order;
|
|
|
|
|
use App\Models\Tenants\ApprovalStep;
|
|
|
|
|
use App\Models\Tenants\Leave;
|
|
|
|
|
use App\Models\Tenants\Purchase;
|
2026-01-21 20:46:53 +09:00
|
|
|
use App\Models\Tenants\Schedule;
|
2026-01-21 10:25:18 +09:00
|
|
|
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)
|
2026-01-21 20:46:53 +09:00
|
|
|
->where('status_code', Order::STATUS_CONFIRMED) // 확정된 수주만
|
2026-01-21 10:25:18 +09:00
|
|
|
->count();
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => 'orders',
|
|
|
|
|
'label' => __('message.status_board.orders'),
|
|
|
|
|
'count' => $count,
|
|
|
|
|
'path' => '/sales/order-management-sales',
|
|
|
|
|
'isHighlighted' => false,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 채권 추심 현황 (추심 진행 중인 건수)
|
|
|
|
|
*/
|
|
|
|
|
private function getBadDebtStatus(int $tenantId): array
|
|
|
|
|
{
|
2026-03-09 16:32:58 +09:00
|
|
|
$query = BadDebt::query()
|
2026-03-09 16:38:17 +09:00
|
|
|
->where('bad_debts.tenant_id', $tenantId)
|
2026-03-09 16:43:12 +09:00
|
|
|
->where('bad_debts.status', BadDebt::STATUS_COLLECTING)
|
|
|
|
|
->where('bad_debts.is_active', true);
|
2026-03-09 16:32:58 +09:00
|
|
|
|
|
|
|
|
$count = (clone $query)->count();
|
|
|
|
|
|
|
|
|
|
// 최다 금액 거래처명 조회
|
|
|
|
|
$subLabel = null;
|
|
|
|
|
if ($count > 0) {
|
|
|
|
|
$topClient = (clone $query)
|
|
|
|
|
->join('clients', 'bad_debts.client_id', '=', 'clients.id')
|
|
|
|
|
->selectRaw('clients.name, SUM(bad_debts.debt_amount) as total_amount')
|
|
|
|
|
->groupBy('clients.id', 'clients.name')
|
|
|
|
|
->orderByDesc('total_amount')
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($topClient) {
|
|
|
|
|
$subLabel = $count > 1
|
|
|
|
|
? $topClient->name.' 외 '.($count - 1).'건'
|
|
|
|
|
: $topClient->name;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-21 10:25:18 +09:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => 'bad_debts',
|
|
|
|
|
'label' => __('message.status_board.bad_debts'),
|
|
|
|
|
'count' => $count,
|
2026-03-09 16:32:58 +09:00
|
|
|
'sub_label' => $subLabel,
|
2026-01-21 10:25:18 +09:00
|
|
|
'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)
|
2026-01-21 20:46:53 +09:00
|
|
|
*
|
|
|
|
|
* Schedule 테이블에서 type='tax'인 가장 가까운 일정 조회
|
|
|
|
|
* - 본사(tenant_id=NULL) 등록 글로벌 일정 + 테넌트 전용 일정 모두 포함
|
2026-01-21 10:25:18 +09:00
|
|
|
*/
|
|
|
|
|
private function getTaxDeadlineStatus(int $tenantId, Carbon $today): array
|
|
|
|
|
{
|
2026-01-21 20:46:53 +09:00
|
|
|
// Schedule 테이블에서 가장 가까운 세금 신고 일정 조회
|
|
|
|
|
$nextTaxSchedule = Schedule::query()
|
|
|
|
|
->forTenant($tenantId)
|
|
|
|
|
->active()
|
|
|
|
|
->tax()
|
|
|
|
|
->upcoming($today->format('Y-m-d'))
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if (! $nextTaxSchedule) {
|
|
|
|
|
// 등록된 세금 일정이 없는 경우
|
|
|
|
|
return [
|
|
|
|
|
'id' => 'tax_deadline',
|
|
|
|
|
'label' => __('message.status_board.tax_deadline'),
|
|
|
|
|
'count' => __('message.status_board.tax_no_schedule'),
|
|
|
|
|
'path' => '/accounting/tax',
|
|
|
|
|
'isHighlighted' => false,
|
|
|
|
|
];
|
2026-01-21 10:25:18 +09:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 20:46:53 +09:00
|
|
|
$deadline = $nextTaxSchedule->start_date;
|
2026-01-21 10:25:18 +09:00
|
|
|
$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
|
|
|
|
|
{
|
2026-03-09 16:32:58 +09:00
|
|
|
$query = Client::query()
|
2026-01-21 10:25:18 +09:00
|
|
|
->where('tenant_id', $tenantId)
|
2026-03-09 16:32:58 +09:00
|
|
|
->where('created_at', '>=', $today->copy()->subDays(7));
|
|
|
|
|
|
|
|
|
|
$count = (clone $query)->count();
|
|
|
|
|
|
|
|
|
|
// 가장 최근 등록 업체명 조회
|
|
|
|
|
$subLabel = null;
|
|
|
|
|
if ($count > 0) {
|
|
|
|
|
$latestClient = (clone $query)
|
|
|
|
|
->orderByDesc('created_at')
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($latestClient) {
|
|
|
|
|
$subLabel = $count > 1
|
|
|
|
|
? $latestClient->name.' 외 '.($count - 1).'건'
|
|
|
|
|
: $latestClient->name;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-21 10:25:18 +09:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => 'new_clients',
|
|
|
|
|
'label' => __('message.status_board.new_clients'),
|
|
|
|
|
'count' => $count,
|
2026-03-09 16:32:58 +09:00
|
|
|
'sub_label' => $subLabel,
|
2026-01-21 10:25:18 +09:00
|
|
|
'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)
|
2026-01-21 20:46:53 +09:00
|
|
|
->where('status', 'draft') // 대기 중인 발주 (임시저장 상태)
|
2026-01-21 10:25:18 +09:00
|
|
|
->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
|
|
|
|
|
{
|
2026-03-09 16:32:58 +09:00
|
|
|
$query = ApprovalStep::query()
|
|
|
|
|
->whereHas('approval', function ($q) use ($tenantId) {
|
|
|
|
|
$q->where('tenant_id', $tenantId)
|
2026-01-21 10:25:18 +09:00
|
|
|
->where('status', 'pending');
|
|
|
|
|
})
|
|
|
|
|
->where('approver_id', $userId)
|
2026-03-09 19:00:40 +09:00
|
|
|
->where('status', 'pending')
|
|
|
|
|
->approvalOnly();
|
2026-03-09 16:32:58 +09:00
|
|
|
|
|
|
|
|
$count = (clone $query)->count();
|
|
|
|
|
|
|
|
|
|
// 최근 결재 유형 조회
|
|
|
|
|
$subLabel = null;
|
|
|
|
|
if ($count > 0) {
|
|
|
|
|
$latestStep = (clone $query)->with('approval')->latest()->first();
|
|
|
|
|
if ($latestStep && $latestStep->approval) {
|
|
|
|
|
$typeLabel = $latestStep->approval->title ?? '결재';
|
|
|
|
|
$subLabel = $count > 1
|
|
|
|
|
? $typeLabel.' 외 '.($count - 1).'건'
|
|
|
|
|
: $typeLabel;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-21 10:25:18 +09:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => 'approvals',
|
|
|
|
|
'label' => __('message.status_board.approvals'),
|
|
|
|
|
'count' => $count,
|
2026-03-09 16:32:58 +09:00
|
|
|
'sub_label' => $subLabel,
|
2026-01-21 10:25:18 +09:00
|
|
|
'path' => '/approval/inbox',
|
|
|
|
|
'isHighlighted' => $count > 0,
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-01-21 20:46:53 +09:00
|
|
|
}
|