Skip to content

Commit

Permalink
fix: ensure consistent donation status (#373)
Browse files Browse the repository at this point in the history
* fix: clarify donation stats on project page
  • Loading branch information
andreiio authored Jun 14, 2024
1 parent 252b804 commit 2c9e45a
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 35 deletions.
30 changes: 30 additions & 0 deletions app/Concerns/Enums/HasColor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Concerns\Enums;

use Illuminate\Support\Str;

trait HasColor
{
public static function colors(): array
{
return [];
}

public static function flipColors(): array
{
return collect(static::colors())
->flip()
->all();
}

public function color(): ?string
{
return collect([
static::colors()[$this->value] ?? 'bg-gray-100',
Str::slug($this->value),
])->join(' ');
}
}
9 changes: 6 additions & 3 deletions app/Console/Commands/ProcessEuPlatescTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Organization;
use App\Services\EuPlatescService;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;

Expand Down Expand Up @@ -44,8 +45,10 @@ public function handle(): void

foreach ($organization->donations as $donation) {
if ($service->recipeTransaction($donation)) {
$donation->update(['status' => EuPlatescStatus::CAPTURE]);
$donation->update(['status_updated_at' => now()]);
$donation->update([
'status' => EuPlatescStatus::CHARGED,
'status_updated_at' => now(),
]);
}
}
}
Expand All @@ -54,7 +57,7 @@ public function handle(): void
private function getOrganizationsWithOpenDonations(): Collection|array
{
return Organization::query()
->withWhereHas('donations', fn ($query) => $query->whereNotNull('ep_id')->where('donations.status', EuPlatescStatus::AUTHORIZED))
->withWhereHas('donations', fn (Builder $query) => $query->whereNotNull('ep_id')->where('donations.status', EuPlatescStatus::AUTHORIZED))
->get();
}
}
9 changes: 4 additions & 5 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
$schedule->command('model:prune')->daily();
$schedule->command('model:prune')
->daily();

$schedule->command(ProcessEuPlatescTransactions::class)
->daily()
->everyFourHours()
->timezone('Europe/Bucharest');
->everyFourHours();
}

