zarbox added 2

This commit is contained in:
2026-06-04 14:42:22 +03:30
parent 92d01f96da
commit f5d53b2747
4 changed files with 86 additions and 9 deletions
+40
View File
@@ -144,9 +144,49 @@ class EtehadiePriceService
return true;
}
/**
* قیمت زنده از API بدون نوشتن در products (برای DB بدون ستون price).
*
* @return array{order_price: int, raw: array<string, mixed>|null}
*/
public function fetchLiveOrderPrice(?string $readingMethod, ?string $apiUrl = null): array
{
$empty = ['order_price' => 0, 'raw' => null];
$readingMethod = trim((string) $readingMethod);
if ($readingMethod === '') {
return $empty;
}
try {
$url = $apiUrl ?: $this->apiUrl();
$prices = Http::withoutVerifying()->timeout(20)->get($url)->json();
if (!is_array($prices)) {
return $empty;
}
$row = collect($prices)->firstWhere('key', $readingMethod)
?? collect($prices)->firstWhere('id', $readingMethod);
if (empty($row)) {
return $empty;
}
$orderPrice = (int) round((float) ($row['price'] ?? 0) + (float) ($row['profit'] ?? 0));
return ['order_price' => $orderPrice, 'raw' => $row];
} catch (\Throwable $e) {
Log::warning('Live board price fetch failed', ['message' => $e->getMessage()]);
return $empty;
}
}
/** به‌روز قیمت یک محصول از API با reading_method */
public function syncProductFromApi(Product $product): bool
{
if (!Product::hasStoredPriceColumns()) {
return false;
}
if (!$product->reading_method) {
return false;
}