From a2db94a1db8a1517e3643e92c5542cfd6db7f54a Mon Sep 17 00:00:00 2001 From: alexPopaCode4 <156655986+alexPopaCode4@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:09:34 +0200 Subject: [PATCH] export projects (#322) * export projects * clean code --- .../ProjectResource/Actions/ExportAction.php | 152 ++++++++++++++++++ .../Widgets/BaseProjectWidget.php | 9 +- .../UserResource/Actions/ExportAction.php | 6 - lang/ro/donation.php | 2 +- 4 files changed, 161 insertions(+), 8 deletions(-) create mode 100644 app/Filament/Resources/ProjectResource/Actions/ExportAction.php diff --git a/app/Filament/Resources/ProjectResource/Actions/ExportAction.php b/app/Filament/Resources/ProjectResource/Actions/ExportAction.php new file mode 100644 index 00000000..153ccc8e --- /dev/null +++ b/app/Filament/Resources/ProjectResource/Actions/ExportAction.php @@ -0,0 +1,152 @@ +color('secondary'); + + $this->exports([ + ExcelExport::make() + ->withFilename(fn () => sprintf( + '%s-%s', + now()->format('Y_m_d-H_i_s'), + Str::slug(ProjectResource::getPluralModelLabel()), + )) + ->modifyQueryUsing(function (Builder $query) { + if ($this->name == 'approved_project') { + $query->whereIsApproved(); + } + + if ($this->name == 'new_project') { + $query->whereIsPending(); + } + + if ($this->name == 'pending_changes') { + $query->withCount([ + 'activities' => fn (Builder $query) => $query->wherePending(), + ]) + ->whereHas('activities', function (Builder $query) { + $query->wherePending(); + }) + ->addSelect([ + 'latest_updated_at' => Activity::query() + ->withoutGlobalScopes() + ->wherePending() + ->select('created_at') + ->whereColumn('subject_id', 'projects.id') + ->whereMorphedTo('subject', Organization::class) + ->latest() + ->take(1), + ]); + } + + if ($this->name == 'rejected_project') { + $query->whereIsRejected(); + } + return $query; + }) + ->withColumns([ + Column::make('name') + ->heading(__('project.labels.name')), + + Column::make('status') + ->heading(__('project.labels.status')), + + Column::make('organization') + ->heading(__('project.labels.organization')) + ->formatStateUsing( + fn (Project $record, $state) => $state->name + ), + + Column::make('start') + ->heading(__('project.labels.start')) + ->formatStateUsing( + fn (Project $record, $state) => $state?->toFormattedDateTime() + ), + + Column::make('end') + ->heading(__('project.labels.end')) + ->formatStateUsing( + fn (Project $record, $state) => $state?->toFormattedDateTime() + ), + + Column::make('counties') + ->heading(__('project.labels.counties')) + ->formatStateUsing( + fn (Project $record) => + $record->counties->map(fn (County $county) => $county->name) + ->join(', ') + ), + + Column::make('category') + ->heading(__('project.labels.category')) + ->formatStateUsing( + fn (Project $record, $state) => + $record->categories->map(fn (ProjectCategory $category) => $category->name) + ->join(', ') + ), + + Column::make('description') + ->heading(__('project.labels.description')), + + Column::make('scope') + ->heading(__('project.labels.scope')), + + Column::make('target_budget') + ->heading(__('project.labels.target_budget')), + + Column::make('beneficiaries') + ->heading(__('project.labels.beneficiaries')), + + Column::make('reason_to_donate') + ->heading(__('project.labels.reason_to_donate')), + + Column::make('accepting_volunteers') + ->heading(__('project.labels.accepting_volunteers')) + ->formatStateUsing(fn (Project $record, $state) => $state ? __('field.boolean.true') : __('field.boolean.false')), + + Column::make('accepting_comments') + ->heading(__('project.labels.accepting_comments')) + ->formatStateUsing(fn (Project $record, $state) => $state ? __('field.boolean.true') : __('field.boolean.false')), + + Column::make('donation_number') + ->heading(__('donation.labels.count')) + ->formatStateUsing( + fn (Project $record) => + $record->donations->count() + ), + + Column::make('donation_amount') + ->heading(__('donation.labels.amount')) + ->formatStateUsing( + fn (Project $record) => + $record->donations + ->map(fn (Donation $donation) => $donation->amount) + ->sum() + ), + ]), + ]); + } +} diff --git a/app/Filament/Resources/ProjectResource/Widgets/BaseProjectWidget.php b/app/Filament/Resources/ProjectResource/Widgets/BaseProjectWidget.php index 0631dc65..d057da44 100644 --- a/app/Filament/Resources/ProjectResource/Widgets/BaseProjectWidget.php +++ b/app/Filament/Resources/ProjectResource/Widgets/BaseProjectWidget.php @@ -14,7 +14,7 @@ class BaseProjectWidget extends BaseWidget { -// protected static string $view = 'filament.resources.project-resource.widgets.new-project'; + // protected static string $view = 'filament.resources.project-resource.widgets.new-project'; protected static ?int $sort = 1; /** @var string */ @@ -110,4 +110,11 @@ protected function paginateTableQuery(Builder $query): Paginator $this->getTablePaginationPageName(), ); } + + protected function getTableHeaderActions(): array + { + return [ + ProjectResource\Actions\ExportAction::make($this->getTableQueryStringIdentifier()) + ]; + } } diff --git a/app/Filament/Resources/UserResource/Actions/ExportAction.php b/app/Filament/Resources/UserResource/Actions/ExportAction.php index cfdcc12e..471b776b 100644 --- a/app/Filament/Resources/UserResource/Actions/ExportAction.php +++ b/app/Filament/Resources/UserResource/Actions/ExportAction.php @@ -98,12 +98,6 @@ protected function setUp(): void $record->donations ->reject(fn ($item) => $item->status === EuPlatescStatus::CHARGED) ->last()?->created_at->toFormattedDateTime() : '' - // Donation::query() - // ->where('user_id', $record->id) - // ->orderByDesc('created_at') - // ->first('created_at') - // ->created_at: - // '' ), ]), diff --git a/lang/ro/donation.php b/lang/ro/donation.php index 5f22652b..6c513942 100644 --- a/lang/ro/donation.php +++ b/lang/ro/donation.php @@ -31,7 +31,7 @@ 'full_name' => 'Donator', 'email' => 'Email', 'charge_date' => 'Data încasării', - + 'count' => 'Numar donatii', ], 'statuses' => [