From a21dbcf3e13ba559ad643d4c365d8b8617669173 Mon Sep 17 00:00:00 2001 From: adonis Date: Thu, 11 Jun 2026 21:17:40 +0330 Subject: [PATCH] fix bug 2 --- .../Controllers/Admin/ProductController.php | 8 +++- .../Customer/CustomerController.php | 2 + .../Controllers/Customer/WalletController.php | 17 ++++++-- app/Services/ProductDisplayPriceService.php | 41 +++++++++++++++++++ app/Services/ProductPriceSyncService.php | 31 ++++++++++++++ 5 files changed, 95 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 0d8a76a..e59c63f 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -160,7 +160,13 @@ class ProductController extends SuperController $products = Product::with(['file'])->where('active', 1)->get(); if (str_contains(request()->path(), '/customer/')) { - app(ProductDisplayPriceService::class)->enrichForApi($products); + $displayPriceService = app(ProductDisplayPriceService::class); + $displayPriceService->enrichForApi($products); + $products->each(function (Product $product) use ($displayPriceService) { + if (!$displayPriceService->hasCustomerDisplayPrice($product)) { + $product->setAttribute('show', false); + } + }); } if (Product::hasPriceOffsetColumn() && str_contains(request()->path(), '/customer/')) { diff --git a/app/Http/Controllers/Customer/CustomerController.php b/app/Http/Controllers/Customer/CustomerController.php index ca5a425..d4a3082 100644 --- a/app/Http/Controllers/Customer/CustomerController.php +++ b/app/Http/Controllers/Customer/CustomerController.php @@ -132,6 +132,8 @@ class CustomerController extends Controller $wallet->cash_from_silver = $count_cash_from_silver; $wallet->cash_from_copper = $count_cash_from_copper; $wallet->count_cash_total = $count_cash_total; + // cash در پاسخ = موجودی ریالی کیف اصلی (نه کیف legacy محصولات main_wallet_products) + $wallet->cash = $count_cash_pure; // جمع واقعی طلا از کیف‌ها — هم‌تراز wallet/me و کارت محصول (بدون دوباره‌شماری customers.gold) $wallet->gold_sum = round((float) $wallet->count_gold, 2); diff --git a/app/Http/Controllers/Customer/WalletController.php b/app/Http/Controllers/Customer/WalletController.php index 2cd6cec..0f6d45c 100644 --- a/app/Http/Controllers/Customer/WalletController.php +++ b/app/Http/Controllers/Customer/WalletController.php @@ -40,11 +40,22 @@ class WalletController extends Controller public function walletMe() { - $wallet = Wallet::select(['owner', 'id', 'gold', 'cash'])->where('owner', auth('customer')->id()) + $customerId = auth('customer')->id(); + $wallet = Wallet::select(['owner', 'id', 'gold', 'cash']) + ->where('owner', $customerId) + ->whereNull('product_id') + ->whereNull('fund_id') ->first(); -// $wallet['gold_sum'] = Wallet::where('owner', auth('customer')->id())->get(); - $wallet['gold_sum'] = round(Wallet::where('owner', auth('customer')->id())->where('product_id', null)->sum('gold'), 2); + if (!$wallet) { + $wallet = Wallet::select(['owner', 'id', 'gold', 'cash']) + ->where('owner', $customerId) + ->whereNull('fund_id') + ->first() ?? new Wallet(); + } + + $wallet['gold_sum'] = round(Wallet::where('owner', $customerId)->where('product_id', null)->sum('gold'), 2); + return response()->json($wallet); } diff --git a/app/Services/ProductDisplayPriceService.php b/app/Services/ProductDisplayPriceService.php index f87c987..1502409 100644 --- a/app/Services/ProductDisplayPriceService.php +++ b/app/Services/ProductDisplayPriceService.php @@ -65,7 +65,20 @@ class ProductDisplayPriceService private function applyAppPrice(Product $product, int $basePrice): void { + $storedPrice = (int) ($product->price ?? 0); + $useLast = Product::hasUseLastPriceIfZeroColumn() + && ($product->use_last_price_if_zero ?? false); + if ($basePrice <= 0) { + if ($useLast && $storedPrice > 0) { + return; + } + + $product->setAttribute('price', 0); + if (Schema::hasColumn($product->getTable(), 'price_profit')) { + $product->setAttribute('price_profit', 0); + } + return; } @@ -75,6 +88,15 @@ class ProductDisplayPriceService $computed = ProductPriceService::adjustedTotal($basePrice, 0, $offset); if ($computed <= 0) { + if ($useLast && $storedPrice > 0) { + return; + } + + $product->setAttribute('price', 0); + if (Schema::hasColumn($product->getTable(), 'price_profit')) { + $product->setAttribute('price_profit', 0); + } + return; } @@ -232,4 +254,23 @@ class ProductDisplayPriceService return in_array($group, ['آبشده', 'کاتد مس'], true); } + + /** + * آیا بعد از enrich قیمت پایه برای نمایش در اپ کاربر وجود دارد؟ + * کارمزد (cash_fee) به‌تنهایی قیمت محسوب نمی‌شود. + */ + public function hasCustomerDisplayPrice(Product $product): bool + { + if ((int) ($product->price ?? 0) > 0) { + return true; + } + + if (Schema::hasColumn($product->getTable(), 'price_profit')) { + if ((int) ($product->price_profit ?? 0) > 0) { + return true; + } + } + + return false; + } } diff --git a/app/Services/ProductPriceSyncService.php b/app/Services/ProductPriceSyncService.php index 822c5ef..3d6abd6 100644 --- a/app/Services/ProductPriceSyncService.php +++ b/app/Services/ProductPriceSyncService.php @@ -39,6 +39,37 @@ class ProductPriceSyncService $synced = true; } + if ($this->syncAppPriceProducts()) { + $synced = true; + } + + return $synced; + } + + /** محصولات آبشده با مرجع «قیمت اپ» — ذخیره آخرین قیمت در DB برای fallback */ + private function syncAppPriceProducts(): bool + { + $display = app(ProductDisplayPriceService::class); + $products = Product::query() + ->where('active', 1) + ->get() + ->filter(static fn (Product $product) => $display->usesAppPrice($product)); + + if ($products->isEmpty()) { + return false; + } + + $basePrice = $display->resolveAppBasePrice(); + if ($basePrice <= 0) { + return false; + } + + $synced = false; + foreach ($products as $product) { + ProductPriceService::syncFromReference($product, $basePrice, 0); + $synced = true; + } + return $synced; }