68 lines
1.8 KiB
PHP
68 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\Customer;
|
|
use LivewireUI\Modal\ModalComponent;
|
|
|
|
/**
|
|
* Close the whole CluPilot account (R5). Guarded: only allowed when no active
|
|
* package remains — an active subscription must be cancelled first. Sets
|
|
* closed_at; financial records are retained. Requires typing "LOESCHEN".
|
|
*/
|
|
class ConfirmCloseAccount extends ModalComponent
|
|
{
|
|
public string $confirmWord = '';
|
|
|
|
public function closeAccount()
|
|
{
|
|
$customer = $this->customer();
|
|
if ($customer === null) {
|
|
return $this->redirectRoute('settings', navigate: true);
|
|
}
|
|
|
|
if ($this->hasActivePackage($customer)) {
|
|
$this->addError('confirmWord', __('settings.close_blocked'));
|
|
|
|
return;
|
|
}
|
|
|
|
if (mb_strtoupper(trim($this->confirmWord)) !== __('settings.close_keyword')) {
|
|
$this->addError('confirmWord', __('settings.close_mismatch'));
|
|
|
|
return;
|
|
}
|
|
|
|
$customer->update(['closed_at' => now(), 'status' => 'closed']);
|
|
|
|
return $this->redirectRoute('settings', navigate: true);
|
|
}
|
|
|
|
private function hasActivePackage(Customer $customer): bool
|
|
{
|
|
return $customer->instances()
|
|
->whereNotIn('status', ['cancelled', 'deprovisioned'])
|
|
->exists();
|
|
}
|
|
|
|
private function customer(): ?Customer
|
|
{
|
|
$user = auth()->user();
|
|
if (! $user) {
|
|
return null;
|
|
}
|
|
|
|
return Customer::query()->where('user_id', $user->id)->first()
|
|
?? Customer::query()->where('email', $user->email)->first();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$customer = $this->customer();
|
|
|
|
return view('livewire.confirm-close-account', [
|
|
'blocked' => $customer !== null && $this->hasActivePackage($customer),
|
|
]);
|
|
}
|
|
}
|