pdv.backend/app/Models/ClientTier.php
2026-01-28 16:46:31 -06:00

77 lines
1.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ClientTier extends Model
{
protected $fillable = [
'tier_name',
'min_purchase_amount',
'max_purchase_amount',
'discount_percentage',
'is_active',
];
protected $casts = [
'min_purchase_amount' => 'decimal:2',
'max_purchase_amount' => 'decimal:2',
'discount_percentage' => 'decimal:2',
'is_active' => 'boolean',
];
/**
* Clientes que pertenecen a este nivel
*/
public function clients(): HasMany
{
return $this->hasMany(Client::class, 'tier_id');
}
/**
* Niveles nuevos en el historial
*/
public function tierHistories(): HasMany
{
return $this->hasMany(ClientTierHistory::class, 'new_tier_id');
}
/**
* Niveles antiguos en el historial
*/
public function oldTierHistories(): HasMany
{
return $this->hasMany(ClientTierHistory::class, 'old_tier_id');
}
/**
* Alcance de consulta para niveles activos
*/
public function scopeActive($query)
{
return $query->where('is_active', true);
}
/**
* Verifica si un monto de compra califica para este nivel
*/
public function qualifies(float $amount): bool
{
if (!$this->is_active) {
return false;
}
if ($amount < $this->min_purchase_amount) {
return false;
}
if ($this->max_purchase_amount !== null && $amount > $this->max_purchase_amount) {
return false;
}
return true;
}
}