Feedback-Umsetzung (dev/2026-07-22/feedback-analyse-2026-07-24.md, alle 10 Punkte):
- VIP-Statistik: eigener Berater ('ich', Linie 0) in Neue-Kundenabos- und beiden Top-10-Listen; Top-10-Umsatz sortiert nach abgerechneten Ist-Punkten + Abo-Anzahl-Spalte; Fußnote nicht-payline-wirksame Punkte (SalesVolumePoints::nonPaylinePoints)
- Führungskräfte-Liste: beide Sichten 'direkt' (an nächster FK abgeschnitten, additiv) und 'gesamt' (komplette Downline), Gesamt-Spalte nach Kundenzählweise
- Team-Abos: je Ebene 'abgerechnet (Monat)' und 'bis zum 25. erwartet'; Payline-Karte mit Teamabo-/Kundenabo-Zählern
- Incentive-Live-Ranking: Qualifizierte zuerst nummeriert (fixt auch isWinner rank<=max_winners)
- Porto: Netto-/Brutto-Kennzeichnung (Berater netto, Endkunden inkl. MwSt., portal_view-Flag; auch CRM-/Shop-Warenkorb + Checkout), Yard::shipping_tax-Fix
- Struktur-Ansicht: Abo-Badges 'A' (eigenes Abo) und '(n)' (Kundenabos) je Teampartner, gebatcht
- Abo-Löschung: Softdelete setzt active=0 (UserAbo deleting-Hook) + Bereinigungs-Migration für gelöschte-aber-aktive Abos
- Doku: technische Analyse + Kundinnen-Feedback-Dokument unter dev/2026-07-22/
- Tests: IncentiveRankingTest, TeamStructureAboBadgesTest, YardShippingTaxTest (neu); BackofficeStatisticsListsTest, TeamAboOverviewTest, PaylineForecastServiceTest, AboAdminDeleteTest, AboOneTimeViewTest erweitert
Außerdem enthalten (Branch-WIP vor dem Feedback): DATEV-Abgleich-Fixes 08+09/2025, USt-ID-Live-Validierung sowie restliche unkommittete Stände (u. a. Abo-Pause-Sprachkeys).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
145 lines
5.1 KiB
PHP
145 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\BusinessPlan;
|
|
|
|
use App\Models\UserBusiness;
|
|
use App\User;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
/**
|
|
* Gemeinsamer Live-Lauf für die "Stand heute"-Kennzahlen (Konzept B1/B8/B10):
|
|
* EIN TreeCalcBotOptimized-Lauf pro User/Monat liefert Payline-Punkte,
|
|
* Provisions-Bestandteile und die Payline-Tiefe, 6 h gecacht (F5).
|
|
*
|
|
* - Liegt für den Monat bereits ein UserBusiness-Datensatz vor (Monats-Batch
|
|
* `business:store-optimized` gelaufen), gelten dessen Werte als final.
|
|
* - Sonst rechnet TreeCalcBotOptimized live — dieselbe Engine wie der
|
|
* Monats-Batch (inkl. GrowthBonusCalculator und korrigierter Level-Auswahl),
|
|
* damit Live- und Finalwerte nicht systematisch auseinanderlaufen.
|
|
* - Fehlgeschlagene Läufe werden NICHT gecacht, damit ein transienter Fehler
|
|
* nicht 6 h lang Nullwerte anzeigt (`failed` markiert diesen Fall).
|
|
*/
|
|
class LiveCommissionService
|
|
{
|
|
private const CACHE_HOURS = 6;
|
|
|
|
/**
|
|
* @return array{payline_points: float, paylines: int, pp: float, shop: float, growth: float, is_live: bool, failed: bool}
|
|
*/
|
|
public function snapshot(User $user, int $month, int $year): array
|
|
{
|
|
$stored = UserBusiness::where('user_id', $user->id)
|
|
->where('month', $month)
|
|
->where('year', $year)
|
|
->first();
|
|
|
|
if ($stored) {
|
|
return $this->buildSnapshot(
|
|
(float) ($stored->payline_points ?? 0),
|
|
$this->paylinesFromQualLevel($stored->qual_user_level),
|
|
(float) ($stored->commission_pp_total ?? 0),
|
|
(float) ($stored->commission_shop_sales ?? 0),
|
|
(float) ($stored->commission_growth_total ?? 0),
|
|
false
|
|
);
|
|
}
|
|
|
|
$cacheKey = "live_business_{$user->id}_{$month}_{$year}";
|
|
$cached = Cache::get($cacheKey);
|
|
if (is_array($cached)) {
|
|
return $cached;
|
|
}
|
|
|
|
$snapshot = $this->calculateLive($user, $month, $year);
|
|
if ($snapshot !== null) {
|
|
Cache::put($cacheKey, $snapshot, now()->addHours(self::CACHE_HOURS));
|
|
|
|
return $snapshot;
|
|
}
|
|
|
|
return $this->buildSnapshot(0.0, 0, 0.0, 0.0, 0.0, true, failed: true);
|
|
}
|
|
|
|
/**
|
|
* Provision "Stand heute" (B1) — alle Bestandteile plus Summe.
|
|
*
|
|
* @return array{total: float, pp: float, shop: float, growth: float, is_live: bool}
|
|
*/
|
|
public function commission(User $user, int $month, int $year): array
|
|
{
|
|
$snapshot = $this->snapshot($user, $month, $year);
|
|
|
|
return [
|
|
'total' => round($snapshot['pp'] + $snapshot['shop'] + $snapshot['growth'], 2),
|
|
'pp' => $snapshot['pp'],
|
|
'shop' => $snapshot['shop'],
|
|
'growth' => $snapshot['growth'],
|
|
'is_live' => $snapshot['is_live'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array{payline_points: float, paylines: int, pp: float, shop: float, growth: float, is_live: bool, failed: bool}|null
|
|
*/
|
|
private function calculateLive(User $user, int $month, int $year): ?array
|
|
{
|
|
try {
|
|
$treeCalcBot = new TreeCalcBotOptimized($month, $year, 'member');
|
|
$treeCalcBot->initBusinesslUserDetail($user);
|
|
$businessUser = $treeCalcBot->business_user;
|
|
|
|
if (! $businessUser) {
|
|
return null;
|
|
}
|
|
|
|
// Kein `?? 0` auf den Magic-Properties: die Items haben kein __isset,
|
|
// isset() wäre dort immer false und das Ergebnis damit immer 0.
|
|
return $this->buildSnapshot(
|
|
(float) $businessUser->payline_points,
|
|
$this->paylinesFromQualLevel($businessUser->qual_user_level),
|
|
(float) $businessUser->commission_pp_total,
|
|
(float) $businessUser->commission_shop_sales,
|
|
(float) $businessUser->commission_growth_total,
|
|
true
|
|
);
|
|
} catch (\Throwable $e) {
|
|
\Log::warning("LiveCommissionService: Live-Berechnung fehlgeschlagen für User {$user->id}: ".$e->getMessage());
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Payline-Tiefe (Anzahl bezahlter Ebenen) aus dem Qualifikationslevel.
|
|
*
|
|
* @param array<string, mixed>|object|null $qualUserLevel
|
|
*/
|
|
private function paylinesFromQualLevel(mixed $qualUserLevel): int
|
|
{
|
|
if (is_array($qualUserLevel)) {
|
|
return (int) ($qualUserLevel['paylines'] ?? 0);
|
|
}
|
|
|
|
if (is_object($qualUserLevel)) {
|
|
return (int) ($qualUserLevel->paylines ?? 0);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* @return array{payline_points: float, paylines: int, pp: float, shop: float, growth: float, is_live: bool, failed: bool}
|
|
*/
|
|
private function buildSnapshot(float $paylinePoints, int $paylines, float $pp, float $shop, float $growth, bool $isLive, bool $failed = false): array
|
|
{
|
|
return [
|
|
'payline_points' => round($paylinePoints, 2),
|
|
'paylines' => $paylines,
|
|
'pp' => round($pp, 2),
|
|
'shop' => round($shop, 2),
|
|
'growth' => round($growth, 2),
|
|
'is_live' => $isLive,
|
|
'failed' => $failed,
|
|
];
|
|
}
|
|
}
|