From 11fb7272cba44d0e8ffffc7eba02fcd5272b0458 Mon Sep 17 00:00:00 2001 From: adonis Date: Fri, 12 Jun 2026 19:23:09 +0330 Subject: [PATCH] added kashkool 5 --- .../Controllers/Admin/ProductController.php | 4 +- app/Services/ProductDisplayPriceService.php | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 6613627..f912c26 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -162,9 +162,7 @@ class ProductController extends SuperController $displayPriceService = app(ProductDisplayPriceService::class); $displayPriceService->enrichForApi($products); $products->each(function (Product $product) use ($displayPriceService) { - if (! $displayPriceService->hasCustomerDisplayPrice($product)) { - $product->setAttribute('show', false); - } + $product->setAttribute('show', $displayPriceService->resolveCustomerShow($product)); }); } diff --git a/app/Services/ProductDisplayPriceService.php b/app/Services/ProductDisplayPriceService.php index f1bc393..f6c17d8 100644 --- a/app/Services/ProductDisplayPriceService.php +++ b/app/Services/ProductDisplayPriceService.php @@ -57,10 +57,76 @@ class ProductDisplayPriceService if ($product->reading_method && is_array($zaarboxFeed)) { $this->applyReferenceFeedPrice($product, $zaarboxFeed, $zaarbox); + + continue; + } + + if ($this->isWeightBasedStoreProduct($product)) { + $this->enrichWeightBasedPrice($product); } } } + /** شمش / سکه و … — قیمت واحد (هر گرم) از مظنه زنده یا آخرین قیمت */ + private function isWeightBasedStoreProduct(Product $product): bool + { + if ($this->usesAppPrice($product) || $product->reading_method) { + return false; + } + + if ((int) ($product->direct_price ?? 0) === 1) { + return false; + } + + return (float) ($product->amount ?? 0) > 0; + } + + private function enrichWeightBasedPrice(Product $product): void + { + $useLast = $this->productUsesLastPriceIfZero($product); + $unitBuy = $useLast + ? $this->resolveLiveBuyPriceWithFallback() + : $this->resolveStrictLiveBuyPrice(); + + if ($unitBuy <= 0 && ! $useLast && app(ConfigService::class)->getBool('show_last_price', false)) { + $unitBuy = $this->resolveHistoricalBasePrice(); + } + + $this->applyPriceOrFallback($product, $unitBuy, $useLast); + } + + private function resolveStrictLiveBuyPrice(): int + { + $cached = Cache::get(self::LIVE_PRICE_CACHE_KEY); + if (! is_array($cached)) { + return 0; + } + + $buy = (int) ($cached['buy'] ?? 0); + if ($buy > 0) { + return $buy; + } + + $priceLast = (int) ($cached['price_last'] ?? 0); + if ($priceLast > 0) { + return $priceLast; + } + + return $this->extractBaseFromPayload($cached); + } + + private function resolveLiveBuyPriceWithFallback(): int + { + $strict = $this->resolveStrictLiveBuyPrice(); + if ($strict > 0) { + return $strict; + } + + $payload = $this->resolveFallbackPayload(); + + return (int) ($payload['buy'] ?? 0) ?: (int) ($payload['price_last'] ?? 0); + } + private function applyAppPrice(Product $product, int $basePrice): void { $useLast = $this->productUsesLastPriceIfZero($product); @@ -347,4 +413,29 @@ class ProductDisplayPriceService return false; } + + /** + * نمایش در اپ کاربر: + * • خاموش (show=false در DB) → false + * • قیمت نمایشی بعد از enrich → true + * • قیمت صفر و show_last_price خاموش → false + * • قیمت صفر و show_last_price روشن و fallback موجود → true + */ + public function resolveCustomerShow(Product $product): bool + { + if (! (bool) ($product->show ?? true)) { + return false; + } + + if ($this->hasCustomerDisplayPrice($product)) { + return true; + } + + if (! app(ConfigService::class)->getBool('show_last_price', false)) { + return false; + } + + return $this->resolveLastPriceFallback($product) > 0 + || $this->resolveHistoricalBasePrice() > 0; + } }