diff --git a/app/Http/Controllers/Site/ReportController.php b/app/Http/Controllers/Site/ReportController.php index 31476d8..e8fa375 100644 --- a/app/Http/Controllers/Site/ReportController.php +++ b/app/Http/Controllers/Site/ReportController.php @@ -118,8 +118,20 @@ class ReportController extends Controller } } if (count($prises) === 0) { + $config = app(ConfigService::class); + if (!$config->getBool('show_last_price', false)) { + return ['sell' => 0, 'buy' => 0]; + } + $price = Price::orderBy('id', 'desc')->first(); - $priceValue = $price->price; + $priceValue = $price ? (float) $price->price : 0.0; + + if ( + $priceValue <= 0 + || !$this->isPlausibleStoredPrice($priceValue, $config->priceProvider(), $config) + ) { + return ['sell' => 0, 'buy' => 0]; + } return ['sell' => $priceValue - $diffSell, 'buy' => $priceValue + $diffBuy]; } @@ -134,6 +146,40 @@ class ReportController extends Controller return $payload; } + /** + * جلوگیری از fallback به قیمت ذخیره‌شدهٔ نامعتبر (مثلاً طلا ~785M روی پروژه مس). + */ + protected function isPlausibleStoredPrice(float $storedPrice, string $priceProvider, ConfigService $config): bool + { + if ($storedPrice <= 0) { + return false; + } + + $up = (float) $config->get('up_price', 0); + $down = (float) $config->get('down_price', 0); + + if ($up > 0 && $down > 0) { + $min = min($up, $down); + $max = max($up, $down); + + if ($storedPrice < $min * 0.25 || $storedPrice > $max * 4) { + return false; + } + + return true; + } + + if (in_array($priceProvider, ['zaarbox', 'main_product'], true)) { + return $storedPrice < 100_000_000; + } + + if ($priceProvider === 'montzar') { + return $storedPrice > 1_000_000 && $storedPrice < 500_000_000; + } + + return $storedPrice < 500_000_000; + } + public function lastPrice() { $config = app(ConfigService::class); @@ -215,11 +261,10 @@ class ReportController extends Controller 'main_product_price', 'buy_fee', 'sell_fee', - 'show_last_price', ])->get(); $settingsMap = $settings->pluck('value', 'key'); - $show_last_price = $toBool($settingsMap->get('show_last_price', false)); + $show_last_price = $config->getBool('show_last_price', false); $taban_gohar_price = $settingsMap->get('tabanGohar_price'); $main_product_price = $settingsMap->get('main_product_price'); @@ -409,8 +454,12 @@ class ReportController extends Controller } $lastPrice = Price::latest()->first(); + $storedPrice = $lastPrice ? (float) $lastPrice->price : 0.0; - if (!$lastPrice || (float) $lastPrice->price <= 0) { + if ( + $storedPrice <= 0 + || !$this->isPlausibleStoredPrice($storedPrice, $priceProvider, $config) + ) { return $this->appendUpPrices([ 'sell' => 0, 'buy' => 0, @@ -425,7 +474,7 @@ class ReportController extends Controller ]); } - $priceValue = (float) $lastPrice->price; + $priceValue = $storedPrice; $priceSource = 'database_last'; } @@ -509,7 +558,7 @@ class ReportController extends Controller } /* - | main_product / zaarbox: سفارش از Price::latest() — نمایش buy/sell همان منبع. + | main_product / zaarbox: buy/sell از همان قیمت زنده (نه رکورد قدیمی DB). */ if (in_array($priceProvider, ['main_product', 'zaarbox'], true) && !$closed) { $base = (float) $priceValue; diff --git a/config/price_providers.php b/config/price_providers.php index 9905c36..399f6e9 100644 --- a/config/price_providers.php +++ b/config/price_providers.php @@ -26,4 +26,6 @@ return [ 'profit_gold_api_enabled' => false, 'open_register' => true, 'auto_active_customer' => false, + /** null/خالی در settings = false — فقط وقتی API قطع است از prices استفاده شود */ + 'show_last_price' => false, ];