feat: 매입관리 품의서/지출결의서 연동 기능 추가
- purchases 테이블에 approval_id 컬럼 추가 (마이그레이션) - Purchase 모델에 approval 관계 정의 - PurchaseService에서 approval 데이터 eager loading 구현 - FormRequest에 approval_id 유효성 검증 추가 - Swagger 문서에 approval 관련 스키마 추가 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,7 +17,7 @@ public function index(array $params): LengthAwarePaginator
|
||||
|
||||
$query = Purchase::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->with(['client:id,name']);
|
||||
->with(['client:id,name', 'approval:id,document_number,title,form_id', 'approval.form:id,name,category']);
|
||||
|
||||
// 검색어 필터
|
||||
if (! empty($params['search'])) {
|
||||
@@ -69,7 +69,7 @@ public function show(int $id): Purchase
|
||||
|
||||
return Purchase::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->with(['client:id,name', 'withdrawal', 'creator:id,name'])
|
||||
->with(['client:id,name', 'withdrawal', 'creator:id,name', 'approval:id,document_number,title,form_id,content', 'approval.form:id,name,category'])
|
||||
->findOrFail($id);
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@ public function store(array $data): Purchase
|
||||
$purchase->description = $data['description'] ?? null;
|
||||
$purchase->status = 'draft';
|
||||
$purchase->withdrawal_id = $data['withdrawal_id'] ?? null;
|
||||
$purchase->approval_id = $data['approval_id'] ?? null;
|
||||
$purchase->created_by = $userId;
|
||||
$purchase->updated_by = $userId;
|
||||
$purchase->save();
|
||||
@@ -154,6 +155,9 @@ public function update(int $id, array $data): Purchase
|
||||
if (array_key_exists('tax_invoice_received', $data)) {
|
||||
$purchase->tax_invoice_received = $data['tax_invoice_received'];
|
||||
}
|
||||
if (array_key_exists('approval_id', $data)) {
|
||||
$purchase->approval_id = $data['approval_id'];
|
||||
}
|
||||
|
||||
$purchase->updated_by = $userId;
|
||||
$purchase->save();
|
||||
@@ -302,6 +306,123 @@ public function bulkUpdateTaxReceived(array $ids, bool $taxInvoiceReceived): int
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 대시보드 상세 조회 (CEO 대시보드 모달용)
|
||||
*
|
||||
* @return array{
|
||||
* summary: array{current_month_total: float, previous_month_total: float, change_rate: float, count: int},
|
||||
* monthly_trend: array<array{month: string, amount: float}>,
|
||||
* by_type: array<array{type: string, label: string, amount: float, ratio: float}>,
|
||||
* items: array<array{id: int, date: string, vendor_name: string, amount: float, type: string, type_label: string}>
|
||||
* }
|
||||
*/
|
||||
public function dashboardDetail(): array
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
// 현재 월 범위
|
||||
$currentMonthStart = now()->startOfMonth()->toDateString();
|
||||
$currentMonthEnd = now()->endOfMonth()->toDateString();
|
||||
|
||||
// 전월 범위
|
||||
$previousMonthStart = now()->subMonth()->startOfMonth()->toDateString();
|
||||
$previousMonthEnd = now()->subMonth()->endOfMonth()->toDateString();
|
||||
|
||||
// 1. 요약 정보
|
||||
$currentMonthTotal = Purchase::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereBetween('purchase_date', [$currentMonthStart, $currentMonthEnd])
|
||||
->sum('total_amount');
|
||||
|
||||
$previousMonthTotal = Purchase::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereBetween('purchase_date', [$previousMonthStart, $previousMonthEnd])
|
||||
->sum('total_amount');
|
||||
|
||||
$currentMonthCount = Purchase::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereBetween('purchase_date', [$currentMonthStart, $currentMonthEnd])
|
||||
->count();
|
||||
|
||||
$changeRate = $previousMonthTotal > 0
|
||||
? round((($currentMonthTotal - $previousMonthTotal) / $previousMonthTotal) * 100, 1)
|
||||
: 0;
|
||||
|
||||
// 2. 월별 추이 (최근 7개월)
|
||||
$monthlyTrend = [];
|
||||
for ($i = 6; $i >= 0; $i--) {
|
||||
$monthStart = now()->subMonths($i)->startOfMonth();
|
||||
$monthEnd = now()->subMonths($i)->endOfMonth();
|
||||
|
||||
$amount = Purchase::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereBetween('purchase_date', [$monthStart->toDateString(), $monthEnd->toDateString()])
|
||||
->sum('total_amount');
|
||||
|
||||
$monthlyTrend[] = [
|
||||
'month' => $monthStart->format('Y-m'),
|
||||
'amount' => (float) $amount,
|
||||
];
|
||||
}
|
||||
|
||||
// 3. 유형별 분포 (현재 월)
|
||||
$byTypeRaw = Purchase::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereBetween('purchase_date', [$currentMonthStart, $currentMonthEnd])
|
||||
->select('purchase_type', DB::raw('SUM(total_amount) as amount'))
|
||||
->groupBy('purchase_type')
|
||||
->get();
|
||||
|
||||
$byType = [];
|
||||
$totalAmount = $byTypeRaw->sum('amount');
|
||||
|
||||
foreach ($byTypeRaw as $item) {
|
||||
$type = $item->purchase_type ?? 'unset';
|
||||
$byType[] = [
|
||||
'type' => $type,
|
||||
'label' => Purchase::PURCHASE_TYPES[$type] ?? '미설정',
|
||||
'amount' => (float) $item->amount,
|
||||
'ratio' => $totalAmount > 0 ? round(($item->amount / $totalAmount) * 100, 1) : 0,
|
||||
];
|
||||
}
|
||||
|
||||
// ratio 내림차순 정렬
|
||||
usort($byType, fn ($a, $b) => $b['ratio'] <=> $a['ratio']);
|
||||
|
||||
// 4. 일별 매입 내역 (현재 월)
|
||||
$items = Purchase::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereBetween('purchase_date', [$currentMonthStart, $currentMonthEnd])
|
||||
->with(['client:id,name'])
|
||||
->orderBy('purchase_date', 'desc')
|
||||
->get()
|
||||
->map(function ($purchase) {
|
||||
$type = $purchase->purchase_type ?? 'unset';
|
||||
|
||||
return [
|
||||
'id' => $purchase->id,
|
||||
'date' => $purchase->purchase_date->format('Y-m-d'),
|
||||
'vendor_name' => $purchase->client?->name ?? '-',
|
||||
'amount' => (float) $purchase->total_amount,
|
||||
'type' => $type,
|
||||
'type_label' => Purchase::PURCHASE_TYPES[$type] ?? '미설정',
|
||||
];
|
||||
})
|
||||
->toArray();
|
||||
|
||||
return [
|
||||
'summary' => [
|
||||
'current_month_total' => (float) $currentMonthTotal,
|
||||
'previous_month_total' => (float) $previousMonthTotal,
|
||||
'change_rate' => $changeRate,
|
||||
'count' => $currentMonthCount,
|
||||
],
|
||||
'monthly_trend' => $monthlyTrend,
|
||||
'by_type' => $byType,
|
||||
'items' => $items,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 매입번호 자동 생성
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user