added pargar 4
This commit is contained in:
@@ -127,8 +127,7 @@ class PargarController extends Controller
|
|||||||
$errors = [];
|
$errors = [];
|
||||||
|
|
||||||
foreach ($validated['customers'] as $item) {
|
foreach ($validated['customers'] as $item) {
|
||||||
$tel = trim((string) ($item['tel'] ?? ''));
|
$mobile = Customer::normalizeMobile($item['tel'] ?? '') ?? '';
|
||||||
$mobile = $tel !== '' ? '0' . ltrim($tel, '0') : '';
|
|
||||||
$fullName = trim($item['name']);
|
$fullName = trim($item['name']);
|
||||||
$accountId = (string) $item['code'];
|
$accountId = (string) $item['code'];
|
||||||
|
|
||||||
@@ -142,8 +141,19 @@ class PargarController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($mobile !== '') {
|
if ($mobile !== '') {
|
||||||
$byMobile = Customer::whereNull('deleted_at')->where('mobile', $mobile)->first();
|
$byMobile = Customer::findActiveByMobile($mobile);
|
||||||
if ($byMobile) {
|
if ($byMobile) {
|
||||||
|
$currentAccount = (string) ($byMobile->pargar_account_id ?? '');
|
||||||
|
if ($currentAccount !== '' && $currentAccount !== $accountId) {
|
||||||
|
$failed++;
|
||||||
|
$errors[] = [
|
||||||
|
'name' => $item['name'],
|
||||||
|
'code' => $item['code'],
|
||||||
|
'error' => 'شماره موبایل تکراری است',
|
||||||
|
];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$byMobile->pargar_account_id = $accountId;
|
$byMobile->pargar_account_id = $accountId;
|
||||||
if ($fullName !== '') {
|
if ($fullName !== '') {
|
||||||
$byMobile->fname = $fullName;
|
$byMobile->fname = $fullName;
|
||||||
@@ -155,6 +165,19 @@ class PargarController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$byAccount = Customer::whereNull('deleted_at')
|
||||||
|
->where('pargar_account_id', $accountId)
|
||||||
|
->first();
|
||||||
|
if ($byAccount) {
|
||||||
|
$failed++;
|
||||||
|
$errors[] = [
|
||||||
|
'name' => $item['name'],
|
||||||
|
'code' => $item['code'],
|
||||||
|
'error' => 'کد حساب پرگار قبلاً ثبت شده است',
|
||||||
|
];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$customer = new Customer();
|
$customer = new Customer();
|
||||||
$customer->fname = $fullName;
|
$customer->fname = $fullName;
|
||||||
|
|||||||
@@ -226,34 +226,70 @@ class TahesabController extends Controller
|
|||||||
$errors = [];
|
$errors = [];
|
||||||
|
|
||||||
foreach ($validated['customers'] as $item) {
|
foreach ($validated['customers'] as $item) {
|
||||||
// ستون mobile در دیتابیس NOT NULL است؛ پس برای تلفن خالی رشتهٔ خالی میگذاریم نه null
|
$mobile = Customer::normalizeMobile($item['tel'] ?? '') ?? '';
|
||||||
$tel = trim((string) ($item['tel'] ?? ''));
|
|
||||||
$mobile = $tel !== '' ? '0' . ltrim($tel, '0') : '';
|
|
||||||
|
|
||||||
$fullName = trim($item['name']);
|
$fullName = trim($item['name']);
|
||||||
|
$code = (int) $item['code'];
|
||||||
|
|
||||||
// اگر مشتری با همین کد قبلاً ثبت شده: فیلدهای ناقص (نام/موبایل/رمز) را تکمیل کن
|
$existing = Customer::whereNull('deleted_at')->where('moshtari_code', $code)->first();
|
||||||
$existing = Customer::whereNull('deleted_at')->where('moshtari_code', $item['code'])->first();
|
|
||||||
if ($existing) {
|
if ($existing) {
|
||||||
$changed = false;
|
$changed = false;
|
||||||
if (($existing->name === null || $existing->name === '') && $fullName !== '') {
|
if (($existing->name === null || $existing->name === '') && $fullName !== '') {
|
||||||
$existing->fname = $fullName;
|
$existing->fname = $fullName;
|
||||||
$existing->lname = (string) ($existing->lname ?? '');
|
$existing->lname = (string) ($existing->lname ?? '');
|
||||||
$existing->name = $fullName; // mutator name را از fname+lname میسازد
|
$existing->name = $fullName;
|
||||||
$changed = true;
|
$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;
|
$existing->mobile = $mobile;
|
||||||
if (empty($existing->password)) {
|
if (empty($existing->password)) {
|
||||||
$existing->password = $mobile; // رمز = شماره موبایل
|
$existing->password = $mobile;
|
||||||
}
|
}
|
||||||
$changed = true;
|
$changed = true;
|
||||||
}
|
}
|
||||||
if ($changed) $existing->save();
|
if ($changed) {
|
||||||
|
$existing->save();
|
||||||
|
}
|
||||||
$success++;
|
$success++;
|
||||||
continue;
|
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';
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||||
do {
|
do {
|
||||||
@@ -284,8 +320,10 @@ class TahesabController extends Controller
|
|||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
$failed++;
|
$failed++;
|
||||||
$msg = $e->getMessage();
|
$msg = $e->getMessage();
|
||||||
if ($e instanceof \Illuminate\Database\QueryException && (int) ($e->errorInfo[1] ?? 0) === 1062) {
|
if ($e instanceof UnprocessableEntityHttpException) {
|
||||||
$msg = 'این مشتری از قبل در پنل ثبت شده است';
|
$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];
|
$errors[] = ['name' => $item['name'], 'code' => $item['code'], 'error' => $msg];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ use Illuminate\Support\Facades\Schema;
|
|||||||
use Morilog\Jalali\Jalalian;
|
use Morilog\Jalali\Jalalian;
|
||||||
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
||||||
use Rooyesh\Wallet\interfaces\WalletInterface;
|
use Rooyesh\Wallet\interfaces\WalletInterface;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||||
|
|
||||||
class PargarInboundController extends Controller
|
class PargarInboundController extends Controller
|
||||||
{
|
{
|
||||||
@@ -77,23 +78,37 @@ class PargarInboundController extends Controller
|
|||||||
|
|
||||||
$accountId = trim((string) ($row['AccountId'] ?? ''));
|
$accountId = trim((string) ($row['AccountId'] ?? ''));
|
||||||
$fullName = trim((string) ($row['FullName'] ?? ''));
|
$fullName = trim((string) ($row['FullName'] ?? ''));
|
||||||
$mobile = $this->normalizeMobile((string) ($row['CellPhone'] ?? ''));
|
$mobile = Customer::normalizeMobile((string) ($row['CellPhone'] ?? ''));
|
||||||
|
|
||||||
if ($accountId === '' || $mobile === '') {
|
if ($accountId === '' || $mobile === null) {
|
||||||
$messages[] = 'ردیف نامعتبر: AccountId یا CellPhone خالی است';
|
$messages[] = 'ردیف نامعتبر: AccountId یا CellPhone خالی است';
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$customer = Customer::whereNull('deleted_at')
|
$byAccount = Customer::whereNull('deleted_at')
|
||||||
->where('mobile', $mobile)
|
->where('pargar_account_id', $accountId)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
|
if ($byAccount && Customer::normalizeMobile($byAccount->mobile) !== $mobile) {
|
||||||
|
$messages[] = "کد حساب {$accountId} قبلاً برای مشتری دیگری ثبت شده است";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$customer = Customer::findActiveByMobile($mobile);
|
||||||
|
|
||||||
if ($customer) {
|
if ($customer) {
|
||||||
if ((string) ($customer->pargar_account_id ?? '') === $accountId) {
|
$currentAccount = (string) ($customer->pargar_account_id ?? '');
|
||||||
|
|
||||||
|
if ($currentAccount === $accountId) {
|
||||||
$messages[] = 'کد حساب مشتری وجود دارد';
|
$messages[] = 'کد حساب مشتری وجود دارد';
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($currentAccount !== '' && $currentAccount !== $accountId) {
|
||||||
|
$messages[] = "شماره {$mobile} قبلاً به حساب پرگار دیگری متصل است";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$customer->pargar_account_id = $accountId;
|
$customer->pargar_account_id = $accountId;
|
||||||
if ($fullName !== '') {
|
if ($fullName !== '') {
|
||||||
$customer->fname = $fullName;
|
$customer->fname = $fullName;
|
||||||
@@ -104,18 +119,45 @@ class PargarInboundController extends Controller
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$customer = new Customer();
|
if ($byAccount) {
|
||||||
$customer->fname = $fullName !== '' ? $fullName : 'مشتری';
|
if (Customer::isMobileTaken($mobile, (int) $byAccount->id)) {
|
||||||
$customer->name = $customer->fname;
|
$messages[] = "شماره {$mobile} تکراری است";
|
||||||
$customer->mobile = $mobile;
|
continue;
|
||||||
$customer->pargar_account_id = $accountId;
|
}
|
||||||
$customer->active = true;
|
|
||||||
$customer->type = 2;
|
|
||||||
$customer->password = $mobile;
|
|
||||||
$customer->referral_id = $this->uniqueReferralId();
|
|
||||||
$customer->save();
|
|
||||||
|
|
||||||
$messages[] = "مشتری با کد حسابداری {$accountId} ایجاد شد";
|
if ($fullName !== '') {
|
||||||
|
$byAccount->fname = $fullName;
|
||||||
|
$byAccount->name = $fullName;
|
||||||
|
}
|
||||||
|
$byAccount->mobile = $mobile;
|
||||||
|
if (empty($byAccount->password)) {
|
||||||
|
$byAccount->password = $mobile;
|
||||||
|
}
|
||||||
|
$byAccount->save();
|
||||||
|
$messages[] = "مشتری با کد حسابداری {$accountId} بهروزرسانی شد";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$customer = new Customer();
|
||||||
|
$customer->fname = $fullName !== '' ? $fullName : 'مشتری';
|
||||||
|
$customer->name = $customer->fname;
|
||||||
|
$customer->mobile = $mobile;
|
||||||
|
$customer->pargar_account_id = $accountId;
|
||||||
|
$customer->active = true;
|
||||||
|
$customer->type = 2;
|
||||||
|
$customer->password = $mobile;
|
||||||
|
$customer->referral_id = $this->uniqueReferralId();
|
||||||
|
$customer->save();
|
||||||
|
|
||||||
|
$messages[] = "مشتری با کد حسابداری {$accountId} ایجاد شد";
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$messages[] = "خطا برای {$mobile}: " . (
|
||||||
|
$e instanceof UnprocessableEntityHttpException
|
||||||
|
? $e->getMessage()
|
||||||
|
: 'ثبت مشتری ناموفق بود'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
@@ -347,16 +389,6 @@ class PargarInboundController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function normalizeMobile(string $mobile): string
|
|
||||||
{
|
|
||||||
$mobile = trim($mobile);
|
|
||||||
if ($mobile === '') {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return '0' . ltrim($mobile, '0');
|
|
||||||
}
|
|
||||||
|
|
||||||
private function uniqueReferralId(): string
|
private function uniqueReferralId(): string
|
||||||
{
|
{
|
||||||
do {
|
do {
|
||||||
|
|||||||
+32
-2
@@ -171,9 +171,39 @@ class Customer extends Authenticatable implements JWTSubject
|
|||||||
|
|
||||||
public static function normalizeMobile(?string $mobile): ?string
|
public static function normalizeMobile(?string $mobile): ?string
|
||||||
{
|
{
|
||||||
$mobile = trim((string) $mobile);
|
$digits = preg_replace('/\D+/', '', trim((string) $mobile));
|
||||||
|
if ($digits === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return $mobile === '' ? null : $mobile;
|
if (str_starts_with($digits, '98') && strlen($digits) >= 12) {
|
||||||
|
$digits = substr($digits, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
$digits = ltrim($digits, '0');
|
||||||
|
if ($digits === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '0' . $digits;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function findActiveByMobile(?string $mobile, ?int $exceptId = null): ?self
|
||||||
|
{
|
||||||
|
$mobile = self::normalizeMobile($mobile);
|
||||||
|
if ($mobile === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = static::query()
|
||||||
|
->whereNull('deleted_at')
|
||||||
|
->where('mobile', $mobile);
|
||||||
|
|
||||||
|
if ($exceptId !== null) {
|
||||||
|
$query->where('id', '<>', $exceptId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->first();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
|
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
|
||||||
|
|||||||
Reference in New Issue
Block a user