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
@@ -16,6 +16,7 @@ use Illuminate\Support\Facades\Schema;
use Morilog\Jalali\Jalalian;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class PargarInboundController extends Controller
{
@@ -77,23 +78,37 @@ class PargarInboundController extends Controller
$accountId = trim((string) ($row['AccountId'] ?? ''));
$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 خالی است';
continue;
}
$customer = Customer::whereNull('deleted_at')
->where('mobile', $mobile)
$byAccount = Customer::whereNull('deleted_at')
->where('pargar_account_id', $accountId)
->first();
if ($byAccount && Customer::normalizeMobile($byAccount->mobile) !== $mobile) {
$messages[] = "کد حساب {$accountId} قبلاً برای مشتری دیگری ثبت شده است";
continue;
}
$customer = Customer::findActiveByMobile($mobile);
if ($customer) {
if ((string) ($customer->pargar_account_id ?? '') === $accountId) {
$currentAccount = (string) ($customer->pargar_account_id ?? '');
if ($currentAccount === $accountId) {
$messages[] = 'کد حساب مشتری وجود دارد';
continue;
}
if ($currentAccount !== '' && $currentAccount !== $accountId) {
$messages[] = "شماره {$mobile} قبلاً به حساب پرگار دیگری متصل است";
continue;
}
$customer->pargar_account_id = $accountId;
if ($fullName !== '') {
$customer->fname = $fullName;
@@ -104,18 +119,45 @@ class PargarInboundController extends Controller
continue;
}
$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();
if ($byAccount) {
if (Customer::isMobileTaken($mobile, (int) $byAccount->id)) {
$messages[] = "شماره {$mobile} تکراری است";
continue;
}
$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([
@@ -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
{
do {