353 lines
12 KiB
PHP
353 lines
12 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use App\Models\Tenants\Bill;
|
||
|
|
use App\Models\Tenants\BillInstallment;
|
||
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
class BillService extends Service
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 어음 목록 조회
|
||
|
|
*/
|
||
|
|
public function index(array $params): LengthAwarePaginator
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
|
||
|
|
$query = Bill::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->with(['client:id,name', 'bankAccount:id,bank_name,account_name', 'installments']);
|
||
|
|
|
||
|
|
// 검색어 필터
|
||
|
|
if (! empty($params['search'])) {
|
||
|
|
$search = $params['search'];
|
||
|
|
$query->where(function ($q) use ($search) {
|
||
|
|
$q->where('bill_number', 'like', "%{$search}%")
|
||
|
|
->orWhere('client_name', 'like', "%{$search}%")
|
||
|
|
->orWhere('note', 'like', "%{$search}%")
|
||
|
|
->orWhereHas('client', function ($q) use ($search) {
|
||
|
|
$q->where('name', 'like', "%{$search}%");
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 어음 구분 필터 (received/issued)
|
||
|
|
if (! empty($params['bill_type'])) {
|
||
|
|
$query->where('bill_type', $params['bill_type']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 상태 필터
|
||
|
|
if (! empty($params['status'])) {
|
||
|
|
$query->where('status', $params['status']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 거래처 필터
|
||
|
|
if (! empty($params['client_id'])) {
|
||
|
|
$query->where('client_id', $params['client_id']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 전자어음 필터
|
||
|
|
if (isset($params['is_electronic']) && $params['is_electronic'] !== '') {
|
||
|
|
$query->where('is_electronic', (bool) $params['is_electronic']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 발행일 범위 필터
|
||
|
|
if (! empty($params['issue_start_date'])) {
|
||
|
|
$query->where('issue_date', '>=', $params['issue_start_date']);
|
||
|
|
}
|
||
|
|
if (! empty($params['issue_end_date'])) {
|
||
|
|
$query->where('issue_date', '<=', $params['issue_end_date']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 만기일 범위 필터
|
||
|
|
if (! empty($params['maturity_start_date'])) {
|
||
|
|
$query->where('maturity_date', '>=', $params['maturity_start_date']);
|
||
|
|
}
|
||
|
|
if (! empty($params['maturity_end_date'])) {
|
||
|
|
$query->where('maturity_date', '<=', $params['maturity_end_date']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 정렬
|
||
|
|
$sortBy = $params['sort_by'] ?? 'issue_date';
|
||
|
|
$sortDir = $params['sort_dir'] ?? 'desc';
|
||
|
|
$query->orderBy($sortBy, $sortDir);
|
||
|
|
|
||
|
|
// 페이지네이션
|
||
|
|
$perPage = $params['per_page'] ?? 20;
|
||
|
|
|
||
|
|
return $query->paginate($perPage);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 어음 상세 조회
|
||
|
|
*/
|
||
|
|
public function show(int $id): Bill
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
|
||
|
|
return Bill::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->with(['client:id,name', 'bankAccount:id,bank_name,account_name', 'installments', 'creator:id,name'])
|
||
|
|
->findOrFail($id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 어음 등록
|
||
|
|
*/
|
||
|
|
public function store(array $data): Bill
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($data, $tenantId, $userId) {
|
||
|
|
// 어음번호 자동 생성 (없을 경우)
|
||
|
|
$billNumber = $data['bill_number'] ?? $this->generateBillNumber($tenantId);
|
||
|
|
|
||
|
|
$bill = new Bill;
|
||
|
|
$bill->tenant_id = $tenantId;
|
||
|
|
$bill->bill_number = $billNumber;
|
||
|
|
$bill->bill_type = $data['bill_type'];
|
||
|
|
$bill->client_id = $data['client_id'] ?? null;
|
||
|
|
$bill->client_name = $data['client_name'] ?? null;
|
||
|
|
$bill->amount = $data['amount'];
|
||
|
|
$bill->issue_date = $data['issue_date'];
|
||
|
|
$bill->maturity_date = $data['maturity_date'];
|
||
|
|
$bill->status = $data['status'] ?? 'stored';
|
||
|
|
$bill->reason = $data['reason'] ?? null;
|
||
|
|
$bill->installment_count = $data['installment_count'] ?? 0;
|
||
|
|
$bill->note = $data['note'] ?? null;
|
||
|
|
$bill->is_electronic = $data['is_electronic'] ?? false;
|
||
|
|
$bill->bank_account_id = $data['bank_account_id'] ?? null;
|
||
|
|
$bill->created_by = $userId;
|
||
|
|
$bill->updated_by = $userId;
|
||
|
|
$bill->save();
|
||
|
|
|
||
|
|
// 차수 관리 저장
|
||
|
|
if (! empty($data['installments'])) {
|
||
|
|
foreach ($data['installments'] as $installment) {
|
||
|
|
BillInstallment::create([
|
||
|
|
'bill_id' => $bill->id,
|
||
|
|
'installment_date' => $installment['date'],
|
||
|
|
'amount' => $installment['amount'],
|
||
|
|
'note' => $installment['note'] ?? null,
|
||
|
|
'created_by' => $userId,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
// 차수 카운트 업데이트
|
||
|
|
$bill->installment_count = count($data['installments']);
|
||
|
|
$bill->save();
|
||
|
|
}
|
||
|
|
|
||
|
|
return $bill->load(['client:id,name', 'bankAccount:id,bank_name,account_name', 'installments']);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 어음 수정
|
||
|
|
*/
|
||
|
|
public function update(int $id, array $data): Bill
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($id, $data, $tenantId, $userId) {
|
||
|
|
$bill = Bill::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->findOrFail($id);
|
||
|
|
|
||
|
|
if (isset($data['bill_number'])) {
|
||
|
|
$bill->bill_number = $data['bill_number'];
|
||
|
|
}
|
||
|
|
if (isset($data['bill_type'])) {
|
||
|
|
$bill->bill_type = $data['bill_type'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('client_id', $data)) {
|
||
|
|
$bill->client_id = $data['client_id'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('client_name', $data)) {
|
||
|
|
$bill->client_name = $data['client_name'];
|
||
|
|
}
|
||
|
|
if (isset($data['amount'])) {
|
||
|
|
$bill->amount = $data['amount'];
|
||
|
|
}
|
||
|
|
if (isset($data['issue_date'])) {
|
||
|
|
$bill->issue_date = $data['issue_date'];
|
||
|
|
}
|
||
|
|
if (isset($data['maturity_date'])) {
|
||
|
|
$bill->maturity_date = $data['maturity_date'];
|
||
|
|
}
|
||
|
|
if (isset($data['status'])) {
|
||
|
|
$bill->status = $data['status'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('reason', $data)) {
|
||
|
|
$bill->reason = $data['reason'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('note', $data)) {
|
||
|
|
$bill->note = $data['note'];
|
||
|
|
}
|
||
|
|
if (isset($data['is_electronic'])) {
|
||
|
|
$bill->is_electronic = $data['is_electronic'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('bank_account_id', $data)) {
|
||
|
|
$bill->bank_account_id = $data['bank_account_id'];
|
||
|
|
}
|
||
|
|
|
||
|
|
$bill->updated_by = $userId;
|
||
|
|
$bill->save();
|
||
|
|
|
||
|
|
// 차수 관리 업데이트 (전체 교체)
|
||
|
|
if (isset($data['installments'])) {
|
||
|
|
// 기존 차수 삭제
|
||
|
|
$bill->installments()->delete();
|
||
|
|
|
||
|
|
// 새 차수 추가
|
||
|
|
foreach ($data['installments'] as $installment) {
|
||
|
|
BillInstallment::create([
|
||
|
|
'bill_id' => $bill->id,
|
||
|
|
'installment_date' => $installment['date'],
|
||
|
|
'amount' => $installment['amount'],
|
||
|
|
'note' => $installment['note'] ?? null,
|
||
|
|
'created_by' => $userId,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
// 차수 카운트 업데이트
|
||
|
|
$bill->installment_count = count($data['installments']);
|
||
|
|
$bill->save();
|
||
|
|
}
|
||
|
|
|
||
|
|
return $bill->fresh(['client:id,name', 'bankAccount:id,bank_name,account_name', 'installments']);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 어음 삭제
|
||
|
|
*/
|
||
|
|
public function destroy(int $id): bool
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($id, $tenantId, $userId) {
|
||
|
|
$bill = Bill::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->findOrFail($id);
|
||
|
|
|
||
|
|
$bill->deleted_by = $userId;
|
||
|
|
$bill->save();
|
||
|
|
$bill->delete();
|
||
|
|
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 어음 상태 변경
|
||
|
|
*/
|
||
|
|
public function updateStatus(int $id, string $status): Bill
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
$bill = Bill::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->findOrFail($id);
|
||
|
|
|
||
|
|
$bill->status = $status;
|
||
|
|
$bill->updated_by = $userId;
|
||
|
|
$bill->save();
|
||
|
|
|
||
|
|
return $bill->fresh(['client:id,name', 'bankAccount:id,bank_name,account_name', 'installments']);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 어음 요약 (기간별 합계)
|
||
|
|
*/
|
||
|
|
public function summary(array $params): array
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
|
||
|
|
$query = Bill::query()
|
||
|
|
->where('tenant_id', $tenantId);
|
||
|
|
|
||
|
|
// 어음 구분 필터
|
||
|
|
if (! empty($params['bill_type'])) {
|
||
|
|
$query->where('bill_type', $params['bill_type']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 발행일 범위 필터
|
||
|
|
if (! empty($params['issue_start_date'])) {
|
||
|
|
$query->where('issue_date', '>=', $params['issue_start_date']);
|
||
|
|
}
|
||
|
|
if (! empty($params['issue_end_date'])) {
|
||
|
|
$query->where('issue_date', '<=', $params['issue_end_date']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 만기일 범위 필터
|
||
|
|
if (! empty($params['maturity_start_date'])) {
|
||
|
|
$query->where('maturity_date', '>=', $params['maturity_start_date']);
|
||
|
|
}
|
||
|
|
if (! empty($params['maturity_end_date'])) {
|
||
|
|
$query->where('maturity_date', '<=', $params['maturity_end_date']);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 전체 합계
|
||
|
|
$total = (clone $query)->sum('amount');
|
||
|
|
$count = (clone $query)->count();
|
||
|
|
|
||
|
|
// 구분별 합계
|
||
|
|
$byType = (clone $query)
|
||
|
|
->select('bill_type', DB::raw('SUM(amount) as total'), DB::raw('COUNT(*) as count'))
|
||
|
|
->groupBy('bill_type')
|
||
|
|
->get()
|
||
|
|
->keyBy('bill_type')
|
||
|
|
->toArray();
|
||
|
|
|
||
|
|
// 상태별 합계
|
||
|
|
$byStatus = (clone $query)
|
||
|
|
->select('status', DB::raw('SUM(amount) as total'), DB::raw('COUNT(*) as count'))
|
||
|
|
->groupBy('status')
|
||
|
|
->get()
|
||
|
|
->keyBy('status')
|
||
|
|
->toArray();
|
||
|
|
|
||
|
|
// 만기 임박 (7일 이내)
|
||
|
|
$maturityAlert = (clone $query)
|
||
|
|
->where('maturity_date', '>=', now()->toDateString())
|
||
|
|
->where('maturity_date', '<=', now()->addDays(7)->toDateString())
|
||
|
|
->whereNotIn('status', ['paymentComplete', 'collectionComplete', 'dishonored'])
|
||
|
|
->sum('amount');
|
||
|
|
|
||
|
|
return [
|
||
|
|
'total_amount' => (float) $total,
|
||
|
|
'total_count' => $count,
|
||
|
|
'by_type' => $byType,
|
||
|
|
'by_status' => $byStatus,
|
||
|
|
'maturity_alert_amount' => (float) $maturityAlert,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 어음번호 자동 생성
|
||
|
|
*/
|
||
|
|
private function generateBillNumber(int $tenantId): string
|
||
|
|
{
|
||
|
|
$prefix = date('Ym');
|
||
|
|
$lastBill = Bill::query()
|
||
|
|
->where('tenant_id', $tenantId)
|
||
|
|
->where('bill_number', 'like', $prefix.'%')
|
||
|
|
->orderBy('bill_number', 'desc')
|
||
|
|
->first();
|
||
|
|
|
||
|
|
if ($lastBill) {
|
||
|
|
$lastNumber = (int) substr($lastBill->bill_number, strlen($prefix));
|
||
|
|
$nextNumber = $lastNumber + 1;
|
||
|
|
} else {
|
||
|
|
$nextNumber = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $prefix.str_pad((string) $nextNumber, 6, '0', STR_PAD_LEFT);
|
||
|
|
}
|
||
|
|
}
|