From 2a91837c2bec1f8bda211a09c878f0b4b24f9739 Mon Sep 17 00:00:00 2001 From: adonis Date: Mon, 22 Jun 2026 19:50:45 +0330 Subject: [PATCH] fixed show true when price is 0 3 --- app/Services/ProductDisplayPriceService.php | 5 +- app/Services/ZaarboxPriceService.php | 89 ++++++++++++++------- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/app/Services/ProductDisplayPriceService.php b/app/Services/ProductDisplayPriceService.php index 3f37689..35443a6 100644 --- a/app/Services/ProductDisplayPriceService.php +++ b/app/Services/ProductDisplayPriceService.php @@ -289,8 +289,7 @@ class ProductDisplayPriceService return; } - $normalized = $zaarbox->normalizeFeedRow($row); - $providerRaw = (int) round((float) ($normalized['price'] ?? 0) + (float) ($normalized['profit'] ?? 0)); + $providerRaw = $zaarbox->resolveReferenceProviderTotal($row); $providerActive = $useLast || $providerRaw > 0; $product->setAttribute('reference_provider_active', $providerActive); @@ -435,7 +434,7 @@ class ProductDisplayPriceService $row = app(ZaarboxPriceService::class)->normalizeFeedRow($row); - return (int) round((float) ($row['price'] ?? 0) + (float) ($row['profit'] ?? 0)); + return app(ZaarboxPriceService::class)->resolveReferenceProviderTotal($row); } private function isMeltedTradeProduct(Product $product): bool diff --git a/app/Services/ZaarboxPriceService.php b/app/Services/ZaarboxPriceService.php index 1b0be70..9437e3b 100644 --- a/app/Services/ZaarboxPriceService.php +++ b/app/Services/ZaarboxPriceService.php @@ -221,9 +221,7 @@ class ZaarboxPriceService return $empty; } - $base = (float) ($row['price'] ?? 0); - $profit = (float) ($row['profit'] ?? 0); - $orderPrice = (int) round($base + $profit); + $orderPrice = $this->resolveReferenceProviderTotal($row); return ['price' => $orderPrice, 'order_price' => $orderPrice, 'raw' => $row]; } catch (\Throwable $e) { @@ -339,7 +337,7 @@ class ZaarboxPriceService return $empty; } - $orderPrice = (int) round((float) ($row['price'] ?? 0) + (float) ($row['profit'] ?? 0)); + $orderPrice = $this->resolveReferenceProviderTotal($row); return ['order_price' => $orderPrice, 'raw' => $row]; } catch (\Throwable $e) { @@ -386,23 +384,24 @@ class ZaarboxPriceService } $row = $this->normalizeFeedRow($RPrice); + $parts = $this->resolveReferenceBaseProfit($row); if (($row['name'] ?? '') === 'TGJU فلز مس') { - $copper = ($row['price'] ?? 0) + ($row['profit'] ?? 0); + $copper = $parts['price'] + $parts['profit']; } if (($row['name'] ?? '') === 'TGJU دلار') { - $dollar = ($row['price'] ?? 0) + ($row['profit'] ?? 0); + $dollar = $parts['price'] + $parts['profit']; } ProductPriceService::syncFromReference( $product, - (int) $row['price'], - (int) ($row['profit'] ?? 0) + $parts['price'], + $parts['profit'] ); $productPrice = (float) ProductPriceService::adjustedTotal( - (int) $row['price'], - (int) ($row['profit'] ?? 0), + $parts['price'], + $parts['profit'], ProductPriceService::offsetPercent($product) ); @@ -477,22 +476,6 @@ class ZaarboxPriceService return null; } - /** - * قیمت نمایشی محصول از یک ردیف فید + offset درصد — بدون نوشتن DB - * - * @param array $row - */ - public function computeAdjustedProductPrice(Product $product, array $row): int - { - $normalized = $this->normalizeFeedRow($row); - - return ProductPriceService::adjustedTotal( - (int) ($normalized['price'] ?? 0), - (int) ($normalized['profit'] ?? 0), - ProductPriceService::offsetPercent($product) - ); - } - /** @param array $row */ public function normalizeFeedRow(array $row): array { @@ -503,6 +486,55 @@ class ZaarboxPriceService return $row; } + /** + * base/profit مرجع zaarbox — وقتی price صفر است provider قیمت نداده (profit مقدار قدیمی است). + * + * @param array $row + * @return array{price: int, profit: int} + */ + public function resolveReferenceBaseProfit(array $row): array + { + $normalized = $this->normalizeFeedRow($row); + $price = (int) round((float) ($normalized['price'] ?? 0)); + if ($price <= 0) { + return ['price' => 0, 'profit' => 0]; + } + + return [ + 'price' => $price, + 'profit' => (int) round((float) ($normalized['profit'] ?? 0)), + ]; + } + + /** + * @param array $row + */ + public function resolveReferenceProviderTotal(array $row): int + { + $parts = $this->resolveReferenceBaseProfit($row); + + return $parts['price'] + $parts['profit']; + } + + /** + * قیمت نمایشی محصول از یک ردیف فید + offset درصد — بدون نوشتن DB + * + * @param array $row + */ + public function computeAdjustedProductPrice(Product $product, array $row): int + { + $parts = $this->resolveReferenceBaseProfit($row); + if ($parts['price'] <= 0) { + return 0; + } + + return ProductPriceService::adjustedTotal( + $parts['price'], + $parts['profit'], + ProductPriceService::offsetPercent($product) + ); + } + public function syncProductFromApi(Product $product): bool { if (!Product::hasStoredPriceColumns()) { @@ -524,11 +556,12 @@ class ZaarboxPriceService } $row = $this->normalizeFeedRow($RPrice); + $parts = $this->resolveReferenceBaseProfit($row); ProductPriceService::syncFromReference( $product, - (int) $row['price'], - (int) ($row['profit'] ?? 0) + $parts['price'], + $parts['profit'] ); return true;