254 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			254 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
<?php
 | 
						|
 | 
						|
namespace Modules\Admin\app\Http\Controllers;
 | 
						|
 | 
						|
use App\Http\Controllers\Controller;
 | 
						|
use App\Models\User;
 | 
						|
use App\Services\JiraService;
 | 
						|
use Carbon\Carbon;
 | 
						|
use Modules\Admin\app\Models\TechnicalUser;
 | 
						|
use Illuminate\Http\Request;
 | 
						|
use Modules\Admin\app\Models\UserCriteria;
 | 
						|
use PhpOffice\PhpWord\IOFactory;
 | 
						|
use PhpOffice\PhpWord\PhpWord;
 | 
						|
 | 
						|
class EvaluationController extends Controller
 | 
						|
{
 | 
						|
    protected $jiraService;
 | 
						|
 | 
						|
    public function __construct(JiraService $jiraService)
 | 
						|
    {
 | 
						|
        $this->jiraService = $jiraService;
 | 
						|
    }
 | 
						|
 | 
						|
    public function sprintReview(Request $request)
 | 
						|
    {
 | 
						|
        $request->validate([
 | 
						|
            'userID' => 'required|exists:users,id',
 | 
						|
            'fromDate' => 'nullable|date',
 | 
						|
            'toDate' => 'nullable|date',
 | 
						|
        ]);
 | 
						|
 | 
						|
        $userEmail = User::findOrFail($request->input('userID'))->email;
 | 
						|
 | 
						|
        $projects = $this->jiraService->getAllProjects();
 | 
						|
 | 
						|
        $query = UserCriteria::where('user_email', $userEmail);
 | 
						|
 | 
						|
        // Date filter
 | 
						|
        if ($request->filled('fromDate')) {
 | 
						|
            $fromDate = Carbon::parse($request->input('fromDate'));
 | 
						|
            $query->where('created_at', '>=', $fromDate);
 | 
						|
        }
 | 
						|
 | 
						|
        if ($request->filled('toDate')) {
 | 
						|
            $toDate = Carbon::parse($request->input('toDate'));
 | 
						|
            $query->where('created_at', '<=', $toDate);
 | 
						|
        }
 | 
						|
 | 
						|
        // End query
 | 
						|
        $userCriterias = $query->with(['sprint', 'criteria'])->get();
 | 
						|
 | 
						|
        $projectsData = $userCriterias->groupBy('sprint.project_id')->map(function ($userCriteriasByProject, $projectId) use ($projects) {
 | 
						|
            $result = self::getProjectById($projects, $projectId);
 | 
						|
            return [
 | 
						|
                'name' => $result['name'],
 | 
						|
                'sprints' => $userCriteriasByProject->groupBy('sprint.id')->map(function ($userCriteriasBySprint) {
 | 
						|
                    $sprint = $userCriteriasBySprint->first()->sprint;
 | 
						|
                    return [
 | 
						|
                        'name' => $sprint->name,
 | 
						|
                        'criterias' => $userCriteriasBySprint->map(function ($userCriteria) {
 | 
						|
                            $criteria = $userCriteria->criteria;
 | 
						|
                            return [
 | 
						|
                                'criteria' => $criteria->name,
 | 
						|
                                'note' => $userCriteria->note ?? '',
 | 
						|
                                'createdBy' => $userCriteria->created_by ?? '',
 | 
						|
                                'point' => $userCriteria->point ?? '',
 | 
						|
                            ];
 | 
						|
                        })
 | 
						|
                    ];
 | 
						|
                })->values()
 | 
						|
            ];
 | 
						|
        })->values();
 | 
						|
 | 
						|
        return AbstractController::ResultSuccess($projectsData);
 | 
						|
    }
 | 
						|
 | 
						|
    public function getProjectById($projects, $inputId)
 | 
						|
    {
 | 
						|
        $filteredProjects = array_filter($projects, function ($project) use ($inputId) {
 | 
						|
            return $project['id'] == $inputId;
 | 
						|
        });
 | 
						|
        return array_values($filteredProjects) ? array_values($filteredProjects)[0] : array_values($filteredProjects);
 | 
						|
    }
 | 
						|
 | 
						|
    public function technical(Request $request)
 | 
						|
    {
 | 
						|
        $request->validate([
 | 
						|
            'user_id' => 'required|exists:users,id',
 | 
						|
            'keyword' => 'nullable|string'
 | 
						|
        ]);
 | 
						|
 | 
						|
        $user = User::findOrFail($request->input('user_id'));
 | 
						|
 | 
						|
        $query = TechnicalUser::query();
 | 
						|
        $query = TechnicalUser::where('user_id', $user->id);
 | 
						|
 | 
						|
        if ($request->filled('keyword')) {
 | 
						|
            $keyword = '%' . $request->input('keyword') . '%';
 | 
						|
            $query->whereHas('technical', fn($q) => $q->where('name', 'like', $keyword))
 | 
						|
                ->with('technical');
 | 
						|
        } else {
 | 
						|
            $query->with('technical');
 | 
						|
        }
 | 
						|
 | 
						|
        $technical_users = $query->where('point', '>', 0)->get();
 | 
						|
 | 
						|
        return AbstractController::ResultSuccess(
 | 
						|
            $technical_users
 | 
						|
                ->map(function ($user) {
 | 
						|
                    return [
 | 
						|
                        'id' => $user->id,
 | 
						|
                        'point' => $user->point,
 | 
						|
                        'updated_at' => $user->updated_at,
 | 
						|
                        'technical' => [
 | 
						|
                            'id' => $user->technical->id,
 | 
						|
                            'name' => $user->technical->name,
 | 
						|
                            'level' => $user->technical->level,
 | 
						|
                        ]
 | 
						|
                    ];
 | 
						|
                })
 | 
						|
        );
 | 
						|
    }
 | 
						|
 | 
						|
    public function report(Request $request)
 | 
						|
    {
 | 
						|
        $request->validate([
 | 
						|
            'userID' => 'required|exists:users,id',
 | 
						|
            'fromDate' => 'nullable|date',
 | 
						|
            'toDate' => 'nullable|date',
 | 
						|
        ]);
 | 
						|
 | 
						|
        // Query User
 | 
						|
        $user = User::findOrFail($request->input('userID'));
 | 
						|
        $userEmail = $user->email;
 | 
						|
 | 
						|
        // Query User Criteria
 | 
						|
        $query = UserCriteria::where('user_email', $userEmail);
 | 
						|
 | 
						|
        if ($request->filled('fromDate')) {
 | 
						|
            $fromDate = Carbon::parse($request->input('fromDate'));
 | 
						|
            $query->where('created_at', '>=', $fromDate);
 | 
						|
        }
 | 
						|
 | 
						|
        if ($request->filled('toDate')) {
 | 
						|
            $toDate = Carbon::parse($request->input('toDate'));
 | 
						|
            $query->where('created_at', '<=', $toDate);
 | 
						|
        }
 | 
						|
 | 
						|
        $userCriterias = $query->with(['sprint', 'criteria'])->get();
 | 
						|
 | 
						|
 | 
						|
        // Structure projects data
 | 
						|
        $projects = $this->jiraService->getAllProjects();
 | 
						|
 | 
						|
        $projectsData = $userCriterias->groupBy('sprint.project_id')->map(function ($userCriteriasByProject, $projectId) use ($projects) {
 | 
						|
            $result = self::getProjectById($projects, $projectId);
 | 
						|
 | 
						|
            return [
 | 
						|
                'name' => $result['name'],
 | 
						|
                'sprints' => $userCriteriasByProject->groupBy('sprint.id')->map(function ($userCriteriasBySprint) {
 | 
						|
                    $sprint = $userCriteriasBySprint->first()->sprint;
 | 
						|
                    return [
 | 
						|
                        'name' => $sprint->name,
 | 
						|
                        'criterias' => $userCriteriasBySprint->map(function ($userCriteria) {
 | 
						|
                            $criteria = $userCriteria->criteria;
 | 
						|
                            return [
 | 
						|
                                'criteria' => $criteria->name,
 | 
						|
                                'note' => $userCriteria->note ?? '',
 | 
						|
                                'createdBy' => $userCriteria->created_by ?? '',
 | 
						|
                                'point' => $userCriteria->point ?? '',
 | 
						|
                            ];
 | 
						|
                        })
 | 
						|
                    ];
 | 
						|
                })->values()
 | 
						|
            ];
 | 
						|
        })->values();
 | 
						|
 | 
						|
        // Generate Word document
 | 
						|
        $phpWord = new PhpWord();
 | 
						|
        $phpWord->setDefaultFontName('Times New Roman');
 | 
						|
        $phpWord->setDefaultFontSize(12);
 | 
						|
        $section = $phpWord->addSection();
 | 
						|
 | 
						|
        $section->addText('Staff Evaluation', [
 | 
						|
            'bold' => true,
 | 
						|
            'size' => 20,
 | 
						|
            'color' => '000000',
 | 
						|
        ], [
 | 
						|
            'align' => 'center',
 | 
						|
            'spaceAfter' => 600,
 | 
						|
        ]);
 | 
						|
 | 
						|
        if ($request->filled('fromDate')) {
 | 
						|
            $fromDate = Carbon::parse($request->input('fromDate'));
 | 
						|
            $section->addText("From: " . $fromDate->format('d-m-Y'), ['name' => 'Times New Roman', 'size' => 12], ['align' => 'end']);
 | 
						|
        }
 | 
						|
 | 
						|
        if ($request->filled('toDate')) {
 | 
						|
            $toDate = Carbon::parse($request->input('toDate'));
 | 
						|
            $section->addText("To: " . $toDate->format('d-m-Y'), ['name' => 'Times New Roman', 'size' => 12], ['align' => 'end']);
 | 
						|
        }
 | 
						|
 | 
						|
        $section->addText("{$user->name}", [
 | 
						|
            'bold' => true,
 | 
						|
            'size' => 14,
 | 
						|
            'color' => '000000',
 | 
						|
        ], [
 | 
						|
            'spaceAfter' => 400,
 | 
						|
        ]);
 | 
						|
 | 
						|
        foreach ($projectsData as $project) {
 | 
						|
            $section->addText("Project: {$project['name']}", ['bold' => true, 'size' => 14, 'color' => '000080']);
 | 
						|
 | 
						|
            foreach ($project['sprints'] as $sprint) {
 | 
						|
                $section->addText("Sprint: {$sprint['name']}", ['bold' => true, 'italic' => true, 'size' => 12, 'color' => '000000']);
 | 
						|
 | 
						|
                $table = $section->addTable([
 | 
						|
                    'borderColor' => '000000',
 | 
						|
                    'borderSize' => 6,
 | 
						|
                    'cellMargin' => 80,
 | 
						|
                ]);
 | 
						|
 | 
						|
                $table->addRow();
 | 
						|
                $table->addCell(2000)->addText('Criteria', ['bold' => true]);
 | 
						|
                $table->addCell(2000)->addText('Note', ['bold' => true]);
 | 
						|
                $table->addCell(2000)->addText('Created By', ['bold' => true]);
 | 
						|
                $table->addCell(2000)->addText('Points', ['bold' => true]);
 | 
						|
 | 
						|
                foreach ($sprint['criterias'] as $criteria) {
 | 
						|
                    $table->addRow();
 | 
						|
                    $table->addCell(2000)->addText($criteria['criteria']);
 | 
						|
                    $table->addCell(2000)->addText($criteria['note']);
 | 
						|
                    $table->addCell(2000)->addText($criteria['createdBy']);
 | 
						|
                    $table->addCell(2000)->addText($criteria['point']);
 | 
						|
                }
 | 
						|
 | 
						|
                $section->addText(' ', [], [
 | 
						|
                    'spaceAfter' => 600,
 | 
						|
                ]);
 | 
						|
            }
 | 
						|
 | 
						|
            $section->addText(' ', [], [
 | 
						|
                'spaceAfter' => 600,
 | 
						|
            ]);
 | 
						|
        }
 | 
						|
 | 
						|
        $tempFile = tempnam(sys_get_temp_dir(), 'word');
 | 
						|
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
 | 
						|
        $objWriter->save($tempFile);
 | 
						|
 | 
						|
        return response()->download($tempFile, "$user->name.docx")->deleteFileAfterSend(true);
 | 
						|
    }
 | 
						|
}
 |