added pargar 4

This commit is contained in:
2026-07-05 21:48:11 +03:30
parent af6c751ede
commit 2f48b88cf6
4 changed files with 166 additions and 43 deletions
@@ -226,34 +226,70 @@ class TahesabController extends Controller
$errors = [];
foreach ($validated['customers'] as $item) {
// ستون mobile در دیتابیس NOT NULL است؛ پس برای تلفن خالی رشتهٔ خالی می‌گذاریم نه null
$tel = trim((string) ($item['tel'] ?? ''));
$mobile = $tel !== '' ? '0' . ltrim($tel, '0') : '';
$mobile = Customer::normalizeMobile($item['tel'] ?? '') ?? '';
$fullName = trim($item['name']);
$code = (int) $item['code'];
// اگر مشتری با همین کد قبلاً ثبت شده: فیلدهای ناقص (نام/موبایل/رمز) را تکمیل کن
$existing = Customer::whereNull('deleted_at')->where('moshtari_code', $item['code'])->first();
$existing = Customer::whereNull('deleted_at')->where('moshtari_code', $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 می‌سازد
$existing->name = $fullName;
$changed = true;
}
if (($existing->mobile === null || $existing->mobile === '') && $mobile !== '') {
if ($mobile !== '' && ($existing->mobile === null || $existing->mobile === '')) {
if (Customer::isMobileTaken($mobile, (int) $existing->id)) {
$failed++;
$errors[] = [
'name' => $item['name'],
'code' => $item['code'],
'error' => 'شماره موبایل تکراری است',
];
continue;
}
$existing->mobile = $mobile;
if (empty($existing->password)) {
$existing->password = $mobile; // رمز = شماره موبایل
$existing->password = $mobile;
}
$changed = true;
}
if ($changed) $existing->save();
if ($changed) {
$existing->save();
}
$success++;
continue;
}
if ($mobile !== '') {
$byMobile = Customer::findActiveByMobile($mobile);
if ($byMobile) {
if (empty($byMobile->moshtari_code)) {
$byMobile->moshtari_code = $code;
if ($fullName !== '') {
$byMobile->fname = $fullName;
$byMobile->lname = '';
$byMobile->name = $fullName;
}
if (empty($byMobile->password)) {
$byMobile->password = $mobile;
}
$byMobile->save();
$success++;
continue;
}
$failed++;
$errors[] = [
'name' => $item['name'],
'code' => $item['code'],
'error' => 'شماره موبایل تکراری است',
];
continue;
}
}
// کد معرف یکتا
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
do {
@@ -284,8 +320,10 @@ class TahesabController extends Controller
} catch (\Throwable $e) {
$failed++;
$msg = $e->getMessage();
if ($e instanceof \Illuminate\Database\QueryException && (int) ($e->errorInfo[1] ?? 0) === 1062) {
$msg = 'این مشتری از قبل در پنل ثبت شده است';
if ($e instanceof UnprocessableEntityHttpException) {
$msg = $e->getMessage();
} elseif ($e instanceof \Illuminate\Database\QueryException && (int) ($e->errorInfo[1] ?? 0) === 1062) {
$msg = 'شماره موبایل تکراری است';
}
$errors[] = ['name' => $item['name'], 'code' => $item['code'], 'error' => $msg];
}