From 8fa3df94caa54ed0b06c0e684010e8ef2485582f Mon Sep 17 00:00:00 2001 From: mohammad sedighi Date: Mon, 29 Jun 2026 01:13:53 +0330 Subject: [PATCH] work on moving customers from tahesab 5 --- .../Controllers/Admin/TahesabController.php | 63 ++++++++++++++++--- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Admin/TahesabController.php b/app/Http/Controllers/Admin/TahesabController.php index 2b15862..e3f6110 100644 --- a/app/Http/Controllers/Admin/TahesabController.php +++ b/app/Http/Controllers/Admin/TahesabController.php @@ -117,16 +117,19 @@ class TahesabController extends Controller ->toArray(); $tahesabOnlyList = []; + $debugSample = []; foreach ($tahesabCustomers as $item) { if (! is_array($item)) continue; + if (count($debugSample) < 3) $debugSample[] = $item; // نمونهٔ خام برای تشخیص نام فیلدها $code = $item['Code'] ?? null; if ($code !== null && isset($panelCodes[(string) $code])) continue; - $tel = ltrim((string) ($item['Tel'] ?? ''), '0'); + $phone = $this->extractTahesabPhone($item); + $tel = ltrim($phone, '0'); if ($tel && isset($panelMobiles[$tel])) continue; $tahesabOnlyList[] = [ 'code' => $item['Code'] ?? null, 'name' => $item['Name'] ?? '', - 'tel' => $item['Tel'] ?? '', + 'tel' => $phone, 'gid' => $item['GID'] ?? null, 'group' => $item['GoroupName'] ?? '', ]; @@ -141,7 +144,7 @@ class TahesabController extends Controller ]; } - return response()->json([ + $payload = [ 'panel_total' => $panelTotal, 'panel_registered' => $panelRegistered, 'panel_only' => $panelOnly, @@ -149,7 +152,35 @@ class TahesabController extends Controller 'tahesab_only_count' => $tahesabOnlyCount, 'panel_only_list' => $panelOnlyList, 'tahesab_only_list' => $tahesabOnlyResult, - ]); + ]; + + // برای تشخیص نام دقیق فیلدها: ?include_tahesab=1&raw=1 + if ($request->boolean('include_tahesab') && $request->boolean('raw')) { + $payload['tahesab_debug_sample'] = $debugSample ?? []; + } + + return response()->json($payload); + } + + /** شماره تماس را از هر کلید محتمل در رکورد مشتری ته‌حساب استخراج می‌کند */ + private function extractTahesabPhone(array $item): string + { + $candidates = ['Mobile', 'CellPhone', 'Cell', 'Tel', 'Tell', 'Phone', 'MobileNo', 'Telephone', 'Mobile1']; + foreach ($candidates as $key) { + if (isset($item[$key]) && trim((string) $item[$key]) !== '') { + return trim((string) $item[$key]); + } + } + // جستجوی عمومی: هر کلیدی که شامل mobile/tel/phone/cell باشد و مقدار داشته باشد + foreach ($item as $k => $v) { + if ($v === null || trim((string) $v) === '') continue; + $lk = strtolower((string) $k); + if (str_contains($lk, 'mobile') || str_contains($lk, 'tel') + || str_contains($lk, 'phone') || str_contains($lk, 'cell')) { + return trim((string) $v); + } + } + return ''; } public function syncPanelToTahesab(): \Illuminate\Http\JsonResponse @@ -199,8 +230,26 @@ class TahesabController extends Controller $tel = trim((string) ($item['tel'] ?? '')); $mobile = $tel !== '' ? '0' . ltrim($tel, '0') : ''; - // اگر مشتری با همین کد قبلاً ثبت شده، عملیات موفق در نظر گرفته می‌شود (idempotent) - if (Customer::whereNull('deleted_at')->where('moshtari_code', $item['code'])->exists()) { + $fullName = trim($item['name']); + + // اگر مشتری با همین کد قبلاً ثبت شده: فیلدهای ناقص (نام/موبایل/رمز) را تکمیل کن + $existing = Customer::whereNull('deleted_at')->where('moshtari_code', $item['code'])->first(); + if ($existing) { + $changed = false; + if (($existing->name === null || $existing->name === '') && $fullName !== '') { + $existing->fname = $fullName; + $existing->lname = (string) ($existing->lname ?? ''); + $existing->name = $fullName; // mutator name را از fname+lname می‌سازد + $changed = true; + } + if (($existing->mobile === null || $existing->mobile === '') && $mobile !== '') { + $existing->mobile = $mobile; + if (empty($existing->password)) { + $existing->password = $mobile; // رمز = شماره موبایل + } + $changed = true; + } + if ($changed) $existing->save(); $success++; continue; } @@ -215,8 +264,6 @@ class TahesabController extends Controller } while (Customer::where('referral_id', $referralId)->exists()); try { - $fullName = trim($item['name']); - // توجه: مدل Customer یک mutator روی name دارد که name را از fname+lname می‌سازد // و مقدار مستقیم name را نادیده می‌گیرد. پس باید fname را قبل از name ست کنیم. $customer = new Customer();