diff --git a/app/Http/Controllers/Site/ReportController.php b/app/Http/Controllers/Site/ReportController.php index 52179e7..657e37a 100644 --- a/app/Http/Controllers/Site/ReportController.php +++ b/app/Http/Controllers/Site/ReportController.php @@ -330,10 +330,24 @@ class ReportController extends Controller } if ($pr_product && !$closed) { if ($priceProvider === 'main_product') { - app(EtehadiePriceService::class)->syncProductFromApi($pr_product->fresh()); - $pr_product->refresh(); + if (Product::hasStoredPriceColumns()) { + app(EtehadiePriceService::class)->syncProductFromApi($pr_product->fresh()); + $pr_product->refresh(); + $pr_price = (float) $pr_product->price + (float) ($pr_product->price_profit ?? 0); + } else { + $apiUrl = $config->getString('zaarbox_api_url', '') + ?: $config->getString('etehadie_api_url', 'https://price.zaarbox.ir/api/free'); + $live = app(EtehadiePriceService::class)->fetchLiveOrderPrice( + $pr_product->reading_method, + $apiUrl + ); + $pr_price = (float) ($live['order_price'] ?? 0); + } + } else { + $pr_price = Product::hasStoredPriceColumns() + ? (float) $pr_product->price + (float) ($pr_product->price_profit ?? 0) + : 0.0; } - $pr_price = (float) $pr_product->price + (float) ($pr_product->price_profit ?? 0); } /* diff --git a/app/Models/Product.php b/app/Models/Product.php index 7e370a5..7d613b9 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -16,6 +16,7 @@ class Product extends Model use HasFactory,Selector,SoftDeletes; private static ?bool $priceOffsetColumnExists = null; + private static ?bool $priceColumnExists = null; protected static function booted(): void { @@ -44,6 +45,22 @@ class Product extends Model return static::$priceOffsetColumnExists; } + /** گلد علی: جدول products ممکن است ستون price نداشته باشد */ + public static function hasStoredPriceColumns(): bool + { + if (static::$priceColumnExists !== null) { + return static::$priceColumnExists; + } + + try { + static::$priceColumnExists = Schema::hasColumn((new static)->getTable(), 'price'); + } catch (\Throwable) { + static::$priceColumnExists = false; + } + + return static::$priceColumnExists; + } + public function getPriceOffsetPercentAttribute($value): float { if (!static::hasPriceOffsetColumn()) { diff --git a/app/Services/EtehadiePriceService.php b/app/Services/EtehadiePriceService.php index e7203e4..2085d6e 100644 --- a/app/Services/EtehadiePriceService.php +++ b/app/Services/EtehadiePriceService.php @@ -144,9 +144,49 @@ class EtehadiePriceService return true; } + /** + * قیمت زنده از API بدون نوشتن در products (برای DB بدون ستون price). + * + * @return array{order_price: int, raw: array|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; } diff --git a/app/Services/ProductPriceService.php b/app/Services/ProductPriceService.php index 79cba0d..34962dd 100644 --- a/app/Services/ProductPriceService.php +++ b/app/Services/ProductPriceService.php @@ -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; }