<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Transaction extends Model
{
    use HasFactory;
    protected $guarded = [];

    public function user(){
        return $this->belongsTo(User::class);
    }

    public function account(){
        return $this->belongsTo(Account::class);
    }

    // Start Generating Account Number//////////////////
    protected static function txExists($tx_id){
        return Transaction::where('tx_id',$tx_id)->exists();
    }
    protected static function generateTxId(){
        $tx_id = 'TX'.mt_rand(100000,999999);
        if(Self::txExists($tx_id)){
            return Self::generateTxId();
        }
        return $tx_id;
    }
}
