fix title

This commit is contained in:
2026-08-03 11:51:16 +03:30
parent ec0fd28efc
commit d360539208
14 changed files with 286 additions and 53 deletions
+104 -6
View File
@@ -9,6 +9,7 @@ use App\Models\Order;
use App\Models\Setting;
use App\Models\TransactionsAdmin;
use App\Models\Withdraw;
use Illuminate\Support\Facades\Log;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
use Rooyesh\Wallet\models\Transaction;
@@ -60,7 +61,7 @@ class ReferralGiftService
/**
* واریز معوق/دستی پاداش (مثلاً جبران وقتی گیت اشتباه جلوی واریز را گرفته).
* Idempotent — اگر قبلاً «پاداش بابت معرفی» خورده باشد دوباره نمی‌زند.
* Idempotent — اگر قبلاً «پاداش معرفی» (یا عنوان قدیمی) خورده باشد دوباره نمی‌زند.
*/
public function givePendingGifts(Customer $customer, bool $requireExistingOrder = false): void
{
@@ -128,7 +129,7 @@ class ReferralGiftService
{
return TransactionsAdmin::where('customer_id', $invited->id)
->where('customer_to_id', $referrer->id)
->where('note', 'پاداش بابت معرفی')
->whereIn('note', Controller::accountingReferralRewardDescAliases())
->exists();
}
@@ -139,6 +140,63 @@ class ReferralGiftService
->exists();
}
/**
* وقتی price_provider=montzar و کلید referral_gift_buy_from_montzar روشن است،
* قبل از واریز پاداش همان وزن از همکار خرید می‌شود.
* خاموش بودن کلید = روال قبلی (واریز محلی بدون خرید همکار).
* شکست خرید → واریز نمی‌شود (ثبت‌نام/سفارش خراب نمی‌شود؛ با givePendingGifts قابل جبران است).
*/
private function ensureMontzarCoverPurchase(
float $grams,
Customer $beneficiary,
string $reason,
ConfigService $config
): bool {
if (!$config->usesMontzarPricing()) {
return true;
}
if (!$config->getBool('referral_gift_buy_from_montzar', true)) {
return true;
}
if ($grams <= 0) {
return true;
}
$livePrice = (float) $config->Config('montzar_live_price');
$data = [
'amount' => $grams,
'direction' => 1, // خرید پوشش از همکار
'customer' => [
'name' => trim($reason . ' - ' . (string) ($beneficiary->name ?? '')),
],
'total_price' => (int) round($livePrice * $grams),
];
try {
$parsed = app(MontzarService::class)->submitOrder($data);
Log::info('Referral gift covered by Montzar buy', [
'grams' => $grams,
'reason' => $reason,
'beneficiary_id' => $beneficiary->id,
'provider_id' => $parsed['provider_id'] ?? null,
]);
return true;
} catch (\Throwable $e) {
report($e);
Log::error('Referral gift blocked: Montzar cover buy failed', [
'grams' => $grams,
'reason' => $reason,
'beneficiary_id' => $beneficiary->id,
'error' => $e->getMessage(),
]);
return false;
}
}
private function giveReferrerGift(
Customer $customer,
Customer $referrer,
@@ -147,6 +205,12 @@ class ReferralGiftService
string $companyUrl,
ConfigService $config
): void {
$rewardDesc = Controller::accountingReferralRewardDesc();
if (!$this->ensureMontzarCoverPurchase($giftForInviter, $referrer, $rewardDesc, $config)) {
return;
}
$withdraw = Withdraw::create([
'user_id' => $customer->id,
'customer_id' => $referrer->id,
@@ -161,7 +225,7 @@ class ReferralGiftService
'gold' => $giftForInviter,
'expire_at' => now(),
'active' => true,
'title' => 'پاداش بابت معرفی',
'title' => $rewardDesc,
'withdraw_id' => $withdraw->id,
'start_at' => now(),
], 8, true, false);
@@ -174,7 +238,7 @@ class ReferralGiftService
'wallet' => null,
'transaction_id' => $transaction->id,
'cash' => 0,
'note' => 'پاداش بابت معرفی',
'note' => $rewardDesc,
'type' => 1,
'gold' => $giftForInviter,
'balance_cash' => 0,
@@ -186,7 +250,7 @@ class ReferralGiftService
app(TahesabController::class)->referrerGoldReward(
$customer,
$giftForInviter,
'پاداش بابت معرفی'
$rewardDesc
);
} catch (\Exception $e) {
report($e);
@@ -198,13 +262,28 @@ class ReferralGiftService
app(\App\Http\Controllers\Admin\ZarinController::class)->referrerGoldReward(
$customer,
$giftForInviter,
'پاداش بابت معرفی'
$rewardDesc
);
} catch (\Exception $e) {
report($e);
}
}
try {
app(PargarSyncService::class)->queueWalletDocument(
$referrer,
'gold',
(float) $giftForInviter,
[
'withdraw_id' => $withdraw->id,
'note' => $rewardDesc,
'source' => 'referral_gift',
]
);
} catch (\Throwable $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);
@@ -217,6 +296,10 @@ class ReferralGiftService
string $companyUrl,
ConfigService $config
): void {
if (!$this->ensureMontzarCoverPurchase($giftForInvited, $customer, 'پاداش بابت ثبت نام', $config)) {
return;
}
$withdraw = Withdraw::create([
'user_id' => $customer->id,
'customer_id' => $customer->id,
@@ -275,6 +358,21 @@ class ReferralGiftService
}
}
try {
app(PargarSyncService::class)->queueWalletDocument(
$customer,
'gold',
(float) $giftForInvited,
[
'withdraw_id' => $withdraw->id,
'note' => 'پاداش بابت ثبت نام',
'source' => 'referral_register_gift',
]
);
} catch (\Throwable $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);