Berater faellt aus (B2): pausieren nach 2 Tagen, Rueckgabe bei Zahlung, harter Cut am 28. Tag
Entwicklungsplan September, B2 (Protokoll 08.09., Rueckmeldung 14.09., R3): - Ablauftag + 2 Kalendertage (Nachtlauf): Team, Kunden, Kundenabos und Portal-Logins gehen umkehrbar an den naechsten aktiven Sponsor, jede Umhaengung in consultant_suspension_transfers protokolliert. Kurzinfo an den Sponsor ohne Listen, keine Mail an das Firmenkonto. - Zahlung bis Tag 27: Berater aktiv, Team und Kundendaten zurueck (nur was noch beim Sponsor liegt); Umsaetze der Zwischenzeit bleiben dort. - Tag 28: was noch am Berater haengt geht endgueltig an den Sponsor, Berater wird dessen Kunde (ohne Duplikat) und geloescht; Listen-Mail mit Vorname, Nachname, Telefon, E-Mail an jeden haltenden Sponsor. - Cron, Zahlungseingang und Admin-Ablaufdatum laufen ueber ConsultantSuspensionService; user:cleanup --dry-run listet beide Stufen. - Mail 35 und Dashboard nennen Pausentag und Frist; Fristen in config/mivita.php (cleanup_suspend_days, cleanup_delete_days). - Nebenbefunde: Kernel referenziert UserCleanUp, user:restore aus der User-Bereinigung laeuft nur noch einmal. - Testschema: shopping_users.auth_user_id nullable wie auf Produktion (Migration aendert nur, wo die Spalte noch NOT NULL ist). - Uebersicht fuer die Kundin: dev/_tickets/2026-09-23-mitgliedschaft- ablauf-uebersicht.md. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
03ad249eeb
commit
bb57522b70
29 changed files with 1528 additions and 193 deletions
|
|
@ -2,202 +2,100 @@
|
|||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\UserUtil;
|
||||
use App\Services\ConsultantSuspensionService;
|
||||
use App\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
/**
|
||||
* Nächtlicher Ablauf für Berater mit abgelaufener Mitgliedschaft (Entwicklungsplan B2).
|
||||
*
|
||||
* Reihenfolge wie bisher: erst der harte Cut (Ablauf + `mivita.cleanup_delete_days`),
|
||||
* dann das Pausieren (Ablauf + `mivita.cleanup_suspend_days`). Die Arbeit macht
|
||||
* {@see ConsultantSuspensionService}; dieser Command wählt nur die Kandidaten aus.
|
||||
*/
|
||||
class UserCleanUp extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'user:cleanup';
|
||||
protected $signature = 'user:cleanup
|
||||
{--dry-run : Nur auflisten, was passieren würde, nichts ändern und keine Mails senden}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'User Clean Up inactive for Business Structure and UserDetails';
|
||||
protected $description = 'Berater mit abgelaufener Mitgliedschaft pausieren (Team und Kunden an den Sponsor) bzw. nach der Frist endgültig entfernen';
|
||||
|
||||
private $timeStart;
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
public function handle(ConsultantSuspensionService $suspension): int
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
$dryRun = (bool) $this->option('dry-run');
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->info('RUN Command user:cleanup');
|
||||
\Log::channel('cleanup')->info('COMMAND [user:cleanup] started.');
|
||||
$this->info('RUN Command user:cleanup'.($dryRun ? ' (dry-run)' : ''));
|
||||
\Log::channel('cleanup')->info('COMMAND [user:cleanup] started.'.($dryRun ? ' dry-run' : ''));
|
||||
|
||||
$this->timeStart = microtime(true);
|
||||
$this->runStage(
|
||||
'Harter Cut (Ablauf vor mind. '.$suspension->deleteDays().' Tagen)',
|
||||
$suspension->terminationCandidates(),
|
||||
$suspension,
|
||||
$dryRun,
|
||||
fn (User $user) => $suspension->terminate($user),
|
||||
);
|
||||
|
||||
// Schritt 1: User löschen, die länger als 2 Monate inaktiv sind
|
||||
$this->deleteInactiveUsers();
|
||||
|
||||
// Schritt 2: Alle inaktiven User deaktivieren (länger als 2 Wochen inaktiv)
|
||||
// Ihre Downline wird dem nächsten aktiven Berater (Sponsor) zugewiesen
|
||||
$this->cleanUpInActiveUser();
|
||||
$this->runStage(
|
||||
'Pausieren (Ablauf vor mind. '.$suspension->suspendDays().' Tagen)',
|
||||
$suspension->suspensionCandidates(),
|
||||
$suspension,
|
||||
$dryRun,
|
||||
fn (User $user) => $suspension->suspend($user, 'user:cleanup'),
|
||||
);
|
||||
|
||||
\Log::channel('cleanup')->info('COMMAND [user:cleanup] finished.');
|
||||
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht User, die länger als 2 Monate inaktiv sind (payment_account < -2 month)
|
||||
* - Weist deren Vertriebspartner-Kinder dem nächsten aktiven Sponsor zu
|
||||
* - Überträgt deren Shopping-Kunden zum neuen Sponsor
|
||||
* - Konvertiert den User zu einem Shopping-Kunden
|
||||
* - Löscht den User (soft delete)
|
||||
* @param iterable<User> $candidates
|
||||
* @param callable(User): (array<string, int>|null) $action
|
||||
*/
|
||||
private function deleteInactiveUsers()
|
||||
private function runStage(string $label, iterable $candidates, ConsultantSuspensionService $suspension, bool $dryRun, callable $action): void
|
||||
{
|
||||
$methodStartTime = microtime(true);
|
||||
$this->info('START Command deleteInactiveUsers');
|
||||
$count = 0;
|
||||
$rows = [];
|
||||
$done = 0;
|
||||
$failed = 0;
|
||||
|
||||
$date = Carbon::now()->modify('-2 month');
|
||||
$delete_users = User::where('admin', 0)->where('payment_account', '<', $date)->get();
|
||||
foreach ($candidates as $user) {
|
||||
$counts = $dryRun ? $suspension->preview($user) : null;
|
||||
|
||||
foreach ($delete_users as $delete_user) {
|
||||
\DB::beginTransaction();
|
||||
|
||||
try {
|
||||
// Finde nächsten aktiven Sponsor
|
||||
$active_sponsor = UserUtil::findNextActiveSponsor($delete_user->id);
|
||||
|
||||
if (! $active_sponsor) {
|
||||
\Log::channel('cleanup')->error('deleteInactiveUsers find no active_sponsor by delete_user_id: '.$delete_user->id);
|
||||
\DB::rollBack();
|
||||
|
||||
continue;
|
||||
if (! $dryRun) {
|
||||
try {
|
||||
$counts = $action($user);
|
||||
} catch (\Throwable $e) {
|
||||
\Log::channel('cleanup')->error('user:cleanup '.$label.' fehlgeschlagen fuer user_id '.$user->id.': '.$e->getMessage());
|
||||
$counts = null;
|
||||
}
|
||||
|
||||
// Prüfe ob User Account-Daten hat
|
||||
if (! $delete_user->account) {
|
||||
\Log::channel('cleanup')->error('deleteInactiveUsers: User has no account data, skipping user_id: '.$delete_user->id);
|
||||
\DB::rollBack();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Setze alle Vertriebspartner-Kinder zum neuen Sponsor
|
||||
UserUtil::setNewSponsorToChilds($delete_user->id, $active_sponsor->id);
|
||||
|
||||
// Übertrage Shopping-User zum neuen Sponsor
|
||||
UserUtil::setShoppingUserToNewMember($delete_user->id, $active_sponsor->id);
|
||||
|
||||
// Konvertiere User zu Client beim neuen Sponsor
|
||||
UserUtil::setUserToClient($delete_user->id, $active_sponsor->id);
|
||||
|
||||
$data = [
|
||||
'user_id' => $delete_user->id,
|
||||
'email' => $delete_user->email,
|
||||
'm_account' => $delete_user->account->m_account,
|
||||
'm_first_name' => $delete_user->account->m_first_name,
|
||||
'm_last_name' => $delete_user->account->m_last_name,
|
||||
];
|
||||
|
||||
// Lösche User (soft delete)
|
||||
UserUtil::deleteUser($delete_user);
|
||||
|
||||
\DB::commit();
|
||||
$count++;
|
||||
\Log::channel('cleanup')->info('deleteUser: '.json_encode($data));
|
||||
} catch (\Exception $e) {
|
||||
\DB::rollBack();
|
||||
\Log::channel('cleanup')->error('deleteInactiveUsers failed for user_id: '.$delete_user->id.' | Error: '.$e->getMessage());
|
||||
|
||||
continue;
|
||||
$counts === null ? $failed++ : $done++;
|
||||
}
|
||||
|
||||
$rows[] = [
|
||||
$user->id,
|
||||
trim($user->account?->first_name.' '.$user->account?->last_name),
|
||||
$user->email,
|
||||
$user->payment_account ? Carbon::parse($user->payment_account)->format('d.m.Y') : '',
|
||||
$counts['team'] ?? '-',
|
||||
$counts['customers'] ?? '-',
|
||||
$counts['abos'] ?? '-',
|
||||
$counts['portal_customers'] ?? '-',
|
||||
$counts['parked'] ?? '-',
|
||||
];
|
||||
}
|
||||
|
||||
$diff = microtime(true) - $methodStartTime;
|
||||
$sec = intval($diff);
|
||||
$micro = $diff - $sec;
|
||||
$this->line('');
|
||||
$this->info($label.': '.count($rows).' Berater'.($dryRun ? '' : " (erledigt {$done}, übersprungen {$failed})"));
|
||||
|
||||
$this->info('END Command deleteInactiveUsers: '.$count.' | Time: '.$sec.'sec :'.round($micro * 1000, 4).' ms');
|
||||
}
|
||||
|
||||
/**
|
||||
* Deaktiviert User, die länger als 2 Wochen inaktiv sind
|
||||
* - Weist deren Vertriebspartner-Kinder dem nächsten aktiven Sponsor zu
|
||||
* - Deaktiviert den User (behält Account, speichert Sponsor in pre_sponsor)
|
||||
* - Shopping-Kunden werden NICHT übertragen (bleiben beim deaktivierten User)
|
||||
*/
|
||||
private function cleanUpInActiveUser()
|
||||
{
|
||||
$methodStartTime = microtime(true);
|
||||
$this->info('START Command cleanUpInActiveUser');
|
||||
$count = 0;
|
||||
|
||||
// Finde User die länger als 2 Wochen inaktiv sind
|
||||
$date = Carbon::now()->modify('-2 weeks');
|
||||
|
||||
$inactive_users = User::where('active', true)
|
||||
->where('m_sponsor', '!=', null)
|
||||
->where('payment_account', '<', $date)
|
||||
->get();
|
||||
|
||||
foreach ($inactive_users as $inactive_user) {
|
||||
\DB::beginTransaction();
|
||||
|
||||
try {
|
||||
// Finde nächsten aktiven Sponsor
|
||||
$active_sponsor = UserUtil::findNextActiveSponsor($inactive_user->m_sponsor);
|
||||
|
||||
if (! $active_sponsor) {
|
||||
\Log::channel('cleanup')->error('cleanUpInActiveUser find no active_sponsor by inactive_user: '.$inactive_user->id);
|
||||
\DB::rollBack();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Setze alle Vertriebspartner-Kinder zum neuen Sponsor
|
||||
UserUtil::setNewSponsorToChilds($inactive_user->id, $active_sponsor->id);
|
||||
|
||||
$data = [
|
||||
'user_id' => $inactive_user->id,
|
||||
'email' => $inactive_user->email,
|
||||
'm_account' => $inactive_user->account ? $inactive_user->account->m_account : '',
|
||||
'm_first_name' => $inactive_user->account ? $inactive_user->account->m_first_name : '',
|
||||
'm_last_name' => $inactive_user->account ? $inactive_user->account->m_last_name : '',
|
||||
];
|
||||
|
||||
// Deaktiviere User (setzt pre_sponsor, entfernt m_sponsor, setzt active=false)
|
||||
UserUtil::deactiveUser($inactive_user);
|
||||
|
||||
\DB::commit();
|
||||
$count++;
|
||||
\Log::channel('cleanup')->info('inactive_user: '.json_encode($data));
|
||||
} catch (\Exception $e) {
|
||||
\DB::rollBack();
|
||||
\Log::channel('cleanup')->error('cleanUpInActiveUser failed for user_id: '.$inactive_user->id.' | Error: '.$e->getMessage());
|
||||
|
||||
continue;
|
||||
}
|
||||
if ($rows !== []) {
|
||||
$this->table(['ID', 'Name', 'E-Mail', 'Ablauf', 'Team', 'Kunden', 'Kundenabos', 'Portal-Logins', 'beim Sponsor geparkt'], $rows);
|
||||
}
|
||||
|
||||
$diff = microtime(true) - $methodStartTime;
|
||||
$sec = intval($diff);
|
||||
$micro = $diff - $sec;
|
||||
|
||||
$this->info('END Command cleanUpInActiveUser: '.$count.' | Time: '.$sec.'sec :'.round($micro * 1000, 4).' ms');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue