Files
boursecu_server/app/Models/Customer.php
T

235 lines
5.8 KiB
PHP
Raw Normal View History

2026-01-24 14:03:11 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Hash;
use Rooyesh\Wallet\models\Wallet;
use Illuminate\Foundation\Auth\User as Authenticatable;
2026-07-02 16:12:39 +03:30
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
2026-01-24 14:03:11 +00:00
use Tymon\JWTAuth\Contracts\JWTSubject;
class Customer extends Authenticatable implements JWTSubject
{
use HasFactory,SoftDeletes,Selector;
protected $fillable = [
'fname',
'lname',
'name',
'active',
'finger_print',
'mobile',
'email',
'avatar_id',
'last_seen_at',
'default_address_id',
'sts',
'total_transaction',
'token_sms',
'password',
'orders_count',
'birth_date',
'shop_name',
'phone_number',
'code',
'city',
'province',
'n_code',
'is_archive',
'gram',
'trade_active',//آیا امکان معامله دارد؟
'type',
'referral_id',
'referral_to_id',
2026-06-06 17:05:39 +03:30
'total_referrals',
2026-01-24 14:03:11 +00:00
'credential_id',
'moshtari_code',
2026-07-05 16:43:34 +03:30
'pargar_account_id',
2026-01-24 14:03:11 +00:00
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'token_sms',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
"active" => "boolean",
"gold" => "float",
"is_archive" => "boolean",
"finger_print" => "boolean",
];
static $TYPE = [
1=>'همکار',
2=>'خانگی',
3=>'سرمایه گذار'
];
static $TYPES = [
1=>'همکار',
2=>'خانگی',
3=>'سرمایه گذار'
];
public function setNameAttribute($value = null)
{
$fname = $this->attributes['fname'] ?? '';
$lname = $this->attributes['lname'] ?? '';
$this->attributes['name'] = trim("$fname $lname");
}
public function wallet()
{
return $this->hasMany(Wallet::class ,'owner')->with(['fund','product'])->select(['cash', 'fund_id','product_id','gold', 'owner']);
}
public function orders()
{
return $this->hasMany(Order::class, 'customer_id');
}
2026-01-24 14:03:11 +00:00
public function user()
{
return $this->belongsTo(User::class)->select(['id','name']);
}
public function referral()
{
return $this->belongsTo(Customer::class, 'referral_to_id', 'referral_id');
}
2026-06-07 18:27:04 +03:30
/** معرف واقعی — با جستجوی صریح و جلوگیری از برگشت خود کاربر */
public function referrer(): ?self
{
if (empty($this->referral_to_id)) {
return null;
}
return static::query()
->where('referral_id', $this->referral_to_id)
->where('id', '!=', $this->id)
->first();
}
2026-01-24 14:03:11 +00:00
// hash password
public function setPasswordAttribute($value){
$this->attributes['password'] = Hash::make($value);
}
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
public function tahesab()
{
return $this->hasMany(Tahesab::class, 'customer_id', 'id');
}
2026-01-31 09:09:32 +00:00
public function customer_settings()
{
return $this->hasMany(CustomersSetting::class, 'agent_id');
}
public function package()
{
return $this->belongsTo(Package::class);
}
public function agent()
{
return $this->belongsTo(Customer::class, 'agent_id');
}
2026-01-24 14:03:11 +00:00
2026-07-02 16:12:39 +03:30
protected static function booted(): void
{
static::saving(function (self $customer) {
$mobile = self::normalizeMobile($customer->mobile);
if ($mobile === null) {
return;
}
$customer->mobile = $mobile;
self::assertMobileUnique($mobile, $customer->exists ? (int) $customer->id : null);
});
}
public static function normalizeMobile(?string $mobile): ?string
{
2026-07-05 21:48:11 +03:30
$digits = preg_replace('/\D+/', '', trim((string) $mobile));
if ($digits === '') {
return null;
}
2026-07-02 16:12:39 +03:30
2026-07-05 21:48:11 +03:30
if (str_starts_with($digits, '98') && strlen($digits) >= 12) {
$digits = substr($digits, 2);
}
$digits = ltrim($digits, '0');
if ($digits === '') {
return null;
}
return '0' . $digits;
}
public static function findActiveByMobile(?string $mobile, ?int $exceptId = null): ?self
{
$mobile = self::normalizeMobile($mobile);
if ($mobile === null) {
return null;
}
$query = static::query()
->whereNull('deleted_at')
->where('mobile', $mobile);
if ($exceptId !== null) {
$query->where('id', '<>', $exceptId);
}
return $query->first();
2026-07-02 16:12:39 +03:30
}
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
{
$mobile = self::normalizeMobile($mobile);
if ($mobile === null) {
return false;
}
$query = static::query()
->where('mobile', $mobile)
->whereNull('deleted_at');
if ($exceptId !== null) {
$query->where('id', '<>', $exceptId);
}
return $query->exists();
}
public static function assertMobileUnique(?string $mobile, ?int $exceptId = null): void
{
if (self::isMobileTaken($mobile, $exceptId)) {
throw new UnprocessableEntityHttpException('این شماره موبایل قبلاً ثبت شده است');
}
}
2026-01-24 14:03:11 +00:00
}