From cbe514cadda8e813dd249e754c8327dd3236474a Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Fri, 7 Jun 2024 11:29:58 -0700 Subject: [PATCH 1/9] [TM-965] Retype the shared_drive_link columns in order to remove the 255 character limit. --- ..._06_07_182710_retype_shared_drive_link.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 database/migrations/2024_06_07_182710_retype_shared_drive_link.php 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(); + }); + } +}; From 089806f283ff2bdab64f02ea96ac107ffc9928ef Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Wed, 5 Jun 2024 21:26:00 -0700 Subject: [PATCH 2/9] [TM-911] Update API responses for projects and sites and their reports to include seeding counts. --- .../ProjectReports/ProjectReportResource.php | 1 + .../V2/SiteReports/SiteReportResource.php | 1 + app/Models/V2/Projects/Project.php | 8 ++-- app/Models/V2/Projects/ProjectReport.php | 37 ++++++++----------- app/Models/V2/Sites/SiteReport.php | 5 +++ 5 files changed, 27 insertions(+), 25 deletions(-) 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/Models/V2/Projects/Project.php b/app/Models/V2/Projects/Project.php index c946f5722..58bc83f7e 100644 --- a/app/Models/V2/Projects/Project.php +++ b/app/Models/V2/Projects/Project.php @@ -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'); } } 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/SiteReport.php b/app/Models/V2/Sites/SiteReport.php index cbae89591..5c9bcac99 100644 --- a/app/Models/V2/Sites/SiteReport.php +++ b/app/Models/V2/Sites/SiteReport.php @@ -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; From 678337325973faff9ad39564f8bb0dd97dc1931c Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Fri, 7 Jun 2024 13:17:37 -0700 Subject: [PATCH 3/9] [TM-940] Support a new field on site reports to collect metrics on tree regeneration. --- app/Models/V2/Projects/Project.php | 8 +---- app/Models/V2/Sites/Site.php | 6 +--- app/Models/V2/Sites/SiteReport.php | 2 ++ config/wri/linked-fields.php | 2 ++ ..._200450_add_anr_metrics_to_site_report.php | 29 +++++++++++++++++++ 5 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 database/migrations/2024_06_07_200450_add_anr_metrics_to_site_report.php diff --git a/app/Models/V2/Projects/Project.php b/app/Models/V2/Projects/Project.php index c946f5722..2451b631d 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 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..cb4974a3a 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', 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_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'); + }); + } +}; From 771641f81b87a0861d026c3b9b80a04c0cec2d38 Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Fri, 7 Jun 2024 13:22:06 -0700 Subject: [PATCH 4/9] [TM-970] Stop trying to call a script that doesn't exist. --- app/Console/Kernel.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 8752549dc..5eda2acca 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -52,7 +52,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() From a334bf8fa3e4db1bd5d2bf34df6a4fcbce98624a Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Mon, 10 Jun 2024 14:36:30 -0700 Subject: [PATCH 5/9] [TM-939] Update timing of PPC report generation. --- app/Console/Kernel.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 5eda2acca..c7acf4d82 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))->yearlyOn(3, 14); + $schedule->job(new CreateTaskDueJob('ppc', 7))->yearlyOn(6, 14); + $schedule->job(new CreateTaskDueJob('ppc', 10))->yearlyOn(9, 13); + $schedule->job(new CreateTaskDueJob('ppc', 1))->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); From 39aa11897ffc33c30d4594274c7fc661e2242ade Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Mon, 10 Jun 2024 14:44:49 -0700 Subject: [PATCH 6/9] [TM-939] Allow the day of the month to be specified. --- app/Console/Kernel.php | 8 ++++---- app/Jobs/V2/CreateTaskDueJob.php | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index c7acf4d82..80b1cc730 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -31,10 +31,10 @@ 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(3, 14); - $schedule->job(new CreateTaskDueJob('ppc', 7))->yearlyOn(6, 14); - $schedule->job(new CreateTaskDueJob('ppc', 10))->yearlyOn(9, 13); - $schedule->job(new CreateTaskDueJob('ppc', 1))->yearlyOn(12, 13); + $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); 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(); From ea0c0a87b05e945a44aedcf6918058f39c06cddd Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Tue, 11 Jun 2024 15:28:06 -0700 Subject: [PATCH 7/9] [TM-754] Respect the filter query param on the tree species API --- .../V2/TreeSpecies/GetTreeSpeciesForEntityController.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php b/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php index 26f621b49..9fa869b38 100644 --- a/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php +++ b/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php @@ -7,6 +7,8 @@ use App\Models\V2\EntityModel; use App\Models\V2\TreeSpecies\TreeSpecies; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; +use Spatie\QueryBuilder\QueryBuilder; class GetTreeSpeciesForEntityController extends Controller { @@ -18,6 +20,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()); } } From a8374f0211f6eb59ce90ad79d8dd4b4baa6f8231 Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Tue, 11 Jun 2024 16:09:17 -0700 Subject: [PATCH 8/9] [TM-754] Lint fix --- .../V2/TreeSpecies/GetTreeSpeciesForEntityController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php b/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php index 9fa869b38..0811786c3 100644 --- a/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php +++ b/app/Http/Controllers/V2/TreeSpecies/GetTreeSpeciesForEntityController.php @@ -7,8 +7,6 @@ use App\Models\V2\EntityModel; use App\Models\V2\TreeSpecies\TreeSpecies; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Log; -use Spatie\QueryBuilder\QueryBuilder; class GetTreeSpeciesForEntityController extends Controller { From 91884d3c2c4884dd6e4d4335c0bb5f1bf07fab7c Mon Sep 17 00:00:00 2001 From: cesarLima1 Date: Thu, 13 Jun 2024 15:24:02 -0400 Subject: [PATCH 9/9] [TM-961] verify existence of workday_demographics table in migration --- ..._19_225021_create_workday_demographics.php | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) 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(); + }); + } } /**