| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/project-slim/app/Models/ |
Upload File : |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Builder;
class Report extends BaseModel
{
/**
* The table associated with the model.
*/
protected $table = 'repo_reports'; // You may need to add this table to schema
/**
* The primary key for the model.
*/
protected $primaryKey = 'report_id';
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
'title',
'description',
'type',
'status',
'company_id',
'created_by',
'data',
'file_path',
'scheduled_at',
'completed_at'
];
/**
* The attributes that should be hidden for serialization.
* Inherits from BaseModel to hide all ID fields
*/
protected $hidden = [
'id',
'report_id',
'company_id',
'created_by', // This is also a user_id
'user_id',
'role_id',
'module_id'
];
/**
* The attributes that should be cast to native types.
*/
protected $casts = [
'data' => 'array',
'scheduled_at' => 'datetime',
'completed_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* Get the company that owns the report.
*/
public function company(): BelongsTo
{
return $this->belongsTo(Company::class, 'company_id', 'company_id');
}
/**
* Get the user who created the report.
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by', 'user_id');
}
/**
* Scope for completed reports.
*/
public function scopeCompleted(Builder $query): Builder
{
return $query->where('status', 'completed');
}
/**
* Scope for pending reports.
*/
public function scopePending(Builder $query): Builder
{
return $query->where('status', 'pending');
}
/**
* Scope for failed reports.
*/
public function scopeFailed(Builder $query): Builder
{
return $query->where('status', 'failed');
}
/**
* Scope for scheduled reports.
*/
public function scopeScheduled(Builder $query): Builder
{
return $query->whereNotNull('scheduled_at');
}
/**
* Scope by report type.
*/
public function scopeByType(Builder $query, string $type): Builder
{
return $query->where('type', $type);
}
/**
* Scope by company.
*/
public function scopeByCompany(Builder $query, int $companyId): Builder
{
return $query->where('company_id', $companyId);
}
/**
* Scope to search reports.
*/
public function scopeSearch(Builder $query, string $term): Builder
{
return $query->where(function (Builder $q) use ($term) {
$q->where('title', 'ILIKE', "%{$term}%")
->orWhere('description', 'ILIKE', "%{$term}%");
});
}
/**
* Check if report is completed.
*/
public function isCompleted(): bool
{
return $this->status === 'completed';
}
/**
* Check if report is pending.
*/
public function isPending(): bool
{
return $this->status === 'pending';
}
/**
* Check if report has failed.
*/
public function hasFailed(): bool
{
return $this->status === 'failed';
}
/**
* Mark report as completed.
*/
public function markCompleted(?string $filePath = null): bool
{
$this->status = 'completed';
$this->completed_at = now();
if ($filePath) {
$this->file_path = $filePath;
}
return $this->save();
}
/**
* Mark report as failed.
*/
public function markFailed(): bool
{
$this->status = 'failed';
$this->completed_at = now();
return $this->save();
}
/**
* Get file download URL.
*/
public function getDownloadUrlAttribute(): ?string
{
if (!$this->file_path) {
return null;
}
return "/api/v1/reports/{$this->report_id}/download";
}
/**
* Get file size if exists.
*/
public function getFileSizeAttribute(): ?int
{
if (!$this->file_path || !file_exists($this->file_path)) {
return null;
}
return filesize($this->file_path);
}
/**
* Get formatted file size.
*/
public function getFormattedFileSizeAttribute(): ?string
{
$size = $this->file_size;
if (!$size) {
return null;
}
$units = ['B', 'KB', 'MB', 'GB'];
$power = floor(log($size, 1024));
return round($size / pow(1024, $power), 2) . ' ' . $units[$power];
}
}