<?php

namespace App\Models\Tenants;

use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class JournalEntry extends Model
{
    use BelongsToTenant, SoftDeletes;

    protected $fillable = [
        'tenant_id',
        'entry_no',
        'entry_date',
        'entry_type',
        'description',
        'total_debit',
        'total_credit',
        'status',
        'source_type',
        'source_key',
        'created_by_name',
        'attachment_note',
    ];

    protected $casts = [
        'entry_date' => 'date',
        'total_debit' => 'integer',
        'total_credit' => 'integer',
    ];

    // Status
    public const STATUS_DRAFT = 'draft';
    public const STATUS_CONFIRMED = 'confirmed';

    // Source type
    public const SOURCE_MANUAL = 'manual';
    public const SOURCE_BANK_TRANSACTION = 'bank_transaction';

    // Entry type
    public const TYPE_GENERAL = 'general';

    /**
     * 분개 행 관계
     */
    public function lines(): HasMany
    {
        return $this->hasMany(JournalEntryLine::class)->orderBy('line_no');
    }
}
