96 lines
3.1 KiB
PHP
96 lines
3.1 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Site\ReportController;
|
||
|
|
use App\Models\Product;
|
||
|
|
use Illuminate\Support\Facades\Http;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* همگامسازی قیمت محصولاتی که reading_method دارند (قیمت متغیر از API).
|
||
|
|
*/
|
||
|
|
class ProductPriceSyncService
|
||
|
|
{
|
||
|
|
public function syncAll(): bool
|
||
|
|
{
|
||
|
|
$synced = false;
|
||
|
|
|
||
|
|
try {
|
||
|
|
if (app(ZaarboxPriceService::class)->syncProductsFromApi()) {
|
||
|
|
$synced = true;
|
||
|
|
}
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
Log::warning('Zaarbox product sync failed', ['message' => $e->getMessage()]);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->syncFromLegacyFeed()) {
|
||
|
|
$synced = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $synced;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** فید قدیمی — reading_method به شکل name (سازگاری با پروژههای قدیمی) */
|
||
|
|
private function syncFromLegacyFeed(): bool
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$RPrices = Http::timeout(40)->get('http://212.23.201.173:8381/price_list', [])->json();
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!is_array($RPrices)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$CP = null;
|
||
|
|
try {
|
||
|
|
$cooper = Http::timeout(40)->get('https://www.livedata.ir/main/static/p.json', [])->json();
|
||
|
|
if (isset($cooper['s_301006'], $cooper['s_200103'])) {
|
||
|
|
$CP = (($cooper['s_301006'] / 1000000) * $cooper['s_200103']) * 10;
|
||
|
|
}
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
// optional copper feed
|
||
|
|
}
|
||
|
|
|
||
|
|
$products = Product::query()->whereNotNull('reading_method')->get();
|
||
|
|
$currentTime = now()->format('H:i:s');
|
||
|
|
$synced = false;
|
||
|
|
|
||
|
|
foreach ($products as $product) {
|
||
|
|
$show = ($currentTime >= $product->auto_open_at && $currentTime <= $product->auto_close_at);
|
||
|
|
|
||
|
|
foreach ($RPrices as $RPrice) {
|
||
|
|
if ($product->reading_method == ($RPrice['name'] ?? null)) {
|
||
|
|
ProductPriceService::syncFromReference(
|
||
|
|
$product,
|
||
|
|
(int) ($RPrice['price'] ?? 0),
|
||
|
|
(int) ($RPrice['profit'] ?? 0)
|
||
|
|
);
|
||
|
|
$synced = true;
|
||
|
|
|
||
|
|
if ($product->bubble == 1) {
|
||
|
|
$reportController = new ReportController();
|
||
|
|
$liquidGold = $reportController->lastPrice();
|
||
|
|
$p = Product::find($product->id);
|
||
|
|
if ($p && ($liquidGold['price'] ?? 0) > 0) {
|
||
|
|
$p->amount = $RPrice['price'] / $liquidGold['price'];
|
||
|
|
$p->show = $show;
|
||
|
|
$p->save();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($product->reading_method == 'مس' && $CP) {
|
||
|
|
ProductPriceService::syncFromReference($product, (int) $CP, 0);
|
||
|
|
Product::where('id', $product->id)->update(['show' => $show]);
|
||
|
|
$synced = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $synced;
|
||
|
|
}
|
||
|
|
}
|