| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBoards\App\Services\BoardService; |
| 6 |
use FluentBoards\App\Services\PermissionManager; |
| 7 |
use FluentBoards\Framework\Http\Request\Request; |
| 8 |
use FluentBoardsPro\App\Modules\TimeTracking\Model\TimeTrack; |
| 9 |
class ReportController extends Controller |
| 10 |
{ |
| 11 |
public function getTimeSheetReport(Request $request) |
| 12 |
{ |
| 13 |
// Sanitize date inputs - validate they are valid date strings |
| 14 |
$startDate = $request->getSafe('start_date', 'sanitize_text_field'); |
| 15 |
$endDate = $request->getSafe('end_date', 'sanitize_text_field'); |
| 16 |
|
| 17 |
// Validate date format (YYYY-MM-DD) and ensure dates are actually valid |
| 18 |
if ($startDate) { |
| 19 |
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $startDate)) { |
| 20 |
$startDate = null; |
| 21 |
} else { |
| 22 |
// Validate that the date is actually valid (e.g., not "2024-13-45") |
| 23 |
$dateParts = explode('-', $startDate); |
| 24 |
if (count($dateParts) !== 3 || !checkdate((int)$dateParts[1], (int)$dateParts[2], (int)$dateParts[0])) { |
| 25 |
$startDate = null; |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
if ($endDate) { |
| 30 |
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $endDate)) { |
| 31 |
$endDate = null; |
| 32 |
} else { |
| 33 |
// Validate that the date is actually valid (e.g., not "2024-13-45") |
| 34 |
$dateParts = explode('-', $endDate); |
| 35 |
if (count($dateParts) !== 3 || !checkdate((int)$dateParts[1], (int)$dateParts[2], (int)$dateParts[0])) { |
| 36 |
$endDate = null; |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
$authUser = wp_get_current_user(); |
| 42 |
$boardIds = PermissionManager::getBoardIdsForUser($authUser->ID); |
| 43 |
$boardId = $request->getSafe('board_id', 'intval'); |
| 44 |
if (!empty($boardId)) { |
| 45 |
$boardIds = PermissionManager::getBoardIdsForUser($authUser->ID, $boardId); |
| 46 |
} |
| 47 |
$timings = TimeTrack::where('status', 'commited')->whereIn('board_id', $boardIds); |
| 48 |
if ($startDate && $endDate) { |
| 49 |
$timings = $timings->whereBetween('completed_at', [$startDate, $endDate]); |
| 50 |
} |
| 51 |
$timings = $timings->get(); |
| 52 |
$timings = $timings->load('task', 'user', 'board'); |
| 53 |
|
| 54 |
$sortTasks = []; |
| 55 |
foreach ($timings as $timing) { |
| 56 |
$times = $timings->where('task_id', $timing->task_id); |
| 57 |
|
| 58 |
if (!isset($sortTasks[$timing['task_id']])) { |
| 59 |
$sortTasks[$timing['task_id']] = [ |
| 60 |
'id' => $timing['task_id'], |
| 61 |
'title' => $timing->task->title, |
| 62 |
'board' => $timing->board, |
| 63 |
'total' => 0, |
| 64 |
'times' => [] |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
$sortTasks[$timing['task_id']]['total'] += $timing['billable_minutes']; |
| 69 |
|
| 70 |
$formattedTimes = []; |
| 71 |
foreach ($times as $time) { |
| 72 |
$user = $time->user; |
| 73 |
|
| 74 |
$formattedTimes[] = [ |
| 75 |
'id' => $time['id'], |
| 76 |
'billable_minutes' => $time['billable_minutes'], |
| 77 |
'working_minutes' => $time['working_minutes'], |
| 78 |
'completed_at' => $time['completed_at'], |
| 79 |
'message' => $time['message'], |
| 80 |
'user' => [ |
| 81 |
'ID' => $user->ID, |
| 82 |
'name' => $user->display_name, |
| 83 |
'avatar' => fluent_boards_user_avatar($user->user_email), |
| 84 |
'email' => $user->user_email |
| 85 |
] |
| 86 |
]; |
| 87 |
|
| 88 |
} |
| 89 |
$sortTasks[$timing['task_id']]['times'] = $formattedTimes; |
| 90 |
} |
| 91 |
|
| 92 |
$sortTasks = array_values($sortTasks); |
| 93 |
|
| 94 |
return $this->sendSuccess([ |
| 95 |
'message' => 'Time sheet report', |
| 96 |
'timings' => $sortTasks |
| 97 |
], 200); |
| 98 |
} |
| 99 |
|
| 100 |
public function getBoardReports(Request $request) |
| 101 |
{ |
| 102 |
try { |
| 103 |
$boardService = new BoardService(); |
| 104 |
$boardId = $request->getSafe('board_id', 'intval'); |
| 105 |
if (!empty($boardId)) |
| 106 |
{ |
| 107 |
$boardReport = $boardService->getBoardReports($boardId); |
| 108 |
} else { |
| 109 |
$boardReport = $boardService->getAllBoardReports(); |
| 110 |
} |
| 111 |
return $this->sendSuccess([ |
| 112 |
'report' => $boardReport, |
| 113 |
], 200); |
| 114 |
} catch (\Exception $e) { |
| 115 |
return $this->sendError($e->getMessage(), 400); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public function getStageWiseBoardReports($board_id) |
| 120 |
{ |
| 121 |
$board_id = absint($board_id); |
| 122 |
try { |
| 123 |
$boardService = new BoardService(); |
| 124 |
$stages = $boardService->getStageWiseBoardReports($board_id); |
| 125 |
return $this->sendSuccess([ |
| 126 |
'stages' => $stages, |
| 127 |
], 200); |
| 128 |
} catch (\Exception $e) { |
| 129 |
return $this->sendError($e->getMessage(), 400); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
} |