diff --git a/app/Helpers/TerrafundDashboardQueryHelper.php b/app/Helpers/TerrafundDashboardQueryHelper.php new file mode 100644 index 000000000..f6a4b8745 --- /dev/null +++ b/app/Helpers/TerrafundDashboardQueryHelper.php @@ -0,0 +1,60 @@ +whereHas('organisation', function ($query) { + $query->whereIn('type', ['for-profit-organization', 'non-profit-organization']); + }); + if ($request->has('country')) { + $country = $request->input('country'); + $query = $query->where('country', $country); + } elseif ($request->has('uuid')) { + $projectId = $request->input('uuid'); + $query = $query->where('v2_projects.uuid', $projectId); + } + + return $query; + } + + public static function getPolygonIdsOfProject($request) + { + $projectIds = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) + ->pluck('uuid'); + $polygonsIds = SitePolygon::whereHas('site.project', function ($query) use ($projectIds) { + $query->whereIn('uuid', $projectIds); + })->pluck('poly_id'); + + return $polygonsIds; + } + + public static function getPolygonsByStatusOfProject($request) + { + $projectIds = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) + ->pluck('uuid'); + + $statuses = ['Needs More Info', 'Submitted', 'Approved']; + + $polygons = []; + + foreach ($statuses as $status) { + // Get polygons of the project filtered by status + $polygonsOfProject = SitePolygon::whereHas('site.project', function ($query) use ($projectIds) { + $query->whereIn('uuid', $projectIds); + }) + ->where('status', $status) + ->pluck('poly_id'); + + $polygons[$status] = $polygonsOfProject; + } + + return $polygons; + } +} diff --git a/app/Http/Controllers/V2/Dashboard/GetJobsCreatedController.php b/app/Http/Controllers/V2/Dashboard/GetJobsCreatedController.php new file mode 100644 index 000000000..0eba73489 --- /dev/null +++ b/app/Http/Controllers/V2/Dashboard/GetJobsCreatedController.php @@ -0,0 +1,115 @@ +join('organisations', 'v2_projects.organisation_id', '=', 'organisations.id') + ->select('v2_projects.id', 'organisations.type') + ->get(); + + $forProfitProjectIds = $this->filterProjectIdsByType($rawProjectIds, 'for-profit-organization'); + $nonProfitProjectIds = $this->filterProjectIdsByType($rawProjectIds, 'non-profit-organization'); + $allProjectIds = $this->getAllProjectIds($rawProjectIds); + + $forProfitJobsCreated = $this->getTotalJobsCreated($forProfitProjectIds); + $nonProfitJobsCreated = $this->getTotalJobsCreated($nonProfitProjectIds); + $totalJobsCreated = $this->getTotalJobsCreated($allProjectIds); + $jobsCreatedDetailed = $this->getJobsCreatedDetailed($allProjectIds); + + $finalResult = (object) [ + 'totalJobsCreated' => $totalJobsCreated, + 'forProfitJobsCreated' => $forProfitJobsCreated, + 'nonProfitJobsCreated' => $nonProfitJobsCreated, + 'total_ft' => $jobsCreatedDetailed->total_ft, + 'total_pt' => $jobsCreatedDetailed->total_pt, + 'total_men' => $this->calculateTotalMen($jobsCreatedDetailed), + 'total_pt_men' => $jobsCreatedDetailed->total_pt_men, + 'total_ft_men' => $jobsCreatedDetailed->total_ft_men, + 'total_women' => $this->calculateTotalWomen($jobsCreatedDetailed), + 'total_pt_women' => $jobsCreatedDetailed->total_pt_women, + 'total_ft_women' => $jobsCreatedDetailed->total_ft_women, + 'total_youth' => $this->calculateTotalYouth($jobsCreatedDetailed), + 'total_pt_youth' => $jobsCreatedDetailed->total_pt_youth, + 'total_ft_youth' => $jobsCreatedDetailed->total_ft_youth, + 'total_non_youth' => $this->calculateTotalNonYouth($jobsCreatedDetailed), + 'total_pt_non_youth' => $jobsCreatedDetailed->total_pt_non_youth, + 'total_ft_non_youth' => $jobsCreatedDetailed->total_ft_non_youth, + ]; + + return new JobsCreatedResource($finalResult); + } + + private function filterProjectIdsByType($projectIds, $type) + { + return $projectIds->filter(function ($row) use ($type) { + return $row->type === $type; + })->pluck('id')->toArray(); + } + + private function getAllProjectIds($projectIds) + { + return $projectIds->pluck('id')->toArray(); + } + + private function calculateTotalMen($jobsCreatedDetailed) + { + return $jobsCreatedDetailed->total_pt_men + $jobsCreatedDetailed->total_ft_men; + } + + private function calculateTotalWomen($jobsCreatedDetailed) + { + return $jobsCreatedDetailed->total_pt_women + $jobsCreatedDetailed->total_ft_women; + } + + private function calculateTotalYouth($jobsCreatedDetailed) + { + return $jobsCreatedDetailed->total_pt_youth + $jobsCreatedDetailed->total_ft_youth; + } + + private function calculateTotalNonYouth($jobsCreatedDetailed) + { + return $jobsCreatedDetailed->total_pt_non_youth + $jobsCreatedDetailed->total_ft_non_youth; + } + + private function getTotalJobsCreated($projectIds) + { + $sumData = ProjectReport::whereIn('project_id', $projectIds) + ->where('framework_key', 'terrafund') + ->selectRaw('SUM(ft_total) as total_ft, SUM(pt_total) as total_pt') + ->first(); + + return $sumData->total_ft + $sumData->total_pt; + } + + private function getJobsCreatedDetailed($projectIds) + { + return ProjectReport::whereIn('project_id', $projectIds) + ->where('framework_key', 'terrafund') + ->selectRaw( + 'SUM(ft_total) as total_ft, + SUM(pt_total) as total_pt, + SUM(pt_men) as total_pt_men, + SUM(ft_men) as total_ft_men, + SUM(pt_women) as total_pt_women, + SUM(ft_women) as total_ft_women, + SUM(pt_youth) as total_pt_youth, + SUM(ft_youth) as total_ft_youth, + SUM(pt_non_youth) as total_pt_non_youth, + SUM(ft_jobs_non_youth) as total_ft_non_youth' + ) + ->first(); + } +} diff --git a/app/Http/Resources/V2/Dashboard/JobsCreatedResource.php b/app/Http/Resources/V2/Dashboard/JobsCreatedResource.php new file mode 100644 index 000000000..763d251b2 --- /dev/null +++ b/app/Http/Resources/V2/Dashboard/JobsCreatedResource.php @@ -0,0 +1,37 @@ + (int) $this->totalJobsCreated, + 'forProfitJobsCreated' => (int) $this->forProfitJobsCreated, + 'nonProfitJobsCreated' => (int) $this->nonProfitJobsCreated, + 'total_ft' => (int) $this->total_ft, + 'total_pt' => (int) $this->total_pt, + 'total_men' => (int) $this->total_men, + 'total_pt_men' => (int) $this->total_pt_men, + 'total_ft_men' => (int) $this->total_ft_men, + 'total_women' => (int) $this->total_women, + 'total_pt_women' => (int) $this->total_pt_women, + 'total_ft_women' => (int) $this->total_ft_women, + 'total_youth' => (int) $this->total_youth, + 'total_pt_youth' => (int) $this->total_pt_youth, + 'total_ft_youth' => (int) $this->total_ft_youth, + 'total_non_youth' => (int) $this->total_non_youth, + 'total_pt_non_youth' => (int) $this->total_pt_non_youth, + 'total_ft_non_youth' => (int) $this->total_ft_non_youth, + ]; + } +} diff --git a/routes/api_v2.php b/routes/api_v2.php index cda46e83c..580e0283c 100644 --- a/routes/api_v2.php +++ b/routes/api_v2.php @@ -15,6 +15,7 @@ use App\Http\Controllers\V2\CoreTeamLeader\DeleteCoreTeamLeaderController; use App\Http\Controllers\V2\CoreTeamLeader\StoreCoreTeamLeaderController; use App\Http\Controllers\V2\CoreTeamLeader\UpdateCoreTeamLeaderController; +use App\Http\Controllers\V2\Dashboard\GetJobsCreatedController; use App\Http\Controllers\V2\Disturbances\DeleteDisturbanceController; use App\Http\Controllers\V2\Disturbances\GetDisturbancesForEntityController; use App\Http\Controllers\V2\Disturbances\StoreDisturbanceController; @@ -651,3 +652,7 @@ function () { Route::resource('files', FilePropertiesController::class); //Route::put('file/{uuid}', [FilePropertiesController::class, 'update']); //Route::delete('file/{uuid}', [FilePropertiesController::class, 'destroy']); + +Route::prefix('dashboard')->group(function () { + Route::get('/jobs-created', GetJobsCreatedController::class); +});