114 lines
2.4 KiB
PHP
114 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Expense extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'project_id',
|
|
'petty_cash_id',
|
|
'employee_id',
|
|
'currency_id',
|
|
'amount',
|
|
'amount_usd',
|
|
'original_amount',
|
|
'category',
|
|
'description',
|
|
'expense_date',
|
|
'receipt_path',
|
|
'requested_by',
|
|
'approved_by',
|
|
'approval_date',
|
|
'approved_at',
|
|
'status',
|
|
];
|
|
|
|
protected $casts = [
|
|
'amount' => 'decimal:2',
|
|
'amount_usd' => 'decimal:2',
|
|
'original_amount' => 'decimal:2',
|
|
'expense_date' => 'date',
|
|
'approval_date' => 'datetime',
|
|
'approved_at' => 'datetime',
|
|
];
|
|
|
|
public function tenant()
|
|
{
|
|
return $this->belongsTo(Tenant::class);
|
|
}
|
|
|
|
public function project()
|
|
{
|
|
return $this->belongsTo(Project::class);
|
|
}
|
|
|
|
public function pettyCash()
|
|
{
|
|
return $this->belongsTo(PettyCash::class);
|
|
}
|
|
|
|
public function employee()
|
|
{
|
|
return $this->belongsTo(Employee::class);
|
|
}
|
|
|
|
public function currency()
|
|
{
|
|
return $this->belongsTo(Currency::class);
|
|
}
|
|
|
|
public function requestedBy()
|
|
{
|
|
return $this->belongsTo(User::class, 'requested_by');
|
|
}
|
|
|
|
public function approvedBy()
|
|
{
|
|
return $this->belongsTo(User::class, 'approved_by');
|
|
}
|
|
|
|
/**
|
|
* Alias for approvedBy (backward compatibility).
|
|
*/
|
|
public function approver()
|
|
{
|
|
return $this->approvedBy();
|
|
}
|
|
|
|
/**
|
|
* Scope: filter by category.
|
|
*/
|
|
public function scopeByCategory($query, $category)
|
|
{
|
|
return $query->where('category', $category);
|
|
}
|
|
|
|
/**
|
|
* Scope: filter by status.
|
|
*/
|
|
public function scopeByStatus($query, $status)
|
|
{
|
|
return $query->where('status', $status);
|
|
}
|
|
|
|
/**
|
|
* Get the status label for display.
|
|
*/
|
|
public function getStatusLabelAttribute(): string
|
|
{
|
|
return [
|
|
'pending' => __('expense.pending'),
|
|
'approved' => __('expense.approved'),
|
|
'rejected' => __('expense.rejected'),
|
|
'paid' => __('expense.paid'),
|
|
][$this->status] ?? $this->status;
|
|
}
|
|
}
|