Skip to content

Commit

Permalink
Merge pull request #242 from wri/release/horrible-hackberry
Browse files Browse the repository at this point in the history
[RELEASE] Horrible Hackberry
  • Loading branch information
cesarLima1 authored May 30, 2024
2 parents 62419c1 + 72a3a99 commit ece7684
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 35 deletions.
35 changes: 27 additions & 8 deletions app/Exports/V2/EntityExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\V2\Forms\Form;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
Expand Down Expand Up @@ -69,21 +70,34 @@ protected function getAttachedMappedForEntity($entity): array
$mapped = [
$entity->ppc_external_id ?? $entity->old_id ?? $entity->id ?? null,
$entity->uuid,
];

if (in_array($this->form->type, ['site', 'nursery', 'site-report', 'nursery-report'])) {
$frontEndUrl = config('app.front_end');
// Our environment variable definitions are inconsistent.
if (! Str::endsWith($frontEndUrl, '/')) {
$frontEndUrl .= '/';
}
$mapped[] = $frontEndUrl . 'admin#/' . Str::camel($entity->shortName) . '/' . $entity->uuid . '/show';
}

$mapped = array_merge($mapped, [
$organisation->readable_type ?? null,
$organisation->name ?? null,
$entity->project->name ?? null,
$entity->status ?? null,
$entity->due_at ?? null,
];
]);

