mivita/tests/Feature/Incentive/IncentivePartnerRepairSponsorChangeTest.php
Kevin 841d87e161 Incentive: Ticket-Fixes 2026-08-12 + Team-Struktur nach active_date sortiert
Incentive (Montenegro 2026):
- Live-Tracker: whereDate() für Zeitraum-Vergleiche – Bestellungen am letzten
  Tag des Qualifikationszeitraums gingen verloren (DATE-Spalte vs. Datetime)
- Repair/Rebuild: Neupartner-Definition auf Starterpaket-Bestelldatum im
  Zeitraum umgestellt (statt users.created_at) – Bestandskunden-Upgrades
  zählen jetzt wie im Live-Tracking (Fall Dino Portaro → Steffi Karrer)
- Neu: removeStaleSponsorTrackings() – Partner-Trackings wandern nach
  Sponsorwechsel zum aktuellen Sponsor (Fall Anna Schall lag beim Firmenkonto)
- incentive:calculate: Sponsorwechsel-Bereinigung eingebunden + Statistik

Team-Struktur (/user/team/structure):
- Downline sortiert nach active_date (Partner-Aktivierung) statt users.id:
  zentral in TreeHtmlRenderer::sortByActivation() (wirkt sofort, auch für
  gespeicherte Monats-Snapshots) + orderBy in beiden Downline-Queries

Doku: dev/2026-08-12-intensiv-tickets/README.md (4 Kundentickets analysiert,
Lücke durch verspätete Zeitraum-Verlängerung hatte sich per Nacht-Cron geheilt)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-12 10:28:06 +00:00

268 lines
9.2 KiB
PHP

