Files
CKB/app/Models/User.php

305 lines
7.6 KiB
PHP
Executable File

<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'role',
'dealer_id',
'password',
'role_id'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get all of the transactions for the User
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function transactions()
{
return $this->hasMany(Transaction::class, 'user_id', 'id');
}
/**
* Get all of the sa_transactions for the User
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function sa_transactions()
{
return $this->hasMany(Transaction::class, 'user_sa_id', 'id');
}
/**
* Get the dealer associated with the User
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function dealer()
{
return $this->hasOne(Dealer::class, 'id', 'dealer_id');
}
/**
* Get the role associated with the User
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function role()
{
return $this->belongsTo(Role::class, 'role_id');
}
/**
* Check if user has a specific role
*
* @param string $roleName
* @return bool
*/
public function hasRole($roleName)
{
// If role_id is 0 or null, user has no role
if (!$this->role_id) {
return false;
}
// For admin role, we can check if user has admin privileges
if (strtolower($roleName) === 'admin') {
return $this->isAdmin();
}
// Load role if not already loaded
if (!$this->relationLoaded('role')) {
$this->load('role');
}
return $this->role && strtolower($this->role->name) === strtolower($roleName);
}
/**
* Check if user is admin by checking admin privileges
*
* @return bool
*/
public function isAdmin()
{
// Check if user has admin privileges by checking if they can access admin area
try {
$adminPrivilege = \App\Models\Privilege::join('menus', 'menus.id', '=', 'privileges.menu_id')
->where('menus.link', 'adminarea')
->where('privileges.role_id', $this->role_id)
->where('privileges.view', 1)
->first();
return $adminPrivilege !== null;
} catch (\Exception $e) {
return false;
}
}
/**
* Get all KPI targets for the User
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function kpiTargets()
{
return $this->hasMany(KpiTarget::class);
}
/**
* Get all KPI achievements for the User
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function kpiAchievements()
{
return $this->hasMany(KpiAchievement::class);
}
/**
* Check if user is mechanic
*
* @return bool
*/
public function isMechanic()
{
return $this->hasRole('mechanic');
}
/**
* Get current KPI target (no longer filtered by year/month)
*
* @return KpiTarget|null
*/
public function getCurrentKpiTarget()
{
return $this->kpiTargets()
->where('is_active', true)
->first();
}
/**
* Get KPI achievement for specific year and month
*
* @param int $year
* @param int $month
* @return KpiAchievement|null
*/
public function getKpiAchievement($year = null, $month = null)
{
$year = $year ?? now()->year;
$month = $month ?? now()->month;
return $this->kpiAchievements()
->where('year', $year)
->where('month', $month)
->first();
}
public function accessibleDealers()
{
if (!$this->role_id) {
return collect();
}
// Load role with dealers
if (!$this->relationLoaded('role')) {
$this->load('role.dealers');
}
// If user has specific dealer_id, check if role allows access
if ($this->dealer_id) {
if ($this->role && $this->role->hasDealer($this->dealer_id)) {
return Dealer::where('id', $this->dealer_id)->get();
}
return collect();
}
// If no specific dealer_id, return all dealers accessible by role
return $this->role ? $this->role->dealers : collect();
}
public function canAccessDealer($dealerId)
{
if (!$this->role_id) {
return false;
}
// Load role with dealers
if (!$this->relationLoaded('role')) {
$this->load('role.dealers');
}
return $this->role && $this->role->hasDealer($dealerId);
}
public function getPrimaryDealer()
{
if ($this->dealer_id && $this->canAccessDealer($this->dealer_id)) {
return $this->dealer;
}
return null;
}
/**
* Get all accessible menus for a specific role
*
* @param int $roleId
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getAccessibleMenus($roleId)
{
return \App\Models\Privilege::join('menus', 'privileges.menu_id', '=', 'menus.id')
->where('privileges.role_id', $roleId)
->where('privileges.view', 1)
->select('menus.*', 'privileges.view', 'privileges.create', 'privileges.update', 'privileges.delete')
->orderBy('menus.id')
->get();
}
/**
* Get accessible menus for current user
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getMyAccessibleMenus()
{
if (!$this->role_id) {
return collect();
}
return self::getAccessibleMenus($this->role_id);
}
/**
* Check if user can access specific menu
*
* @param string $menuLink
* @return bool
*/
public function canAccessMenu($menuLink)
{
if (!$this->role_id) {
return false;
}
return \App\Models\Privilege::join('menus', 'privileges.menu_id', '=', 'menus.id')
->where('privileges.role_id', $this->role_id)
->where('menus.link', $menuLink)
->where('privileges.view', 1)
->exists();
}
/**
* Check if role can access specific menu (static method)
*
* @param int $roleId
* @param string $menuLink
* @return bool
*/
public static function roleCanAccessMenu($roleId, $menuLink)
{
return \App\Models\Privilege::join('menus', 'privileges.menu_id', '=', 'menus.id')
->where('privileges.role_id', $roleId)
->where('menus.link', $menuLink)
->where('privileges.view', 1)
->exists();
}
}