Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM-1407] approved statuses used in Dashboard #564

Merged
merged 11 commits into from
Nov 11, 2024
Merged
2 changes: 1 addition & 1 deletion app/Helpers/TerrafundDashboardQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function buildQueryFromRequest(Request $request)
public static function retrievePolygonUuidsForProject($projectUuId)
{
$project = Project::where('uuid', $projectUuId)->first();
$sitePolygons = $project->sitePolygons;
$sitePolygons = $project->sitePolygons->where('status', 'approved');

$polygonsIds = $sitePolygons->pluck('poly_id');

Expand Down
3 changes: 3 additions & 0 deletions app/Http/Resources/V2/Projects/ProjectResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ public function toArray($request)
'restoration_strategy' => $this->restoration_strategy,
'trees_restored_count' => $this->trees_restored_count,
'trees_planted_count' => $this->trees_planted_count,
'approved_trees_planted_count' => $this->approved_trees_planted_count,
'seeds_planted_count' => $this->seeds_planted_count,
'regenerated_trees_count' => $this->regenerated_trees_count,
'workday_count' => $this->workday_count,
// These two are temporary until we have bulk import completed.
'self_reported_workday_count' => $this->self_reported_workday_count,
'combined_workday_count' => $this->combined_workday_count,
'total_jobs_created' => $this->total_jobs_created,
'total_approved_jobs_created' => $this->total_approved_jobs_created,
'approved_volunteers_count' => $this->approved_volunteers_count,
'total_sites' => $this->total_sites,
'total_nurseries' => $this->total_nurseries,
'total_project_reports' => $this->total_project_reports,
Expand Down
56 changes: 55 additions & 1 deletion app/Models/V2/Projects/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use App\Models\V2\TreeSpecies\TreeSpecies;
use App\Models\V2\User;
use App\Models\V2\Workdays\Workday;
use App\StateMachines\ReportStatusStateMachine;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -354,6 +355,15 @@ public function getTreesPlantedCountAttribute(): int
->sum('amount');
}

public function getApprovedTreesPlantedCountAttribute(): int
{
return TreeSpecies::where('speciesable_type', SiteReport::class)
->whereIn('speciesable_id', $this->approvedSiteReportIds())
->where('collection', TreeSpecies::COLLECTION_PLANTED)
->visible()
->sum('amount');
}

public function getSeedsPlantedCountAttribute(): int
{
return Seeding::where('seedable_type', SiteReport::class)
Expand Down Expand Up @@ -434,6 +444,31 @@ public function getTotalJobsCreatedAttribute(): int
return $ftTotal + $ptTotal;
}

/**
* Get the total number of approved jobs created (both full-time and part-time)
*
* @return int
*/
public function getTotalApprovedJobsCreatedAttribute(): int
{
return $this->reports()
->approved()
->select(DB::raw('SUM(COALESCE(ft_total, 0) + COALESCE(pt_total, 0)) as total_jobs'))
->value('total_jobs') ?? 0;
}

/**
* Get the total number of approved volunteers
*
* @return int
*/
public function getApprovedVolunteersCountAttribute(): int
{
return $this->reports()
->approved()
->sum('volunteer_total') ?? 0;
}

public function getTotalSitesAttribute(): int
{
return $this->sites()->isApproved()->count();
Expand Down Expand Up @@ -549,9 +584,28 @@ private function submittedSiteReportIds(): HasManyThrough
return $this->submittedSiteReports()->select('v2_site_reports.id');
}

private function approvedSiteReports(): HasManyThrough
{
// scopes that use status don't work on the HasManyThrough because both Site and SiteReport have
// a status field.
return $this
->siteReports()
->whereIn('v2_sites.status', Site::$approvedStatuses)
->where('v2_site_reports.status', ReportStatusStateMachine::APPROVED);
}

/**
* @return HasManyThrough The query of site report IDs for all reports associated with sites that have been
* approved, and have a report status approved.
*/
private function approvedSiteReportIds(): HasManyThrough
{
return $this->approvedSiteReports()->select('v2_site_reports.id');
}

public function getTotalSitePolygonsAttribute()
{
return $this->sitePolygons()->count();
return $this->sitePolygons->where('status', 'approved')->count();
}

public function getTotalHectaresRestoredSumAttribute(): float
Expand Down
6 changes: 6 additions & 0 deletions app/Models/V2/Projects/ProjectReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use App\Models\V2\TreeSpecies\TreeSpecies;
use App\Models\V2\User;
use App\Models\V2\Workdays\Workday;
use App\StateMachines\ReportStatusStateMachine;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -488,4 +489,9 @@ public function getAuditableNameAttribute(): string
{
return $this->title ?? '';
}

