diff --git a/app/Http/Controllers/Admin/TahesabController.php b/app/Http/Controllers/Admin/TahesabController.php index 137a7c7..1e0bda5 100644 --- a/app/Http/Controllers/Admin/TahesabController.php +++ b/app/Http/Controllers/Admin/TahesabController.php @@ -109,9 +109,18 @@ class TahesabController extends Controller ->mapWithKeys(fn ($m) => [ltrim((string) $m, '0') => true]) ->toArray(); + // کدهای مشتریِ قبلاً ثبت‌شده در پنل (برای جلوگیری از تکرار) + $panelCodes = Customer::whereNull('deleted_at') + ->whereNotNull('moshtari_code') + ->pluck('moshtari_code') + ->mapWithKeys(fn ($c) => [(string) $c => true]) + ->toArray(); + $tahesabOnlyList = []; foreach ($tahesabCustomers as $item) { if (! is_array($item)) continue; + $code = $item['Code'] ?? null; + if ($code !== null && isset($panelCodes[(string) $code])) continue; $tel = ltrim((string) ($item['Tel'] ?? ''), '0'); if ($tel && isset($panelMobiles[$tel])) continue; $tahesabOnlyList[] = [ @@ -186,7 +195,15 @@ class TahesabController extends Controller $errors = []; foreach ($validated['customers'] as $item) { - $mobile = '0' . ltrim((string) ($item['tel'] ?? ''), '0'); + // تلفن خالی نباید به موبایل «0» تبدیل شود (وگرنه روی unique برخورد می‌کند) + $tel = trim((string) ($item['tel'] ?? '')); + $mobile = $tel !== '' ? '0' . ltrim($tel, '0') : null; + + // اگر مشتری با همین کد قبلاً ثبت شده، عملیات موفق در نظر گرفته می‌شود (idempotent) + if (Customer::whereNull('deleted_at')->where('moshtari_code', $item['code'])->exists()) { + $success++; + continue; + } // کد معرف یکتا $characters = '0123456789abcdefghijklmnopqrstuvwxyz';