Skip to content

Commit

Permalink
fix donations in admin fix show project page
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Oct 20, 2023
1 parent 7f04d80 commit eb1dae3
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 55 deletions.
22 changes: 19 additions & 3 deletions app/Enums/EuPlatescStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@

namespace App\Enums;

use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use App\Concerns\Enums\HasLabel;

enum EuPlatescStatus: string
{
case padding = 'padding';
case in_progress = 'in-progress';
case succeeded = 'succeeded';
use Arrayable;
use HasLabel;
case INITIALIZE = 'initialize';
case AUTHORIZED = 'authorized';
case UNAUTHORIZED = 'unauthorized';
case CANCELED = 'canceled';
case ABORTED = 'aborted';
case PAYMENT_DECLINED = 'payment_declined';
case POSSIBLE_FRAUD = 'possible_fraud';
case CHARGED = 'charged';

public function labelKeyPrefix(): string
{
return 'donation.statuses';
}
}
148 changes: 136 additions & 12 deletions app/Filament/Resources/DonationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,155 @@

namespace App\Filament\Resources;

use App\Enums\EuPlatescStatus;
use App\Filament\Resources\DonationResource\Pages;
use App\Forms\Components\Link;
use App\Models\Donation;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;
use Str;

class DonationResource extends Resource
{
protected static ?string $model = Donation::class;

protected static ?string $navigationGroup = 'Administrează';

protected static ?int $navigationSort = 4;

protected static ?string $navigationIcon = 'heroicon-o-collection';

public static function getPluralLabel(): ?string
{
return __('donation.label.plural');
}

public static function getModelLabel(): string
{
return __('donation.label.singular');
}

protected static function getNavigationGroup(): ?string
{
return __('navigation.group.manage');
}

public static function form(Form $form): Form
{
return $form
->schema([
//
Link::make('organizatii')->type('organization')
->label(__('donation.labels.organization')),

Link::make('project')->type('project')
->label(__('donation.labels.project')),

TextInput::make('full_name')
->label(__('donation.labels.full_name'))
->formatStateUsing(fn (Donation $record) => $record->full_name),

TextInput::make('email')->email()
->label(__('donation.labels.email')),

TextInput::make('amount')
->label(__('donation.labels.amount')),

TextInput::make('created_at')
->label(__('donation.labels.created_at')),

TextInput::make('charge_date')
->label(__('donation.labels.created_at')),

TextInput::make('status')
->label(__('donation.labels.status'))
->formatStateUsing(fn (Donation $record) => __($record->status->label())),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
//
TextColumn::make('id')
->formatStateUsing(function (Donation $record) {
return sprintf('#%d', $record->id);
})
->label(__('donation.labels.id'))
->sortable(),
TextColumn::make('organization.name')
->label(__('donation.labels.organization'))
->formatStateUsing(fn (Donation $record) => self::getOrganizationLink($record))
->searchable()
->sortable(),
TextColumn::make('project.name')
->label(__('donation.labels.project'))
->formatStateUsing(fn (Donation $record) => self::getProjectLink($record))
->searchable()
->sortable(),
TextColumn::make('full_name')
->label(__('donation.labels.full_name')),
TextColumn::make('email')
->searchable(),
TextColumn::make('amount')
->label(__('donation.labels.amount'))
->searchable()
->sortable(),
TextColumn::make('created_at')
->formatStateUsing(fn (Donation $record) => $record->created_at->format('Y-m-d H:i'))
->label(__('donation.labels.created_at'))
->searchable()
->sortable(),
TextColumn::make('created_at')
->formatStateUsing(fn (Donation $record) => $record->created_at->format('Y-m-d H:i'))
->label(__('donation.labels.created_at'))
->searchable()
->sortable(),
TextColumn::make('charge_date')
->formatStateUsing(fn (Donation $record) => $record->charge_date?->format('Y-m-d H:i'))
->label(__('donation.labels.charge_date'))
->searchable()
->sortable(),
TextColumn::make('status')
->label(__('donation.labels.organization'))
->formatStateUsing(fn (Donation $record) => __($record->status->label()))
->searchable()
->sortable(),
])
->filters([
//
SelectFilter::make('status')
->label(__('donation.labels.status'))
->options(EuPlatescStatus::options())
->multiple(),
SelectFilter::make('organization_id')
->label(__('donation.labels.organization'))
->relationship('organization', 'name')
->multiple(),
SelectFilter::make('project_id')
->label(__('donation.labels.project'))
->relationship('project', 'name')
->multiple(),
TernaryFilter::make('has_user')
->label(__('donation.labels.has_user'))
->queries(
true: fn (Builder $query) => $query->whereNotNull('user_id'),
false: fn (Builder $query) => $query->whereNull('user_id'),
),
TernaryFilter::make('in_championship')
->label(__('donation.labels.in_championship'))
->queries(
true: fn (Builder $query) => $query->whereHas('championshipStage'),
false: fn (Builder $query) => $query->whereDoesntHave('championshipStage'),
),

])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
ViewAction::make(),
]);
}

