Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Jun 14, 2024
2 parents a53d714 + df9c546 commit 490044d
Show file tree
Hide file tree
Showing 19 changed files with 219 additions and 55 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(' ');
}
}
16 changes: 11 additions & 5 deletions app/Console/Commands/ProcessEuPlatescTransactions.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Enums\EuPlatescStatus;
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;

class ProcessEuPlatescTransactions extends Command
{
Expand All @@ -31,16 +35,20 @@ public function handle(): void
{
$organizations = $this->getOrganizationsWithOpenDonations();

Log::info('Processing EuPlatesc transactions' . \count($organizations));
foreach ($organizations as $organization) {
$organizationID = $organization->id;
$service = new EuPlatescService($organizationID);
if (!$service->canCaptureTransaction()) {
if (! $service->canCaptureTransaction()) {
continue;
}

foreach ($organization->donations as $donation) {
if ($service->recipeTransaction($donation)) {
$donation->update(['status' => EuPlatescStatus::CAPTURE]);
$donation->update([
'status' => EuPlatescStatus::CHARGED,
'status_updated_at' => now(),
]);
}
}
}
Expand All @@ -49,9 +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',
];
}
}
4 changes: 4 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Exceptions;

use App\Http\Middleware\HandleInertiaRequests;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Inertia\Inertia;
use Throwable;
Expand Down Expand Up @@ -38,6 +39,9 @@ public function render($request, Throwable $e)
$response = parent::render($request, $e);

if (! app()->isLocal() && \in_array($response->status(), [401, 403, 404, 429, 500, 503])) {
// This fixes SSR errors on error pages.
Inertia::share((new HandleInertiaRequests)->share($request));

return Inertia::render('Error', [
'status' => $response->status(),
'title' => __('error.' . $response->status() . '.title'),
Expand Down
12 changes: 4 additions & 8 deletions app/Filament/Resources/DonationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,13 @@ public static function table(Table $table): Table
->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')
TextColumn::make('status_updated_at')
->formatStateUsing(fn (Donation $record) => $record->charge_date?->format('Y-m-d H:i'))
->label(__('donation.labels.charge_date'))
->label(__('donation.labels.status_updated_at'))
->searchable()
->sortable(),
TextColumn::make('status')
->label(__('donation.labels.organization'))
->label(__('donation.labels.status'))
->formatStateUsing(fn (Donation $record) => __($record->status->label()))
->searchable()
->sortable(),
Expand Down Expand Up @@ -152,6 +147,7 @@ public static function table(Table $table): Table
),
DateFilter::make('created_at'),
])
->defaultSort('created_at', 'desc')
->actions([
ViewAction::make()->iconButton(),
]);
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/EditionsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function form(Form $form): Form
->maxFiles(1)
->columnSpanFull(),

Select::make('rule_page')
Select::make('page_id')
->relationship('page', 'title')
->label(__('edition.labels.rule_page'))
->preload()
Expand Down
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
4 changes: 4 additions & 0 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function shareOnce(Request $request): array

protected function flash(Request $request): ?array
{
if (! $request->hasSession()) {
return null;
}

$type = match (true) {
$request->session()->has('error') => 'error',
$request->session()->has('success') => 'success',
Expand Down
Loading

0 comments on commit 490044d

Please sign in to comment.