all customer from pargar fixing 2

This commit is contained in:
2026-07-22 00:31:38 +03:30
parent 1476f6efb1
commit 01031e9e01
7 changed files with 273 additions and 22 deletions
+64 -4
View File
@@ -201,13 +201,55 @@ class Customer extends Authenticatable implements JWTSubject
$query = static::query()
->whereNull('deleted_at')
->where('mobile', $mobile);
->whereIn('mobile', self::mobileLookupVariants($mobile));
if ($exceptId !== null) {
$query->where('id', '<>', $exceptId);
}
return $query->first();
return $query->orderByDesc('id')->first();
}
/**
* مشتریِ ازقبل‌واردشده از حسابداری که هنوز وارد اپ نشده — قابل claim در ثبت‌نام.
* شامل soft-deleted با کد حسابداری (برای جلوگیری از ساخت ردیف تکراری).
*/
public static function findClaimableImportedByMobile(?string $mobile): ?self
{
$normalized = self::normalizeMobile($mobile);
if ($normalized === null) {
return null;
}
$variants = self::mobileLookupVariants($normalized);
$active = static::query()
->whereNull('deleted_at')
->whereIn('mobile', $variants)
->whereNull('last_seen_at')
->orderByDesc('id')
->first();
if ($active) {
return $active;
}
return static::onlyTrashed()
->whereIn('mobile', $variants)
->whereNull('last_seen_at')
->where(function ($q) {
$q->where(function ($inner) {
$inner->whereNotNull('pargar_account_id')
->where('pargar_account_id', '!=', '')
->whereNotIn('pargar_account_id', ['0', '-1']);
})->orWhere(function ($inner) {
$inner->whereNotNull('moshtari_code')
->where('moshtari_code', '!=', '')
->whereNotIn('moshtari_code', ['0', '-1']);
});
})
->orderByDesc('id')
->first();
}
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
@@ -240,14 +282,16 @@ class Customer extends Authenticatable implements JWTSubject
return $query->exists();
}
private static function mobileLookupVariants(string $normalized): array
public static function mobileLookupVariants(string $normalized): array
{
$digits = ltrim($normalized, '0');
return array_values(array_unique(array_filter([
$normalized,
$digits,
'0' . $digits,
'0'.$digits,
'98'.$digits,
'+98'.$digits,
])));
}
@@ -275,4 +319,20 @@ class Customer extends Authenticatable implements JWTSubject
return self::isValidAccountingCode($this->pargar_account_id);
}
public function hasValidMoshtariCode(): bool
{
return self::isValidAccountingCode($this->moshtari_code);
}
public function hasAccountingLink(): bool
{
return $this->hasValidPargarAccountId() || $this->hasValidMoshtariCode();
}
/** هنوز وارد اپ نشده — معمولاً از import حسابداری آمده */
public function isUnclaimedAppIdentity(): bool
{
return $this->last_seen_at === null;
}
}