fix some bugs from fee

This commit is contained in:
2026-07-28 10:33:11 +03:30
parent 7c68115b39
commit 745f7fcfd6
8 changed files with 51 additions and 80 deletions
@@ -8,7 +8,6 @@ use App\Http\Controllers\Single\SingleOrderController;
use App\Models\Customer;
use App\Models\Order;
use App\Models\Price;
use App\Models\Setting;
use App\Services\WalletBalanceService;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
@@ -55,8 +54,8 @@ class OrderController extends Controller
return (int) $order->direction === 1 ? $profitPerMoment : -$profitPerMoment;
});
$feeSingle = (float) (Setting::where('key', 'fee_single')->value('value') ?? config('fee.single', 0));
$currentPriceWithFee = $lastPrice + $feeSingle;
$buyFee = app(\App\Services\ConfigService::class)->displayBuyPriceDiffRial();
$currentPriceWithFee = $lastPrice + $buyFee;
$averagePriceWithFee = $averagePrice + $averageFee;
return response()->json([
@@ -78,7 +78,7 @@ class SingleOrderController extends Controller
$data['all_price'] = $latestPrice;
$data['price'] = $basePrice;
$data['price_id'] = $latestPrice?->id;
$data['difference'] = $config->orderPriceDiffRial();
$data['difference'] = $config->orderPriceDiffRial(isset($data['direction']) ? (int) $data['direction'] : null);
$data['service_charge_percentage'] = $config->orderServiceChargePercentage();
$fee_free = Setting::where('key', 'fee_free')->where('is_active', true)->value('value');
@@ -1265,8 +1265,8 @@ class SingleOrderController extends Controller
/**
* اختلاف قیمت سفارش:
* - محصول با «قیمت اپ»: اختلاف پایه تنظیمات (sell_fee/buy_fee/fee_single) + تب اختلاف محصول
* - بدون قیمت اپ: فقط تب اختلاف محصول (cash_fee) — بدون ۱M تنظیمات اپ
* - محصول با «قیمت اپ»: buy_fee / sell_fee + تب اختلاف محصول
* - بدون قیمت اپ: فقط تب اختلاف محصول (cash_fee)
*
* @return array{
* amount: float,
@@ -1302,21 +1302,14 @@ class SingleOrderController extends Controller
$keys = [];
if ($usesAppPrice) {
$feeSingle = (float) $config->get('fee_single', 0);
if ($isBuy) {
$buyFee = (float) $config->get('buy_fee', $feeSingle);
$settingsAmt = $buyFee > 0 ? $buyFee : $feeSingle;
$settingsKey = $buyFee > 0 ? 'buy_fee' : 'fee_single';
$settingsTitle = $buyFee > 0
? 'اختلاف پایه اپ · اضافه قیمت خرید (buy_fee)'
: 'اختلاف پایه اپ · اختلاف پایه (fee_single)';
$settingsAmt = $config->displayBuyPriceDiffRial();
$settingsKey = 'buy_fee';
$settingsTitle = 'اختلاف پایه اپ · اضافه قیمت خرید (buy_fee)';
} else {
$sellFee = (float) $config->get('sell_fee', $feeSingle);
$settingsAmt = $sellFee > 0 ? $sellFee : $feeSingle;
$settingsKey = $sellFee > 0 ? 'sell_fee' : 'fee_single';
$settingsTitle = $sellFee > 0
? 'اختلاف پایه اپ · کسر قیمت فروش (sell_fee)'
: 'اختلاف پایه اپ · اختلاف پایه (fee_single)';
$settingsAmt = $config->displaySellPriceDiffRial();
$settingsKey = 'sell_fee';
$settingsTitle = 'اختلاف پایه اپ · کسر قیمت فروش (sell_fee)';
}
if ($settingsAmt > 0) {
@@ -51,7 +51,7 @@ class ReportController extends Controller
public function updatePrise()
{
$difference = (float) (Setting::where('key', 'fee_single')->value('value') ?? config('fee.single', 0));
$config = app(ConfigService::class);
$lastPrice = Price::latest()->first();
if (!$lastPrice) {
@@ -60,9 +60,12 @@ class ReportController extends Controller
$PriceTriggers = PriceTrigger::where("is_active", true)->get();
foreach ($PriceTriggers as $PriceTrigger) {
$diff = $PriceTrigger->dirction == 1
? $config->displaySellPriceDiffRial()
: ($PriceTrigger->dirction == 2 ? $config->displayBuyPriceDiffRial() : 0.0);
$priseGold = $PriceTrigger->dirction == 1
? $lastPrice->price - $difference
: ($PriceTrigger->dirction == 2 ? $lastPrice->price + $difference : $lastPrice->price);
? $lastPrice->price - $diff
: ($PriceTrigger->dirction == 2 ? $lastPrice->price + $diff : $lastPrice->price);
if (eval('return $priseGold ' . $PriceTrigger->price_condition . ' $PriceTrigger->target_price;')) {
$customer = Customer::find($PriceTrigger->customer_id);
+1 -1
View File
@@ -24,7 +24,7 @@ class Order extends Model
'fee',//سود هر گرم
'total_fee',//سود کل
'fee_source',//settings|product|app+product|none
'fee_source_key',//sell_fee|buy_fee|fee_single|cash_fee_sell|...
'fee_source_key',//sell_fee|buy_fee|cash_fee_sell|...
'fee_source_label',
'fee_breakdown',//فاکتور اقلام سود (JSON)
'service_charge_percentage',//درصد کارمزد
+2 -2
View File
@@ -773,8 +773,8 @@ class BalanceReportService
}
if ($source === 'settings') {
return $direction === 2
? 'تنظیمات سراسری (sell_fee / fee_single)'
: 'تنظیمات سراسری (buy_fee / fee_single)';
? 'تنظیمات سراسری (sell_fee)'
: 'تنظیمات سراسری (buy_fee)';
}
return $direction === 2
+19 -30
View File
@@ -101,38 +101,35 @@ class ConfigService
return $fromDb !== '' ? $fromDb : (string) env('NODE_SHARED_SECRET', '');
}
/** اختلاف خرید نمایش (/api/price/last) — fee_single / buy_fee */
/** اختلاف خرید نمایش (/api/price/last) — فقط buy_fee (صفر یعنی بدون اختلاف) */
public function displayBuyPriceDiffRial(): float
{
$feeSingle = (float) $this->get('fee_single', 0);
$buyFee = (float) $this->get('buy_fee', $feeSingle);
$raw = $this->getString('buy_fee', '');
return $buyFee > 0 ? $buyFee : $feeSingle;
return $raw === '' ? 0.0 : (float) $raw;
}
/** اختلاف فروش نمایش — fee_single / sell_fee */
/** اختلاف فروش نمایش — فقط sell_fee (صفر یعنی بدون اختلاف) */
public function displaySellPriceDiffRial(): float
{
$feeSingle = (float) $this->get('fee_single', 0);
$sellFee = (float) $this->get('sell_fee', $feeSingle);
$raw = $this->getString('sell_fee', '');
return $sellFee > 0 ? $sellFee : $feeSingle;
return $raw === '' ? 0.0 : (float) $raw;
}
/** اختلاف ریالی سفارش — fee_single، بعد fee_cash (legacy)، بعد config/fee.php */
public function orderPriceDiffRial(): float
/**
* اختلاف ریالی سفارش — فقط buy_fee / sell_fee بر اساس جهت.
* fee_single دیگر منبع نیست.
*
* @param int|null $direction 1=خرید، 2=فروش، null=خرید (برای سازگاری)
*/
public function orderPriceDiffRial(?int $direction = null): float
{
$feeSingle = $this->getString('fee_single', '');
if ($feeSingle !== '') {
return (float) $feeSingle;
if ($direction === 2) {
return $this->displaySellPriceDiffRial();
}
$legacyCash = $this->getString('fee_cash', '');
if ($legacyCash !== '') {
return (float) $legacyCash;
}
return (float) config('fee.single', 0);
return $this->displayBuyPriceDiffRial();
}
/** درصد کارمزد سفارش — fee_service_charge_percentage، service_charge_percentage، config/fee.php */
@@ -172,7 +169,6 @@ class ConfigService
): array {
$diffBuy = (float) $this->displayBuyPriceDiffRial();
$diffSell = (float) $this->displaySellPriceDiffRial();
$feeSingle = (float) $this->get('fee_single', 0);
$openBuy = true;
$openSell = true;
@@ -191,16 +187,9 @@ class ConfigService
return compact('sell', 'buy', 'priceLast');
}
$sell = $openSell ? (int) ceil($priceValue - $feeSingle) : 0;
$buy = $openBuy ? (int) ceil($priceValue + $feeSingle) : 0;
$priceLast = (int) ceil($priceValue + $feeSingle);
$provider = $this->priceProvider();
if (in_array($provider, ['main_product', 'zaarbox'], true)) {
$sell = $openSell ? (int) ceil($priceValue - $diffSell) : 0;
$buy = $openBuy ? (int) ceil($priceValue + $diffBuy) : 0;
$priceLast = (int) ceil($priceValue + $diffBuy);
}
$sell = $openSell ? (int) ceil($priceValue - $diffSell) : 0;
$buy = $openBuy ? (int) ceil($priceValue + $diffBuy) : 0;
$priceLast = (int) ceil($priceValue + $diffBuy);
return compact('sell', 'buy', 'priceLast');
}
+6 -13
View File
@@ -754,9 +754,7 @@ INSERT INTO `settings` (`key`, `value`, `title_fa`, `can_edit`, `is_active`, `de
SELECT 'referral_id', 'required', 'کد معرف اجباری', 1, 1, 'روشن = اجباری در ثبت‌نام | خاموش = اختیاری', 'switch'
WHERE NOT EXISTS (SELECT 1 FROM `settings` WHERE `key` = 'referral_id' LIMIT 1);
INSERT INTO `settings` (`key`, `value`, `title_fa`, `can_edit`, `is_active`, `description`, `type`)
SELECT 'fee_single', '500000', 'اختلاف پایه نمایش', 0, 1, NULL, 'price'
WHERE NOT EXISTS (SELECT 1 FROM `settings` WHERE `key` = 'fee_single' LIMIT 1);
-- fee_single حذف شد — اختلاف فقط buy_fee / sell_fee (خالی = صفر)
INSERT INTO `settings` (`key`, `value`, `title_fa`, `can_edit`, `is_active`, `description`, `type`)
SELECT 'buy_fee', '', 'سود خرید نمایش', 1, 1, NULL, 'price'
@@ -865,9 +863,6 @@ WHERE `key` = 'zaarbox_api_url';
UPDATE `settings` SET `title_fa` = COALESCE(NULLIF(TRIM(`title_fa`), ''), 'شناسه ردیف Zaarbox')
WHERE `key` = 'zaarbox_price_key';
UPDATE `settings` SET `title_fa` = COALESCE(NULLIF(TRIM(`title_fa`), ''), 'اختلاف پایه نمایش')
WHERE `key` = 'fee_single';
UPDATE `settings` SET `title_fa` = COALESCE(NULLIF(TRIM(`title_fa`), ''), 'سود خرید نمایش')
WHERE `key` = 'buy_fee';
@@ -980,7 +975,7 @@ UPDATE `settings` SET `type` = 'switch' WHERE `key` IN (
-- price (مبالغ ریالی)
UPDATE `settings` SET `type` = 'price' WHERE `key` IN (
'fee_single', 'buy_fee', 'sell_fee',
'buy_fee', 'sell_fee',
'min_withdraw', 'min_deposit', 'min_first_buy', 'minimum_sell',
'up_price', 'down_price'
);
@@ -1127,6 +1122,7 @@ DELETE FROM `settings` WHERE `key` IN (
'name_en', 'bace_url', 'server_url', 'key', 'color_primary', 'color_secondary',
'price_from_nasiryan',
'montzar_price',
'fee_single',
'goldir_base_url', 'fee_cash', 'default_vc_number', 'payping_gateway_item',
'wss_path', 'wss_link', 'wss_trigger_path',
'wss_trigger_host', 'wss_trigger_scheme', 'wss_http_port', 'wss_socket_port'
@@ -1307,9 +1303,8 @@ UPDATE `settings` SET `description` = 'روشن: بعد از انتخاب محص
UPDATE `settings` SET `description` = 'روشن: خارج از بازه «ساعت شروع» تا «ساعت پایان» معامله و نمایش قیمت بسته می‌شود. خاموش: محدودیت ساعتی سراسری اعمال نمی‌شود.' WHERE `key` = 'close_price';
UPDATE `settings` SET `description` = 'ساعت شروع فعالیت روزانه. فقط وقتی «بستن خودکار خارج از ساعت کاری» روشن باشد اعمال می‌شود (مثلاً ۰۹:۰۰).' WHERE `key` = 'startDay';
UPDATE `settings` SET `description` = 'ساعت پایان فعالیت روزانه. فقط وقتی «بستن خودکار خارج از ساعت کاری» روشن باشد اعمال می‌شود (مثلاً ۲۲:۰۰).' WHERE `key` = 'endDay';
UPDATE `settings` SET `description` = 'اختلاف پایه (ریال) بین قیمت خرید و فروش روی مظنه. معمولاً نصف مجموع اختلاف قیمت خرید و فروش است.' WHERE `key` = 'fee_single';
UPDATE `settings` SET `description` = 'مبلغ اضافه (ریال) روی قیمت پایه برای سمت خرید. خالی = استفاده از «اختلاف پایه».' WHERE `key` = 'buy_fee';
UPDATE `settings` SET `description` = 'مبلغ کسر (ریال) از قیمت پایه برای سمت فروش. خالی = استفاده از «اختلاف پایه».' WHERE `key` = 'sell_fee';
UPDATE `settings` SET `description` = 'مبلغ اضافه (ریال) روی قیمت پایه برای سمت خرید. خالی یا صفر = بدون اختلاف.' WHERE `key` = 'buy_fee';
UPDATE `settings` SET `description` = 'مبلغ کسر (ریال) از قیمت پایه برای سمت فروش. خالی یا صفر = بدون اختلاف.' WHERE `key` = 'sell_fee';
UPDATE `settings` SET `description` = 'درصد کارمزد هر سفارش از مبلغ معامله (اعشاری؛ مثلاً ۰٫۰۰۵ یعنی نیم در هزار).' WHERE `key` = 'fee_service_charge_percentage';
UPDATE `settings` SET
`title_fa` = 'اعمال مبلغ دقیق در معامله مشتری',
@@ -1529,7 +1524,6 @@ UPDATE `settings` SET `title_fa` = 'کارمزد (قدیمی — درصد)' WHER
UPDATE `settings` SET `title_fa` = 'تعداد سفارش اول بدون کارمزد' WHERE `key` = 'fee_free';
UPDATE `settings` SET `title_fa` = 'درصد کارمزد سفارش' WHERE `key` = 'fee_service_charge_percentage';
UPDATE `settings` SET `title_fa` = 'اعمال مبلغ دقیق در معامله مشتری' WHERE `key` = 'fee_deduct_from_gold';
UPDATE `settings` SET `title_fa` = 'اختلاف پایه نمایش' WHERE `key` = 'fee_single';
UPDATE `settings` SET `title_fa` = 'پاداش معرف‌شونده' WHERE `key` = 'gift_for_referral';
UPDATE `settings` SET `title_fa` = 'پاداش معرف' WHERE `key` = 'gift_for_referrer';
UPDATE `settings` SET `title_fa` = 'پیامک گروهی' WHERE `key` = 'group_sms';
@@ -1647,7 +1641,6 @@ UPDATE `settings` SET `can_edit` = 0 WHERE `key` IN (
'chart_id', 'chart_use_local_data',
'external_order_api_enabled',
'fee_deduct_from_gold',
'fee_single',
'referral_bonus_amount',
'group_sms',
'customer_direct_sms',
@@ -1723,7 +1716,7 @@ UPDATE `settings` SET `can_edit` = 1 WHERE `key` IN (
-- تفکیک نهایی type (بعد از همه batchها — override روی دیتابیس موجود)
UPDATE `settings` SET `type` = 'price' WHERE `key` IN (
'fee_single', 'buy_fee', 'sell_fee',
'buy_fee', 'sell_fee',
'min_withdraw', 'min_deposit', 'min_first_buy', 'minimum_sell',
'up_price', 'down_price'
);
@@ -627,9 +627,7 @@ INSERT INTO `settings` (`key`, `value`, `title_fa`, `can_edit`, `is_active`, `de
SELECT 'referral_id', 'required', 'کد معرف اجباری', 1, 1, 'روشن = اجباری در ثبت‌نام | خاموش = اختیاری', 'switch'
WHERE NOT EXISTS (SELECT 1 FROM `settings` WHERE `key` = 'referral_id' LIMIT 1);
INSERT INTO `settings` (`key`, `value`, `title_fa`, `can_edit`, `is_active`, `description`, `type`)
SELECT 'fee_single', '500000', 'اختلاف پایه نمایش', 0, 1, NULL, 'price'
WHERE NOT EXISTS (SELECT 1 FROM `settings` WHERE `key` = 'fee_single' LIMIT 1);
-- fee_single حذف شد — اختلاف فقط buy_fee / sell_fee (خالی = صفر)
INSERT INTO `settings` (`key`, `value`, `title_fa`, `can_edit`, `is_active`, `description`, `type`)
SELECT 'buy_fee', '', 'سود خرید نمایش', 1, 1, NULL, 'price'
@@ -738,9 +736,6 @@ WHERE `key` = 'zaarbox_api_url';
UPDATE `settings` SET `title_fa` = COALESCE(NULLIF(TRIM(`title_fa`), ''), 'شناسه ردیف Zaarbox')
WHERE `key` = 'zaarbox_price_key';
UPDATE `settings` SET `title_fa` = COALESCE(NULLIF(TRIM(`title_fa`), ''), 'اختلاف پایه نمایش')
WHERE `key` = 'fee_single';
UPDATE `settings` SET `title_fa` = COALESCE(NULLIF(TRIM(`title_fa`), ''), 'سود خرید نمایش')
WHERE `key` = 'buy_fee';
@@ -850,7 +845,7 @@ UPDATE `settings` SET `type` = 'switch' WHERE `key` IN (
-- price (مبالغ ریالی)
UPDATE `settings` SET `type` = 'price' WHERE `key` IN (
'fee_single', 'buy_fee', 'sell_fee',
'buy_fee', 'sell_fee',
'min_withdraw', 'min_deposit', 'min_first_buy', 'minimum_sell',
'up_price', 'down_price'
);
@@ -973,6 +968,7 @@ DELETE FROM `settings` WHERE `key` IN (
'name_en', 'bace_url', 'server_url', 'key', 'color_primary', 'color_secondary',
'price_from_nasiryan',
'montzar_price',
'fee_single',
'goldir_base_url',
'fee_cash',
'default_vc_number',
@@ -1194,9 +1190,8 @@ UPDATE `settings` SET `description` = 'روشن: بعد از انتخاب محص
UPDATE `settings` SET `description` = 'روشن: خارج از بازه «ساعت شروع» تا «ساعت پایان» معامله و نمایش قیمت بسته می‌شود. خاموش: محدودیت ساعتی سراسری اعمال نمی‌شود.' WHERE `key` = 'close_price';
UPDATE `settings` SET `description` = 'ساعت شروع فعالیت روزانه. فقط وقتی «بستن خودکار خارج از ساعت کاری» روشن باشد اعمال می‌شود (مثلاً ۰۹:۰۰).' WHERE `key` = 'startDay';
UPDATE `settings` SET `description` = 'ساعت پایان فعالیت روزانه. فقط وقتی «بستن خودکار خارج از ساعت کاری» روشن باشد اعمال می‌شود (مثلاً ۲۲:۰۰).' WHERE `key` = 'endDay';
UPDATE `settings` SET `description` = 'اختلاف پایه (ریال) بین قیمت خرید و فروش روی مظنه. معمولاً نصف مجموع اختلاف قیمت خرید و فروش است.' WHERE `key` = 'fee_single';
UPDATE `settings` SET `description` = 'مبلغ اضافه (ریال) روی قیمت پایه برای سمت خرید. خالی = استفاده از «اختلاف پایه».' WHERE `key` = 'buy_fee';
UPDATE `settings` SET `description` = 'مبلغ کسر (ریال) از قیمت پایه برای سمت فروش. خالی = استفاده از «اختلاف پایه».' WHERE `key` = 'sell_fee';
UPDATE `settings` SET `description` = 'مبلغ اضافه (ریال) روی قیمت پایه برای سمت خرید. خالی یا صفر = بدون اختلاف.' WHERE `key` = 'buy_fee';
UPDATE `settings` SET `description` = 'مبلغ کسر (ریال) از قیمت پایه برای سمت فروش. خالی یا صفر = بدون اختلاف.' WHERE `key` = 'sell_fee';
UPDATE `settings` SET `description` = 'درصد کارمزد هر سفارش از مبلغ معامله (اعشاری؛ مثلاً ۰٫۰۰۵ یعنی نیم در هزار).' WHERE `key` = 'fee_service_charge_percentage';
UPDATE `settings` SET
`title_fa` = 'اعمال مبلغ دقیق در معامله مشتری',
@@ -1354,7 +1349,6 @@ UPDATE `settings` SET `can_edit` = 0 WHERE `key` IN (
'bale_bot_token',
'chart_id', 'chart_use_local_data',
'external_order_api_enabled',
'fee_single',
'referral_bonus_amount',
'group_sms',
'customer_direct_sms',
@@ -1426,7 +1420,7 @@ UPDATE `settings` SET `title_fa` = 'حداقل واریز کیف پول (ریا
-- تفکیک نهایی type (بعد از همه batchها — override روی دیتابیس موجود)
UPDATE `settings` SET `type` = 'price' WHERE `key` IN (
'fee_single', 'buy_fee', 'sell_fee',
'buy_fee', 'sell_fee',
'min_withdraw', 'min_deposit', 'min_first_buy', 'minimum_sell',
'up_price', 'down_price'
);