- BillController: 어음 CRUD + 상태변경 + 요약 API - BillService: 비즈니스 로직 (멀티테넌트 지원) - Bill, BillInstallment 모델: 날짜 포맷(Y-m-d) toArray 오버라이드 - FormRequest: Store/Update/UpdateStatus 유효성 검사 - Swagger 문서: BillApi.php - 마이그레이션: bills, bill_installments 테이블 - DummyBillSeeder: 테스트 데이터 30건 + 차수 12건 - API Routes: /api/v1/bills 엔드포인트 7개
176 lines
4.0 KiB
PHP
176 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Tenants;
|
|
|
|
use App\Traits\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Bill extends Model
|
|
{
|
|
use BelongsToTenant, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'bill_number',
|
|
'bill_type',
|
|
'client_id',
|
|
'client_name',
|
|
'amount',
|
|
'issue_date',
|
|
'maturity_date',
|
|
'status',
|
|
'reason',
|
|
'installment_count',
|
|
'note',
|
|
'is_electronic',
|
|
'bank_account_id',
|
|
'created_by',
|
|
'updated_by',
|
|
'deleted_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'issue_date' => 'date',
|
|
'maturity_date' => 'date',
|
|
'amount' => 'decimal:2',
|
|
'client_id' => 'integer',
|
|
'bank_account_id' => 'integer',
|
|
'installment_count' => 'integer',
|
|
'is_electronic' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* 배열/JSON 변환 시 날짜 형식 지정
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
$array = parent::toArray();
|
|
|
|
// 날짜 필드를 Y-m-d 형식으로 변환
|
|
if (isset($array['issue_date']) && $this->issue_date) {
|
|
$array['issue_date'] = $this->issue_date->format('Y-m-d');
|
|
}
|
|
if (isset($array['maturity_date']) && $this->maturity_date) {
|
|
$array['maturity_date'] = $this->maturity_date->format('Y-m-d');
|
|
}
|
|
|
|
return $array;
|
|
}
|
|
|
|
/**
|
|
* 어음 구분 목록
|
|
*/
|
|
public const BILL_TYPES = [
|
|
'received' => '수취',
|
|
'issued' => '발행',
|
|
];
|
|
|
|
/**
|
|
* 수취 어음 상태 목록
|
|
*/
|
|
public const RECEIVED_STATUSES = [
|
|
'stored' => '보관중',
|
|
'maturityAlert' => '만기입금(7일전)',
|
|
'maturityResult' => '만기결과',
|
|
'paymentComplete' => '결제완료',
|
|
'dishonored' => '부도',
|
|
];
|
|
|
|
/**
|
|
* 발행 어음 상태 목록
|
|
*/
|
|
public const ISSUED_STATUSES = [
|
|
'stored' => '보관중',
|
|
'maturityAlert' => '만기입금(7일전)',
|
|
'collectionRequest' => '추심의뢰',
|
|
'collectionComplete' => '추심완료',
|
|
'suing' => '추소중',
|
|
'dishonored' => '부도',
|
|
];
|
|
|
|
/**
|
|
* 거래처 관계
|
|
*/
|
|
public function client(): BelongsTo
|
|
{
|
|
return $this->belongsTo(\App\Models\Orders\Client::class);
|
|
}
|
|
|
|
/**
|
|
* 입금/출금 계좌 관계
|
|
*/
|
|
public function bankAccount(): BelongsTo
|
|
{
|
|
return $this->belongsTo(BankAccount::class);
|
|
}
|
|
|
|
/**
|
|
* 차수 관계
|
|
*/
|
|
public function installments(): HasMany
|
|
{
|
|
return $this->hasMany(BillInstallment::class);
|
|
}
|
|
|
|
/**
|
|
* 생성자 관계
|
|
*/
|
|
public function creator(): BelongsTo
|
|
{
|
|
return $this->belongsTo(\App\Models\Members\User::class, 'created_by');
|
|
}
|
|
|
|
/**
|
|
* 거래처명 조회 (회원/비회원 통합)
|
|
*/
|
|
public function getDisplayClientNameAttribute(): string
|
|
{
|
|
if ($this->client) {
|
|
return $this->client->name;
|
|
}
|
|
|
|
return $this->client_name ?? '';
|
|
}
|
|
|
|
/**
|
|
* 어음 구분 라벨
|
|
*/
|
|
public function getBillTypeLabelAttribute(): string
|
|
{
|
|
return self::BILL_TYPES[$this->bill_type] ?? $this->bill_type;
|
|
}
|
|
|
|
/**
|
|
* 상태 라벨
|
|
*/
|
|
public function getStatusLabelAttribute(): string
|
|
{
|
|
if ($this->bill_type === 'received') {
|
|
return self::RECEIVED_STATUSES[$this->status] ?? $this->status;
|
|
}
|
|
|
|
return self::ISSUED_STATUSES[$this->status] ?? $this->status;
|
|
}
|
|
|
|
/**
|
|
* 만기까지 남은 일수
|
|
*/
|
|
public function getDaysToMaturityAttribute(): int
|
|
{
|
|
return now()->diffInDays($this->maturity_date, false);
|
|
}
|
|
|
|
/**
|
|
* 만기 7일 전 여부
|
|
*/
|
|
public function isMaturityAlertPeriod(): bool
|
|
{
|
|
$days = $this->days_to_maturity;
|
|
|
|
return $days >= 0 && $days <= 7;
|
|
}
|
|
}
|