<?php

namespace App\Models;

use App\Traits\HasPeriod;
use App\Traits\HasUser;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Journal extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $casts = ['happened_on' => 'date'];

    public function ledgers(): HasMany
    {
        return $this->hasMany(Ledger::class);
    }

    public function scopeDateCreatedFilter(Builder $query, $date) : Builder
    {
        $dates = explode(' to ',$date);
        if (isset($dates[1])) {
            return $query->whereBetween('happened_on' ,[$dates[0], $dates[1]]);
        }
        return $query->where('happened_on', $dates[0]);
    }
}
