mivita/app/Console/Commands/IncentiveDebugTrackPartner.php
2026-04-10 17:15:27 +02:00

196 lines
7.6 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Incentive;
use App\Models\IncentiveNewPartner;
use App\Models\IncentiveParticipant;
use App\Models\ShoppingOrder;
use App\User;
use Illuminate\Console\Command;
class IncentiveDebugTrackPartner extends Command
{
protected $signature = 'incentive:debug-track-partner {order_id : Shopping Order ID}';
protected $description = 'Debuggt trackNewPartner Schritt fuer Schritt fuer eine bestimmte Bestellung';
public function handle(): int
{
$order_id = $this->argument('order_id');
$shopping_order = ShoppingOrder::find($order_id);
if (! $shopping_order) {
$this->error("Shopping Order #{$order_id} nicht gefunden.");
return self::FAILURE;
}
$this->info("=== Debug trackNewPartner fuer Order #{$order_id} ===");
$this->newLine();
// 1. Bestelldaten
$this->info('[1] Bestelldaten:');
$this->table(['Feld', 'Wert'], [
['id', $shopping_order->id],
['auth_user_id', $shopping_order->auth_user_id ?? 'NULL'],
['member_id', $shopping_order->member_id ?? 'NULL'],
['payment_for', $shopping_order->payment_for],
['paid', $shopping_order->paid],
['txaction', $shopping_order->txaction],
['mode', $shopping_order->mode],
['created_at', $shopping_order->created_at],
]);
// 2. Prüfe payment_for == 1 (Voraussetzung im Payment.php)
if ($shopping_order->payment_for != 1) {
$this->warn("[!] payment_for = {$shopping_order->payment_for} (nicht 1/registration). trackNewPartner wird nur bei payment_for=1 aufgerufen!");
}
// 3. Neuer User
$this->newLine();
$this->info('[2] Neuer User (auth_user_id):');
if (! $shopping_order->auth_user_id) {
$this->error(' auth_user_id ist NULL -> ABBRUCH (return)');
return self::SUCCESS;
}
$new_user = User::find($shopping_order->auth_user_id);
if (! $new_user) {
$this->error(" User #{$shopping_order->auth_user_id} nicht gefunden -> ABBRUCH (return)");
return self::SUCCESS;
}
$this->table(['Feld', 'Wert'], [
['id', $new_user->id],
['email', $new_user->email],
['m_sponsor', $new_user->m_sponsor ?? 'NULL'],
['active', $new_user->active],
['created_at', $new_user->created_at],
]);
if (! $new_user->m_sponsor) {
$this->error(' m_sponsor ist NULL -> ABBRUCH (return)');
return self::SUCCESS;
}
$sponsor_id = $new_user->m_sponsor;
$this->info(" Sponsor ID: {$sponsor_id}");
// 4. Registration Date
$registration_date = $shopping_order->created_at;
$this->newLine();
$this->info("[3] Registration Date: {$registration_date}");
// 5. Aktive Incentives
$this->newLine();
$this->info('[4] Aktive Incentives pruefen:');
$all_incentives = Incentive::query()->get();
$this->info(" Incentives gesamt: {$all_incentives->count()}");
foreach ($all_incentives as $incentive) {
$is_active = $incentive->status == 1;
$in_range = $registration_date >= $incentive->qualification_start
&& $registration_date <= $incentive->qualification_end;
$status_icon = $is_active ? 'AKTIV' : 'INAKTIV';
$range_icon = $in_range ? 'IM ZEITRAUM' : 'AUSSERHALB';
$this->table(['Feld', 'Wert'], [
['Incentive', "#{$incentive->id}: {$incentive->name}"],
['Status', "{$incentive->status} ({$status_icon})"],
['qualification_start', $incentive->qualification_start],
['qualification_end', $incentive->qualification_end],
['Registration Date', "{$registration_date} ({$range_icon})"],
]);
if (! $is_active) {
$this->warn(' -> Uebersprungen: Incentive nicht aktiv');
continue;
}
if (! $in_range) {
$this->warn(' -> Uebersprungen: Registration Date ausserhalb Qualifikationszeitraum');
continue;
}
$this->info(" -> MATCH! Incentive #{$incentive->id} ist aktiv und Registration Date liegt im Zeitraum.");
// 6. Participant prüfen
$this->newLine();
$this->info("[5] Participant-Check: Sponsor #{$sponsor_id} in Incentive #{$incentive->id}");
$participant = IncentiveParticipant::where('incentive_id', $incentive->id)
->where('user_id', $sponsor_id)
->first();
if (! $participant) {
$this->error(" Sponsor #{$sponsor_id} ist KEIN Teilnehmer in Incentive #{$incentive->id} -> SKIP");
// Zeige alle Teilnehmer-User-IDs
$participant_ids = IncentiveParticipant::where('incentive_id', $incentive->id)
->pluck('user_id')
->toArray();
$this->info(' Teilnehmer User-IDs: '.implode(', ', array_slice($participant_ids, 0, 20))
.(count($participant_ids) > 20 ? '... (+'.count($participant_ids) - 20 .')' : ''));
continue;
}
$this->info(" Participant gefunden: #{$participant->id} (User #{$participant->user_id})");
$this->table(['Feld', 'Wert'], [
['participant.id', $participant->id],
['user_id', $participant->user_id],
['total_points', $participant->total_points],
['qualified_partners', $participant->qualified_partners],
['accepted_terms_at', $participant->accepted_terms_at ?? 'NULL'],
]);
// 7. Tracking-Eintrag prüfen
$this->newLine();
$this->info('[6] Tracking-Eintrag (incentive_new_partners):');
$existing = IncentiveNewPartner::where('participant_id', $participant->id)
->where('user_id', $new_user->id)
->first();
if ($existing) {
$this->warn(" Eintrag existiert bereits: #{$existing->id} (erstellt: {$existing->created_at})");
} else {
$this->info(' Kein Eintrag vorhanden -> wuerde neu erstellt werden.');
}
// 8. Zusammenfassung
$this->newLine();
$this->info('=== ERGEBNIS ===');
$this->info('trackNewPartner WUERDE erfolgreich laufen fuer:');
$this->info(" Neuer Partner: User #{$new_user->id} ({$new_user->email})");
$this->info(" Sponsor/Teilnehmer: User #{$sponsor_id} (Participant #{$participant->id})");
$this->info(" Incentive: #{$incentive->id} ({$incentive->name})");
$this->info(" Einmalpunkte: {$incentive->points_partner_onetime}");
}
// Prüfe den Query wie er im Code steht
$this->newLine();
$this->info('[7] Exakter Query wie im Code:');
$matched_incentives = Incentive::query()
->active()
->where('qualification_start', '<=', $registration_date)
->where('qualification_end', '>=', $registration_date)
->get();
$this->info(" Incentive::active()->where(start <= {$registration_date})->where(end >= {$registration_date})");
$this->info(" Ergebnis: {$matched_incentives->count()} Incentive(s)");
foreach ($matched_incentives as $mi) {
$this->info(" -> #{$mi->id}: {$mi->name}");
}
return self::SUCCESS;
}
}