- feat: [barobill] 바로빌 카드/은행/홈택스 REST API 구현 - feat: [equipment] 설비관리 API 백엔드 구현 - feat: [payroll] 급여관리 계산 엔진 및 일괄 처리 API - feat: [QMS] 점검표 템플릿 관리 + 로트심사 개선 - feat: [생산/출하] 수주 단위 출하 자동생성 + 상태 흐름 개선 - feat: [receiving] 입고 성적서 파일 연결 - feat: [견적] 제어기 타입 체계 변경 - feat: [email] 테넌트 메일 설정 마이그레이션 및 모델 - feat: [pmis] 시공관리 테이블 마이그레이션 - feat: [R2] 파일 업로드 커맨드 + filesystems 설정 - feat: [배포] Jenkinsfile 롤백 기능 추가 - fix: [approval] SAM API 규칙 준수 코드 개선 - fix: [account-codes] 계정과목 중복 데이터 정리 - fix: [payroll] 일괄 생성 시 삭제된 사용자 건너뛰기 - fix: [db] codebridge DB 분리 후 깨진 FK 제약조건 제거 - refactor: [barobill] 바로빌 연동 코드 전면 개선
154 lines
4.5 KiB
PHP
154 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Equipment;
|
|
|
|
use App\Models\Equipment\Equipment;
|
|
use App\Services\Service;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class EquipmentService extends Service
|
|
{
|
|
public function index(array $filters = []): LengthAwarePaginator
|
|
{
|
|
$query = Equipment::query()->with(['manager', 'subManager']);
|
|
|
|
if (! empty($filters['search'])) {
|
|
$search = $filters['search'];
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('equipment_code', 'like', "%{$search}%")
|
|
->orWhere('name', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
if (! empty($filters['status'])) {
|
|
$query->byStatus($filters['status']);
|
|
}
|
|
|
|
if (! empty($filters['production_line'])) {
|
|
$query->byLine($filters['production_line']);
|
|
}
|
|
|
|
if (! empty($filters['equipment_type'])) {
|
|
$query->byType($filters['equipment_type']);
|
|
}
|
|
|
|
$sortBy = $filters['sort_by'] ?? 'sort_order';
|
|
$sortDir = $filters['sort_direction'] ?? 'asc';
|
|
$query->orderBy($sortBy, $sortDir);
|
|
|
|
return $query->paginate($filters['per_page'] ?? 20);
|
|
}
|
|
|
|
public function show(int $id): Equipment
|
|
{
|
|
$equipment = Equipment::with(['manager', 'subManager', 'inspectionTemplates', 'repairs', 'processes', 'photos'])->find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
return $equipment;
|
|
}
|
|
|
|
public function store(array $data): Equipment
|
|
{
|
|
return DB::transaction(function () use ($data) {
|
|
$data['tenant_id'] = $this->tenantId();
|
|
|
|
return Equipment::create($data);
|
|
});
|
|
}
|
|
|
|
public function update(int $id, array $data): Equipment
|
|
{
|
|
return DB::transaction(function () use ($id, $data) {
|
|
$equipment = Equipment::find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
$equipment->update($data);
|
|
|
|
return $equipment->fresh();
|
|
});
|
|
}
|
|
|
|
public function destroy(int $id): bool
|
|
{
|
|
return DB::transaction(function () use ($id) {
|
|
$equipment = Equipment::find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
return $equipment->delete();
|
|
});
|
|
}
|
|
|
|
public function restore(int $id): Equipment
|
|
{
|
|
return DB::transaction(function () use ($id) {
|
|
$equipment = Equipment::onlyTrashed()->find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
$equipment->restore();
|
|
|
|
return $equipment->fresh();
|
|
});
|
|
}
|
|
|
|
public function toggleActive(int $id): Equipment
|
|
{
|
|
return DB::transaction(function () use ($id) {
|
|
$equipment = Equipment::find($id);
|
|
|
|
if (! $equipment) {
|
|
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
|
}
|
|
|
|
$equipment->update(['is_active' => ! $equipment->is_active]);
|
|
|
|
return $equipment->fresh();
|
|
});
|
|
}
|
|
|
|
public function stats(): array
|
|
{
|
|
$total = Equipment::count();
|
|
$active = Equipment::where('status', 'active')->count();
|
|
$idle = Equipment::where('status', 'idle')->count();
|
|
$disposed = Equipment::where('status', 'disposed')->count();
|
|
|
|
return compact('total', 'active', 'idle', 'disposed');
|
|
}
|
|
|
|
public function options(): array
|
|
{
|
|
return [
|
|
'equipment_types' => Equipment::getEquipmentTypes(),
|
|
'production_lines' => Equipment::getProductionLines(),
|
|
'statuses' => Equipment::getStatuses(),
|
|
'equipment_list' => Equipment::active()
|
|
->orderBy('sort_order')
|
|
->orderBy('name')
|
|
->get(['id', 'equipment_code', 'name', 'equipment_type', 'production_line']),
|
|
];
|
|
}
|
|
|
|
public function typeStats(): array
|
|
{
|
|
return Equipment::where('status', '!=', 'disposed')
|
|
->selectRaw('equipment_type, count(*) as count')
|
|
->groupBy('equipment_type')
|
|
->pluck('count', 'equipment_type')
|
|
->toArray();
|
|
}
|
|
}
|