2025-08-21 18:38:27 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models\Tenants;
|
|
|
|
|
|
|
|
|
|
use App\Models\Members\User;
|
2025-11-06 17:45:49 +09:00
|
|
|
use App\Models\Permissions\PermissionOverride;
|
|
|
|
|
use App\Models\Tenants\Pivots\DepartmentUser;
|
2026-01-29 15:33:54 +09:00
|
|
|
use App\Traits\Auditable;
|
2025-12-26 15:00:57 +09:00
|
|
|
use App\Traits\BelongsToTenant;
|
2025-11-06 17:45:49 +09:00
|
|
|
use App\Traits\ModelTrait;
|
2025-08-21 18:38:27 +09:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2025-12-26 15:00:57 +09:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2025-08-21 18:38:27 +09:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
2025-12-25 03:48:32 +09:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2025-08-21 18:38:27 +09:00
|
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
|
|
|
|
|
|
class Department extends Model
|
|
|
|
|
{
|
2026-01-29 15:33:54 +09:00
|
|
|
use Auditable, BelongsToTenant, HasRoles, ModelTrait, SoftDeletes; // 부서도 권한/역할을 가짐
|
2025-08-21 18:38:27 +09:00
|
|
|
|
|
|
|
|
protected $table = 'departments';
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-08-21 18:38:27 +09:00
|
|
|
protected $guarded = ['id'];
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-08-21 18:38:27 +09:00
|
|
|
protected $casts = [
|
|
|
|
|
'tenant_id' => 'int',
|
|
|
|
|
'parent_id' => 'int',
|
|
|
|
|
'is_active' => 'bool',
|
2025-11-06 17:45:49 +09:00
|
|
|
'sort_order' => 'int',
|
2025-08-21 18:38:27 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $hidden = [
|
2025-11-06 17:45:49 +09:00
|
|
|
'deleted_by', 'deleted_at',
|
2025-08-21 18:38:27 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 스파티 가드명(프로젝트 설정에 맞게 조정)
|
|
|
|
|
protected string $guard_name = 'web';
|
|
|
|
|
|
|
|
|
|
/** 상위/하위 부서 */
|
|
|
|
|
public function parent(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(self::class, 'parent_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function children()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(self::class, 'parent_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 부서-사용자 N:N (추가 컬럼 포함 Pivot) */
|
|
|
|
|
public function users()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(User::class, 'department_user')
|
|
|
|
|
->using(DepartmentUser::class)
|
2025-12-26 15:00:57 +09:00
|
|
|
->wherePivotNull('deleted_at')
|
2025-08-21 18:38:27 +09:00
|
|
|
->withTimestamps()
|
2025-11-06 17:45:49 +09:00
|
|
|
->withPivot(['tenant_id', 'is_primary', 'joined_at', 'left_at']);
|
2025-08-21 18:38:27 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 부서의 권한 오버라이드(DENY/임시허용) */
|
|
|
|
|
public function permissionOverrides(): MorphMany
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(PermissionOverride::class, 'model');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 부서-사용자 매핑 로우들(피벗 테이블의 레코드들) */
|
|
|
|
|
public function departmentUsers()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(DepartmentUser::class, 'department_id');
|
|
|
|
|
}
|
2025-12-26 15:00:57 +09:00
|
|
|
|
|
|
|
|
/** 부서 소속 사원 (tenant_user_profiles 기반) */
|
|
|
|
|
public function employees(): HasMany
|
|
|
|
|
{
|
|
|
|
|
// Department가 이미 BelongsToTenant로 필터링되므로
|
|
|
|
|
// department_id 연결만으로 테넌트 격리됨
|
|
|
|
|
return $this->hasMany(TenantUserProfile::class, 'department_id')
|
|
|
|
|
->where('employee_status', 'active');
|
|
|
|
|
}
|
2025-08-21 18:38:27 +09:00
|
|
|
}
|