From ec0fd28efc22551664c2d1e3edd63337a9cc72d4 Mon Sep 17 00:00:00 2001 From: adonis Date: Sun, 2 Aug 2026 16:47:19 +0330 Subject: [PATCH] fix gift --- .../GivePendingReferralGiftCommand.php | 72 +++++++++++++++++++ .../Controllers/Admin/CustomerController.php | 28 ++++---- .../Customer/CustomerController.php | 19 +++-- app/Services/ReferralGiftService.php | 25 +++++-- 4 files changed, 113 insertions(+), 31 deletions(-) create mode 100644 app/Console/Commands/GivePendingReferralGiftCommand.php diff --git a/app/Console/Commands/GivePendingReferralGiftCommand.php b/app/Console/Commands/GivePendingReferralGiftCommand.php new file mode 100644 index 0000000..66a1be8 --- /dev/null +++ b/app/Console/Commands/GivePendingReferralGiftCommand.php @@ -0,0 +1,72 @@ +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; + } +} diff --git a/app/Http/Controllers/Admin/CustomerController.php b/app/Http/Controllers/Admin/CustomerController.php index de43545..a11b4e2 100644 --- a/app/Http/Controllers/Admin/CustomerController.php +++ b/app/Http/Controllers/Admin/CustomerController.php @@ -28,7 +28,9 @@ class CustomerController extends SuperController public function __construct() { - $this->with = []; + // referral لازم است تا در show/جزئیات ادمین «نام/کد معرف کاربر» خالی نماند + // (referral_to_id در DB هست؛ بدون with فقط کد خام دیده نمی‌شود و UI از userInfo.referral می‌خواند) + $this->with = ['referral']; $this->select = ['*']; $search=['name','mobile']; $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->save(); - if ($this->Config('has_page_earn_money') == 'true') { - $referrer = $customer->referrer(); - if ( - $referrer - && Customer::where('referral_to_id', $referrer->referral_id)->where('is_legal', '!=', 0)->count() < 4 - ) { - $gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer); - app(ReferralGiftService::class)->tryGiveOnRegistration( - $customer, - $referrer, - $gift_permision, - false - ); - } + // واریز پاداش معرف مستقل از has_page_earn_money (آن فقط منوی اپ است) + $referrer = $customer->referrer(); + if ($referrer) { + $gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer); + app(ReferralGiftService::class)->tryGiveOnRegistration( + $customer, + $referrer, + $gift_permision, + false + ); } diff --git a/app/Http/Controllers/Customer/CustomerController.php b/app/Http/Controllers/Customer/CustomerController.php index c86c807..e6b2825 100644 --- a/app/Http/Controllers/Customer/CustomerController.php +++ b/app/Http/Controllers/Customer/CustomerController.php @@ -212,16 +212,15 @@ class CustomerController extends Controller if (isset($response['matched']) and $response['matched'] == true) { $customer->is_legal = 1; $customer->save(); - if ($this->Config('has_page_earn_money') == 'true') { - $referrer = $customer->referrer(); - $gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer); - app(ReferralGiftService::class)->tryGiveOnRegistration( - $customer, - $referrer, - $gift_permision, - true - ); - } + // واریز پاداش معرف مستقل از has_page_earn_money (آن فقط منوی اپ است) + $referrer = $customer->referrer(); + $gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer); + app(ReferralGiftService::class)->tryGiveOnRegistration( + $customer, + $referrer, + $gift_permision, + true + ); } else { if (isset($response['matched']) && $response['matched'] === false) { return response()->json([ diff --git a/app/Services/ReferralGiftService.php b/app/Services/ReferralGiftService.php index f73d007..acae836 100644 --- a/app/Services/ReferralGiftService.php +++ b/app/Services/ReferralGiftService.php @@ -38,6 +38,9 @@ class ReferralGiftService /** * پاداش معرف/معرف‌شونده پس از اولین سفارش — فقط وقتی تنظیم referral_gift_on_first_order روشن است. + * + * توجه: has_page_earn_money فقط نمایش منوی «کسب درآمد» در اپ است؛ واریز پاداش را قطع نمی‌کند. + * اگر اولین سفارش قبلاً ثبت شده و پاداش نرفته باشد، با سفارش‌های بعدی هم (تا وقتی تکراری نباشد) جبران می‌شود. */ public function tryGiveOnFirstOrder(Customer $customer): void { @@ -45,12 +48,7 @@ class ReferralGiftService return; } - $config = app(ConfigService::class); - if ($config->getString('has_page_earn_money', 'false') !== 'true') { - return; - } - - if (Order::where('customer_id', $customer->id)->count() !== 1) { + if (! Order::where('customer_id', $customer->id)->exists()) { return; } @@ -60,6 +58,21 @@ class ReferralGiftService $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 { if ($referrer === null) {