added percent in product price 4

This commit is contained in:
2026-07-05 13:04:00 +03:30
parent 3373bc82ca
commit 694fddc909
4 changed files with 45 additions and 25 deletions
@@ -201,7 +201,10 @@ class CartController extends Controller
->load('items'); ->load('items');
} }
$wallet = Wallet::where('owner',auth('customer')->id())->whereNull('fund_id')->whereNull('product_id') $wallet = Wallet::where('owner', auth('customer')->id())
->where('owner_type', auth('customer')->user()->type)
->whereNull('fund_id')
->whereNull('product_id')
->first(); ->first();
if (!$wallet) { if (!$wallet) {
@@ -19,6 +19,7 @@ use App\Models\Setting;
use App\Http\Controllers\Admin\TahesabController; use App\Http\Controllers\Admin\TahesabController;
use App\Services\ConfigService; use App\Services\ConfigService;
use App\Services\ReferralGiftService; use App\Services\ReferralGiftService;
use App\Services\WalletBalanceService;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
@@ -56,14 +57,20 @@ class CustomerController extends Controller
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds(); $mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
$treasureGrams = 0; $treasureGrams = 0;
$wallet->wp = Wallet::with('product')->where('owner', $user->id)->get(); $wallet->wp = Wallet::with('product')
->where('owner', $user->id)
->where('owner_type', $user->type)
->get();
foreach ($wallet->wp as $row) { foreach ($wallet->wp as $row) {
if ($row->product_id != null && !in_array((int) $row->product_id, $mainWalletProductIds, true)) { if ($row->product_id != null && !in_array((int) $row->product_id, $mainWalletProductIds, true)) {
$treasureGrams += self::productLineWeightToGrams($row->product, $row->gold); $treasureGrams += self::productLineWeightToGrams($row->product, $row->gold);
} }
} }
$goldAbGrams = self::weightToGrams((float) Wallet::where('owner', $user->id)->where('product_id', null)->sum('gold')); $goldAbGrams = self::weightToGrams((float) Wallet::where('owner', $user->id)
->where('owner_type', $user->type)
->where('product_id', null)
->sum('gold'));
$userGoldGrams = self::weightToGrams((float) $user->gold); $userGoldGrams = self::weightToGrams((float) $user->gold);
$wallet->treasure = round(self::gramsToTradeUnit($treasureGrams), 3); $wallet->treasure = round(self::gramsToTradeUnit($treasureGrams), 3);
@@ -76,7 +83,10 @@ class CustomerController extends Controller
} }
$wallet->gold_ab = round(self::gramsToTradeUnit($goldAbGrams), 2); $wallet->gold_ab = round(self::gramsToTradeUnit($goldAbGrams), 2);
$user->wallets = Wallet::with(['product'])->where('owner', $user->id)->get() ?? new Wallet(); $user->wallets = Wallet::with(['product'])
->where('owner', $user->id)
->where('owner_type', $user->type)
->get() ?? new Wallet();
$count_gold = 0; $count_gold = 0;
$count_silver = 0; $count_silver = 0;
@@ -112,7 +122,7 @@ class CustomerController extends Controller
$count_cash_from_copper += ($walletSingle->product->price * self::productPriceMultiplier($walletSingle->product)) * $walletSingle->gold; $count_cash_from_copper += ($walletSingle->product->price * self::productPriceMultiplier($walletSingle->product)) * $walletSingle->gold;
$count_product += $walletSingle->gold; $count_product += $walletSingle->gold;
} }
} else { } elseif ($walletSingle->fund_id === null) {
$count_gold += self::weightToGrams((float) $walletSingle->gold); $count_gold += self::weightToGrams((float) $walletSingle->gold);
$count_cash_pure += $walletSingle->cash; $count_cash_pure += $walletSingle->cash;
$wallet->count_cash_wallet = $walletSingle->cash; $wallet->count_cash_wallet = $walletSingle->cash;
@@ -133,19 +143,21 @@ class CustomerController extends Controller
$count_cash_total = $count_cash_from_gold + $count_cash_from_silver + $count_cash_from_copper + $count_cash_pure; $count_cash_total = $count_cash_from_gold + $count_cash_from_silver + $count_cash_from_copper + $count_cash_pure;
$wallet->count_cash_wallets = $count_cash_total - $wallet->count_cash_wallet; $mainWalletCash = app(WalletBalanceService::class)->mainWalletCash($user->wallets);
$wallet->count_cash_wallet = $mainWalletCash;
$wallet->count_cash_wallets = $count_cash_total - $mainWalletCash;
$wallet->count_gold = round(self::gramsToTradeUnit($count_gold), 3); $wallet->count_gold = round(self::gramsToTradeUnit($count_gold), 3);
$wallet->count_gold_wallets = round($wallet->count_gold - $wallet->count_gold_wallet, 3); $wallet->count_gold_wallets = round($wallet->count_gold - $wallet->count_gold_wallet, 3);
$wallet->count_silver = round(self::gramsToTradeUnit($count_silver), 3); $wallet->count_silver = round(self::gramsToTradeUnit($count_silver), 3);
$wallet->count_copper = round(self::gramsToTradeUnit($count_copper), 3); $wallet->count_copper = round(self::gramsToTradeUnit($count_copper), 3);
$wallet->count_cash = $count_cash_pure;
$wallet->count_product = $count_product; $wallet->count_product = $count_product;
$wallet->cash_from_gold = $count_cash_from_gold; $wallet->cash_from_gold = $count_cash_from_gold;
$wallet->cash_from_silver = $count_cash_from_silver; $wallet->cash_from_silver = $count_cash_from_silver;
$wallet->cash_from_copper = $count_cash_from_copper; $wallet->cash_from_copper = $count_cash_from_copper;
$wallet->count_cash_total = $count_cash_total; $wallet->count_cash_total = $count_cash_total;
// cash در پاسخ = موجودی ریالی کیف اصلی (نه کیف legacy محصولات main_wallet_products) // cash در پاسخ = موجودی ریالی کیف اصلی (نه کیف legacy محصولات main_wallet_products)
$wallet->cash = $count_cash_pure; $wallet->cash = $mainWalletCash;
$wallet->count_cash = $mainWalletCash;
// جمع واقعی طلا از کیف‌ها — هم‌تراز wallet/me و کارت محصول (بدون دوباره‌شماری customers.gold) // جمع واقعی طلا از کیف‌ها — هم‌تراز wallet/me و کارت محصول (بدون دوباره‌شماری customers.gold)
$wallet->gold_sum = round((float) $wallet->count_gold, 2); $wallet->gold_sum = round((float) $wallet->count_gold, 2);
@@ -15,6 +15,7 @@ use App\Models\Price;
use App\Models\Setting; use App\Models\Setting;
use App\Http\Controllers\Admin\TahesabController; use App\Http\Controllers\Admin\TahesabController;
use App\Services\AssetSummaryService; use App\Services\AssetSummaryService;
use App\Services\WalletBalanceService;
use App\Services\ZaarboxPriceService; use App\Services\ZaarboxPriceService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
@@ -38,25 +39,28 @@ class WalletController extends Controller
return response()->json($records); return response()->json($records);
} }
public function walletMe() public function walletMe(WalletBalanceService $balanceService)
{ {
$customerId = auth('customer')->id(); $customer = auth('customer')->user();
$wallet = Wallet::select(['owner', 'id', 'gold', 'cash']) $customerId = (int) $customer->id;
$ownerType = (int) $customer->type;
$wallets = Wallet::query()
->where('owner', $customerId) ->where('owner', $customerId)
->whereNull('product_id') ->where('owner_type', $ownerType)
->whereNull('fund_id') ->get();
->first();
if (!$wallet) { $mainWallet = $wallets->first(
$wallet = Wallet::select(['owner', 'id', 'gold', 'cash']) fn (Wallet $row) => $row->fund_id === null && $row->product_id === null
->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([
'owner' => $customerId,
return response()->json($wallet); 'id' => $mainWallet?->id,
'gold' => $balanceService->mainWalletGold($wallets),
'cash' => $balanceService->mainWalletCash($wallets),
'gold_sum' => round($balanceService->totalGoldWeight($wallets), 2),
]);
} }
public function assetsSummary(AssetSummaryService $assetSummaryService) public function assetsSummary(AssetSummaryService $assetSummaryService)
+3 -2
View File
@@ -19,7 +19,8 @@ class AssetSummaryService
]; ];
public function __construct( public function __construct(
private readonly ConfigService $configService private readonly ConfigService $configService,
private readonly WalletBalanceService $walletBalanceService
) { ) {
} }
@@ -53,7 +54,7 @@ class AssetSummaryService
$this->buildInventoryCards($userWallets, $catalogById, $mainWalletProductIds, $mainWallet), $this->buildInventoryCards($userWallets, $catalogById, $mainWalletProductIds, $mainWallet),
); );
$metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById, $metalsWithProductWallet, $mainWalletProductIds); $metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById, $metalsWithProductWallet, $mainWalletProductIds);
$cashWallet = (float) ($mainWallet?->cash ?? 0); $cashWallet = $this->walletBalanceService->mainWalletCash($userWallets);
$assetsRialValue = $this->sumAssetsRialValue( $assetsRialValue = $this->sumAssetsRialValue(
$metalTotals, $metalTotals,
$userWallets, $userWallets,