getBool('referral_gift_on_first_order', false); } /** * پاداش معرف/معرف‌شونده هنگام ثبت‌نام یا تأیید احراز — مگر اینکه به اولین سفارش موکول شده باشد. */ public function tryGiveOnRegistration( Customer $customer, ?Customer $referrer, bool $referrerGiftAllowed, bool $allowInvitedGiftWithoutReferrer = true ): void { if ($this->isDeferredToFirstOrder()) { return; } $this->depositGifts($customer, $referrer, $referrerGiftAllowed, $allowInvitedGiftWithoutReferrer); } /** * پاداش معرف/معرف‌شونده پس از اولین سفارش — فقط وقتی تنظیم referral_gift_on_first_order روشن است. */ public function tryGiveOnFirstOrder(Customer $customer): void { if (!$this->isDeferredToFirstOrder()) { return; } $config = app(ConfigService::class); if ($config->getString('has_page_earn_money', 'false') !== 'true') { return; } if (Order::where('customer_id', $customer->id)->count() !== 1) { return; } $referrer = $customer->referrer(); $referrerGiftAllowed = $this->resolveReferrerGiftPermission($referrer); $this->depositGifts($customer, $referrer, $referrerGiftAllowed, true); } public function resolveReferrerGiftPermission(?Customer $referrer): bool { if ($referrer === null) { return false; } $referralLimit = Setting::where('key', 'referral_limit')->first(); $limitValue = $referralLimit->value ?? null; if ( $referralLimit && $referralLimit->is_active && $limitValue !== null && $limitValue !== '' && is_numeric($limitValue) ) { $totalReferrals = Customer::where('referral_to_id', $referrer->referral_id) ->where('is_legal', '!=', 0) ->count(); return $totalReferrals < (int) $limitValue; } return true; } private function depositGifts( Customer $customer, ?Customer $referrer, bool $referrerGiftAllowed, bool $allowInvitedGiftWithoutReferrer ): void { $config = app(ConfigService::class); $giftForInviter = (float) $config->get('gift_for_referrer', 0); $giftForInvited = (float) $config->get('gift_for_referral', 0); $companyName = $config->getString('name_fa'); $companyUrl = $config->getString('app_url'); if ($referrer && $referrerGiftAllowed && $giftForInviter > 0 && !$this->referrerGiftAlreadyGiven($customer, $referrer)) { $this->giveReferrerGift($customer, $referrer, $giftForInviter, $companyName, $companyUrl, $config); } $canGiveInvited = $giftForInvited > 0 && !$this->invitedGiftAlreadyGiven($customer) && ($allowInvitedGiftWithoutReferrer || $referrer !== null); if ($canGiveInvited) { $this->giveInvitedGift($customer, $giftForInvited, $companyName, $companyUrl, $config); } } private function referrerGiftAlreadyGiven(Customer $invited, Customer $referrer): bool { return TransactionsAdmin::where('customer_id', $invited->id) ->where('customer_to_id', $referrer->id) ->where('note', 'پاداش بابت معرفی') ->exists(); } private function invitedGiftAlreadyGiven(Customer $customer): bool { return TransactionsAdmin::where('customer_id', $customer->id) ->where('note', 'پاداش بابت ثبت نام') ->exists(); } private function giveReferrerGift( Customer $customer, Customer $referrer, float $giftForInviter, string $companyName, string $companyUrl, ConfigService $config ): void { $withdraw = Withdraw::create([ 'user_id' => $customer->id, 'customer_id' => $referrer->id, 'cash' => 0, 'gold' => $giftForInviter, 'sts' => 5, ]); $walletInterface = new WalletInterface($referrer->id, 2); new TransactionInterface($walletInterface, [ 'cash' => 0, 'gold' => $giftForInviter, 'expire_at' => now(), 'active' => true, 'title' => 'پاداش بابت معرفی', 'withdraw_id' => $withdraw->id, 'start_at' => now(), ], 8, true, false); $transaction = Transaction::where('customer_id', $referrer->id)->latest()->first(); TransactionsAdmin::create([ 'customer_id' => $customer->id, 'customer_to_id' => $referrer->id, 'user_id' => $customer->id, 'wallet' => null, 'transaction_id' => $transaction->id, 'cash' => 0, 'note' => 'پاداش بابت معرفی', 'type' => 1, 'gold' => $giftForInviter, 'balance_cash' => 0, 'withdraw_id' => $withdraw->id, ]); if ($config->isTahesabAccounting()) { try { app(TahesabController::class)->referrerGoldReward( $customer, $giftForInviter, 'پاداش بابت معرفی' ); } catch (\Exception $e) { report($e); } } $currentGold = Controller::formatSmsGoldWeight((float) $walletInterface->wallet->refresh()->gold); $message = "سلام {$referrer->name} عزیز،\n\n{$giftForInviter} گرم طلا بابت معرفی {$customer->name} به حساب شما واریز شد.\nموجودی فعلی: {$currentGold} گرم.\n\nاطلاعات بیشتر: {$companyUrl}\n\nبا احترام،\n{$companyName}"; Controller::Send($referrer->mobile, $message); } private function giveInvitedGift( Customer $customer, float $giftForInvited, string $companyName, string $companyUrl, ConfigService $config ): void { $withdraw = Withdraw::create([ 'user_id' => $customer->id, 'customer_id' => $customer->id, 'cash' => 0, 'gold' => $giftForInvited, 'sts' => 5, ]); $walletInterface = new WalletInterface($customer->id, 2); new TransactionInterface($walletInterface, [ 'cash' => 0, 'gold' => $giftForInvited, 'expire_at' => now(), 'active' => true, 'title' => 'پاداش بابت ثبت نام', 'withdraw_id' => $withdraw->id, 'start_at' => now(), ], 8, true, false); $transaction = Transaction::where('customer_id', $customer->id)->latest()->first(); TransactionsAdmin::create([ 'customer_id' => $customer->id, 'customer_to_id' => $customer->id, 'user_id' => $customer->id, 'wallet' => null, 'transaction_id' => $transaction->id, 'cash' => 0, 'note' => 'پاداش بابت ثبت نام', 'type' => 1, 'gold' => $giftForInvited, 'balance_cash' => 0, 'withdraw_id' => $withdraw->id, ]); if ($config->isTahesabAccounting()) { try { app(TahesabController::class)->goldReward([ 'customer_id' => $customer->id, 'value' => $giftForInvited, 'desc' => 'پاداش بابت ثبت نام', ]); } catch (\Exception $e) { report($e); } } $currentGold = Controller::formatSmsGoldWeight((float) $walletInterface->wallet->refresh()->gold); $message = "سلام {$customer->name} عزیز،\n\n{$giftForInvited} گرم طلا بابت ثبت نام به حساب شما واریز شد.\nموجودی فعلی: {$currentGold} گرم.\n\nاطلاعات بیشتر: {$companyUrl}\n\nبا احترام،\n{$companyName}"; Controller::Send($customer->mobile, $message); } }