store-manage/app/Http/Controllers/Api/Account/TaskPerformanceController.php

51 lines
2.0 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Account;
use App\Http\Controllers\Api\Controller;
use App\Http\Resources\TaskPerformanceResource;
use App\Models\TaskPerformance;
use Illuminate\Http\Request;
class TaskPerformanceController extends Controller
{
public function index(Request $request)
{
/** @var \App\Models\Employee */
$user = $request->user();
/** @var \Illuminate\Database\Eloquent\Collection */
$taskPerformances = TaskPerformance::with(['task'])
->where('store_id', $user->store_id)
->when($request->input('filter'), function ($query, $filter) {
$now = now();
switch ($filter) {
case 'future':
$query->whereHas('task', fn ($query) => $query->where('end_at', '>', $now));
break;
case 'history':
$query->whereHas('task', fn ($query) => $query->where('end_at', '<=', $now));
break;
}
})
->get();
return TaskPerformanceResource::collection($taskPerformances);
// return $taskPerformances->groupBy(fn (TaskPerformance $taskable) => Carbon::createFromFormat('Y-m', $taskable->month)->year)
// ->map(function (Collection $collection) {
// return $collection->mapWithKeys(function (TaskPerformance $taskable) {
// return [
// Carbon::createFromFormat('Y-m', $taskable->month)->month => [
// 'id' => $taskable->id,
// 'month' => $taskable->month,
// 'actual_performance' => trim_zeros($taskable->actual_performance),
// 'expected_performance' => trim_zeros($taskable->expected_performance),
// 'status' => $taskable->task_status,
// ],
// ];
// })->sortKeysDesc();
// });
}
}