From 49727c5e55d190870322771c6f95a0b2aabcc08c Mon Sep 17 00:00:00 2001 From: mohammad sedighi Date: Fri, 10 Jul 2026 17:01:57 +0330 Subject: [PATCH] fix pargar 2 --- .../Pargar/PargarInboundController.php | 53 ++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/Pargar/PargarInboundController.php b/app/Http/Controllers/Pargar/PargarInboundController.php index 92aa0ed..54c718e 100644 --- a/app/Http/Controllers/Pargar/PargarInboundController.php +++ b/app/Http/Controllers/Pargar/PargarInboundController.php @@ -110,10 +110,7 @@ class PargarInboundController extends Controller } $customer->pargar_account_id = $accountId; - if ($fullName !== '') { - $customer->fname = $fullName; - $customer->name = $fullName; - } + $this->applyPargarCustomerName($customer, $fullName); $customer->save(); $messages[] = "کد حسابداری مشتری {$fullName} ویرایش شد"; continue; @@ -126,8 +123,7 @@ class PargarInboundController extends Controller } if ($fullName !== '') { - $byAccount->fname = $fullName; - $byAccount->name = $fullName; + $this->applyPargarCustomerName($byAccount, $fullName); } $byAccount->mobile = $mobile; if (empty($byAccount->password)) { @@ -140,8 +136,7 @@ class PargarInboundController extends Controller try { $customer = new Customer(); - $customer->fname = $fullName !== '' ? $fullName : 'مشتری'; - $customer->name = $customer->fname; + $this->applyPargarCustomerName($customer, $fullName !== '' ? $fullName : 'مشتری'); $customer->mobile = $mobile; $customer->pargar_account_id = $accountId; $customer->active = true; @@ -315,7 +310,7 @@ class PargarInboundController extends Controller ->whereNull('deleted_at') ->whereNull('pargar_account_id') ->limit(100) - ->get(['id', 'fname', 'lname', 'mobile']); + ->get(['id', 'fname', 'lname', 'name', 'mobile']); if ($customers->isEmpty()) { return response()->json([ @@ -326,12 +321,19 @@ class PargarInboundController extends Controller } $rows = $customers->map(function (Customer $customer) { - $nameParts = preg_split('/\s+/', trim((string) $customer->name), 2) ?: ['', '']; + $fname = trim((string) ($customer->fname ?? '')); + $lname = trim((string) ($customer->lname ?? '')); + + if ($fname === '' && $lname === '') { + $nameParts = preg_split('/\s+/', trim((string) ($customer->name ?? '')), 2) ?: ['', '']; + $fname = trim((string) ($nameParts[0] ?? '')); + $lname = trim((string) ($nameParts[1] ?? '')); + } return [ 'id' => (string) $customer->id, - 'name' => $nameParts[0] ?? '', - 'family' => $nameParts[1] ?? '', + 'name' => $fname, + 'family' => $lname, 'mobile' => (string) $customer->mobile, ]; }); @@ -428,6 +430,33 @@ class PargarInboundController extends Controller return trim(str_replace(array_merge($persian, $arabic), array_merge($latin, $latin), $value)); } + private function applyPargarCustomerName(Customer $customer, string $fullName): void + { + $fullName = trim($fullName); + if ($fullName === '') { + return; + } + + [$fname, $lname] = $this->splitPargarFullName($fullName); + $customer->fname = $fname; + $customer->lname = $lname; + // mutator مدل Customer مقدار name را از fname + lname می‌سازد + $customer->name = trim("$fname $lname"); + } + + /** + * @return array{0: string, 1: string} + */ + private function splitPargarFullName(string $fullName): array + { + $parts = preg_split('/\s+/u', trim($fullName), 2) ?: ['', '']; + + return [ + trim((string) ($parts[0] ?? '')), + trim((string) ($parts[1] ?? '')), + ]; + } + private function uniqueReferralId(): string { do {