/**
Expand Down
20 changes: 19 additions & 1 deletion app/Enums/EuPlatescStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
namespace App\Enums;

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

enum EuPlatescStatus: string
{
use Arrayable;
use Comparable;
use HasColor;
use HasLabel;

case INITIALIZE = 'initialize';
case AUTHORIZED = 'authorized';
case UNAUTHORIZED = 'unauthorized';
case CAPTURE = 'capture';
case CANCELED = 'canceled';
case ABORTED = 'aborted';
case PAYMENT_DECLINED = 'payment_declined';
Expand All @@ -25,4 +29,18 @@ public function labelKeyPrefix(): string
{
return 'donation.statuses';
}

public static function colors(): array
{
return [
'initialize' => 'bg-blue-100 text-blue-800',
'authorized' => 'bg-blue-50 text-blue-800',
'unauthorized' => 'bg-red-100 text-red-800',
'canceled' => 'bg-yellow-100 text-yellow-800',
'aborted' => 'bg-yellow-50 text-yellow-800',
'payment_declined' => 'bg-red-600 text-red-100',
'possible_fraud' => 'bg-red-700 text-red-100',
'charged' => 'bg-green-100 text-green-800',
];
}
}
8 changes: 8 additions & 0 deletions app/Filament/Resources/ProjectResource/Pages/ViewProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Filament\Resources\ProjectResource\Pages;

use App\Filament\Resources\ProjectResource;
use App\Filament\Resources\ProjectResource\Widgets\DonationsOverviewWidget;
use Filament\Resources\Pages\ViewRecord;

class ViewProject extends ViewRecord
Expand All @@ -15,4 +16,11 @@ public function hasCombinedRelationManagerTabsWithForm(): bool
{
return true;
}

protected function getHeaderWidgets(): array
{
return [
DonationsOverviewWidget::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace App\Filament\Resources\ProjectResource\RelationManagers;

use Filament\Forms;
use App\Enums\EuPlatescStatus;
use App\Filament\Forms\Components\Value;
use App\Models\Donation;
use Filament\Resources\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Support\Number;

class DonationsRelationManager extends RelationManager
{
Expand All @@ -21,45 +24,71 @@ public static function getTitle(): string
return __('donation.label.plural');
}

protected function getTableHeading(): string
{
return __(
'donation.labels.count_with_amount',
[
'count' => $this->getTableQuery()->count(),
'total' => $this->getTableQuery()->sum('amount'),
]
);
}

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('uuid')
->required()
->maxLength(255),
Value::make('full_name')
->label(__('donation.labels.full_name')),

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

Value::make('amount')
->label(__('donation.labels.amount'))
->content(fn (Donation $record) => Number::currency($record->amount, 'RON', app()->getLocale())),

Value::make('status')
->label(__('donation.labels.status')),

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

Value::make('charge_date')
->label(__('donation.labels.charge_date'))
->withTime(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('uuid'),

Tables\Columns\TextColumn::make('full_name')
->label(__('donation.labels.full_name'))
->searchable(),

Tables\Columns\TextColumn::make('amount')
->label(__('donation.labels.amount'))
->formatStateUsing(fn ($state) => Number::currency($state, 'RON', app()->getLocale()))
->sortable()
->alignRight(),

Tables\Columns\TextColumn::make('status')
->label(__('donation.labels.status'))
->formatStateUsing(fn ($state) => EuPlatescStatus::tryFrom($state)?->label()),

Tables\Columns\TextColumn::make('created_at')
->label(__('donation.labels.created_at'))
->sortable(),

Tables\Columns\TextColumn::make('charge_date')
->label(__('donation.labels.charge_date'))
->sortable(),

])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\ViewAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
])
->defaultSort('created_at', 'desc');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\ProjectResource\Widgets;

use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Card;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Number;

class DonationsOverviewWidget extends StatsOverviewWidget
{
public ?Model $record = null;

protected function getCards(): array
{
return [
Card::make(__('donation.stats.charged'), $this->record->donations()->whereCharged()->count()),
Card::make(__('donation.stats.charged_amount'), Number::currency(
$this->record->donations()->whereCharged()->sum('amount'),
'RON',
app()->getLocale()
)),
Card::make(__('donation.stats.pending'), $this->record->donations()->wherePending()->count()),
Card::make(__('donation.stats.failed'), $this->record->donations()->wherePending()->count()),
];
}
}
2 changes: 1 addition & 1 deletion app/Filament/Widgets/StatisticsDonationsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function getData(): array
]
)
->where('created_at', '>', $whereCreatedCondition)
->whereIn('status', [EuPlatescStatus::CAPTURE])
->whereIn('status', [EuPlatescStatus::CHARGED])
->groupBy($chartInterval)
->orderBy($chartInterval)
->get();
Expand Down
24 changes: 24 additions & 0 deletions app/Models/Donation.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@ public function scopeSearch(Builder $query, string $searchedText): Builder
->orWhere('email', 'LIKE', "%{$searchedText}%");
}

public function scopeWhereCharged(Builder $query): Builder
{
return $query->where('status', EuPlatescStatus::CHARGED);
}

public function scopeWherePending(Builder $query): Builder
{
return $query->whereIn('status', [
EuPlatescStatus::INITIALIZE,
EuPlatescStatus::AUTHORIZED,
]);
}

public function scopeWhereFailed(Builder $query): Builder
{
return $query->whereIn('status', [
EuPlatescStatus::UNAUTHORIZED,
EuPlatescStatus::CANCELED,
EuPlatescStatus::ABORTED,
EuPlatescStatus::PAYMENT_DECLINED,
EuPlatescStatus::POSSIBLE_FRAUD,
]);
}

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
Expand Down
2 changes: 1 addition & 1 deletion app/Services/EuPlatescService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Enums\EuPlatescStatus;
use App\Models\Donation;
use App\Models\Organization;
use Http;
use Illuminate\Support\Facades\Http;

class EuPlatescService
{
Expand Down
2 changes: 1 addition & 1 deletion app/Services/UserBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UserBadge
public function updateDonationBadge(User $user): void
{
$donationCount = $user->donations
->filter(fn (Donation $donation) => $donation->status == EuPlatescStatus::CAPTURE)
->filter(fn (Donation $donation) => EuPlatescStatus::CHARGED->is($donation->status))
->count();

$badgeRule = $this->getBadgeRuleByDonationCount($donationCount);
Expand Down
10 changes: 8 additions & 2 deletions lang/ro/donation.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@
'aborted' => 'Abandonată',
'payment_declined' => 'Plata refuzată',
'possible_fraud' => 'Posibil fraudulos',
'charged' => 'Incasată',
'capture' => 'Incasată',
'charged' => 'Încasată',
],

'header' => 'Donatii (:number)',

'stats' => [
'charged' => 'Donații încasate',
'charged_amount' => 'Sumă încasată',
'pending' => 'Donații în așteptare',
'failed' => 'Donații eșuate',
],
];

0 comments on commit 2c9e45a

Please sign in to comment.