2025-12-26 13:23:07 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models\Tenants;
|
|
|
|
|
|
2026-01-29 15:33:54 +09:00
|
|
|
use App\Traits\Auditable;
|
2025-12-26 13:23:07 +09:00
|
|
|
use App\Traits\BelongsToTenant;
|
2026-01-23 11:06:06 +09:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2025-12-26 13:23:07 +09:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-01-23 11:06:06 +09:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
2025-12-26 13:23:07 +09:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
|
|
|
|
|
class ExpectedExpense extends Model
|
|
|
|
|
{
|
2026-01-29 15:33:54 +09:00
|
|
|
use Auditable, BelongsToTenant, SoftDeletes;
|
2025-12-26 13:23:07 +09:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'tenant_id',
|
|
|
|
|
'expected_payment_date',
|
|
|
|
|
'settlement_date',
|
|
|
|
|
'transaction_type',
|
|
|
|
|
'amount',
|
|
|
|
|
'client_id',
|
|
|
|
|
'client_name',
|
|
|
|
|
'bank_account_id',
|
|
|
|
|
'account_code',
|
|
|
|
|
'payment_status',
|
|
|
|
|
'approval_status',
|
|
|
|
|
'description',
|
2026-01-23 11:06:06 +09:00
|
|
|
'source_type',
|
|
|
|
|
'source_id',
|
2025-12-26 13:23:07 +09:00
|
|
|
'created_by',
|
|
|
|
|
'updated_by',
|
|
|
|
|
'deleted_by',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'expected_payment_date' => 'date',
|
|
|
|
|
'settlement_date' => 'date',
|
|
|
|
|
'amount' => 'decimal:2',
|
|
|
|
|
'client_id' => 'integer',
|
|
|
|
|
'bank_account_id' => 'integer',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 거래유형 목록
|
|
|
|
|
*/
|
|
|
|
|
public const TRANSACTION_TYPES = [
|
|
|
|
|
'purchase' => '매입',
|
2026-01-23 11:06:06 +09:00
|
|
|
'card' => '카드결제',
|
|
|
|
|
'bill' => '발행어음',
|
2025-12-26 13:23:07 +09:00
|
|
|
'advance' => '선급금',
|
|
|
|
|
'suspense' => '가지급금',
|
|
|
|
|
'rent' => '임대료',
|
|
|
|
|
'salary' => '급여',
|
|
|
|
|
'insurance' => '보험료',
|
|
|
|
|
'tax' => '세금',
|
|
|
|
|
'utilities' => '공과금',
|
|
|
|
|
'other' => '기타',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 지급상태 목록
|
|
|
|
|
*/
|
|
|
|
|
public const PAYMENT_STATUSES = [
|
|
|
|
|
'pending' => '미지급',
|
|
|
|
|
'partial' => '부분지급',
|
|
|
|
|
'paid' => '지급완료',
|
|
|
|
|
'overdue' => '연체',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 결재상태 목록
|
|
|
|
|
*/
|
|
|
|
|
public const APPROVAL_STATUSES = [
|
|
|
|
|
'none' => '미신청',
|
|
|
|
|
'pending' => '결재대기',
|
|
|
|
|
'approved' => '결재완료',
|
|
|
|
|
'rejected' => '반려',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 거래처 관계
|
|
|
|
|
*/
|
|
|
|
|
public function client(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(\App\Models\Orders\Client::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 계좌 관계
|
|
|
|
|
*/
|
|
|
|
|
public function bankAccount(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(BankAccount::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 getTransactionTypeLabelAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return self::TRANSACTION_TYPES[$this->transaction_type] ?? $this->transaction_type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 지급상태 라벨
|
|
|
|
|
*/
|
|
|
|
|
public function getPaymentStatusLabelAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return self::PAYMENT_STATUSES[$this->payment_status] ?? $this->payment_status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 결재상태 라벨
|
|
|
|
|
*/
|
|
|
|
|
public function getApprovalStatusLabelAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return self::APPROVAL_STATUSES[$this->approval_status] ?? $this->approval_status;
|
|
|
|
|
}
|
2026-01-23 11:06:06 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 원본 소스 관계 (Polymorphic)
|
|
|
|
|
*/
|
|
|
|
|
public function source(): MorphTo
|
|
|
|
|
{
|
|
|
|
|
return $this->morphTo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 원본 소스로 검색
|
|
|
|
|
*/
|
|
|
|
|
public function scopeBySource(Builder $query, string $sourceType, int $sourceId): Builder
|
|
|
|
|
{
|
|
|
|
|
return $query->where('source_type', $sourceType)
|
|
|
|
|
->where('source_id', $sourceId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 동기화된 레코드 여부
|
|
|
|
|
*/
|
|
|
|
|
public function isSynced(): bool
|
|
|
|
|
{
|
|
|
|
|
return ! is_null($this->source_type) && ! is_null($this->source_id);
|
|
|
|
|
}
|
2025-12-26 13:23:07 +09:00
|
|
|
}
|