140 lines
4.6 KiB
PHP
140 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Dyrynda\Database\Support\CascadeSoftDeletes;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Veelasky\LaravelHashId\Eloquent\HashableId;
|
|
|
|
class Tier extends Model
|
|
{
|
|
use CascadeSoftDeletes, HasFactory, HashableId, SoftDeletes;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $cascadeDeletes = ['memberships'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'min_points' => 'int',
|
|
'max_points' => 'int',
|
|
];
|
|
}
|
|
|
|
public static function isOverlap(int $minPoints, ?int $maxPoints, ?int $ignoreId = null): bool
|
|
{
|
|
$maxPointsCheck = $maxPoints ?? PHP_INT_MAX;
|
|
|
|
return self::when($ignoreId, fn ($query) => $query->where('id', '!=', $ignoreId))
|
|
->get()
|
|
->contains(function ($tier) use ($minPoints, $maxPointsCheck) {
|
|
$tierMax = $tier->max_points ?? PHP_INT_MAX;
|
|
|
|
return ($minPoints <= $tierMax) && ($maxPointsCheck >= $tier->min_points);
|
|
});
|
|
}
|
|
|
|
public function memberships(): HasMany
|
|
{
|
|
return $this->hasMany(Membership::class);
|
|
}
|
|
|
|
public function rewards(): HasMany
|
|
{
|
|
return $this->hasMany(TierReward::class);
|
|
}
|
|
|
|
public static function getHighestTier()
|
|
{
|
|
return self::orderByDesc('min_points')->first();
|
|
}
|
|
|
|
public static function updatePreviousTierMaxPoints(int $newMinPoints): void
|
|
{
|
|
$previousTier = self::whereNull('max_points')
|
|
->orWhere('max_points', '>=', $newMinPoints)
|
|
->orderByDesc('min_points')
|
|
->first();
|
|
|
|
if ($previousTier && is_null($previousTier->max_points)) {
|
|
$previousTier->update(['max_points' => $newMinPoints - 1]);
|
|
}
|
|
}
|
|
|
|
public static function validateTierPoints(int $minPoints, ?int $maxPoints = null, ?int $ignoreId = null): array
|
|
{
|
|
$errors = [];
|
|
|
|
// Get the highest tier (excluding current if updating)
|
|
$highestTier = self::when($ignoreId, fn ($query) => $query->where('id', '!=', $ignoreId))
|
|
->orderByDesc('min_points')
|
|
->first();
|
|
|
|
// If there's a highest tier with null max_points, it should be updated first
|
|
if ($highestTier && is_null($highestTier->max_points)) {
|
|
if ($minPoints <= $highestTier->min_points) {
|
|
$errors[] = "Min. poin harus lebih besar dari {$highestTier->min_points} (tier {$highestTier->name})";
|
|
}
|
|
} elseif ($highestTier) {
|
|
$requiredMinPoints = ($highestTier->max_points ?? $highestTier->min_points) + 1;
|
|
if ($minPoints <= $highestTier->max_points) {
|
|
$errors[] = "Min. poin harus lebih besar dari {$highestTier->max_points} (tier {$highestTier->name})";
|
|
}
|
|
}
|
|
|
|
// Check for overlap with existing tiers
|
|
if (self::isOverlap($minPoints, $maxPoints, $ignoreId)) {
|
|
$errors[] = 'Rentang poin tier tumpang tindih dengan tier lain';
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
|
|
public static function reorderTierChain(): void
|
|
{
|
|
$tiers = self::orderBy('min_points')->get();
|
|
|
|
foreach ($tiers as $index => $tier) {
|
|
if ($index < $tiers->count() - 1) {
|
|
// Set max_points to min_points of next tier - 1
|
|
$nextTier = $tiers[$index + 1];
|
|
$tier->update(['max_points' => $nextTier->min_points - 1]);
|
|
} else {
|
|
// Last tier should have null max_points
|
|
$tier->update(['max_points' => null]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function canDeleteTier(int $tierId): array
|
|
{
|
|
$tier = self::find($tierId);
|
|
if (! $tier) {
|
|
return ['can_delete' => false, 'message' => 'Tier tidak ditemukan'];
|
|
}
|
|
|
|
$tiers = self::orderBy('min_points')->get();
|
|
$tierIndex = $tiers->search(fn ($t) => $t->id === $tierId);
|
|
|
|
// Can always delete the lowest tier (first tier)
|
|
if ($tierIndex === 0) {
|
|
return ['can_delete' => true, 'message' => ''];
|
|
}
|
|
|
|
// Can always delete the highest tier (last tier with null max_points)
|
|
if ($tierIndex === $tiers->count() - 1) {
|
|
return ['can_delete' => true, 'message' => ''];
|
|
}
|
|
|
|
// Cannot delete middle tiers as it would create gaps in the tier chain
|
|
return [
|
|
'can_delete' => false,
|
|
'message' => 'Tidak dapat menghapus tier tengah karena akan mengacaukan sistem poin. Tier harus membentuk rantai yang kontinyu.',
|
|
];
|
|
}
|
|
}
|