-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51d2de4
commit cbb762f
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/Filament/Resources/Customers/UserResource/Actions/TransferPurchaseToUserAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\Customers\UserResource\Actions; | ||
|
||
use App\Domain\Shop\Models\Purchase; | ||
use App\Models\User; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Notifications\Notification; | ||
use Filament\Tables\Actions\Action; | ||
use Laravel\Paddle\Receipt; | ||
|
||
class TransferPurchaseToUserAction | ||
{ | ||
public static function make() | ||
{ | ||
return Action::make('transfer_purchases_to_user') | ||
->icon('heroicon-o-user-plus') | ||
->form([ | ||
TextInput::make('email') | ||
->label('Email') | ||
->required() | ||
->email(), | ||
]) | ||
->action(function (array $data, User $record): void { | ||
$otherUser = User::where('email', $data['email'])->first(); | ||
$user = $record; | ||
|
||
if (! $otherUser) { | ||
Notification::make() | ||
->title("No user found with email {$data['email']}") | ||
->danger() | ||
->send(); | ||
} | ||
|
||
$user->purchases->each(function (Purchase $purchase) use ($otherUser) { | ||
$purchase->update(['user_id' => $otherUser->id]); | ||
}); | ||
|
||
$user->receipts->each(function (Receipt $receipt) use ($otherUser) { | ||
$receipt->update(['billable_id' => $otherUser->id]); | ||
}); | ||
|
||
Notification::make() | ||
->title("All purchases transfered to {$otherUser->name} ({$otherUser->email})!") | ||
->success() | ||
->send(); | ||
}); | ||
} | ||
} |