Files
sam-api/app/Models/Equipment/Equipment.php
김보곤 069d0206a0 feat: [equipment] 설비관리 API 백엔드 구현 (Phase 1)
- 모델 6개: Equipment, EquipmentInspection, EquipmentInspectionDetail, EquipmentInspectionTemplate, EquipmentRepair, EquipmentProcess
- InspectionCycle Enum: 6주기(일/주/월/격월/분기/반기) 날짜 해석
- 서비스 4개: EquipmentService, EquipmentInspectionService, EquipmentRepairService, EquipmentPhotoService
- 컨트롤러 4개: CRUD + 점검 토글/결과 설정/메모/초기화 + 템플릿 관리 + 수리이력 + 사진
- FormRequest 6개: 설비등록/수정, 수리이력, 점검템플릿, 토글, 메모
- 라우트 26개: equipment prefix 하위 RESTful 엔드포인트
- i18n 메시지: message.equipment.*, error.equipment.*
- 마이그레이션: equipments/equipment_repairs options JSON 컬럼 추가
2026-03-12 10:52:30 +09:00

155 lines
3.8 KiB
PHP

<?php
namespace App\Models\Equipment;
use App\Models\Boards\File;
use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Equipment extends Model
{
use Auditable, BelongsToTenant, ModelTrait, SoftDeletes;
protected $table = 'equipments';
protected $fillable = [
'tenant_id',
'equipment_code',
'name',
'equipment_type',
'specification',
'manufacturer',
'model_name',
'serial_no',
'location',
'production_line',
'purchase_date',
'install_date',
'purchase_price',
'useful_life',
'status',
'disposed_date',
'manager_id',
'sub_manager_id',
'photo_path',
'memo',
'options',
'is_active',
'sort_order',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'purchase_date' => 'date',
'install_date' => 'date',
'disposed_date' => 'date',
'purchase_price' => 'decimal:2',
'is_active' => 'boolean',
'options' => 'array',
];
public function getOption(string $key, mixed $default = null): mixed
{
return data_get($this->options, $key, $default);
}
public function setOption(string $key, mixed $value): self
{
$options = $this->options ?? [];
data_set($options, $key, $value);
$this->options = $options;
return $this;
}
public function manager(): BelongsTo
{
return $this->belongsTo(\App\Models\User::class, 'manager_id');
}
public function subManager(): BelongsTo
{
return $this->belongsTo(\App\Models\User::class, 'sub_manager_id');
}
public function canInspect(?int $userId = null): bool
{
if (! $userId) {
return false;
}
return $this->manager_id === $userId || $this->sub_manager_id === $userId;
}
public function inspectionTemplates(): HasMany
{
return $this->hasMany(EquipmentInspectionTemplate::class, 'equipment_id')->orderBy('sort_order');
}
public function inspections(): HasMany
{
return $this->hasMany(EquipmentInspection::class, 'equipment_id');
}
public function repairs(): HasMany
{
return $this->hasMany(EquipmentRepair::class, 'equipment_id');
}
public function photos(): HasMany
{
return $this->hasMany(File::class, 'document_id')
->where('document_type', 'equipment')
->orderBy('id');
}
public function processes(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Process::class, 'equipment_process')
->withPivot('is_primary')
->withTimestamps();
}
public function scopeByLine($query, string $line)
{
return $query->where('production_line', $line);
}
public function scopeByType($query, string $type)
{
return $query->where('equipment_type', $type);
}
public function scopeByStatus($query, string $status)
{
return $query->where('status', $status);
}
public static function getEquipmentTypes(): array
{
return ['포밍기', '미싱기', '샤링기', 'V컷팅기', '절곡기', '프레스', '드릴', '기타'];
}
public static function getProductionLines(): array
{
return ['스라트', '스크린', '절곡', '기타'];
}
public static function getStatuses(): array
{
return [
'active' => '가동',
'idle' => '유휴',
'disposed' => '폐기',
];
}
}