Skip to content

Commit

Permalink
[TM-1352] add logic to get new fields results (#566)
Browse files Browse the repository at this point in the history
* [TM-1352] add logic to get new fields results

* test lint

* lint fix

* [TM-1352] change non-tree to TreeSpecies

* [TM-1352] remove regenerating function

* [TM-1352] collections added removed

* [TM-1352] improve code to total community partners

* [TM-1352] fix lint

* [TM-1352] remove unnecessary functions
  • Loading branch information
LimberHope authored Nov 15, 2024
1 parent 880a64c commit e1913a8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ public function toArray($request)
'volunteer_scstobc' => $this->volunteer_scstobc,
'beneficiaries_scstobc_farmers' => $this->beneficiaries_scstobc_farmers,
'beneficiaries_scstobc' => $this->beneficiaries_scstobc,
'people_knowledge_skills_increased' => $this->people_knowledge_skills_increased,
'indirect_beneficiaries' => $this->indirect_beneficiaries,
'indirect_beneficiaries_description' => $this->indirect_beneficiaries_description,
'workdays_direct_total' => $this->workdays_direct,
'workdays_convergence_total' => $this->workdays_convergence,
'non_tree_total' => $this->non_tree_total,
'total_community_partners' => $this->total_community_partners,
];

return $this->appendFilesToResource($data);
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Resources/V2/SiteReports/SiteReportResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function toArray($request)
'migrated' => ! empty($this->old_model),
'approved_by' => new UserLiteResource($this->approvedBy),
'created_by' => $this->handleCreatedBy(),
'regeneration_description' => $this->regeneration_description,
'total_non_tree_species_planted_count' => $this->total_non_tree_species_planted_count,
];

return $this->appendFilesToResource($data);
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Traits/HasWorkdays.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public function getWorkdaysVolunteerAttribute(): int
return $this->sumTotalWorkdaysAmounts(self::WORKDAY_COLLECTIONS['volunteer']);
}

public function getWorkdaysDirectAttribute(): int
{
return $this->sumTotalWorkdaysAmounts(self::WORKDAY_COLLECTIONS['direct']);
}

public function getWorkdaysConvergenceAttribute(): int
{
return $this->sumTotalWorkdaysAmounts(self::WORKDAY_COLLECTIONS['convergence']);
}

public function getOtherWorkdaysDescriptionAttribute(): ?string
{
return $this
Expand Down
28 changes: 28 additions & 0 deletions app/Models/V2/Projects/ProjectReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ class ProjectReport extends Model implements MediaModel, AuditableContract, Repo
Workday::COLLECTION_PROJECT_DIRECT,
Workday::COLLECTION_PROJECT_CONVERGENCE,
],
'direct' => [
Workday::COLLECTION_PROJECT_DIRECT,
],
'convergence' => [
Workday::COLLECTION_PROJECT_CONVERGENCE,
],
];

public const RESTORATION_PARTNER_COLLECTIONS = [
Expand Down Expand Up @@ -439,11 +445,33 @@ public function getWorkdaysTotalAttribute(): int
return $projectReportTotal + $sumTotals('paid') + $sumTotals('volunteer');
}

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

return TreeSpecies::where('speciesable_type', SiteReport::class)
->whereIn('speciesable_id', $this->task->siteReports()->hasBeenSubmitted()->select('id'))
->where('collection', TreeSpecies::COLLECTION_NON_TREE)
->visible()
->sum('amount');
}

public function getSiteReportsCountAttribute(): int
{
return $this->task?->siteReports()->count() ?? 0;
}

public function getTotalCommunityPartnersAttribute(): int
{
$beneficiaries = ['men', 'women', 'youth', 'scstobc', 'scstobc_farmers', 'smallholder', 'large_scale'];

return collect($beneficiaries)->reduce(function ($sum, $beneficiary) {
return $sum + ($this->{"beneficiaries_$beneficiary"} ?? 0);
}, 0);
}

public function getNurseryReportsCountAttribute(): ?int
{
return $this->task?->nurseryReports()->count() ?? 0;
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 @@ -293,6 +293,11 @@ public function getTotalTreesPlantedCountAttribute(): int
return $this->treeSpecies()->visible()->sum('amount');
}

public function getTotalNonTreeSpeciesPlantedCountAttribute(): int
{
return $this->nonTreeSpecies()->visible()->sum('amount');
}

public function getTotalSeedsPlantedCountAttribute(): int
{
return $this->seedings()->visible()->sum('amount');
Expand Down
2 changes: 2 additions & 0 deletions app/Models/V2/Workdays/Workday.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Workday extends Model implements HandlesLinkedFieldSync
self::COLLECTION_PROJECT_VOLUNTEER_NURSERY_OPERATIONS => 'Volunteer Nursery Operations',
self::COLLECTION_PROJECT_VOLUNTEER_PROJECT_MANAGEMENT => 'Volunteer Project Management',
self::COLLECTION_PROJECT_VOLUNTEER_OTHER => 'Volunteer Other Activities',
self::COLLECTION_PROJECT_DIRECT => 'Direct Workdays',
self::COLLECTION_PROJECT_CONVERGENCE => 'Convergence Workdays',
];

public const COLLECTION_SITE_PAID_SITE_ESTABLISHMENT = 'paid-site-establishment';
Expand Down

0 comments on commit e1913a8

Please sign in to comment.