Skip to main content

Overview

The Reporte model represents issue reports filed by users regarding assets in the system. Reports can trigger corrective maintenance operations and track the lifecycle from issue identification to resolution.

Model Information

table
string
default:"reportes"
Database table name
namespace
string
App\Models\Reporte

Fillable Fields

The following fields can be mass-assigned:
usuario_id
integer
required
Foreign key reference to the users table. The user who filed this report.
activo_id
integer
required
Foreign key reference to the activos table. The asset that this report is about.
descripcion
string
required
Detailed description of the issue or problem being reported.
prioridad
string
default:"media"
Priority level of the report. Uses string values from NivelPrioridad enum.Possible values:
  • alta - High priority (urgent)
  • media - Medium priority (normal)
  • baja - Low priority (non-urgent)
estado
string
default:"abierto"
Current status of the report. Uses string values from EstadoReporte enum.Possible values:
  • abierto - Newly opened report
  • asignado - Assigned to technician
  • en_progreso - Work in progress
  • resuelto - Issue resolved
  • cerrado - Report closed

Casts

The model automatically casts the following attributes:
protected $casts = [
    'prioridad' => 'string',
    'estado' => 'string',
];

Relationships

Belongs To

usuario

Returns the user who created this report.
$reporte = Reporte::find(1);
$usuario = $reporte->usuario;

echo $usuario->name;
echo $usuario->email;
Relationship Details:
  • Type: BelongsTo
  • Related Model: App\Models\User
  • Foreign Key: usuario_id

activo

Returns the asset that this report is about.
$reporte = Reporte::find(1);
$activo = $reporte->activo;

echo $activo->articulo->modelo;
echo $activo->ubicacion->nombre;
Relationship Details:
  • Type: BelongsTo
  • Related Model: App\Models\Activo
  • Foreign Key: activo_id

Has Many

mantenimientos

Returns all maintenance operations generated from this report. A report can trigger multiple maintenance activities.
$reporte = Reporte::find(1);
$mantenimientos = $reporte->mantenimientos;

foreach ($mantenimientos as $mantenimiento) {
    echo $mantenimiento->tipo;
    echo $mantenimiento->estado;
    echo $mantenimiento->tecnicoPrincipal->name;
}
Relationship Details:
  • Type: HasMany
  • Related Model: App\Models\Mantenimiento
  • Foreign Key: reporte_id

Usage Examples

Creating a New Report

use App\Models\Reporte;

$reporte = Reporte::create([
    'usuario_id' => 5,
    'activo_id' => 12,
    'descripcion' => 'El equipo hace ruido extraño y no enciende correctamente',
    'prioridad' => 'alta',
    'estado' => 'abierto',
]);

Updating Report Status

$reporte = Reporte::find(1);
$reporte->estado = 'asignado';
$reporte->save();

Escalating Report Priority

$reporte = Reporte::find(1);
$reporte->prioridad = 'alta';
$reporte->save();

Querying Reports

// Get all open reports
$abiertos = Reporte::where('estado', 'abierto')
    ->orderBy('created_at', 'desc')
    ->get();

// Get high priority reports
$urgentes = Reporte::where('prioridad', 'alta')
    ->whereIn('estado', ['abierto', 'asignado'])
    ->get();

// Get reports for a specific asset
$reportesActivo = Reporte::where('activo_id', 1)
    ->with('usuario')
    ->orderBy('created_at', 'desc')
    ->get();

// Get reports with related data
$reporte = Reporte::with([
    'usuario',
    'activo.articulo',
    'activo.ubicacion',
    'mantenimientos.tecnicoPrincipal'
])->find(1);

Getting Reports by User

$usuario = User::find(5);
$reportes = $usuario->reportes;

foreach ($reportes as $reporte) {
    echo "{$reporte->activo->articulo->modelo} - {$reporte->estado}";
}

Checking if Report Has Maintenance

$reporte = Reporte::find(1);

if ($reporte->mantenimientos()->exists()) {
    echo "Este reporte tiene mantenimientos asociados";
    
    $ultimoMantenimiento = $reporte->mantenimientos()
        ->orderBy('created_at', 'desc')
        ->first();
    
    echo "Estado del último mantenimiento: {$ultimoMantenimiento->estado}";
} else {
    echo "Este reporte aún no tiene mantenimientos asignados";
}

Creating Maintenance from Report

use App\Models\Mantenimiento;
use Carbon\Carbon;

$reporte = Reporte::find(1);

// Update report status
$reporte->estado = 'asignado';
$reporte->save();

// Create corrective maintenance
$mantenimiento = Mantenimiento::create([
    'activo_id' => $reporte->activo_id,
    'supervisor_id' => 2,
    'tecnico_principal_id' => 3,
    'tipo' => 'correctivo',
    'reporte_id' => $reporte->id,
    'fecha_apertura' => Carbon::now(),
    'fecha_cierre' => Carbon::now()->addDays(2),
    'estado' => 'pendiente',
    'descripcion' => $reporte->descripcion,
    'validado' => false,
    'costo_total' => 0,
]);

Getting Report Statistics

// Count reports by status
$estadisticas = [
    'abiertos' => Reporte::where('estado', 'abierto')->count(),
    'en_progreso' => Reporte::where('estado', 'en_progreso')->count(),
    'resueltos' => Reporte::where('estado', 'resuelto')->count(),
    'cerrados' => Reporte::where('estado', 'cerrado')->count(),
];

// Count reports by priority
$porPrioridad = [
    'alta' => Reporte::where('prioridad', 'alta')->count(),
    'media' => Reporte::where('prioridad', 'media')->count(),
    'baja' => Reporte::where('prioridad', 'baja')->count(),
];

Closing a Report

$reporte = Reporte::find(1);

// Verify all maintenance is completed
$mantenimientosPendientes = $reporte->mantenimientos()
    ->whereNotIn('estado', ['completado', 'cancelado'])
    ->count();

if ($mantenimientosPendientes === 0) {
    $reporte->estado = 'cerrado';
    $reporte->save();
    echo "Reporte cerrado exitosamente";
} else {
    echo "No se puede cerrar: hay {$mantenimientosPendientes} mantenimientos pendientes";
}

Database Schema

CREATE TABLE reportes (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    usuario_id BIGINT UNSIGNED NOT NULL,
    activo_id BIGINT UNSIGNED NOT NULL,
    descripcion VARCHAR(255) NOT NULL,
    prioridad VARCHAR(255) DEFAULT 'media',
    estado VARCHAR(255) DEFAULT 'abierto',
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL,
    
    FOREIGN KEY (usuario_id) REFERENCES users(id),
    FOREIGN KEY (activo_id) REFERENCES activos(id)
);
  • Activo - Assets that reports are about
  • Mantenimiento - Maintenance triggered by reports
  • User - Users who create reports

Build docs developers (and LLMs) love