2024-11-27 11:16:45 +07:00
|
|
|
<?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;
|
2024-12-11 12:29:37 +07:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2024-11-27 11:16:45 +07:00
|
|
|
|
|
|
|
|
class User extends Authenticatable
|
|
|
|
|
{
|
|
|
|
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
|
|
|
|
use HasFactory, Notifiable;
|
2024-12-11 12:29:37 +07:00
|
|
|
use SoftDeletes;
|
2024-11-27 11:16:45 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array<int, string>
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
|
|
|
|
'email',
|
2024-12-11 12:29:37 +07:00
|
|
|
'username',
|
2024-11-27 11:16:45 +07:00
|
|
|
'password',
|
2024-12-11 12:29:37 +07:00
|
|
|
'phone',
|
2026-06-01 15:01:03 +07:00
|
|
|
'region_id',
|
|
|
|
|
'cabang_id'
|
2024-11-27 11:16:45 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be hidden for serialization.
|
|
|
|
|
*
|
|
|
|
|
* @var array<int, string>
|
|
|
|
|
*/
|
|
|
|
|
protected $hidden = [
|
|
|
|
|
'password',
|
|
|
|
|
'remember_token',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'email_verified_at' => 'datetime',
|
|
|
|
|
'password' => 'hashed',
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-06-01 15:01:03 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper untuk mendapatkan ID Cabang & Region milik user
|
|
|
|
|
*/
|
|
|
|
|
public function getMyCabangAndRegionId(): array
|
|
|
|
|
{
|
|
|
|
|
// Jika User punya cabang_id (Level AM / MR)
|
|
|
|
|
if ($this->cabang_id) {
|
|
|
|
|
$cabang = \DB::table('cabang')->where('id', $this->cabang_id)->first();
|
|
|
|
|
return [
|
|
|
|
|
'cabang' => (int) $this->cabang_id,
|
|
|
|
|
'region' => $cabang ? (int) $cabang->region_id : null
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Jika User level Region (Admin Region / MOM Region)
|
|
|
|
|
// Pastikan kolom 'region_id' ada di tabel users Anda
|
|
|
|
|
return [
|
|
|
|
|
'cabang' => null,
|
|
|
|
|
'region' => $this->region_id ? (int) $this->region_id : null
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function cabangs()
|
|
|
|
|
{
|
|
|
|
|
// Relasi Many-to-Many melalui tabel pivot user_has_cabang
|
|
|
|
|
return $this->belongsToMany(Cabang::class, 'user_has_cabang', 'user_id', 'cabang_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mendapatkan ID Region berdasarkan penugasan cabang pertama
|
|
|
|
|
*/
|
|
|
|
|
public function getAssignedRegionId()
|
|
|
|
|
{
|
|
|
|
|
$firstAssignment = $this->cabangs()->first();
|
|
|
|
|
return $firstAssignment ? $firstAssignment->region_id : null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 11:16:45 +07:00
|
|
|
}
|