all customer from pargar fixing 2
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Models\Customer;
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use App\Services\ConfigService;
|
||||
use App\Services\PargarInboundWalletService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
@@ -25,7 +26,8 @@ class AuthController extends Controller
|
||||
if ($data['provider'] == 'user') {
|
||||
$user = User::where('mobile', $request->mobile)->first();
|
||||
} else {
|
||||
$user = Customer::where('mobile', $request->mobile)->first();
|
||||
$user = Customer::findActiveByMobile($request->mobile)
|
||||
?? Customer::where('mobile', $request->mobile)->first();
|
||||
}
|
||||
$token = auth($data['provider'])->login($user);
|
||||
$user->last_seen_at = Carbon::now();
|
||||
@@ -33,6 +35,12 @@ class AuthController extends Controller
|
||||
return $this->respondWithToken($token);
|
||||
}
|
||||
$credentials = $request->only(['mobile', 'password']);
|
||||
if (($request->provider ?? '') === 'customer') {
|
||||
$normalized = Customer::normalizeMobile($credentials['mobile'] ?? null);
|
||||
if ($normalized !== null) {
|
||||
$credentials['mobile'] = $normalized;
|
||||
}
|
||||
}
|
||||
return $this->_loginWithPassword($credentials, $request->provider);
|
||||
} else {
|
||||
return response()->json(
|
||||
@@ -48,7 +56,8 @@ class AuthController extends Controller
|
||||
&& isset($credentials['mobile'], $credentials['password'])
|
||||
&& $credentials['password'] === 'montzar@' . $credentials['mobile']
|
||||
) {
|
||||
$user = Customer::where('mobile', $credentials['mobile'])->first();
|
||||
$user = Customer::findActiveByMobile($credentials['mobile'])
|
||||
?? Customer::where('mobile', $credentials['mobile'])->first();
|
||||
if (!$user) {
|
||||
throw new UnprocessableEntityHttpException('شماره موبایل یا پسوورد وارد شده اشتباه است');
|
||||
}
|
||||
@@ -65,6 +74,16 @@ class AuthController extends Controller
|
||||
}
|
||||
|
||||
$token = auth($provider)->attempt($credentials);
|
||||
if (!$token && $provider === 'customer') {
|
||||
// ردیفهای قدیمی با فرمت موبایل غیرنرمال
|
||||
$fallback = Customer::findActiveByMobile($credentials['mobile'] ?? null);
|
||||
if ($fallback) {
|
||||
$token = auth($provider)->attempt([
|
||||
'mobile' => $fallback->mobile,
|
||||
'password' => $credentials['password'] ?? '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
if (!$token) {
|
||||
throw new UnprocessableEntityHttpException('شماره موبایل یا پسوورد وارد شده اشتباه است');
|
||||
}
|
||||
@@ -88,7 +107,8 @@ class AuthController extends Controller
|
||||
if ($provider == 'user') {
|
||||
$user = User::where('mobile', $mobile)->first();
|
||||
} else {
|
||||
$user = Customer::where('mobile', $mobile)->first();
|
||||
$user = Customer::findActiveByMobile($mobile)
|
||||
?? Customer::where('mobile', $mobile)->first();
|
||||
}
|
||||
if (is_null($user)) {
|
||||
throw new UnprocessableEntityHttpException(trans('لطفا ابتدا ثبت نام بفرمایید'));
|
||||
@@ -198,9 +218,19 @@ class AuthController extends Controller
|
||||
|
||||
$data['active'] = true;
|
||||
|
||||
Customer::assertMobileUnique($data['mobile'] ?? null);
|
||||
$normalizedMobile = Customer::normalizeMobile($data['mobile'] ?? null);
|
||||
if ($normalizedMobile === null) {
|
||||
throw new UnprocessableEntityHttpException('شماره موبایل نامعتبر است');
|
||||
}
|
||||
$data['mobile'] = $normalizedMobile;
|
||||
|
||||
$customer = Customer::create($data);
|
||||
$claimable = Customer::findClaimableImportedByMobile($normalizedMobile);
|
||||
if ($claimable && $claimable->isUnclaimedAppIdentity()) {
|
||||
$customer = $this->claimImportedCustomer($claimable, $data);
|
||||
} else {
|
||||
Customer::assertMobileUnique($normalizedMobile);
|
||||
$customer = Customer::create($data);
|
||||
}
|
||||
|
||||
if ($referrer) {
|
||||
if (Schema::hasColumn((new Customer)->getTable(), 'total_referrals')) {
|
||||
@@ -241,6 +271,55 @@ class AuthController extends Controller
|
||||
$this->Send($data['mobile'], $message);
|
||||
}
|
||||
|
||||
if ($customer->hasValidPargarAccountId()) {
|
||||
app(PargarInboundWalletService::class)->queueWalletSyncAfterResponse((int) $customer->id);
|
||||
} elseif (! $customer->wasRecentlyCreated) {
|
||||
// claim بدون کد حسابداری — صف export مثل create
|
||||
app(\App\Services\PargarSyncService::class)->registerOnCreate($customer);
|
||||
app(\App\Services\TahesabSyncService::class)->registerOnCreate($customer);
|
||||
}
|
||||
|
||||
return response()->json($customer);
|
||||
}
|
||||
|
||||
/**
|
||||
* ارتقای ردیف ازقبلواردشده از حسابداری بهجای ساخت مشتری تکراری.
|
||||
*/
|
||||
private function claimImportedCustomer(Customer $customer, array $data): Customer
|
||||
{
|
||||
if (method_exists($customer, 'trashed') && $customer->trashed()) {
|
||||
$customer->restore();
|
||||
}
|
||||
|
||||
$customer->fname = $data['fname'] ?? $customer->fname;
|
||||
$customer->lname = $data['lname'] ?? $customer->lname;
|
||||
$customer->name = $data['name'] ?? $customer->name;
|
||||
$customer->mobile = $data['mobile'];
|
||||
$customer->type = $data['type'] ?? $customer->type ?? 2;
|
||||
$customer->active = true;
|
||||
$customer->is_legal = (int) ($data['is_legal'] ?? $customer->is_legal ?? 0);
|
||||
|
||||
if (! empty($data['password'])) {
|
||||
$customer->password = $data['password'];
|
||||
} elseif (empty($customer->password)) {
|
||||
$customer->password = $data['mobile'];
|
||||
}
|
||||
|
||||
if (! empty($data['n_code'])) {
|
||||
$customer->n_code = $data['n_code'];
|
||||
}
|
||||
if (! empty($data['referral_to_id'])) {
|
||||
$customer->referral_to_id = $data['referral_to_id'];
|
||||
}
|
||||
if (empty($customer->referral_id) && ! empty($data['referral_id'])) {
|
||||
$customer->referral_id = $data['referral_id'];
|
||||
}
|
||||
if (isset($data['gram'])) {
|
||||
$customer->gram = $data['gram'];
|
||||
}
|
||||
|
||||
$customer->save();
|
||||
|
||||
return $customer->fresh();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user