Files
lotteryLaravel/app/Services/Agent/RebateLimitValidator.php

33 lines
962 B
PHP

<?php
namespace App\Services\Agent;
use App\Models\AgentNode;
use App\Models\AgentProfile;
use Illuminate\Validation\ValidationException;
final class RebateLimitValidator
{
public function assertPlayerRebateWithinAgent(AgentNode $agent, float $rebateRate, float $extraRebateRate = 0): void
{
$profile = AgentProfile::query()->where('agent_node_id', $agent->id)->first();
if ($profile === null) {
return;
}
// Both $rebateRate and $profile->rebate_limit are ratios (0-1)
$limit = (float) $profile->rebate_limit;
if ($rebateRate > $limit) {
throw ValidationException::withMessages([
'rebate_rate' => ['exceeds_limit'],
]);
}
if ($extraRebateRate > 0 && ! $profile->can_grant_extra_rebate) {
throw ValidationException::withMessages([
'extra_rebate_rate' => ['not_allowed'],
]);
}
}
}