<?php
use App\Models\Country;
use App\Models\Incentive;
use App\Models\IncentiveNewPartner;
use App\Models\IncentiveParticipant;
use App\Models\IncentivePointsLog;
use App\Models\Product;
use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingOrderItem;
use App\Models\ShoppingUser;
use App\Models\UserShop;
use App\Services\Incentive\IncentivePointsLogRepairService;
use App\Services\Incentive\IncentiveTracker;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
beforeEach(function () {
Carbon::setTestNow(Carbon::parse('2026-05-15 12:00:00'));
});
afterEach(function () {
Carbon::setTestNow();
});
function repairTestCreateUser(?int $sponsorId = null, ?string $createdAt = null): User
{
$user = User::forceCreate([
'email' => 'u-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
'm_sponsor' => $sponsorId,
]);
if ($createdAt) {
$user->created_at = Carbon::parse($createdAt);
$user->save();
}
return $user->fresh();
}
function repairTestCreateStarterKitOrder(User $registrant, User $shopOwner, ?string $orderCreatedAt = null): ShoppingOrder
{
$country = Country::firstOrCreate(['code' => 'DE'], [
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$shipping = Shipping::create(['name' => 'S', 'active' => true]);
$shippingCountry = ShippingCountry::create(['shipping_id' => $shipping->id, 'country_id' => $country->id]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'T'.substr(uniqid('', true), 0, 6),
'slug' => 't-'.uniqid(),
'active' => true,
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $registrant->id,
'member_id' => $shopOwner->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'su-'.uniqid('', true).'@example.com',
'is_from' => 'wizard',
]);
$order = ShoppingOrder::create([
'shopping_user_id' => $shoppingUser->id,
'auth_user_id' => $registrant->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 1,
'paid' => true,
'txaction' => 'paid',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
$productId = DB::table('products')->insertGetId([
'name' => 'Starter '.uniqid(),
'title' => 'T',
'is_membership_only' => false,
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
ShoppingOrderItem::create([
'shopping_order_id' => $order->id,
'product_id' => Product::findOrFail($productId)->id,
'qty' => 1,
'price' => 50,
]);
if ($orderCreatedAt) {
$order->created_at = Carbon::parse($orderCreatedAt);
$order->save();
}
return $order->fresh(['shopping_order_items.product']);
}
function repairTestCreateIncentive(): Incentive
{
return Incentive::factory()->create([
'qualification_start' => '2026-04-01',
'qualification_end' => '2026-07-31',
'calculation_end' => '2026-08-31',
'points_partner_onetime' => 600,
'points_abo_onetime' => 400,
'status' => 1,
]);
}
it('syncMissingTrackingPartners zaehlt Bestandskunden, die im Zeitraum zum Berater upgraden (Bestelldatum zaehlt)', function () {
$incentive = repairTestCreateIncentive();
$sponsor = repairTestCreateUser();
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $sponsor->id,
]);
// User lange vor dem Qualifikationszeitraum registriert, Starterpaket aber im Zeitraum bezahlt
$partner = repairTestCreateUser($sponsor->id, '2024-10-28 16:48:05');
repairTestCreateStarterKitOrder($partner, $sponsor, '2026-05-10 11:05:52');
$added = app(IncentivePointsLogRepairService::class)->syncMissingTrackingPartners($participant);
expect($added)->toBe(1);
expect(IncentiveNewPartner::where('participant_id', $participant->id)->where('user_id', $partner->id)->exists())->toBeTrue();
$log = IncentivePointsLog::where('participant_id', $participant->id)
->where('type', 'partner')
->where('source_id', $partner->id)
->where('is_storno', false)
->first();
expect($log)->not->toBeNull();
expect((int) $log->points_onetime)->toBe(600);
expect((int) $log->month)->toBe(5);
});
it('syncMissingTrackingPartners ignoriert Starterpakete ausserhalb des Qualifikationszeitraums', function () {
$incentive = repairTestCreateIncentive();
$sponsor = repairTestCreateUser();
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $sponsor->id,
]);
$partner = repairTestCreateUser($sponsor->id, '2026-05-01 10:00:00');
repairTestCreateStarterKitOrder($partner, $sponsor, '2026-03-15 11:00:00');
$added = app(IncentivePointsLogRepairService::class)->syncMissingTrackingPartners($participant);
expect($added)->toBe(0);
expect(IncentiveNewPartner::where('user_id', $partner->id)->exists())->toBeFalse();
});
it('trackNewPartner zaehlt Bestellungen am letzten Tag des Qualifikationszeitraums (Datums-Randfall)', function () {
Carbon::setTestNow(Carbon::parse('2026-07-31 11:05:52'));
$incentive = repairTestCreateIncentive();
$sponsor = repairTestCreateUser();
IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $sponsor->id,
]);
$partner = repairTestCreateUser($sponsor->id);
$order = repairTestCreateStarterKitOrder($partner, $sponsor, '2026-07-31 11:05:52');
IncentiveTracker::trackNewPartner($order);
expect(IncentiveNewPartner::where('user_id', $partner->id)->exists())->toBeTrue();
});
it('removeStaleSponsorTrackings verschiebt Partner nach Sponsorwechsel zum aktuellen Sponsor', function () {
$incentive = repairTestCreateIncentive();
$oldSponsor = repairTestCreateUser();
$newSponsor = repairTestCreateUser();
$participantOld = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $oldSponsor->id,
]);
$participantNew = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $newSponsor->id,
]);
// Partner wurde unter dem alten Sponsor getrackt, haengt inzwischen aber unter dem neuen
$partner = repairTestCreateUser($newSponsor->id);
repairTestCreateStarterKitOrder($partner, $newSponsor, '2026-05-10 09:00:00');
$staleTracking = IncentiveNewPartner::create([
'participant_id' => $participantOld->id,
'user_id' => $partner->id,
'registered_at' => Carbon::parse('2026-05-10 09:00:00'),
]);
IncentivePointsLog::create([
'participant_id' => $participantOld->id,
'type' => 'partner',
'source_type' => User::class,
'source_id' => $partner->id,
'source_label' => 'Partner',
'month' => 5,
'year' => 2026,
'points_onetime' => 600,
'points_accumulated' => 0,
'incentive_new_partner_id' => $staleTracking->id,
]);
$participantOld->recalculateFromTrackingTables()->save();
expect((int) $participantOld->fresh()->total_points)->toBe(600);
$repair = app(IncentivePointsLogRepairService::class);
$removed = $repair->removeStaleSponsorTrackings($participantOld);
expect($removed)->toBe(1);
expect(IncentiveNewPartner::where('participant_id', $participantOld->id)->exists())->toBeFalse();
expect(IncentivePointsLog::where('participant_id', $participantOld->id)->exists())->toBeFalse();
$participantOld->recalculateFromTrackingTables()->save();
expect((int) $participantOld->fresh()->total_points)->toBe(0);
// Beim aktuellen Sponsor zieht der Repair den Partner nach
$added = $repair->syncMissingTrackingPartners($participantNew);
expect($added)->toBe(1);
$participantNew->recalculateFromTrackingTables()->save();
expect((int) $participantNew->fresh()->total_points)->toBe(600);
expect((int) $participantNew->fresh()->qualified_partners)->toBe(1);
});
it('removeStaleSponsorTrackings laesst korrekte Trackings unangetastet', function () {
$incentive = repairTestCreateIncentive();
$sponsor = repairTestCreateUser();
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $sponsor->id,
]);
$partner = repairTestCreateUser($sponsor->id);
IncentiveNewPartner::create([
'participant_id' => $participant->id,
'user_id' => $partner->id,
'registered_at' => now(),
]);
$removed = app(IncentivePointsLogRepairService::class)->removeStaleSponsorTrackings($participant);
expect($removed)->toBe(0);
expect(IncentiveNewPartner::where('participant_id', $participant->id)->exists())->toBeTrue();
});