diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 8752549dc..80b1cc730 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -31,17 +31,13 @@ protected function schedule(Schedule $schedule) $schedule->command('generate-control-site-due-submissions')->weeklyOn(5, '00:00'); // PPC report jobs - $schedule->job(new CreateTaskDueJob('ppc', 4))->yearlyOn(1, 7); - - $schedule->job(new CreateTaskDueJob('ppc', 7))->yearlyOn(3, 15); - - $schedule->job(new CreateTaskDueJob('ppc', 10))->yearlyOn(7, 7); - - $schedule->job(new CreateTaskDueJob('ppc', 1))->yearlyOn(10, 6); + $schedule->job(new CreateTaskDueJob('ppc', 4, 4))->yearlyOn(3, 14); + $schedule->job(new CreateTaskDueJob('ppc', 7, 5))->yearlyOn(6, 14); + $schedule->job(new CreateTaskDueJob('ppc', 10, 4))->yearlyOn(9, 13); + $schedule->job(new CreateTaskDueJob('ppc', 1, 3))->yearlyOn(12, 13); // Terrafund report jobs $schedule->job(new CreateTaskDueJob('terrafund'))->yearlyOn(12, 31); - $schedule->job(new CreateTaskDueJob('terrafund'))->yearlyOn(6, 30); $schedule->job(new SendReportRemindersJob('terrafund'))->yearlyOn(5, 30); @@ -52,7 +48,6 @@ protected function schedule(Schedule $schedule) $schedule->command('generate-application-export')->twiceDaily(13, 20); $schedule->command('generate-admin-all-entity-records-export')->twiceDaily(13, 20); - $schedule->command('populate-v2-temporary-sites')->hourly(); } protected function commands() diff --git a/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php b/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php index 26f621b49..0811786c3 100644 --- a/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php +++ b/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php @@ -18,6 +18,11 @@ public function __invoke(Request $request, EntityModel $entity) ->where('speciesable_type', get_class($entity)) ->where('speciesable_id', $entity->id); + $filter = $request->query('filter'); + if (! empty($filter['collection'])) { + $query->where('collection', $filter['collection']); + } + return new TreeSpeciesCollection($query->paginate()); } } diff --git a/app/Http/Resources/V2/ProjectReports/ProjectReportResource.php b/app/Http/Resources/V2/ProjectReports/ProjectReportResource.php index f1e449077..9ce59f811 100644 --- a/app/Http/Resources/V2/ProjectReports/ProjectReportResource.php +++ b/app/Http/Resources/V2/ProjectReports/ProjectReportResource.php @@ -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, diff --git a/app/Http/Resources/V2/SiteReports/SiteReportResource.php b/app/Http/Resources/V2/SiteReports/SiteReportResource.php index 1752d0ea5..c9cfda911 100644 --- a/app/Http/Resources/V2/SiteReports/SiteReportResource.php +++ b/app/Http/Resources/V2/SiteReports/SiteReportResource.php @@ -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, diff --git a/app/Jobs/V2/CreateTaskDueJob.php b/app/Jobs/V2/CreateTaskDueJob.php index 013ba48aa..1fbdca997 100644 --- a/app/Jobs/V2/CreateTaskDueJob.php +++ b/app/Jobs/V2/CreateTaskDueJob.php @@ -26,12 +26,12 @@ class CreateTaskDueJob implements ShouldQueue private $frameworkKey; - public function __construct(string $frameworkKey, int $dueMonth = null) + public function __construct(string $frameworkKey, int $dueMonth = null, int $dueDay = null) { $this->frameworkKey = $frameworkKey; if ($dueMonth) { - $carbonDate = Carbon::createFromFormat('m', $dueMonth)->startOfMonth()->setDay(5); + $carbonDate = Carbon::createFromFormat('m', $dueMonth)->startOfMonth()->setDay($dueDay ?? 5); $this->dueDate = $carbonDate->isPast() ? $carbonDate->addYear() : $carbonDate; } else { $this->dueDate = Carbon::now()->addMonth()->startOfDay(); diff --git a/app/Models/V2/Projects/Project.php b/app/Models/V2/Projects/Project.php index c946f5722..5952a5e91 100644 --- a/app/Models/V2/Projects/Project.php +++ b/app/Models/V2/Projects/Project.php @@ -349,13 +349,7 @@ public function getSeedsPlantedCountAttribute(): int public function getRegeneratedTreesCountAttribute(): int { - $sites = Site::where('project_id', $this->id)->get(); - $total = 0; - foreach ($sites as $site) { - $total += $site->regenerated_trees_count; - } - - return $total; + return $this->submittedSiteReports()->sum('num_trees_regenerating'); } public function getWorkdayCountAttribute(): int @@ -489,11 +483,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'); } } diff --git a/app/Models/V2/Projects/ProjectReport.php b/app/Models/V2/Projects/ProjectReport.php index cd2835fb7..6a740cc22 100644 --- a/app/Models/V2/Projects/ProjectReport.php +++ b/app/Models/V2/Projects/ProjectReport.php @@ -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; @@ -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 diff --git a/app/Models/V2/Sites/Site.php b/app/Models/V2/Sites/Site.php index 96b69b828..993d9db56 100644 --- a/app/Models/V2/Sites/Site.php +++ b/app/Models/V2/Sites/Site.php @@ -295,11 +295,7 @@ public function getTreesPlantedCountAttribute(): int public function getRegeneratedTreesCountAttribute(): int { - if (empty($this->a_nat_regeneration) || empty($this->a_nat_regeneration_trees_per_hectare)) { - return 0; - } else { - return $this->a_nat_regeneration * $this->a_nat_regeneration_trees_per_hectare; - } + return $this->reports()->hasBeenSubmitted()->sum('num_trees_regenerating'); } public function getWorkdayCountAttribute(): int diff --git a/app/Models/V2/Sites/SiteReport.php b/app/Models/V2/Sites/SiteReport.php index cbae89591..dbc5752d5 100644 --- a/app/Models/V2/Sites/SiteReport.php +++ b/app/Models/V2/Sites/SiteReport.php @@ -91,6 +91,8 @@ class SiteReport extends Model implements MediaModel, AuditableContract, ReportM 'polygon_status', 'answers', 'paid_other_activity_description', + 'num_trees_regenerating', + 'regeneration_description', // virtual (see HasWorkdays trait) 'other_workdays_description', @@ -281,6 +283,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; diff --git a/config/wri/linked-fields.php b/config/wri/linked-fields.php index 27c8bb58c..cd25da2c6 100644 --- a/config/wri/linked-fields.php +++ b/config/wri/linked-fields.php @@ -594,6 +594,8 @@ // TODO (TM-912) Deprecated, to be removed. 'site-rep-paid-other-activity-description' => ['property' => 'paid_other_activity_description', 'label' => 'Paid Other Activities Description', 'input_type' => 'long-text'], 'site-rep-other-workdays-description' => ['property' => 'other_workdays_description', 'label' => 'Other Activities Description', 'input_type' => 'long-text'], + 'site-rep-num-trees-regenerating' => ['property' => 'num_trees_regenerating', 'label' => 'Estimate Number of Trees Restored via ANR', 'input_type' => 'number'], + 'site-rep-regeneration-description' => ['property' => 'regeneration_description', 'label' => 'Description of ANR Activities', 'input_type' => 'long-text'], ], 'file-collections' => [ 'site-rep-col-media' => ['property' => 'media', 'label' => 'Media', 'input_type' => 'file', 'multichoice' => true], diff --git a/database/migrations/2024_04_19_225021_create_workday_demographics.php b/database/migrations/2024_04_19_225021_create_workday_demographics.php index e3d742fb5..651ea7e85 100644 --- a/database/migrations/2024_04_19_225021_create_workday_demographics.php +++ b/database/migrations/2024_04_19_225021_create_workday_demographics.php @@ -5,23 +5,25 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration { +return new class () extends Migration { /** * Run the migrations. */ public function up(): void { - Schema::create('workday_demographics', function (Blueprint $table) { - $table->id(); - $table->foreignIdFor(Workday::class); - $table->string('type'); - $table->string('subtype')->nullable(); - $table->string('name')->nullable(); - $table->integer('amount'); + if (! Schema::hasTable('workday_demographics')) { + Schema::create('workday_demographics', function (Blueprint $table) { + $table->id(); + $table->foreignIdFor(Workday::class); + $table->string('type'); + $table->string('subtype')->nullable(); + $table->string('name')->nullable(); + $table->integer('amount'); - $table->softDeletes(); - $table->timestamps(); - }); + $table->softDeletes(); + $table->timestamps(); + }); + } } /** diff --git a/database/migrations/2024_06_07_182710_retype_shared_drive_link.php b/database/migrations/2024_06_07_182710_retype_shared_drive_link.php new file mode 100644 index 000000000..d5b546494 --- /dev/null +++ b/database/migrations/2024_06_07_182710_retype_shared_drive_link.php @@ -0,0 +1,39 @@ +text('shared_drive_link')->nullable()->change(); + }); + Schema::table('v2_site_reports', function (Blueprint $table) { + $table->text('shared_drive_link')->nullable()->change(); + }); + Schema::table('v2_nursery_reports', function (Blueprint $table) { + $table->text('shared_drive_link')->nullable()->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('v2_project_reports', function (Blueprint $table) { + $table->string('shared_drive_link')->nullable()->change(); + }); + Schema::table('v2_site_reports', function (Blueprint $table) { + $table->string('shared_drive_link')->nullable()->change(); + }); + Schema::table('v2_nursery_reports', function (Blueprint $table) { + $table->string('shared_drive_link')->nullable()->change(); + }); + } +}; diff --git a/database/migrations/2024_06_07_200450_add_anr_metrics_to_site_report.php b/database/migrations/2024_06_07_200450_add_anr_metrics_to_site_report.php new file mode 100644 index 000000000..5e17c8f98 --- /dev/null +++ b/database/migrations/2024_06_07_200450_add_anr_metrics_to_site_report.php @@ -0,0 +1,29 @@ +integer('num_trees_regenerating')->nullable(); + $table->text('regeneration_description')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('v2_site_reports', function (Blueprint $table) { + $table->dropColumn('num_trees_regenerating'); + $table->dropColumn('regeneration_description'); + }); + } +};