fix bug 2

This commit is contained in:
2026-06-11 21:17:40 +03:30
parent 2636df99ad
commit a21dbcf3e1
5 changed files with 95 additions and 4 deletions
@@ -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/')) {
@@ -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);
@@ -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);
}
@@ -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;
}
}
+31
View File
@@ -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;
}