setting changed 2
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Product;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
@@ -149,6 +150,135 @@ class ZaarboxPriceService
|
||||
}
|
||||
}
|
||||
|
||||
/** @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('Zaarbox price list fetch failed', ['message' => $e->getMessage()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 = $this->findRow(collect($prices), $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('Zaarbox live board price fetch failed', ['message' => $e->getMessage()]);
|
||||
|
||||
return $empty;
|
||||
}
|
||||
}
|
||||
|
||||
public function syncProductsFromApi(): bool
|
||||
{
|
||||
$prices = $this->fetchPriceList();
|
||||
if ($prices === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$products = 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 = Product::find(33);
|
||||
if ($copperProduct) {
|
||||
ProductPriceService::syncFromReference($copperProduct, (int) round($cp), 0);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{DAILY: array<int, array{timestamp: int, value: int}>, WEEKLY: array, MONTHLY: array}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user