217 lines
6.7 KiB
PHP
217 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Product;
|
|
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() === '') {
|
|
return ['price' => 0, 'order_price' => 0, 'raw' => null];
|
|
}
|
|
|
|
try {
|
|
$prices = Http::withoutVerifying()
|
|
->timeout(20)
|
|
->get($this->apiUrl())
|
|
->json();
|
|
|
|
if (!is_array($prices)) {
|
|
return ['price' => 0, 'order_price' => 0, 'raw' => null];
|
|
}
|
|
|
|
$key = $this->priceKey();
|
|
$collection = collect($prices);
|
|
$row = $key !== ''
|
|
? $collection->firstWhere('key', $key)
|
|
: $collection->first();
|
|
|
|
if (empty($row)) {
|
|
return ['price' => 0, 'order_price' => 0, 'raw' => null];
|
|
}
|
|
|
|
$base = (float) ($row['price'] ?? 0);
|
|
$profit = (float) ($row['profit'] ?? 0);
|
|
$gramPrice = (int) round($base / 10 + $profit);
|
|
// همان مقیاسی که سفارش/جدول prices گلد علی استفاده میکند (base + profit)
|
|
$orderPrice = (int) round($base + $profit);
|
|
|
|
return ['price' => $gramPrice, 'order_price' => $orderPrice, 'raw' => $row];
|
|
} catch (\Throwable $e) {
|
|
Log::warning('Etehadie price fetch failed', ['message' => $e->getMessage()]);
|
|
|
|
return ['price' => 0, 'order_price' => 0, 'raw' => null];
|
|
}
|
|
}
|
|
|
|
/** @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;
|
|
}
|
|
|
|
/**
|
|
* قیمت زنده از 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;
|
|
}
|
|
|
|
$prices = $this->fetchPriceList();
|
|
if ($prices === null) {
|
|
return false;
|
|
}
|
|
|
|
$RPrice = collect($prices)->firstWhere('key', $product->reading_method);
|
|
if (empty($RPrice)) {
|
|
return false;
|
|
}
|
|
|
|
if (($RPrice['name'] ?? '') === 'گلد دات ای از آبشده - رسمی - نقد فردا') {
|
|
$RPrice['price'] = ($RPrice['price'] ?? 0) / 4.3318;
|
|
}
|
|
|
|
ProductPriceService::syncFromReference(
|
|
$product,
|
|
(int) $RPrice['price'],
|
|
(int) ($RPrice['profit'] ?? 0)
|
|
);
|
|
|
|
return true;
|
|
}
|
|
}
|