fix price force refresh

This commit is contained in:
2026-06-14 12:01:20 +03:30
parent 50a726f251
commit 1abddfbab8
6 changed files with 229 additions and 37 deletions
@@ -74,7 +74,7 @@ class CategoryController extends Controller
$category->name = $name;
}
foreach (['active', 'show_in_filter', 'show_in_select', 'sort_order'] as $field) {
foreach (['active', 'show_in_select', 'sort_order'] as $field) {
if ($request->has($field)) {
$category->{$field} = $field === 'sort_order'
? (int) $request->input($field)
@@ -100,22 +100,18 @@ class CategoryController extends Controller
]);
}
/** تب فیلتر اپ کاربر */
public function clientIndex(Request $request)
/** تب فیلتر اپ کاربر — خودکار از محصولات فعال */
public function clientIndex(Request $request, CategoryService $categoryService)
{
$query = Category::query()
->where('active', 1)
->where('show_in_filter', 1);
$categories = $categoryService->clientFilterCategories();
if ($request->filled('search')) {
$query->where('name', 'like', '%' . $request->search . '%');
$needle = trim((string) $request->search);
$categories = $categories->filter(
static fn ($category) => str_contains((string) $category->name, $needle)
)->values();
}
$categories = $query
->orderBy('sort_order')
->orderBy('id')
->get(['id', 'name', 'sort_order']);
return response()->json($categories);
}
@@ -148,7 +148,7 @@ class ReportController extends Controller
$dbBuilt = $cache->buildFromDatabase();
if (($dbBuilt['price'] ?? 0) > 0) {
return $dbBuilt;
return $cache->put($dbBuilt);
}
}
+29
View File
@@ -123,4 +123,33 @@ class CategoryService
return $created;
}
/**
* تب‌های فیلتر اپ — فقط دسته‌های فعال که حداقل یک محصول فعال دارند.
* show_in_filter دستی لازم نیست؛ از همان منطق نمایش محصولات مشتق می‌شود.
*/
public function clientFilterCategories(): Collection
{
$groupsWithProducts = Product::query()
->where('active', 1)
->whereNotNull('group_type')
->where('group_type', '!=', '')
->distinct()
->pluck('group_type')
->map(static fn ($name) => trim((string) $name))
->filter()
->unique()
->values();
if ($groupsWithProducts->isEmpty()) {
return collect();
}
return Category::query()
->where('active', 1)
->whereIn('name', $groupsWithProducts)
->orderBy('sort_order')
->orderBy('id')
->get(['id', 'name', 'sort_order']);
}
}
+28 -5
View File
@@ -44,6 +44,11 @@ class LivePriceCacheService
$display = app(ProductDisplayPriceService::class);
$last = Price::orderByDesc('id')->first();
$base = $last ? (int) $last->price : 0;
$provider = $config->priceProvider();
$montzarPrice = 0;
$zaarboxPrice = 0;
$mainProductPrice = 0;
if ($base <= 0) {
$payload = [
@@ -60,17 +65,33 @@ class LivePriceCacheService
} else {
$diffBuy = (int) $config->displayBuySpreadRial();
$diffSell = (int) $config->displaySellSpreadRial();
$buy = (int) ceil($base + $diffBuy);
$sell = max(0, (int) ceil($base - $diffSell));
$openBuy = true;
$openSell = true;
if ($provider === 'montzar') {
$montzarPrice = $config->getInt('montzar_live_price', 0) ?: $base;
$montzarDiff = (int) $config->getInt('montzar_diff', 0);
$diffBuy += $montzarDiff;
$diffSell += $montzarDiff;
$openBuy = $config->getBool('montzar_open_buy', true);
$openSell = $config->getBool('montzar_open_sell', true);
} elseif ($provider === 'zaarbox') {
$zaarboxPrice = $base;
} elseif ($provider === 'main_product') {
$mainProductPrice = $base;
}
$buy = $openBuy ? (int) ceil($base + $diffBuy) : 0;
$sell = $openSell ? max(0, (int) ceil($base - $diffSell)) : 0;
$payload = [
'sell' => $sell,
'buy' => $buy,
'price_last' => $buy,
'price' => $base,
'main_product' => 0,
'montzar' => 0,
'zaarbox' => 0,
'main_product' => $mainProductPrice,
'montzar' => $montzarPrice,
'zaarbox' => $zaarboxPrice,
'closed' => false,
'time' => now(),
];
@@ -84,6 +105,8 @@ class LivePriceCacheService
: ['sell' => $payload['sell'] ?? 0, 'buy' => $payload['buy'] ?? 0];
$payload['last_historical'] = $display->resolveHistoricalBasePrice();
$display->cacheLivePricePayload($payload);
return $payload;
}
}
+10 -1
View File
@@ -4,6 +4,7 @@ namespace App\Services;
use App\Models\Cnt;
use App\Models\Order;
use App\Models\Setting;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
@@ -44,10 +45,18 @@ class MontzarService
$token = $response['access_token'] ?? $response['token'] ?? null;
if (!$token) {
Log::error('Montzar login failed', [
'base_url' => $this->baseUrl(),
'mobile' => $this->config()->Config('montzar_mobile'),
'response' => $response,
]);
throw new \Exception('Montzar login failed: ' . json_encode($response, JSON_UNESCAPED_UNICODE));
}
$this->config()->UpdateConfig('montzar_token', $token);
Setting::updateOrCreate(
['key' => 'montzar_token'],
['value' => $token, 'is_active' => 1]
);
return $token;
}