275 lines
7.1 KiB
PHP
275 lines
7.1 KiB
PHP
<?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;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
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',
|
|
'total_referrals',
|
|
'credential_id',
|
|
'moshtari_code',
|
|
'pargar_account_id',
|
|
|
|
];
|
|
/**
|
|
* 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', 'owner_type']);
|
|
}
|
|
|
|
public function orders()
|
|
{
|
|
return $this->hasMany(Order::class, 'customer_id');
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class)->select(['id','name']);
|
|
}
|
|
public function referral()
|
|
{
|
|
return $this->belongsTo(Customer::class, 'referral_to_id', 'referral_id');
|
|
}
|
|
|
|
/** معرف واقعی — با جستجوی صریح و جلوگیری از برگشت خود کاربر */
|
|
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();
|
|
}
|
|
|
|
// 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');
|
|
}
|
|
|
|
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');
|
|
}
|
|
|
|
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
|
|
{
|
|
$digits = preg_replace('/\D+/', '', trim((string) $mobile));
|
|
if ($digits === '') {
|
|
return null;
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
|
|
{
|
|
return self::isMobileTakenOnTable(self::class, $mobile, $exceptId);
|
|
}
|
|
|
|
public static function isMobileTakenOnTable(string $modelClass, ?string $mobile, ?int $exceptId = null): bool
|
|
{
|
|
$normalized = self::normalizeMobile($mobile);
|
|
if ($normalized === null) {
|
|
return false;
|
|
}
|
|
|
|
if ($exceptId !== null) {
|
|
$current = $modelClass::query()->whereNull('deleted_at')->find($exceptId);
|
|
if ($current !== null && self::normalizeMobile($current->mobile ?? '') === $normalized) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
$query = $modelClass::query()
|
|
->whereNull('deleted_at')
|
|
->whereIn('mobile', self::mobileLookupVariants($normalized));
|
|
|
|
if ($exceptId !== null) {
|
|
$query->where('id', '<>', $exceptId);
|
|
}
|
|
|
|
return $query->exists();
|
|
}
|
|
|
|
private static function mobileLookupVariants(string $normalized): array
|
|
{
|
|
$digits = ltrim($normalized, '0');
|
|
|
|
return array_values(array_unique(array_filter([
|
|
$normalized,
|
|
$digits,
|
|
'0' . $digits,
|
|
])));
|
|
}
|
|
|
|
public static function assertMobileUnique(?string $mobile, ?int $exceptId = null): void
|
|
{
|
|
if (self::isMobileTaken($mobile, $exceptId)) {
|
|
throw new UnprocessableEntityHttpException('این شماره موبایل قبلاً ثبت شده است');
|
|
}
|
|
}
|
|
|
|
/** کد معتبر حسابداری (پرگار/تهحساب) — خالی و 0 و -1 نامعتبرند */
|
|
public static function isValidAccountingCode(mixed $value): bool
|
|
{
|
|
if ($value === null) {
|
|
return false;
|
|
}
|
|
|
|
$code = trim((string) $value);
|
|
|
|
return $code !== '' && ! in_array($code, ['0', '-1'], true);
|
|
}
|
|
|
|
public function hasValidPargarAccountId(): bool
|
|
{
|
|
return self::isValidAccountingCode($this->pargar_account_id);
|
|
}
|
|
|
|
}
|