Introduces a new rank management system including the Rank model, migrations for the ranks table and user IP address, Livewire components for managing and displaying ranks, and related Blade views. Updates navigation and routes to include the new 'Gestión Rangos' section, and enhances the ShareCodeComponent with user data. Also adds a reusable input field component for forms.
150 lines
3.2 KiB
PHP
Executable File
150 lines
3.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Illuminate\Support\Str;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasApiTokens, HasFactory, Notifiable;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'cedula',
|
|
'estado',
|
|
'img_perfil',
|
|
'rol_id',
|
|
'subvendedor_id',
|
|
'subvendedor',
|
|
'ip_address',
|
|
|
|
'referral_code',
|
|
'parent_id',
|
|
|
|
'rank_id',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
protected $casts = [
|
|
'email_verified_at' => 'datetime',
|
|
];
|
|
|
|
public function rol()
|
|
{
|
|
return $this->belongsTo(Role::class);
|
|
}
|
|
|
|
public function roles()
|
|
{
|
|
return $this->hasMany(Role::class, 'subvendedor_id');
|
|
}
|
|
|
|
public function saldo()
|
|
{
|
|
return $this->hasOne(Saldo::class, 'usuario_id');
|
|
}
|
|
|
|
public function modificaciones()
|
|
{
|
|
return $this->hasOne(Configuracione::class, 'usermodifico_id');
|
|
}
|
|
|
|
public function permisos()
|
|
{
|
|
return $this->hasMany(Permiso::class, 'usuario_id');
|
|
}
|
|
|
|
public function preferenciales()
|
|
{
|
|
return $this->hasMany(Preferencial::class, 'usuario_id');
|
|
}
|
|
|
|
public function historiales_venta()
|
|
{
|
|
return $this->hasMany(Historiale::class, 'vendedor_id');
|
|
}
|
|
|
|
public function historiales_compra()
|
|
{
|
|
return $this->hasMany(Historiale::class, 'cliente_id');
|
|
}
|
|
|
|
public function recargas()
|
|
{
|
|
return $this->hasMany(recarga::class, 'usuario_id');
|
|
}
|
|
|
|
public function usuario_tarifas()
|
|
{
|
|
return $this->hasMany(Usuario_tarifa::class, 'usuario_id');
|
|
}
|
|
|
|
public function usuario_promos()
|
|
{
|
|
return $this->hasMany(Usuario_promo::class, 'usuario_id');
|
|
}
|
|
|
|
public function countActiveTarifas()
|
|
{
|
|
return $this->usuario_tarifas()->where('visible', 'true')->count();
|
|
}
|
|
|
|
public function countActivePromos()
|
|
{
|
|
return $this->usuario_promos()->where('visible', 'true')->count();
|
|
}
|
|
|
|
public function subvendedor_relacion()
|
|
{
|
|
return $this->belongsTo(User::class, 'subvendedor_id');
|
|
}
|
|
|
|
public function subusuarios()
|
|
{
|
|
return $this->hasMany(User::class, 'subvendedor_id');
|
|
}
|
|
|
|
// Referral relationships
|
|
public function parent()
|
|
{
|
|
return $this->belongsTo(User::class, 'parent_id');
|
|
}
|
|
|
|
public function children()
|
|
{
|
|
return $this->hasMany(User::class, 'parent_id')->orderBy('created_at', 'desc');
|
|
}
|
|
|
|
public function rank()
|
|
{
|
|
return $this->belongsTo(Rank::class, 'rank_id');
|
|
}
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($user) {
|
|
if (empty($user->referral_code)) {
|
|
do {
|
|
$code = strtoupper(Str::random(8));
|
|
} while (self::where('referral_code', $code)->exists());
|
|
|
|
$user->referral_code = $code;
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|