This commit is contained in:
2026-08-02 16:47:19 +03:30
parent ef32046e85
commit ec0fd28efc
4 changed files with 113 additions and 31 deletions
@@ -0,0 +1,72 @@
<?php
namespace App\Console\Commands;
use App\Models\Customer;
use App\Models\TransactionsAdmin;
use App\Services\ReferralGiftService;
use Illuminate\Console\Command;
/**
* جبران پاداش معرف وقتی به‌خاطر باگ/گیت اشتباه واریز نشده (مثلاً has_page_earn_money خاموش بوده).
*/
class GivePendingReferralGiftCommand extends Command
{
protected $signature = 'referral:give-pending
{customer_id : شناسه مشتری معرف‌شونده}
{--dry-run : فقط گزارش، بدون واریز}
{--require-order : فقط اگر حداقل یک سفارش داشته باشد}';
protected $description = 'واریز معوق پاداش معرف برای یک مشتری (idempotent)';
public function handle(ReferralGiftService $gifts): int
{
$customer = Customer::find($this->argument('customer_id'));
if (! $customer) {
$this->error('مشتری پیدا نشد.');
return 1;
}
$referrer = $customer->referrer();
$this->line("مشتری: #{$customer->id} {$customer->name} | referral_to_id={$customer->referral_to_id}");
$this->line($referrer
? "معرف: #{$referrer->id} {$referrer->name} | referral_id={$referrer->referral_id}"
: 'معرف: یافت نشد');
if (! $referrer) {
return 1;
}
$already = TransactionsAdmin::where('customer_id', $customer->id)
->where('customer_to_id', $referrer->id)
->where('note', 'پاداش بابت معرفی')
->exists();
if ($already) {
$this->warn('پاداش معرف قبلاً واریز شده؛ کاری انجام نشد.');
return 0;
}
if ($this->option('dry-run')) {
$this->info('dry-run: پاداش هنوز واریز نشده و آمادهٔ واریز است.');
return 0;
}
// جبران صریح: بدون وابستگی به has_page_earn_money / تعداد دقیق سفارش
$requireOrder = (bool) $this->option('require-order');
$gifts->givePendingGifts($customer, $requireOrder);
$given = TransactionsAdmin::where('customer_id', $customer->id)
->where('customer_to_id', $referrer->id)
->where('note', 'پاداش بابت معرفی')
->exists();
if ($given) {
$this->info('پاداش معرف با موفقیت واریز شد.');
return 0;
}
$this->error('واریز انجام نشد (سفارش/سقف معرفی/مقدار gift_for_referrer را چک کنید).');
return 1;
}
}
@@ -28,7 +28,9 @@ class CustomerController extends SuperController
public function __construct() public function __construct()
{ {
$this->with = []; // referral لازم است تا در show/جزئیات ادمین «نام/کد معرف کاربر» خالی نماند
// (referral_to_id در DB هست؛ بدون with فقط کد خام دیده نمی‌شود و UI از userInfo.referral می‌خواند)
$this->with = ['referral'];
$this->select = ['*']; $this->select = ['*'];
$search=['name','mobile']; $search=['name','mobile'];
$this->query = Customer::with($this->with,'wallet')->where('is_archive',false)->where('type',1); $this->query = Customer::with($this->with,'wallet')->where('is_archive',false)->where('type',1);
@@ -573,20 +575,16 @@ class CustomerController extends SuperController
$customer->is_legal = 2; $customer->is_legal = 2;
$customer->save(); $customer->save();
if ($this->Config('has_page_earn_money') == 'true') { // واریز پاداش معرف مستقل از has_page_earn_money (آن فقط منوی اپ است)
$referrer = $customer->referrer(); $referrer = $customer->referrer();
if ( if ($referrer) {
$referrer $gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer);
&& Customer::where('referral_to_id', $referrer->referral_id)->where('is_legal', '!=', 0)->count() < 4 app(ReferralGiftService::class)->tryGiveOnRegistration(
) { $customer,
$gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer); $referrer,
app(ReferralGiftService::class)->tryGiveOnRegistration( $gift_permision,
$customer, false
$referrer, );
$gift_permision,
false
);
}
} }
@@ -212,16 +212,15 @@ class CustomerController extends Controller
if (isset($response['matched']) and $response['matched'] == true) { if (isset($response['matched']) and $response['matched'] == true) {
$customer->is_legal = 1; $customer->is_legal = 1;
$customer->save(); $customer->save();
if ($this->Config('has_page_earn_money') == 'true') { // واریز پاداش معرف مستقل از has_page_earn_money (آن فقط منوی اپ است)
$referrer = $customer->referrer(); $referrer = $customer->referrer();
$gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer); $gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer);
app(ReferralGiftService::class)->tryGiveOnRegistration( app(ReferralGiftService::class)->tryGiveOnRegistration(
$customer, $customer,
$referrer, $referrer,
$gift_permision, $gift_permision,
true true
); );
}
} else { } else {
if (isset($response['matched']) && $response['matched'] === false) { if (isset($response['matched']) && $response['matched'] === false) {
return response()->json([ return response()->json([
+19 -6
View File
@@ -38,6 +38,9 @@ class ReferralGiftService
/** /**
* پاداش معرف/معرف‌شونده پس از اولین سفارش — فقط وقتی تنظیم referral_gift_on_first_order روشن است. * پاداش معرف/معرف‌شونده پس از اولین سفارش — فقط وقتی تنظیم referral_gift_on_first_order روشن است.
*
* توجه: has_page_earn_money فقط نمایش منوی «کسب درآمد» در اپ است؛ واریز پاداش را قطع نمی‌کند.
* اگر اولین سفارش قبلاً ثبت شده و پاداش نرفته باشد، با سفارش‌های بعدی هم (تا وقتی تکراری نباشد) جبران می‌شود.
*/ */
public function tryGiveOnFirstOrder(Customer $customer): void public function tryGiveOnFirstOrder(Customer $customer): void
{ {
@@ -45,12 +48,7 @@ class ReferralGiftService
return; return;
} }
$config = app(ConfigService::class); if (! Order::where('customer_id', $customer->id)->exists()) {
if ($config->getString('has_page_earn_money', 'false') !== 'true') {
return;
}
if (Order::where('customer_id', $customer->id)->count() !== 1) {
return; return;
} }
@@ -60,6 +58,21 @@ class ReferralGiftService
$this->depositGifts($customer, $referrer, $referrerGiftAllowed, true); $this->depositGifts($customer, $referrer, $referrerGiftAllowed, true);
} }
/**
* واریز معوق/دستی پاداش (مثلاً جبران وقتی گیت اشتباه جلوی واریز را گرفته).
* Idempotent — اگر قبلاً «پاداش بابت معرفی» خورده باشد دوباره نمی‌زند.
*/
public function givePendingGifts(Customer $customer, bool $requireExistingOrder = false): void
{
if ($requireExistingOrder && ! Order::where('customer_id', $customer->id)->exists()) {
return;
}
$referrer = $customer->referrer();
$referrerGiftAllowed = $this->resolveReferrerGiftPermission($referrer);
$this->depositGifts($customer, $referrer, $referrerGiftAllowed, true);
}
public function resolveReferrerGiftPermission(?Customer $referrer): bool public function resolveReferrerGiftPermission(?Customer $referrer): bool
{ {
if ($referrer === null) { if ($referrer === null) {