Files
boursecu_server/app/Services/EtehadiePriceService.php
T

146 lines
4.5 KiB
PHP
Raw Normal View History

2026-06-04 12:18:53 +03:30
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class EtehadiePriceService
{
public function __construct(private ConfigService $config)
{
}
public function isEnabled(): bool
{
return $this->config->usesEtehadiePricing();
}
public function apiUrl(): string
{
$url = trim((string) $this->config->getString(
'etehadie_api_url',
(string) config('price_providers.etehadie_api_url', 'http://109.122.246.48:4949/api/free')
));
return $url !== '' ? $url : 'http://109.122.246.48:4949/api/free';
}
/** کلید محصول در پاسخ API اتحادیه (همان price_method گلد علی) */
public function priceKey(): string
{
$key = trim((string) $this->config->getString(
'price_method',
(string) config('price_providers.price_method', '')
));
return $key;
}
/**
* @return array{price: int, raw: array<string, mixed>|null}
*/
public function fetchMainGramPrice(): array
{
if (!$this->isEnabled() && $this->priceKey() === '') {
2026-06-04 13:48:37 +03:30
return ['price' => 0, 'order_price' => 0, 'raw' => null];
2026-06-04 12:18:53 +03:30
}
try {
$prices = Http::withoutVerifying()
->timeout(20)
->get($this->apiUrl())
->json();
if (!is_array($prices)) {
2026-06-04 13:48:37 +03:30
return ['price' => 0, 'order_price' => 0, 'raw' => null];
2026-06-04 12:18:53 +03:30
}
$key = $this->priceKey();
$collection = collect($prices);
$row = $key !== ''
? $collection->firstWhere('key', $key)
: $collection->first();
if (empty($row)) {
2026-06-04 13:48:37 +03:30
return ['price' => 0, 'order_price' => 0, 'raw' => null];
2026-06-04 12:18:53 +03:30
}
$base = (float) ($row['price'] ?? 0);
$profit = (float) ($row['profit'] ?? 0);
$gramPrice = (int) round($base / 10 + $profit);
2026-06-04 13:48:37 +03:30
// همان مقیاسی که سفارش/جدول prices گلد علی استفاده می‌کند (base + profit)
$orderPrice = (int) round($base + $profit);
2026-06-04 12:18:53 +03:30
2026-06-04 13:48:37 +03:30
return ['price' => $gramPrice, 'order_price' => $orderPrice, 'raw' => $row];
2026-06-04 12:18:53 +03:30
} catch (\Throwable $e) {
Log::warning('Etehadie price fetch failed', ['message' => $e->getMessage()]);
2026-06-04 13:48:37 +03:30
return ['price' => 0, 'order_price' => 0, 'raw' => null];
2026-06-04 12:18:53 +03:30
}
}
/** @return array<int, array<string, mixed>>|null */
public function fetchPriceList(): ?array
{
try {
$prices = Http::withoutVerifying()
->timeout(20)
->get($this->apiUrl())
->json();
return is_array($prices) ? $prices : null;
} catch (\Throwable $e) {
Log::warning('Etehadie price list fetch failed', ['message' => $e->getMessage()]);
return null;
}
}
public function syncProductsFromApi(): bool
{
$prices = $this->fetchPriceList();
if ($prices === null) {
return false;
}
$products = \App\Models\Product::with([])->whereNotNull('reading_method')->get();
$collection = collect($prices);
$copper = null;
$dollar = null;
foreach ($products as $product) {
$RPrice = $collection->firstWhere('key', $product->reading_method);
if (empty($RPrice)) {
continue;
}
if (($RPrice['name'] ?? '') === 'TGJU فلز مس') {
$copper = ($RPrice['price'] ?? 0) + ($RPrice['profit'] ?? 0);
}
if (($RPrice['name'] ?? '') === 'TGJU دلار') {
$dollar = ($RPrice['price'] ?? 0) + ($RPrice['profit'] ?? 0);
}
if (($RPrice['name'] ?? '') === 'گلد دات ای از آبشده - رسمی - نقد فردا') {
$RPrice['price'] = ($RPrice['price'] ?? 0) / 4.3318;
}
ProductPriceService::syncFromReference(
$product,
(int) $RPrice['price'],
(int) ($RPrice['profit'] ?? 0)
);
}
if ($copper !== null && $dollar !== null) {
$cp = ($copper / 1000000) * $dollar;
$copperProduct = \App\Models\Product::find(33);
if ($copperProduct) {
ProductPriceService::syncFromReference($copperProduct, (int) round($cp), 0);
}
}
return true;
}
}