303 lines
9.9 KiB
PHP
303 lines
9.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use App\Models\Tenants\ExpectedExpense;
|
||
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
class ExpectedExpenseService extends Service
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 미지급비용 목록 조회
|
||
|
|
*/
|
||
|
|
public function index(array $params): LengthAwarePaginator
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
|
||
|
|
$query = ExpectedExpense::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->with(['client:id,name', 'bankAccount:id,bank_name,account_name']);
|
||
|
|
|
||
|
|
// 검색어 필터
|
||
|
|
if (! empty($params['search'])) {
|
||
|
|
$search = $params['search'];
|
||
|
|
$query->where(function ($q) use ($search) {
|
||
|
|
$q->where('client_name', 'like', "%{$search}%")
|
||
|
|
->orWhere('account_code', 'like', "%{$search}%")
|
||
|
|
->orWhere('description', 'like', "%{$search}%")
|
||
|
|
->orWhereHas('client', function ($q) use ($search) {
|
||
|
|
$q->where('name', 'like', "%{$search}%");
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 날짜 범위 필터 (예상 지급일 기준)
|
||
|
|
if (! empty($params['start_date'])) {
|
||
|
|
$query->where('expected_payment_date', '>=', $params['start_date']);
|
||
|
|
}
|
||
|
|
if (! empty($params['end_date'])) {
|
||
|
|
$query->where('expected_payment_date', '<=', $params['end_date']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 거래처 필터
|
||
|
|
if (! empty($params['client_id'])) {
|
||
|
|
$query->where('client_id', $params['client_id']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 거래유형 필터
|
||
|
|
if (! empty($params['transaction_type'])) {
|
||
|
|
$query->where('transaction_type', $params['transaction_type']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 지급상태 필터
|
||
|
|
if (! empty($params['payment_status'])) {
|
||
|
|
$query->where('payment_status', $params['payment_status']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 결재상태 필터
|
||
|
|
if (! empty($params['approval_status'])) {
|
||
|
|
$query->where('approval_status', $params['approval_status']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 정렬
|
||
|
|
$sortBy = $params['sort_by'] ?? 'expected_payment_date';
|
||
|
|
$sortDir = $params['sort_dir'] ?? 'asc';
|
||
|
|
$query->orderBy($sortBy, $sortDir);
|
||
|
|
|
||
|
|
// 페이지네이션
|
||
|
|
$perPage = $params['per_page'] ?? 50;
|
||
|
|
|
||
|
|
return $query->paginate($perPage);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 미지급비용 상세 조회
|
||
|
|
*/
|
||
|
|
public function show(int $id): ExpectedExpense
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
|
||
|
|
return ExpectedExpense::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->with(['client:id,name', 'bankAccount:id,bank_name,account_name', 'creator:id,name'])
|
||
|
|
->findOrFail($id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 미지급비용 등록
|
||
|
|
*/
|
||
|
|
public function store(array $data): ExpectedExpense
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($data, $tenantId, $userId) {
|
||
|
|
$expense = new ExpectedExpense;
|
||
|
|
$expense->tenant_id = $tenantId;
|
||
|
|
$expense->expected_payment_date = $data['expected_payment_date'];
|
||
|
|
$expense->settlement_date = $data['settlement_date'] ?? null;
|
||
|
|
$expense->transaction_type = $data['transaction_type'];
|
||
|
|
$expense->amount = $data['amount'];
|
||
|
|
$expense->client_id = $data['client_id'] ?? null;
|
||
|
|
$expense->client_name = $data['client_name'] ?? null;
|
||
|
|
$expense->bank_account_id = $data['bank_account_id'] ?? null;
|
||
|
|
$expense->account_code = $data['account_code'] ?? null;
|
||
|
|
$expense->payment_status = $data['payment_status'] ?? 'pending';
|
||
|
|
$expense->approval_status = $data['approval_status'] ?? 'none';
|
||
|
|
$expense->description = $data['description'] ?? null;
|
||
|
|
$expense->created_by = $userId;
|
||
|
|
$expense->updated_by = $userId;
|
||
|
|
$expense->save();
|
||
|
|
|
||
|
|
return $expense->load(['client:id,name', 'bankAccount:id,bank_name,account_name']);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 미지급비용 수정
|
||
|
|
*/
|
||
|
|
public function update(int $id, array $data): ExpectedExpense
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($id, $data, $tenantId, $userId) {
|
||
|
|
$expense = ExpectedExpense::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->findOrFail($id);
|
||
|
|
|
||
|
|
if (isset($data['expected_payment_date'])) {
|
||
|
|
$expense->expected_payment_date = $data['expected_payment_date'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('settlement_date', $data)) {
|
||
|
|
$expense->settlement_date = $data['settlement_date'];
|
||
|
|
}
|
||
|
|
if (isset($data['transaction_type'])) {
|
||
|
|
$expense->transaction_type = $data['transaction_type'];
|
||
|
|
}
|
||
|
|
if (isset($data['amount'])) {
|
||
|
|
$expense->amount = $data['amount'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('client_id', $data)) {
|
||
|
|
$expense->client_id = $data['client_id'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('client_name', $data)) {
|
||
|
|
$expense->client_name = $data['client_name'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('bank_account_id', $data)) {
|
||
|
|
$expense->bank_account_id = $data['bank_account_id'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('account_code', $data)) {
|
||
|
|
$expense->account_code = $data['account_code'];
|
||
|
|
}
|
||
|
|
if (isset($data['payment_status'])) {
|
||
|
|
$expense->payment_status = $data['payment_status'];
|
||
|
|
}
|
||
|
|
if (isset($data['approval_status'])) {
|
||
|
|
$expense->approval_status = $data['approval_status'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('description', $data)) {
|
||
|
|
$expense->description = $data['description'];
|
||
|
|
}
|
||
|
|
|
||
|
|
$expense->updated_by = $userId;
|
||
|
|
$expense->save();
|
||
|
|
|
||
|
|
return $expense->fresh(['client:id,name', 'bankAccount:id,bank_name,account_name']);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 미지급비용 삭제
|
||
|
|
*/
|
||
|
|
public function destroy(int $id): bool
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($id, $tenantId, $userId) {
|
||
|
|
$expense = ExpectedExpense::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->findOrFail($id);
|
||
|
|
|
||
|
|
$expense->deleted_by = $userId;
|
||
|
|
$expense->save();
|
||
|
|
$expense->delete();
|
||
|
|
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 일괄 삭제
|
||
|
|
*/
|
||
|
|
public function destroyMany(array $ids): int
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($ids, $tenantId, $userId) {
|
||
|
|
$count = 0;
|
||
|
|
foreach ($ids as $id) {
|
||
|
|
$expense = ExpectedExpense::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->find($id);
|
||
|
|
|
||
|
|
if ($expense) {
|
||
|
|
$expense->deleted_by = $userId;
|
||
|
|
$expense->save();
|
||
|
|
$expense->delete();
|
||
|
|
$count++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $count;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 예상 지급일 일괄 변경
|
||
|
|
*/
|
||
|
|
public function updateExpectedPaymentDate(array $ids, string $newDate): int
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($ids, $newDate, $tenantId, $userId) {
|
||
|
|
return ExpectedExpense::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->whereIn('id', $ids)
|
||
|
|
->update([
|
||
|
|
'expected_payment_date' => $newDate,
|
||
|
|
'updated_by' => $userId,
|
||
|
|
]);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 미지급비용 요약 (기간별 합계)
|
||
|
|
*/
|
||
|
|
public function summary(array $params): array
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
|
||
|
|
$query = ExpectedExpense::query()
|
||
|
|
->where('tenant_id', $tenantId);
|
||
|
|
|
||
|
|
// 날짜 범위 필터
|
||
|
|
if (! empty($params['start_date'])) {
|
||
|
|
$query->where('expected_payment_date', '>=', $params['start_date']);
|
||
|
|
}
|
||
|
|
if (! empty($params['end_date'])) {
|
||
|
|
$query->where('expected_payment_date', '<=', $params['end_date']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 지급상태 필터
|
||
|
|
if (! empty($params['payment_status'])) {
|
||
|
|
$query->where('payment_status', $params['payment_status']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 전체 합계
|
||
|
|
$total = (clone $query)->sum('amount');
|
||
|
|
$count = (clone $query)->count();
|
||
|
|
|
||
|
|
// 지급상태별 합계
|
||
|
|
$byPaymentStatus = (clone $query)
|
||
|
|
->select('payment_status', DB::raw('SUM(amount) as total'), DB::raw('COUNT(*) as count'))
|
||
|
|
->groupBy('payment_status')
|
||
|
|
->get()
|
||
|
|
->keyBy('payment_status')
|
||
|
|
->toArray();
|
||
|
|
|
||
|
|
// 거래유형별 합계
|
||
|
|
$byTransactionType = (clone $query)
|
||
|
|
->select('transaction_type', DB::raw('SUM(amount) as total'), DB::raw('COUNT(*) as count'))
|
||
|
|
->groupBy('transaction_type')
|
||
|
|
->get()
|
||
|
|
->keyBy('transaction_type')
|
||
|
|
->toArray();
|
||
|
|
|
||
|
|
// 월별 합계
|
||
|
|
$byMonth = (clone $query)
|
||
|
|
->select(
|
||
|
|
DB::raw("DATE_FORMAT(expected_payment_date, '%Y-%m') as month"),
|
||
|
|
DB::raw('SUM(amount) as total'),
|
||
|
|
DB::raw('COUNT(*) as count')
|
||
|
|
)
|
||
|
|
->groupBy('month')
|
||
|
|
->orderBy('month')
|
||
|
|
->get()
|
||
|
|
->keyBy('month')
|
||
|
|
->toArray();
|
||
|
|
|
||
|
|
return [
|
||
|
|
'total_amount' => (float) $total,
|
||
|
|
'total_count' => $count,
|
||
|
|
'by_payment_status' => $byPaymentStatus,
|
||
|
|
'by_transaction_type' => $byTransactionType,
|
||
|
|
'by_month' => $byMonth,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|