Skip to content

Commit

Permalink
export projects (#322)
Browse files Browse the repository at this point in the history
* export projects

* clean code
  • Loading branch information
alexPopaCode4 authored Jan 29, 2024
1 parent 3ef6934 commit a2db94a
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 8 deletions.
152 changes: 152 additions & 0 deletions app/Filament/Resources/ProjectResource/Actions/ExportAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\ProjectResource\Actions;

use App\Filament\Resources\ProjectResource;
use App\Models\Activity;
use App\Models\County;
use App\Models\Donation;
use App\Models\Organization;
use App\Models\Project;
use App\Models\ProjectCategory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use pxlrbt\FilamentExcel\Actions\Tables\ExportAction as BaseAction;
use pxlrbt\FilamentExcel\Columns\Column;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

class ExportAction extends BaseAction
{
protected string | null $status = null;

protected function setUp(): void
{
parent::setUp();

$this->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()
),
]),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -110,4 +110,11 @@ protected function paginateTableQuery(Builder $query): Paginator
$this->getTablePaginationPageName(),
);
}

protected function getTableHeaderActions(): array
{
return [
ProjectResource\Actions\ExportAction::make($this->getTableQueryStringIdentifier())
];
}
}
6 changes: 0 additions & 6 deletions app/Filament/Resources/UserResource/Actions/ExportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// ''
),

]),
Expand Down
2 changes: 1 addition & 1 deletion lang/ro/donation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'full_name' => 'Donator',
'email' => 'Email',
'charge_date' => 'Data încasării',

'count' => 'Numar donatii',
],

'statuses' => [
Expand Down

0 comments on commit a2db94a

Please sign in to comment.