- 법인차량 CRUD (CorporateVehicle) - 차량 운행일지 CRUD (VehicleLog) - 차량 정비이력 CRUD (VehicleMaintenance) - 모델, 서비스, 컨트롤러, 라우트 구성 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
130 lines
4.2 KiB
PHP
130 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Vehicle;
|
|
|
|
use App\Models\Tenants\VehicleLog;
|
|
use App\Services\Service;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class VehicleLogService extends Service
|
|
{
|
|
public function index(array $filters = []): LengthAwarePaginator
|
|
{
|
|
$query = VehicleLog::query()->with(['vehicle:id,plate_number,model']);
|
|
|
|
if (! empty($filters['search'])) {
|
|
$search = $filters['search'];
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('driver_name', 'like', "%{$search}%")
|
|
->orWhere('departure_name', 'like', "%{$search}%")
|
|
->orWhere('arrival_name', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
if (! empty($filters['vehicle_id'])) {
|
|
$query->where('vehicle_id', $filters['vehicle_id']);
|
|
}
|
|
|
|
if (! empty($filters['year']) && ! empty($filters['month'])) {
|
|
$query->whereYear('log_date', $filters['year'])
|
|
->whereMonth('log_date', $filters['month']);
|
|
}
|
|
|
|
if (! empty($filters['trip_type'])) {
|
|
$query->where('trip_type', $filters['trip_type']);
|
|
}
|
|
|
|
$query->orderByDesc('log_date')->orderByDesc('id');
|
|
|
|
return $query->paginate($filters['per_page'] ?? 20);
|
|
}
|
|
|
|
public function show(int $id): VehicleLog
|
|
{
|
|
$log = VehicleLog::with('vehicle:id,plate_number,model')->find($id);
|
|
|
|
if (! $log) {
|
|
throw new NotFoundHttpException('운행기록을 찾을 수 없습니다.');
|
|
}
|
|
|
|
return $log;
|
|
}
|
|
|
|
public function store(array $data): VehicleLog
|
|
{
|
|
return DB::transaction(function () use ($data) {
|
|
$data['tenant_id'] = $this->tenantId();
|
|
|
|
return VehicleLog::create($data);
|
|
});
|
|
}
|
|
|
|
public function update(int $id, array $data): VehicleLog
|
|
{
|
|
return DB::transaction(function () use ($id, $data) {
|
|
$log = VehicleLog::find($id);
|
|
|
|
if (! $log) {
|
|
throw new NotFoundHttpException('운행기록을 찾을 수 없습니다.');
|
|
}
|
|
|
|
$log->update($data);
|
|
|
|
return $log->fresh(['vehicle:id,plate_number,model']);
|
|
});
|
|
}
|
|
|
|
public function destroy(int $id): bool
|
|
{
|
|
return DB::transaction(function () use ($id) {
|
|
$log = VehicleLog::find($id);
|
|
|
|
if (! $log) {
|
|
throw new NotFoundHttpException('운행기록을 찾을 수 없습니다.');
|
|
}
|
|
|
|
return $log->delete();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 월별 통계
|
|
*/
|
|
public function summary(array $filters = []): array
|
|
{
|
|
$query = VehicleLog::query();
|
|
|
|
if (! empty($filters['vehicle_id'])) {
|
|
$query->where('vehicle_id', $filters['vehicle_id']);
|
|
}
|
|
|
|
if (! empty($filters['year']) && ! empty($filters['month'])) {
|
|
$query->whereYear('log_date', $filters['year'])
|
|
->whereMonth('log_date', $filters['month']);
|
|
}
|
|
|
|
$totalDistance = (clone $query)->sum('distance_km');
|
|
$totalCount = (clone $query)->count();
|
|
|
|
$commuteToQuery = (clone $query)->whereIn('trip_type', ['commute_to', 'commute_round']);
|
|
$commuteFromQuery = (clone $query)->whereIn('trip_type', ['commute_from', 'commute_round']);
|
|
$businessQuery = (clone $query)->whereIn('trip_type', ['business', 'business_round']);
|
|
$personalQuery = (clone $query)->whereIn('trip_type', ['personal', 'personal_round']);
|
|
|
|
return [
|
|
'total_distance' => (int) $totalDistance,
|
|
'total_count' => $totalCount,
|
|
'commute_to_distance' => (int) $commuteToQuery->sum('distance_km'),
|
|
'commute_to_count' => $commuteToQuery->count(),
|
|
'commute_from_distance' => (int) $commuteFromQuery->sum('distance_km'),
|
|
'commute_from_count' => $commuteFromQuery->count(),
|
|
'business_distance' => (int) $businessQuery->sum('distance_km'),
|
|
'business_count' => $businessQuery->count(),
|
|
'personal_distance' => (int) $personalQuery->sum('distance_km'),
|
|
'personal_count' => $personalQuery->count(),
|
|
];
|
|
}
|
|
}
|