Expand All @@ -57,8 +167,22 @@ public static function getPages(): array
{
return [
'index' => Pages\ListDonations::route('/'),
'create' => Pages\CreateDonation::route('/create'),
'edit' => Pages\EditDonation::route('/{record}/edit'),
];
}

private static function getProjectLink(Donation $record): HtmlString
{
$url = route('filament.resources.projects.view', $record->project->id);
$name = \Illuminate\Support\Str::words($record->project->name, 3, '...');

return new HtmlString(sprintf('<a href="%s">%s</a>', $url, $name));
}

private static function getOrganizationLink(Donation $record)
{
$url = route('filament.resources.organizations.view', $record->organization->id);
$name = Str::words($record->organization->name, 3, '...');

return new HtmlString(sprintf('<a href="%s">%s</a>', $url, $name));
}
}
13 changes: 0 additions & 13 deletions app/Filament/Resources/DonationResource/Pages/CreateDonation.php

This file was deleted.

21 changes: 0 additions & 21 deletions app/Filament/Resources/DonationResource/Pages/EditDonation.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Filament/Resources/VolunteerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function table(Table $table): Table
->formatStateUsing(function (VolunteerRequest $record) {
return sprintf('#%d', $record->id);
})
->label(__('project.labels.id'))
->label(__('volunteer.column.id'))
->sortable(),
TextColumn::make('volunteer.name')
->label(__('volunteer.column.name'))
Expand Down
13 changes: 11 additions & 2 deletions app/Forms/Components/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Forms\Components;

use App\Filament\Resources\OrganizationResource;
use App\Filament\Resources\ProjectResource;
use App\Filament\Resources\UserResource;
use App\Models\User;
use Filament\Forms\Components\Field;
Expand All @@ -31,6 +32,11 @@ public function getOrganization(): Collection
return collect([])->push(['name' => $this->getRecord()->organization->name, 'url' => OrganizationResource::getUrl('view', $this->getRecord()->organization)]);
}

public function getProject(): Collection
{
return collect([])->push(['name' => $this->getRecord()->project->name, 'url' => ProjectResource::getUrl('view', $this->getRecord()->project)]);
}

public function type(string $type): self
{
$this->type = $type;
Expand All @@ -48,7 +54,10 @@ public function getLinks(): Collection
if ($this->type === 'user') {
return $this->getUsers();
}

return $this->getOrganization();
return match ($this->type) {
'user' => $this->getUsers(),
'organization' => $this->getOrganization(),
'project' => $this->getProject(),
};
}
}
12 changes: 12 additions & 0 deletions app/Models/Donation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Models;

use App\Concerns\HasUuid;
use App\Enums\EuPlatescStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -31,6 +32,12 @@ class Donation extends Model
'charge_date',
'updated_without_correct_e_pid',
];
protected $casts = [
'status' => EuPlatescStatus::class,
'updated_without_correct_e_pid' => 'boolean',
'approval_date' => 'datetime',
'charge_date' => 'datetime',
];

public function project(): BelongsTo
{
Expand All @@ -51,4 +58,9 @@ public function championshipStage(): BelongsTo
{
return $this->belongsTo(ChampionshipStage::class);
}

public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
}
3 changes: 3 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ public function getEmbeddedVideosAttribute(): array
{
$videos = $this->videos;
$embeddedVideos = [];
if (empty($videos)) {
return [];
}
foreach ($videos as $video) {
$embeddedUrl = rescue(fn () => (new Embed())->get($video['url'])->code, '');
$embeddedVideos[] = $embeddedUrl;
Expand Down
9 changes: 8 additions & 1 deletion database/factories/DonationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Database\Factories;

use App\Enums\EuPlatescStatus;
use App\Models\Organization;
use App\Models\Project;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

Expand All @@ -22,6 +24,10 @@ class DonationFactory extends Factory
public function definition(): array
{
$amount = fake()->numberBetween(20, 10_000);
$start = CarbonImmutable::createFromInterface(
fake()->dateTimeBetween('-1 days', 'today')
);


return [
'organization_id' => Organization::factory(),
Expand All @@ -32,7 +38,8 @@ public function definition(): array
'first_name' => fake()->firstName(),
'last_name' => fake()->lastName(),
'email' => fake()->safeEmail(),
'status' => 'TBD',
'status' => fake()->randomElement(EuPlatescStatus::values()),
'charge_date' => $start,
'updated_without_correct_e_pid' => fake()->boolean(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function up(): void
$table->string('status');
$table->string('card_status')->nullable();
$table->string('card_holder_status_message')->nullable();
$table->timestamp('approval_date')->nullable();
$table->timestamp('charge_date')->nullable();
$table->dateTime('approval_date')->nullable();
$table->dateTime('charge_date')->nullable();
$table->boolean('updated_without_correct_e_pid');
$table->timestamps();
});
Expand Down
Loading

0 comments on commit eb1dae3

Please sign in to comment.