<?php

namespace App\Models;

use App\Http\Filters\QueryFilter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

class Transaction extends Model
{
    public function scopeFilter($query, QueryFilter $filters)
    {
        return $filters->apply($query);
    }

    protected $fillable = [
        'user_id',
        'transaction_type', // enum (debit, credit)
        'trading_year_id',
        'account_type_id', // 1 for income, 2 for expenses
        'account_id', // sub types of income / expenses
        'debit', // amount field
        'credit', // amount field
        'description',
        'transaction_date',
        'attachment',
    ];

    protected $dates = [
        'transaction_date',
    ];

    public function getAttachmentAttribute($value) {
        if(!is_null($value))
            return asset('storage/'.$value);
    }

    public function account_type()
    {
        return $this->belongsTo(AccountType::class);
    }

}