merge goldali with abshode_khanegi branch

This commit is contained in:
2026-06-04 12:18:53 +03:30
parent aea3cbbc6e
commit a9815fa717
28 changed files with 1359 additions and 379 deletions
+94 -2
View File
@@ -6,9 +6,101 @@ use App\Models\Setting;
class ConfigService
{
public const PRICE_PROVIDERS = ['montzar', 'etehadie', 'taban_gohar', 'main_product'];
public function Config($name)
{
return Setting::where('key', $name)->value('value') ?? false;
return $this->get($name, false);
}
/**
* مقدار setting؛ اگر نبود از config/price_providers.php یا default پارامتر.
*/
public function get(string $key, $default = null)
{
$row = Setting::where('key', $key)->first();
if ($row !== null && $row->value !== null && $row->value !== '') {
return $row->value;
}
$fromFile = config("price_providers.{$key}");
return $fromFile !== null ? $fromFile : $default;
}
public function getString(string $key, string $default = ''): string
{
$value = $this->get($key, $default);
return trim((string) ($value === false ? $default : $value));
}
public function getBool(string $key, bool $default = false): bool
{
$value = $this->get($key, $default);
if ($value === false || $value === null || $value === '') {
return $default;
}
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
public function getInt(string $key, int $default = 0): int
{
$value = $this->get($key, $default);
return (int) ($value === false || $value === null || $value === '' ? $default : $value);
}
/**
* تنها منبع فعال قیمت — فقط یکی از: montzar | etehadie | taban_gohar | main_product
*
* اولویت:
* 1) price_provider (یا price_primary_source قدیمی)
* 2) montzar_price=true → montzar (ابشده خانگی و پروژه‌های فعلی)
* 3) price_method پر و montzar خاموش → etehadie (گلد علی)
* 4) پیش‌فرض فایل: montzar
*/
public function priceProvider(): string
{
foreach (['price_provider', 'price_primary_source'] as $key) {
$explicit = strtolower($this->getString($key, ''));
if (in_array($explicit, self::PRICE_PROVIDERS, true)) {
return $explicit;
}
}
if ($this->getBool('montzar_price', false)) {
return 'montzar';
}
if ($this->getString('price_method') !== '') {
return 'etehadie';
}
$default = strtolower((string) config('price_providers.price_provider', 'montzar'));
return in_array($default, self::PRICE_PROVIDERS, true) ? $default : 'montzar';
}
/** @deprecated از priceProvider() استفاده کن */
public function pricePrimarySource(): string
{
$provider = $this->priceProvider();
return in_array($provider, ['montzar', 'etehadie'], true) ? $provider : 'montzar';
}
public function usesMontzarPricing(): bool
{
return $this->priceProvider() === 'montzar';
}
public function usesEtehadiePricing(): bool
{
return $this->priceProvider() === 'etehadie';
}
public function UpdateConfig($name, $value)
@@ -53,4 +145,4 @@ class ConfigService
$this->UpdateConfig('down_price', $priceInt);
}
}
}
}
+143
View File
@@ -0,0 +1,143 @@
<?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() === '') {
return ['price' => 0, 'raw' => null];
}
try {
$prices = Http::withoutVerifying()
->timeout(20)
->get($this->apiUrl())
->json();
if (!is_array($prices)) {
return ['price' => 0, 'raw' => null];
}
$key = $this->priceKey();
$collection = collect($prices);
$row = $key !== ''
? $collection->firstWhere('key', $key)
: $collection->first();
if (empty($row)) {
return ['price' => 0, 'raw' => null];
}
$base = (float) ($row['price'] ?? 0);
$profit = (float) ($row['profit'] ?? 0);
$gramPrice = (int) round($base / 10 + $profit);
return ['price' => $gramPrice, 'raw' => $row];
} catch (\Throwable $e) {
Log::warning('Etehadie price fetch failed', ['message' => $e->getMessage()]);
return ['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;
}
}