zarbox added 2
This commit is contained in:
@@ -144,9 +144,49 @@ class EtehadiePriceService
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -28,18 +28,24 @@ class ProductPriceService
|
||||
|
||||
public static function syncFromReference(Product $product, int $basePrice, int $baseProfit = 0): void
|
||||
{
|
||||
$offsetPercent = self::offsetPercent($product);
|
||||
if (!Product::hasStoredPriceColumns()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Product::where('id', $product->id)->update([
|
||||
'price' => self::adjustedTotal($basePrice, $baseProfit, $offsetPercent),
|
||||
'price_profit' => 0,
|
||||
]);
|
||||
$offsetPercent = self::offsetPercent($product);
|
||||
$data = ['price' => self::adjustedTotal($basePrice, $baseProfit, $offsetPercent)];
|
||||
|
||||
if (\Illuminate\Support\Facades\Schema::hasColumn($product->getTable(), 'price_profit')) {
|
||||
$data['price_profit'] = 0;
|
||||
}
|
||||
|
||||
Product::where('id', $product->id)->update($data);
|
||||
}
|
||||
|
||||
/** وقتی فقط درصد در ادمین عوض میشود (بدون sync جدید) */
|
||||
public static function reapplyOffsetPercent(Product $product, float $newOffsetPercent, ?float $oldOffsetPercent = null): void
|
||||
{
|
||||
if (!Product::hasPriceOffsetColumn()) {
|
||||
if (!Product::hasPriceOffsetColumn() || !Product::hasStoredPriceColumns()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user