10.April 2026
This commit is contained in:
parent
a00c42e770
commit
f58c709945
208 changed files with 19280 additions and 2914 deletions
54
app/Services/Incentive/IncentiveCalculationService.php
Normal file
54
app/Services/Incentive/IncentiveCalculationService.php
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Incentive;
|
||||
|
||||
use App\Models\Incentive;
|
||||
use App\Models\IncentiveParticipant;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class IncentiveCalculationService
|
||||
{
|
||||
/**
|
||||
* Full recalculation of an incentive (batch / cron / manual).
|
||||
* Normal: Neuberechnung aus Tracking-Tabellen + Log.
|
||||
* Force: Kompletter Neuaufbau aus Quelldaten (Users, UserAbos, UserSalesVolumes).
|
||||
*/
|
||||
public function recalculate(Incentive $incentive, bool $force = false): array
|
||||
{
|
||||
$stats = ['participants' => 0, 'errors' => 0];
|
||||
|
||||
$participants = $incentive->participants()->with('user')->get();
|
||||
|
||||
foreach ($participants as $participant) {
|
||||
try {
|
||||
$this->recalculateParticipant($participant, $force);
|
||||
$stats['participants']++;
|
||||
} catch (\Throwable $e) {
|
||||
$stats['errors']++;
|
||||
Log::error('IncentiveCalculation error for participant '.$participant->id.': '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
IncentiveTracker::updateRanking($incentive);
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate a single participant.
|
||||
* Force: Kompletter Neuaufbau aus Quelldaten.
|
||||
* Normal: Neuberechnung aus vorhandenen Tracking-Tabellen + Log.
|
||||
*/
|
||||
public function recalculateParticipant(IncentiveParticipant $participant, bool $force = false): void
|
||||
{
|
||||
if (! $participant->user) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($force) {
|
||||
$participant->rebuildFromSourceTables()->save();
|
||||
} else {
|
||||
$participant->recalculateFromTrackingTables()->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue