- 근무 설정 API (GET/PUT /settings/work) - 근무유형, 소정근로시간, 연장근로시간, 근무요일, 출퇴근시간, 휴게시간 - 출퇴근 설정 API (GET/PUT /settings/attendance) - GPS 출퇴근, 허용 반경, 본사 위치 설정 - 현장 관리 API (CRUD /sites) - 현장 등록/수정/삭제, 활성화된 현장 목록(셀렉트박스용) - GPS 좌표 기반 위치 관리 마이그레이션: work_settings, attendance_settings, sites 테이블 모델: WorkSetting, AttendanceSetting, Site (BelongsToTenant, SoftDeletes) 서비스: WorkSettingService, SiteService Swagger 문서 및 i18n 메시지 키 추가
146 lines
3.8 KiB
PHP
146 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Tenants\Site;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class SiteService extends Service
|
|
{
|
|
/**
|
|
* 현장 목록 조회
|
|
*/
|
|
public function index(array $params): LengthAwarePaginator
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
|
|
$query = Site::query()
|
|
->where('tenant_id', $tenantId);
|
|
|
|
// 검색 필터
|
|
if (! empty($params['search'])) {
|
|
$search = $params['search'];
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%")
|
|
->orWhere('address', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
// 활성화 상태 필터
|
|
if (isset($params['is_active'])) {
|
|
$query->where('is_active', $params['is_active']);
|
|
}
|
|
|
|
// 정렬
|
|
$sortBy = $params['sort_by'] ?? 'created_at';
|
|
$sortDir = $params['sort_dir'] ?? 'desc';
|
|
$query->orderBy($sortBy, $sortDir);
|
|
|
|
// 페이지네이션
|
|
$perPage = $params['per_page'] ?? 20;
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* 현장 상세 조회
|
|
*/
|
|
public function show(int $id): Site
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
|
|
return Site::query()
|
|
->where('tenant_id', $tenantId)
|
|
->findOrFail($id);
|
|
}
|
|
|
|
/**
|
|
* 현장 등록
|
|
*/
|
|
public function store(array $data): Site
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
$userId = $this->apiUserId();
|
|
|
|
return DB::transaction(function () use ($data, $tenantId, $userId) {
|
|
$site = Site::create([
|
|
'tenant_id' => $tenantId,
|
|
'name' => $data['name'],
|
|
'address' => $data['address'] ?? null,
|
|
'latitude' => $data['latitude'] ?? null,
|
|
'longitude' => $data['longitude'] ?? null,
|
|
'is_active' => $data['is_active'] ?? true,
|
|
'created_by' => $userId,
|
|
'updated_by' => $userId,
|
|
]);
|
|
|
|
return $site;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 현장 수정
|
|
*/
|
|
public function update(int $id, array $data): Site
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
$userId = $this->apiUserId();
|
|
|
|
return DB::transaction(function () use ($id, $data, $tenantId, $userId) {
|
|
$site = Site::query()
|
|
->where('tenant_id', $tenantId)
|
|
->findOrFail($id);
|
|
|
|
$site->fill([
|
|
'name' => $data['name'] ?? $site->name,
|
|
'address' => $data['address'] ?? $site->address,
|
|
'latitude' => $data['latitude'] ?? $site->latitude,
|
|
'longitude' => $data['longitude'] ?? $site->longitude,
|
|
'is_active' => $data['is_active'] ?? $site->is_active,
|
|
'updated_by' => $userId,
|
|
]);
|
|
|
|
$site->save();
|
|
|
|
return $site->fresh();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 현장 삭제
|
|
*/
|
|
public function destroy(int $id): bool
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
$userId = $this->apiUserId();
|
|
|
|
return DB::transaction(function () use ($id, $tenantId, $userId) {
|
|
$site = Site::query()
|
|
->where('tenant_id', $tenantId)
|
|
->findOrFail($id);
|
|
|
|
$site->deleted_by = $userId;
|
|
$site->save();
|
|
$site->delete();
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 활성화된 현장 목록 조회 (셀렉트박스용)
|
|
*/
|
|
public function getActiveSites(): array
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
|
|
return Site::query()
|
|
->where('tenant_id', $tenantId)
|
|
->where('is_active', true)
|
|
->orderBy('name')
|
|
->get(['id', 'name', 'address'])
|
|
->toArray();
|
|
}
|
|
}
|