Incentive (E1): Gleichstand nach "zuerst erreicht", Neupartner zaehlen im Monat der Zahlung
- Rangliste: bei gleichen Punkten bestaetigte Teilnahme vor anonym, danach wer den Stand zuerst erreicht hat (letzter zaehlender Neupartner bzw. Neuabo laut Tracking-Tabellen), zuletzt die Teilnehmer-ID. Vorher entschied bei Gleichstand die Datenbankreihenfolge (Teilnahmebedingungen Recruiting-Challenge, Punkt 6). - Neupartner zaehlen fuer Incentives ab ihrer ersten Kontozahlung (users.partner_paid_at), wie in der Statistik (Kundenentscheid 16.09.2026), nicht ab dem Bestelldatum: Live-Tracking, naechtliche Reparatur und Neuaufbau nutzen dieselbe Regel (IncentiveTracker::newPartnerCountedAt). Wer am 29.09. bestellt und am 01.10. zahlt, zaehlt in der Challenge. Montenegro ist abgeschlossen; gegen die Live-Kopie tauschen dort nur Plaetze ab 81, und der naechtliche Lauf entfernt keine bestehenden Trackings. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a50be6fd51
commit
1885402a6c
5 changed files with 207 additions and 29 deletions
|
|
@ -34,7 +34,10 @@ class IncentiveTracker
|
|||
}
|
||||
|
||||
$sponsor_id = $new_user->m_sponsor;
|
||||
$registration_date = $shopping_order->created_at ?? Carbon::now();
|
||||
// Neupartner zaehlen im Moment der ersten Kontozahlung, nicht der Bestellung
|
||||
// (Kundenentscheid 16.09.2026, wie in der Statistik): wer Ende September
|
||||
// bestellt und am 1. Oktober bezahlt, zaehlt ab Oktober.
|
||||
$registration_date = self::newPartnerCountedAt($new_user);
|
||||
|
||||
$incentives = Incentive::query()
|
||||
->active()
|
||||
|
|
@ -65,6 +68,17 @@ class IncentiveTracker
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeitpunkt, zu dem ein Neupartner fuer ein Incentive zaehlt: seine erste
|
||||
* Kontozahlung (`users.partner_paid_at`, {@see User::scopeNewPartnerIn()}). Im
|
||||
* Zahlungspfad ist der Wert schon gesetzt; fehlt er ausnahmsweise, ist jetzt der
|
||||
* Zahlungsmoment.
|
||||
*/
|
||||
public static function newPartnerCountedAt(User $partner): Carbon
|
||||
{
|
||||
return $partner->partner_paid_at ? Carbon::parse($partner->partner_paid_at) : Carbon::now();
|
||||
}
|
||||
|
||||
/**
|
||||
* Track an abo activation (Kundenabo is_for=ot oder Berater-Eigenabo is_for=me, bezahlt + aktiv).
|
||||
* Fuegt Abo in Tracking-Tabelle ein + Log-Eintrag + Neuberechnung.
|
||||
|
|
@ -322,17 +336,25 @@ class IncentiveTracker
|
|||
* Update ranking for all participants of an incentive.
|
||||
* Der Rang entspricht der Anzeigereihenfolge des Leaderboards: Qualifizierte zuerst (1..n),
|
||||
* dahinter Nicht-Qualifizierte, jeweils nach Punkten absteigend.
|
||||
*
|
||||
* Bei Punktgleichstand: Teilnahme bestaetigt vor anonym, danach liegt vorn, wer den Stand
|
||||
* zuerst erreicht hat (Teilnahmebedingungen Recruiting-Challenge, Punkt 6), zuletzt die
|
||||
* Teilnehmer-ID, damit die Reihenfolge nie von der Datenbank abhaengt.
|
||||
*/
|
||||
public static function updateRanking(Incentive $incentive): void
|
||||
{
|
||||
// Nur Teilnehmer mit Punkten bekommen einen Rang (bei Punktgleichstand: Teilnahme bestaetigt vor anonym)
|
||||
// Nur Teilnehmer mit Punkten bekommen einen Rang
|
||||
$with_points = IncentiveParticipant::where('incentive_id', $incentive->id)
|
||||
->where('total_points', '>', 0)
|
||||
->orderByDesc('is_qualified')
|
||||
->orderByDesc('total_points')
|
||||
->orderByRaw('accepted_terms_at IS NOT NULL DESC')
|
||||
->get();
|
||||
|
||||
$reached_at = self::pointsReachedAtByParticipant($incentive, $with_points->pluck('id')->all());
|
||||
|
||||
$with_points = $with_points->sort(function (IncentiveParticipant $a, IncentiveParticipant $b) use ($reached_at): int {
|
||||
return [(int) $b->is_qualified, (int) $b->total_points, (int) $b->hasAcceptedTerms(), $reached_at[$a->id] ?? PHP_INT_MAX, $a->id]
|
||||
<=> [(int) $a->is_qualified, (int) $a->total_points, (int) $a->hasAcceptedTerms(), $reached_at[$b->id] ?? PHP_INT_MAX, $b->id];
|
||||
});
|
||||
|
||||
$rank = 1;
|
||||
foreach ($with_points as $participant) {
|
||||
$participant->rank = $rank;
|
||||
|
|
@ -347,6 +369,46 @@ class IncentiveTracker
|
|||
->update(['rank' => null]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeitpunkt, zu dem ein Teilnehmer seinen heutigen Stand erreicht hat: der letzte
|
||||
* zaehlende Neupartner bzw. das letzte zaehlende Neuabo. Grundlage sind die fachlichen
|
||||
* Zeitpunkte der Tracking-Tabellen (registered_at / activated_at), nicht die
|
||||
* Log-Zeitstempel — ein Neuaufbau (rebuildFromSourceTables) schreibt die Logs neu.
|
||||
*
|
||||
* @param list<int> $participantIds
|
||||
* @return array<int, int> Teilnehmer-ID => Unix-Zeitstempel
|
||||
*/
|
||||
private static function pointsReachedAtByParticipant(Incentive $incentive, array $participantIds): array
|
||||
{
|
||||
if ($participantIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$reached = [];
|
||||
$sources = [
|
||||
[IncentiveNewPartner::class, 'registered_at', (int) $incentive->points_partner_onetime],
|
||||
[IncentiveNewAbo::class, 'activated_at', (int) $incentive->points_abo_onetime],
|
||||
];
|
||||
|
||||
foreach ($sources as [$model, $column, $points]) {
|
||||
if ($points <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$latest = $model::query()
|
||||
->whereIn('participant_id', $participantIds)
|
||||
->groupBy('participant_id')
|
||||
->selectRaw("participant_id, MAX({$column}) as reached_at")
|
||||
->pluck('reached_at', 'participant_id');
|
||||
|
||||
foreach ($latest as $participantId => $timestamp) {
|
||||
$reached[$participantId] = max($reached[$participantId] ?? 0, Carbon::parse($timestamp)->getTimestamp());
|
||||
}
|
||||
}
|
||||
|
||||
return $reached;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log-Eintrag schreiben (Audit-Trail, keine Duplikate).
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue