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-911] Update API responses for projects and sites and their reports to include seeding counts. #266

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function toArray($request)
'created_by' => $this->handleCreatedBy(),
'seedlings_grown' => $this->seedlings_grown,
'trees_planted_count' => $this->trees_planted_count,
'seeds_planted_count' => $this->seeds_planted_count,
'community_progress' => $this->community_progress,
'equitable_opportunities' => $this->equitable_opportunities,
'local_engagement' => $this->local_engagement,
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/V2/SiteReports/SiteReportResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function toArray($request)

'total_workdays_count' => $this->total_workdays_count,
'total_trees_planted_count' => $this->total_trees_planted_count,
'total_seeds_planted_count' => $this->total_seeds_planted_count,

'due_at' => $this->due_at,
'approved_at' => $this->approved_at,
Expand Down
8 changes: 4 additions & 4 deletions app/Models/V2/Projects/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,11 @@ private function submittedSiteReports(): HasManyThrough
}

/**
* @return array The array of site report IDs for all reports associated with sites that have been approved, and
* have a report status not in due or started (reports that have been submitted).
* @return HasManyThrough The query of site report IDs for all reports associated with sites that have been
* approved, and have a report status not in due or started (reports that have been submitted).
*/
private function submittedSiteReportIds(): array
private function submittedSiteReportIds(): HasManyThrough
{
return $this->submittedSiteReports()->pluck('v2_site_reports.id')->toArray();
return $this->submittedSiteReports()->select('v2_site_reports.id');
}
}
37 changes: 16 additions & 21 deletions app/Models/V2/Projects/ProjectReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use App\Models\V2\Organisation;
use App\Models\V2\Polygon;
use App\Models\V2\ReportModel;
use App\Models\V2\Sites\Site;
use App\Models\V2\Seeding;
use App\Models\V2\Sites\SiteReport;
use App\Models\V2\Tasks\Task;
use App\Models\V2\TreeSpecies\TreeSpecies;
Expand Down Expand Up @@ -319,30 +319,25 @@ public function getSeedlingsGrownToDateAttribute(): int

public function getTreesPlantedCountAttribute(): int
{
$total = 0;
if (empty($this->due_at)) {
return $total;
if (empty($this->task_id)) {
return 0;
}

$month = $this->due_at->month;
$year = $this->due_at->year;
$siteIds = Site::where('project_id', data_get($this->project, 'id'))
->isApproved()
->pluck('id')
->toArray();

if (count($siteIds) > 0) {
$reports = SiteReport::whereIn('site_id', $siteIds)
->whereMonth('due_at', $month)
->whereYear('due_at', $year)
->get();

foreach ($reports as $report) {
$total += $report->treeSpecies()->sum('amount');
}
return TreeSpecies::where('speciesable_type', SiteReport::class)
->whereIn('speciesable_id', $this->task->siteReports()->select('id'))
->where('collection', TreeSpecies::COLLECTION_PLANTED)
->sum('amount');
}

public function getSeedsPlantedCountAttribute(): int
{
if (empty($this->task_id)) {
return 0;
}

return $total;
return Seeding::where('seedable_type', SiteReport::class)
->whereIn('seedable_id', $this->task->siteReports()->select('id'))
->sum('amount');
}

public function getTotalJobsCreatedAttribute(): int
Expand Down
5 changes: 5 additions & 0 deletions app/Models/V2/Sites/SiteReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ public function getTotalTreesPlantedCountAttribute(): int
return $this->treeSpecies()->sum('amount');
}

public function getTotalSeedsPlantedCountAttribute(): int
{
return $this->seedings()->sum('amount');
}

public function getOrganisationAttribute()
{
return $this->site ? $this->site->organisation : null;
Expand Down
Loading