if (in_array($this->form->type, ['nursery', 'nursery-report','site', 'site-report', 'project-report'])) {
if (in_array($this->form->type, ['nursery', 'nursery-report', 'site', 'site-report', 'project-report'])) {
$mapped[] = $entity->project->ppc_external_id ?? $entity->project->id ?? null;
}

if ($this->form->type === 'project-report') {
$mapped[] = $entity->project->uuid ?? null;
if($this->form->framework_key === 'ppc') {
$mapped[] = $entity->seedlings_grown ?? null;
$mapped[] = $entity->seedlings_grown_to_date ?? null;
}
}
if ($this->form->type === 'nursery-report') {
Expand All @@ -94,8 +108,8 @@ protected function getAttachedMappedForEntity($entity): array
if ($this->form->type === 'site-report') {
$mapped[] = $entity->site->ppc_external_id ?? $entity->site->id ?? null;
$mapped[] = $entity->site->name ?? null;
$sumTreeSPecies = $entity->treeSpecies()->sum('amount');
$mapped[] = $sumTreeSPecies > 0 ? $sumTreeSPecies : null;
$sumTreeSpecies = $entity->treeSpecies()->sum('amount');
$mapped[] = $sumTreeSpecies > 0 ? $sumTreeSpecies : null;
$mapped[] = $entity->site->trees_planted_count ?? null;
if($this->form->framework_key === 'ppc') {
$sumSeeding = $entity->seedings()->sum('amount');
Expand All @@ -109,15 +123,19 @@ protected function getAttachedMappedForEntity($entity): array

protected function getAttachedHeadingsForEntity(): array
{
$initialHeadings = [
'id',
'uuid',
$initialHeadings = ['id', 'uuid'];

if (in_array($this->form->type, ['site', 'nursery', 'site-report', 'nursery-report'])) {
$initialHeadings[] = 'link_to_terramatch';
}

$initialHeadings = array_merge($initialHeadings, [
'organization-readable_type',
'organization-name',
'project_name',
'status',
'due_date',
];
]);

if (in_array($this->form->type, ['nursery', 'nursery-report','site', 'site-report', 'project-report'])) {
$initialHeadings[] = 'project-id';
Expand All @@ -126,6 +144,7 @@ protected function getAttachedHeadingsForEntity(): array
if ($this->form->type === 'project-report') {
$initialHeadings[] = 'project_uuid';
if($this->form->framework_key === 'ppc') {
$initialHeadings[] = 'total_seedlings_grown_report';
$initialHeadings[] = 'total_seedlings_grown';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(Request $request): NurseriesCollection
AllowedFilter::scope('country'),
AllowedFilter::scope('organisation_uuid', 'organisationUuid'),
AllowedFilter::scope('project_uuid', 'projectUuid'),
AllowedFilter::exact('framework', 'framework_key'),
AllowedFilter::exact('framework_key'),
AllowedFilter::exact('status'),
AllowedFilter::exact('update_request_status'),
]);
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Requests/V2/Forms/StoreFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function rules()

'form_sections.*.form_questions' => ['sometimes', 'array'],
'form_sections.*.form_questions.*.linked_field_key' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.collection' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.label' => ['required', 'string'],
'form_sections.*.form_questions.*.input_type' => ['sometimes', 'nullable'],
'form_sections.*.form_questions.*.name' => ['sometimes', 'nullable'],
Expand All @@ -58,6 +59,7 @@ public function rules()

'form_sections.*.form_questions.*.child_form_questions' => ['sometimes'],
'form_sections.*.form_questions.*.child_form_questions.*.linked_field_key' => ['sometimes', 'required', 'string'],
'form_sections.*.form_questions.*.child_form_questions.*.collection' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.child_form_questions.*.label' => ['sometimes', 'required', 'string'],
'form_sections.*.form_questions.*.child_form_questions.*.name' => ['sometimes', 'nullable'],
'form_sections.*.form_questions.*.child_form_questions.*.input_type' => ['sometimes', 'nullable'],
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Requests/V2/Forms/UpdateFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function rules()

'form_sections.*.form_questions' => ['sometimes', 'nullable', 'array'],
'form_sections.*.form_questions.*.linked_field_key' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.collection' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.label' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.uuid' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.name' => ['sometimes', 'nullable'],
Expand Down Expand Up @@ -63,6 +64,7 @@ public function rules()
'form_sections.*.form_questions.*.child_form_questions' => ['sometimes', 'nullable'],
'form_sections.*.form_questions.*.child_form_questions.*.uuid' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.child_form_questions.*.linked_field_key' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.child_form_questions.*.collection' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.child_form_questions.*.label' => ['sometimes', 'nullable', 'string'],
'form_sections.*.form_questions.*.child_form_questions.*.name' => ['sometimes', 'nullable'],
'form_sections.*.form_questions.*.child_form_questions.*.input_type' => ['sometimes', 'nullable'],
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Resources/V2/Projects/ProjectResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function toArray($request)
'seeds_planted_count' => $this->seeds_planted_count,
'regenerated_trees_count' => $this->regenerated_trees_count,
'workday_count' => $this->workday_count,
// Temporary until we have bulk import completed.
'self_reported_workday_count' => $this->self_reported_workday_count,
'total_jobs_created' => $this->total_jobs_created,
'total_sites' => $this->total_sites,
'total_nurseries' => $this->total_nurseries,
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Resources/V2/Sites/SiteResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function toArray($request)
'survival_rate_planted' => $this->survival_rate_planted,
'direct_seeding_survival_rate' => $this->direct_seeding_survival_rate,
'a_nat_regeneration_trees_per_hectare' => $this->a_nat_regeneration_trees_per_hectare,
'a_nat_regeneration' => $this->a_nat_regeneration,
'a_nat_regeneration' => $this->a_nat_regeneration > 1 ? intval(round($this->a_nat_regeneration)) : $this->a_nat_regeneration,
'hectares_to_restore_goal' => $this->hectares_to_restore_goal,
'landscape_community_contribution' => $this->landscape_community_contribution,
'planting_pattern' => $this->planting_pattern,
Expand All @@ -48,6 +48,8 @@ public function toArray($request)
'site_reports_total' => $this->site_reports_total,
'overdue_site_reports_total' => $this->overdue_site_reports_total,
'workday_count' => $this->workday_count,
// Temporary until we have bulk import completed.
'self_reported_workday_count' => $this->self_reported_workday_count,
'trees_planted_count' => $this->trees_planted_count,
'regenerated_trees_count' => $this->regenerated_trees_count,
'migrated' => ! empty($this->old_model),
Expand Down
12 changes: 6 additions & 6 deletions app/Mail/FormSubmissionRejected.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class FormSubmissionRejected extends Mail
{
public function __construct(String $feedback = null)
{
$this->subject = 'Application Rejected';
$this->title = 'Your application has been rejected';
$this->body =
'Your application has been rejected.';
$this->subject = 'Application Status Update';
$this->title = 'THANK YOU FOR YOUR APPLICATION';
$this->body = 'After careful review, our team has decided your application will not move forward.';
if (! is_null($feedback)) {
$this->body = 'Your application has been rejected. Please see comments below:<br><br>' .
e($feedback);
$this->body .=
' Please see the comments below for more details or any follow-up resources.<br><br>' .
e($feedback);
}
$this->transactional = true;
}
Expand Down
17 changes: 17 additions & 0 deletions app/Models/V2/Projects/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use App\Models\V2\Sites\SiteReport;
use App\Models\V2\Tasks\Task;
use App\Models\V2\TreeSpecies\TreeSpecies;
use App\Models\V2\Workdays\Workday;
use App\Models\V2\Workdays\WorkdayDemographic;
use App\StateMachines\EntityStatusStateMachine;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -357,6 +359,21 @@ public function getRegeneratedTreesCountAttribute(): int
}

public function getWorkdayCountAttribute(): int
{
return WorkdayDemographic::whereIn(
'workday_id',
Workday::where('workdayable_type', SiteReport::class)
->whereIn('workdayable_id', $this->submittedSiteReports()->select('v2_site_reports.id'))
->select('id')
)->orWhereIn(
'workday_id',
Workday::where('workdayable_type', ProjectReport::class)
->whereIn('workdayable_id', $this->reports()->hasBeenSubmitted()->select('id'))
->select('id')
)->gender()->sum('amount') ?? 0;
}

public function getSelfReportedWorkdayCountAttribute(): int
{
$sumQueries = [
DB::raw('sum(`workdays_paid`) as paid'),
Expand Down
36 changes: 18 additions & 18 deletions app/Models/V2/Projects/ProjectReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use App\Models\Traits\HasWorkdays;
use App\Models\Traits\UsesLinkedFields;
use App\Models\V2\MediaModel;
use App\Models\V2\Nurseries\Nursery;
use App\Models\V2\Nurseries\NurseryReport;
use App\Models\V2\Organisation;
use App\Models\V2\Polygon;
use App\Models\V2\ReportModel;
Expand Down Expand Up @@ -290,30 +288,32 @@ public function getReportTitleAttribute(): string
public function getSeedlingsGrownAttribute(): int
{
if ($this->framework_key == 'ppc') {
return $this->treeSpecies()
->sum('amount');
return $this->treeSpecies()->sum('amount');
}

if ($this->framework_key == 'terrafund') {
if (empty($this->due_at)) {
if (empty($this->task_id)) {
return 0;
}

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

if (count($nurseryIds) > 0) {
return NurseryReport::whereIn('nursery_id', $nurseryIds)
->whereMonth('due_at', $month)
->whereYear('due_at', $year)
->sum('seedlings_young_trees');
}
return $this->task->nurseryReports()->sum('seedlings_young_trees');
}

return 0;
}

public function getSeedlingsGrownToDateAttribute(): int
{
if ($this->framework_key == 'ppc') {
return TreeSpecies::where('speciesable_type', ProjectReport::class)
->whereIn(
'speciesable_id',
$this->project->reports()->where('created_at', '<=', $this->created_at)->select('id')
)
->sum('amount');
}

// this attribute is currently only used for PPC report exports.
return 0;
}

Expand Down
12 changes: 12 additions & 0 deletions app/Models/V2/Sites/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use App\Models\V2\Seeding;
use App\Models\V2\Stratas\Strata;
use App\Models\V2\TreeSpecies\TreeSpecies;
use App\Models\V2\Workdays\Workday;
use App\Models\V2\Workdays\WorkdayDemographic;
use App\StateMachines\ReportStatusStateMachine;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -301,6 +303,16 @@ public function getRegeneratedTreesCountAttribute(): int
}

public function getWorkdayCountAttribute(): int
{
return WorkdayDemographic::whereIn(
'workday_id',
Workday::where('workdayable_type', SiteReport::class)
->whereIn('workdayable_id', $this->reports()->hasBeenSubmitted()->select('id'))
->select('id')
)->gender()->sum('amount') ?? 0;
}

public function getSelfReportedWorkdayCountAttribute(): int
{
$totals = $this->reports()->hasBeenSubmitted()->get([
DB::raw('sum(`workdays_volunteer`) as volunteer'),
Expand Down
12 changes: 12 additions & 0 deletions database/factories/V2/Workdays/WorkdayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories\V2\Workdays;

use App\Models\V2\Projects\ProjectReport;
use App\Models\V2\Sites\SiteReport;
use App\Models\V2\Workdays\Workday;
use Illuminate\Database\Eloquent\Factories\Factory;
Expand All @@ -22,4 +23,15 @@ public function definition()
'collection' => $this->faker->randomElement(array_keys(Workday::SITE_COLLECTIONS)),
];
}

public function projectReport(): Factory
{
return $this->state(function (array $attributes) {
return [
'workdayable_type' => ProjectReport::class,
'workdayable_id' => ProjectReport::factory()->create(),
'collection' => $this->faker->randomElement(array_keys(Workday::PROJECT_COLLECTION)),
];
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::dropIfExists('point_geometry');
Schema::create('point_geometry', function (Blueprint $table) {
$table->id();
$table->uuid('uuid')->unique();
$table->geometry('geom')->nullable();
$table->decimal('est_area', 15, 2)->nullable();
$table->string('created_by')->nullable();
$table->string('last_modified_by')->nullable();
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('point_geometry');
}
};
27 changes: 27 additions & 0 deletions database/migrations/2024_05_21_193901_float_regeneration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('v2_sites', function (Blueprint $table) {
$table->float('a_nat_regeneration')->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('v2_sites', function (Blueprint $table) {
$table->unsignedInteger('a_nat_regeneration')->change();
});
}
};
Loading

0 comments on commit ece7684

Please sign in to comment.