public function scopeApproved($query)
{
return $query->where('status', ReportStatusStateMachine::APPROVED);
}
}
2 changes: 2 additions & 0 deletions app/Services/Dashboard/JobsCreatedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private function calculateTotalNonYouth($jobsCreatedDetailed)
private function getTotalJobsCreated($projectIds)
{
$sumData = ProjectReport::whereIn('project_id', $projectIds)
->where('status', 'approved')
->selectRaw('SUM(ft_total) as total_ft, SUM(pt_total) as total_pt')
->first();

Expand All @@ -77,6 +78,7 @@ private function getTotalJobsCreated($projectIds)
private function getJobsCreatedDetailed($projectIds)
{
return ProjectReport::whereIn('project_id', $projectIds)
->where('status', 'approved')
->selectRaw(
'SUM(ft_total) as total_ft,
SUM(pt_total) as total_pt,
Expand Down
8 changes: 3 additions & 5 deletions app/Services/Dashboard/RunActiveCountriesTableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function getAllCountries(Request $request)
'country_slug' => $country->slug,
'country' => $country->label,
'number_of_projects' => $countryProjects->count(),
'total_trees_planted' => $this->sumField($countryProjects, 'trees_planted_count'),
'total_jobs_created' => $this->sumField($countryProjects, 'total_jobs_created'),
'total_trees_planted' => $this->sumField($countryProjects, 'approved_trees_planted_count'),
'total_jobs_created' => $this->sumField($countryProjects, 'total_approved_jobs_created'),
'hectares_restored' => round($this->sumHectares($countryProjects)),
];
}
Expand All @@ -52,8 +52,6 @@ private function sumField($projects, $field)

private function sumHectares($projects)
{
return $projects->sum(function ($project) {
return $project->sitePolygons->sum('calc_area');
});
return $projects->sum('total_hectares_restored_sum');
}
}
47 changes: 4 additions & 43 deletions app/Services/Dashboard/RunActiveProjectsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,56 +48,17 @@ public function getAllProjects($request, $perPage, $page)
'uuid' => $project->uuid,
'name' => $project->name,
'organisation' => $project->organisation->name,
'trees_under_restoration' => $this->treesUnderRestoration($project),
'jobs_created' => $this->jobsCreated($project),
'volunteers' => $this->volunteers($project),
'beneficiaries' => $this->beneficiaries($project),
'survival_rate' => $project->survival_rate,
'number_of_sites' => $project->sites_count,
'number_of_nurseries' => $project->nurseries_count,
'trees_under_restoration' => $project->approved_trees_planted_count,
'jobs_created' => $project->total_approved_jobs_created ?? 0,
'volunteers' => $project->approved_volunteers_count ?? 0,
'project_country' => $this->projectCountry($project->country),
'country_slug' => $project->country,
'number_of_trees_goal' => $project->trees_grown_goal,
'date_added' => $project->created_at,
'hectares_under_restoration' => round($project->sitePolygons->sum('calc_area')),
'hectares_under_restoration' => $project->total_hectares_restored_sum,
'programme' => $project->framework_key,
];
});
}

public function treesUnderRestoration($project)
{
return $project->trees_planted_count;
}

public function jobsCreated($project)
{
$projectReport = $project->reports()
->selectRaw('SUM(ft_total) as total_ft, SUM(pt_total) as total_pt')
->groupBy('project_id')
->first();

if ($projectReport) {
return $projectReport->total_ft + $projectReport->total_pt;
} else {
return 0;
}
}

public function volunteers($project)
{
$totalVolunteers = $project->reports()->selectRaw('SUM(volunteer_total) as total')->first();

return $totalVolunteers ? intval($totalVolunteers->total) : 0;
}

public function beneficiaries($project)
{
$totalBeneficiaries = $project->reports()->selectRaw('SUM(beneficiaries) as total')->first();

return $totalBeneficiaries ? intval($totalBeneficiaries->total) : 0;
}

public function projectCountry($slug)
{
$countryId = FormOptionList::where('key', 'countries')->value('id');
Expand Down
3 changes: 3 additions & 0 deletions app/Services/Dashboard/RunHectaresRestoredService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Services\Dashboard;

use App\Helpers\TerrafundDashboardQueryHelper;
use App\Models\V2\Sites\Site;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

Expand Down Expand Up @@ -49,6 +50,8 @@ public function getProjectsPolygons($projects)
return DB::table('site_polygon as sp')
->join('v2_sites as s', 'sp.site_id', '=', 's.uuid')
->join('v2_projects as p', 's.project_id', '=', 'p.id')
->whereIn('s.status', Site::$approvedStatuses)
->where('sp.status', 'approved')
->whereIn('p.uuid', $projects)
->select('sp.id')
->get();
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Dashboard/RunTopTreesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getTopProjects($projects)
'organization' => $project->organisation->name,
'project' => $project->name,
'uuid' => $project->uuid,
'trees_planted' => $project->trees_planted_count,
'trees_planted' => $project->approved_trees_planted_count,
];
}));

