added ons

This commit is contained in:
2026-07-12 14:08:16 +03:30
parent 7095a7a102
commit 2cade8a334
4 changed files with 123 additions and 2 deletions
@@ -139,6 +139,7 @@ class ReportController extends Controller
$payload['up'] = $this->buildUpDownPrices();
$payload['last_historical'] = app(ProductDisplayPriceService::class)->resolveHistoricalBasePrice();
$payload['gold_ounce'] = $this->resolveGoldOuncePayload();
app(ProductDisplayPriceService::class)->cacheLivePricePayload($payload);
$isFreshPrice = $saveFullCache || request()->boolean('force_refresh');
@@ -164,6 +165,29 @@ class ReportController extends Controller
return $payload;
}
protected function resolveGoldOuncePayload(): ?array
{
$config = app(ConfigService::class);
if (! $config->getBool('show_gold_ounce_price', false)) {
return null;
}
$zaarbox = app(ZaarboxPriceService::class);
if (! $zaarbox->hasApiUrlConfigured()) {
return null;
}
$ounce = $zaarbox->fetchOuncePrice();
if (($ounce['price'] ?? 0) <= 0) {
return null;
}
return [
'price' => (int) $ounce['price'],
'label' => trim((string) ($ounce['name'] ?? 'انس طلا')) ?: 'انس طلا',
];
}
public function lastPrice(Request $request)
{
$config = app(ConfigService::class);
+66
View File
@@ -54,6 +54,11 @@ class ZaarboxPriceService
return $url !== '' ? $url : 'https://price.zaarbox.ir/api/free';
}
public function hasApiUrlConfigured(): bool
{
return trim($this->config->getString('zaarbox_api_url', '')) !== '';
}
/** کلید یا id ردیف در JSON تابلو (settings: zaarbox_price_key) */
public function priceIdentifier(): string
{
@@ -190,6 +195,67 @@ class ZaarboxPriceService
return $prices;
}
/**
* @param array<int, array<string, mixed>> $prices
* @return array<string, mixed>|null
*/
public function findOunceFeedRow(array $prices): ?array
{
$row = collect($prices)
->map(fn ($item) => is_array($item) ? $item : (array) $item)
->filter(function (array $item) {
$name = trim((string) ($item['name'] ?? ''));
return $name !== '' && mb_strpos($name, 'انس') !== false;
})
->sortBy([
fn (array $item) => (bool) ($item['active'] ?? false) ? 0 : 1,
fn (array $item) => str_contains(trim((string) ($item['name'] ?? '')), 'انس طلا') ? 0 : 1,
fn (array $item) => mb_strlen(trim((string) ($item['name'] ?? ''))),
])
->first();
return is_array($row) ? $row : null;
}
/**
* @return array{price: int, name: string, raw: array<string, mixed>|null}
*/
public function fetchOuncePrice(): array
{
$empty = ['price' => 0, 'name' => '', 'raw' => null];
if (! $this->hasApiUrlConfigured()) {
return $empty;
}
try {
$prices = $this->cachedPriceList(false) ?? $this->fetchPriceList();
if (! is_array($prices)) {
return $empty;
}
$row = $this->findOunceFeedRow($prices);
if ($row === null) {
return $empty;
}
$price = $this->resolveReferenceProviderTotal($row);
if ($price <= 0) {
return $empty;
}
return [
'price' => $price,
'name' => trim((string) ($row['name'] ?? 'انس طلا')),
'raw' => $row,
];
} catch (\Throwable $e) {
Log::warning('Zaarbox ounce price fetch failed', ['message' => $e->getMessage()]);
return $empty;
}
}
/**
* @return array{price: int, order_price: int, raw: array<string, mixed>|null}
*/