<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Transaction extends Model
{
    protected $fillable = [
        'company_id',
        'user_id',
        'transaction_date',
        'reference',
        'description',
        'status'
    ];

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

   
    
    // app/Models/Transaction.php

public function journalEntries()
{
    return $this->hasMany(JournalEntry::class);
}

public function draftJournalEntries()
{
    return $this->hasMany(DraftJournalEntry::class);
}

// Accessor to get the relevant total
public function getTotalValueAttribute()
{
    if ($this->status === 'draft') {
        return $this->draftJournalEntries->sum('debit');
    }
    return $this->journalEntries->sum('debit');
}
    
}