mivita/tests/Feature/Incentive/IncentiveRankingTest.php
Kevin Adametz fe1a9478f4 Recruiting Challenge: Rangliste nach Umsatz der Neupartner, Steuerpflicht
- 50 EUR je Neupartner bleiben; die Top 3 (500/300/200 EUR) ermittelt
  jetzt der Umsatz der Neupartner (wie Montenegro, ohne Einmalpunkte/Abos)
- Gleichstand bei Umsatzwertung: vorn liegt, wessen letzte gezaehlte
  Umsatzbuchung frueher kam
- Challenge-Seite: Praemie und Top-3-Darstellung auch bei Umsatzwertung,
  Ranglisten-Hinweis auf Umsatz (DE/EN/ES/FR)
- Seeder: neue Texte und Bedingungen (Rangliste, Steuerpflicht,
  vergünstigter Einstieg bis 31.10.), gleicht Wertung beim Einspielen ab

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 16:19:30 +00:00

279 lines
11 KiB
PHP

<?php
use App\Models\Incentive;
use App\Models\IncentiveNewPartner;
use App\Models\IncentiveParticipant;
use App\Models\UserSalesVolume;
use App\Services\Incentive\IncentiveTracker;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
$makeIncentiveRankingUser = fn () => User::forceCreate([
'email' => 'rank-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
it('vergibt Raenge zuerst an Qualifizierte, dann nach Punkten', function () use ($makeIncentiveRankingUser) {
$incentive = Incentive::factory()->create(['max_winners' => 2]);
$notQualifiedTopPoints = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'is_qualified' => false,
'total_points' => 5000,
]);
$qualifiedMidPoints = IncentiveParticipant::factory()->qualified()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'total_points' => 900,
]);
$qualifiedLowPoints = IncentiveParticipant::factory()->qualified()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'total_points' => 400,
]);
$zeroPoints = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'total_points' => 0,
'rank' => 7,
]);
IncentiveTracker::updateRanking($incentive);
expect($qualifiedMidPoints->refresh()->rank)->toBe(1)
->and($qualifiedLowPoints->refresh()->rank)->toBe(2)
->and($notQualifiedTopPoints->refresh()->rank)->toBe(3)
->and($zeroPoints->refresh()->rank)->toBeNull();
});
it('macht Qualifizierte mit gespeichertem Rang innerhalb max_winners zu Gewinnern', function () use ($makeIncentiveRankingUser) {
$incentive = Incentive::factory()->create(['max_winners' => 2]);
$notQualifiedTopPoints = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'is_qualified' => false,
'total_points' => 5000,
]);
$qualifiedA = IncentiveParticipant::factory()->qualified()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'total_points' => 900,
]);
$qualifiedB = IncentiveParticipant::factory()->qualified()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'total_points' => 400,
]);
IncentiveTracker::updateRanking($incentive);
expect($qualifiedA->refresh()->isWinner())->toBeTrue()
->and($qualifiedB->refresh()->isWinner())->toBeTrue()
->and($notQualifiedTopPoints->refresh()->isWinner())->toBeFalse();
});
it('vergibt nur bestaetigten Teilnehmern einen Rang (Kundenentscheid 25.09.2026)', function () use ($makeIncentiveRankingUser) {
$incentive = Incentive::factory()->create();
$anonymous = IncentiveParticipant::factory()->unconfirmed()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'total_points' => 900,
'rank' => 1,
]);
$confirmed = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'accepted_terms_at' => now(),
'total_points' => 500,
]);
IncentiveTracker::updateRanking($incentive);
expect($confirmed->refresh()->rank)->toBe(1)
->and($anonymous->refresh()->rank)->toBeNull();
});
it('stimmt mit der Leaderboard-Anzeigereihenfolge ueberein', function () use ($makeIncentiveRankingUser) {
$incentive = Incentive::factory()->create();
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'is_qualified' => false,
'total_points' => 5000,
]);
IncentiveParticipant::factory()->qualified()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'total_points' => 900,
]);
IncentiveParticipant::factory()->qualified()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'total_points' => 400,
]);
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'is_qualified' => false,
'total_points' => 200,
]);
IncentiveTracker::updateRanking($incentive);
$displayOrder = IncentiveParticipant::query()
->where('incentive_id', $incentive->id)
->orderByIncentiveLeaderboard()
->pluck('rank')
->all();
expect($displayOrder)->toBe([1, 2, 3, 4]);
});
describe('Gleichstand: wer den Stand zuerst erreicht hat (Recruiting-Challenge, Punkt 6)', function () use ($makeIncentiveRankingUser) {
$challengeWithTie = function () use ($makeIncentiveRankingUser): array {
$incentive = Incentive::factory()->create([
'points_partner_onetime' => 1,
'points_abo_onetime' => 0,
'min_direct_partners' => 0,
'min_customer_abos' => 0,
]);
$participant = function (array $registrations) use ($incentive, $makeIncentiveRankingUser): IncentiveParticipant {
$participant = IncentiveParticipant::factory()->qualified()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'accepted_terms_at' => now(),
'total_points' => count($registrations),
]);
foreach ($registrations as $registeredAt) {
IncentiveNewPartner::create([
'participant_id' => $participant->id,
'user_id' => $makeIncentiveRankingUser()->id,
'registered_at' => $registeredAt,
]);
}
return $participant;
};
return [$incentive, $participant];
};
it('setzt bei gleicher Anzahl den vor, dessen letzter Neupartner frueher kam', function () use ($challengeWithTie) {
[$incentive, $participant] = $challengeWithTie();
// Beide haben 2 Neupartner; der spaeter angelegte hat die Anzahl frueher erreicht.
$reachedLater = $participant(['2026-10-02 10:00:00', '2026-11-20 09:00:00']);
$reachedEarlier = $participant(['2026-10-05 10:00:00', '2026-10-30 18:00:00']);
IncentiveTracker::updateRanking($incentive);
expect($reachedEarlier->refresh()->rank)->toBe(1)
->and($reachedLater->refresh()->rank)->toBe(2);
});
it('laesst die Anzahl vor dem Zeitpunkt entscheiden', function () use ($challengeWithTie) {
[$incentive, $participant] = $challengeWithTie();
$fewerButEarly = $participant(['2026-10-01 08:00:00']);
$moreButLate = $participant(['2026-12-01 08:00:00', '2027-01-30 08:00:00']);
IncentiveTracker::updateRanking($incentive);
expect($moreButLate->refresh()->rank)->toBe(1)
->and($fewerButEarly->refresh()->rank)->toBe(2);
});
it('laesst unbestaetigte Teilnehmer trotz frueherem Zeitpunkt ohne Rang', function () use ($challengeWithTie) {
[$incentive, $participant] = $challengeWithTie();
$anonymousEarly = $participant(['2026-10-01 08:00:00']);
$anonymousEarly->update(['accepted_terms_at' => null]);
$confirmedLate = $participant(['2026-12-01 08:00:00']);
IncentiveTracker::updateRanking($incentive);
expect($confirmedLate->refresh()->rank)->toBe(1)
->and($anonymousEarly->refresh()->rank)->toBeNull();
});
it('vergibt bei vollstaendigem Gleichstand stabil nach Teilnehmer-ID', function () use ($challengeWithTie) {
[$incentive, $participant] = $challengeWithTie();
$first = $participant(['2026-10-10 10:00:00']);
$second = $participant(['2026-10-10 10:00:00']);
IncentiveTracker::updateRanking($incentive);
expect($first->refresh()->rank)->toBe(1)
->and($second->refresh()->rank)->toBe(2);
});
});
describe('Gleichstand bei Wertung nach Umsatz der Neupartner (Recruiting-Challenge ab 08.10.2026)', function () use ($makeIncentiveRankingUser) {
it('setzt bei gleicher Punktzahl den vor, dessen letzte Umsatzbuchung frueher kam', function () use ($makeIncentiveRankingUser) {
$incentive = Incentive::factory()->create([
'status' => 1,
'qualification_start' => '2026-10-09',
'qualification_end' => '2027-01-31',
'calculation_end' => '2027-01-31',
'points_partner_onetime' => 0,
'points_abo_onetime' => 0,
'min_direct_partners' => 0,
'min_customer_abos' => 0,
'max_winners' => 3,
'amount_per_partner' => 50,
'bonus_rank_1' => 500,
'include_sales_points' => true,
'include_abos' => false,
]);
$participantWithPartnerSales = function (string $bookedAt) use ($incentive, $makeIncentiveRankingUser): IncentiveParticipant {
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $makeIncentiveRankingUser()->id,
'accepted_terms_at' => now(),
'total_points' => 0,
]);
$partner = $makeIncentiveRankingUser();
IncentiveNewPartner::create([
'participant_id' => $participant->id,
'user_id' => $partner->id,
'registered_at' => '2026-10-15 10:00:00',
]);
$this->travelTo($bookedAt);
IncentiveTracker::trackSalesVolume(UserSalesVolume::create([
'user_id' => $partner->id,
'month' => 11,
'year' => 2026,
'date' => '15.11.2026',
'points' => 120,
'total_net' => 150,
'status_points' => 1,
'status' => 1,
'message' => 'Starterpaket Neupartner',
]));
$this->travelBack();
return $participant;
};
$bookedLater = $participantWithPartnerSales('2026-11-20 10:00:00');
$bookedEarlier = $participantWithPartnerSales('2026-11-05 10:00:00');
expect($incentive->ranksBySalesOfNewPartners())->toBeTrue()
->and($bookedLater->refresh()->total_points)->toBe(120)
->and($bookedLater->qualified_partners)->toBe(1)
->and($bookedEarlier->refresh()->total_points)->toBe(120)
->and($bookedEarlier->rank)->toBe(1)
->and($bookedLater->rank)->toBe(2);
});
});