73 lines
2.7 KiB
PHP
73 lines
2.7 KiB
PHP
<?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;
|
||
|
|
}
|
||
|
|
}
|