fix gift
This commit is contained in:
@@ -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()
|
||||
{
|
||||
$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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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([
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user