added ons

This commit is contained in:
2026-07-12 20:58:34 +03:30
parent 555f09b9c7
commit 300c909b9e
6 changed files with 85 additions and 25 deletions
+67 -3
View File
@@ -59,6 +59,15 @@ class ZaarboxPriceService
return trim($this->config->getString('zaarbox_api_url', '')) !== '';
}
/** کلید یا id ردیف انس در JSON تابلو (settings: zaarbox_ounce_key) */
public function ouncePriceIdentifier(): string
{
return trim((string) $this->config->getString(
'zaarbox_ounce_key',
(string) config('price_providers.zaarbox_ounce_key', 'tala_ounce')
));
}
/** کلید یا id ردیف در JSON تابلو (settings: zaarbox_price_key) */
public function priceIdentifier(): string
{
@@ -201,15 +210,29 @@ class ZaarboxPriceService
*/
public function findOunceFeedRow(array $prices): ?array
{
$identifier = $this->ouncePriceIdentifier();
if ($identifier !== '') {
$row = $this->findFeedRow($prices, $identifier);
if ($row !== null) {
return $row;
}
}
$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;
return $name !== '' && (
mb_strpos($name, 'انس') !== false
|| mb_strpos($name, 'اونس') !== false
);
})
->sortBy([
fn (array $item) => trim((string) ($item['key'] ?? '')) === 'tala_ounce' ? 0 : 1,
fn (array $item) => (bool) ($item['active'] ?? false) ? 0 : 1,
fn (array $item) => trim((string) ($item['category'] ?? '')) === 'ounce' ? 0 : 1,
fn (array $item) => str_contains(trim((string) ($item['name'] ?? '')), 'اونس طلا') ? 0 : 1,
fn (array $item) => str_contains(trim((string) ($item['name'] ?? '')), 'انس طلا') ? 0 : 1,
fn (array $item) => mb_strlen(trim((string) ($item['name'] ?? ''))),
])
@@ -229,7 +252,11 @@ class ZaarboxPriceService
}
try {
$prices = $this->cachedPriceList(false) ?? $this->fetchPriceList();
// مثل fetchPrice — اول HTTP تازه؛ کش فقط fallback (کش خالی/قدیمی باعث null شدن gold_ounce می‌شد)
$prices = $this->fetchPriceList();
if (! is_array($prices) || $prices === []) {
$prices = $this->cachedPriceList(false);
}
if (! is_array($prices)) {
return $empty;
}
@@ -246,7 +273,7 @@ class ZaarboxPriceService
return [
'price' => $price,
'name' => trim((string) ($row['name'] ?? 'انس طلا')),
'name' => trim((string) ($row['name'] ?? 'اونس طلا')) ?: 'اونس طلا',
'raw' => $row,
];
} catch (\Throwable $e) {
@@ -256,6 +283,43 @@ class ZaarboxPriceService
}
}
/** payload انس برای last/SSE — null یعنی نمایش خاموش یا بدون مظنه */
public function clientGoldOuncePayload(): ?array
{
if (! $this->config->getBool('show_gold_ounce_price', false)) {
return null;
}
if (! $this->hasApiUrlConfigured()) {
return null;
}
$ounce = $this->fetchOuncePrice();
if (($ounce['price'] ?? 0) <= 0) {
return null;
}
$rawPrice = is_array($ounce['raw'] ?? null)
? ($ounce['raw']['price'] ?? $ounce['price'])
: $ounce['price'];
return [
'price' => round((float) $rawPrice, 2),
'label' => trim((string) ($ounce['name'] ?? 'اونس طلا')) ?: 'اونس طلا',
];
}
/**
* @param array<string, mixed> $payload
* @return array<string, mixed>
*/
public function attachGoldOunceToPayload(array $payload): array
{
$payload['gold_ounce'] = $this->clientGoldOuncePayload();
return $payload;
}
/**
* @return array{price: int, order_price: int, raw: array<string, mixed>|null}
*/