<?php

namespace App\Models;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class Account extends BaseModel
{
    use SoftDeletes;

    protected $fillable = [
        'company_id','parent_id','code','name','type','sub_type','detail_type',
        'is_group','is_system','is_active','is_bank','is_cash',
        'description','sort_order','depth','path'
    ];

    protected $casts = [
        'is_group'  => 'boolean',
        'is_system' => 'boolean',
        'is_active' => 'boolean',
        'is_bank'   => 'boolean',
        'is_cash'   => 'boolean',
    ];

    public function parent()
    {
        return $this->belongsTo(Account::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(Account::class, 'parent_id');
    }

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopePostable($query)
    {
        return $query->where('is_group', false)->where('is_active', true);
    }
}