Expand Down
13 changes: 4 additions & 9 deletions app/Services/Dashboard/RunTotalHeaderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ private function getProjectsData(Request $request)
'v2_projects.total_hectares_restored_goal',
'v2_projects.trees_grown_goal',
])
->get()
->map(function ($project) {
$project->total_hectares_restored = $project->sitePolygons->sum('calc_area');

return $project;
});
->get();
}

private function getCountryName(Request $request)
Expand Down Expand Up @@ -70,16 +65,16 @@ public function getTotalEnterpriseCount($projects)

public function getTotalJobsCreatedSum($projects)
{
return $projects->sum('total_jobs_created');
return $projects->sum('total_approved_jobs_created');
}

public function getTotalHectaresSum($projects)
{
return $projects->sum('total_hectares_restored');
return $projects->sum('total_hectares_restored_sum');
}

public function getTotalTreesRestoredSum($projects)
{
return $projects->sum('trees_planted_count');
return $projects->sum('approved_trees_planted_count');
}
}
6 changes: 3 additions & 3 deletions app/Services/Dashboard/RunVolunteersAverageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function runVolunteersAverageJob(Request $request): object
public function getTotalVolunteerSum(Collection $projects): int
{
return $projects->sum(function ($project) {
return $project->reports->sum('volunteer_total');
return $project->reports()->approved()->sum('volunteer_total');
});
}

Expand All @@ -47,7 +47,7 @@ public function getTotalVolunteerSum(Collection $projects): int
public function getVolunteersSum(Collection $projects, string $volunteerType): int
{
return $projects->sum(function ($project) use ($volunteerType) {
return $project->reports->sum($volunteerType);
return $project->reports()->approved()->sum($volunteerType);
});
}

Expand All @@ -59,6 +59,6 @@ public function getVolunteersSum(Collection $projects, string $volunteerType): i
*/
public function numberOfSites(Collection $projects): int
{
return $projects->sum(fn ($project) => $project->sites->count());
return $projects->sum('total_sites');
}
}
8 changes: 4 additions & 4 deletions app/Services/Dashboard/TreeRestorationGoalService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\V2\Sites\Site;
use App\Models\V2\Sites\SiteReport;
use App\Models\V2\TreeSpecies\TreeSpecies;
use App\StateMachines\ReportStatusStateMachine;
use Carbon\Carbon;
use Illuminate\Http\Request;

Expand Down Expand Up @@ -59,6 +60,7 @@ private function getSiteIds($projectIds)
private function getDistinctDates($siteIds)
{
return SiteReport::selectRaw('YEAR(due_at) as year, MONTH(due_at) as month')
->where('status', ReportStatusStateMachine::APPROVED)
->whereNotNull('due_at')
->whereIn('site_id', $siteIds)
->groupBy('year', 'month')
Expand All @@ -75,9 +77,7 @@ private function treeCountByDueDate(array $projectIds)
{
$projects = Project::whereIn('id', $projectIds)->get();

return $projects->sum(function ($project) {
return $project->trees_planted_count;
});
return $projects->sum('approved_trees_planted_count');
}

private function treeCountPerPeriod($siteIds, $distinctDates, $totalTreesGrownGoal)
Expand Down Expand Up @@ -105,7 +105,7 @@ private function treeCountPerPeriod($siteIds, $distinctDates, $totalTreesGrownGo
private function calculateTreeSpeciesAmountForPeriod($siteIds, $year, $month)
{
return SiteReport::whereIn('site_id', $siteIds)
->whereNotIn('v2_site_reports.status', SiteReport::UNSUBMITTED_STATUSES)
->where('v2_site_reports.status', ReportStatusStateMachine::APPROVED)
->whereYear('v2_site_reports.due_at', $year)
->whereMonth('v2_site_reports.due_at', $month)
->get()
Expand Down
12 changes: 2 additions & 10 deletions openapi-src/V2/definitions/DashboardActiveProjectData.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,11 @@ properties:
type: integer
volunteers:
type: integer
beneficiaries:
type: integer
survival_rate:
type: integer
number_of_sites:
type: integer
number_of_nurseries:
type: integer
project_country:
type: string
country_slug:
type: string
number_of_trees_goal:
hectares_under_restoration:
type: integer
date_added:
programme:
type: string
Loading
Loading