added merge wallet 2

This commit is contained in:
2026-06-10 17:31:38 +03:30
parent 285767fcd3
commit 95ee496df0
6 changed files with 180 additions and 22 deletions
@@ -29,17 +29,48 @@ class ProductController extends SuperController
public function pTable(Request $request)
{
$user = auth('customer')->user();
// $products = Product::with(['wallet' => function ($query) use ($user) {
// $query->where('owner', $user->id);
// }])->get();
$products = Product::whereHas('wallet', function ($query) use ($user) {
$query->where('owner', $user->id);
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
$products = Product::where(function ($query) use ($user, $mainWalletProductIds) {
$query->whereHas('wallet', function ($q) use ($user) {
$q->where('owner', $user->id);
});
if ($mainWalletProductIds !== []) {
$query->orWhereIn('id', $mainWalletProductIds);
}
})->with(['wallet' => function ($query) use ($user) {
$query->where('owner', $user->id);
}, 'file'])->get();
// محصولات متصل به کیف اصلی (main_wallet_products): فقط نمایندهٔ لیست با موجودی کیف اصلی
$representativeId = null;
$mainWalletGold = 0.0;
if ($mainWalletProductIds !== []) {
foreach ($mainWalletProductIds as $pid) {
if ($products->firstWhere('id', $pid)) {
$representativeId = (int) $pid;
break;
}
}
if ($representativeId !== null) {
$mainWallet = Wallet::where('owner', $user->id)
->whereNull('fund_id')
->whereNull('product_id')
->first();
$mainWalletGold = (float) ($mainWallet->gold ?? 0);
}
$products = $products->filter(function ($product) use ($mainWalletProductIds, $representativeId) {
if (in_array((int) $product->id, array_map('intval', $mainWalletProductIds), true)) {
return (int) $product->id === $representativeId;
}
return true;
})->values();
}
// Prepare the response
$user['rows'] = $products->map(function ($product) {
$user['rows'] = $products->map(function ($product) use ($user, $representativeId, $mainWalletGold) {
$product->unit = self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
@@ -50,6 +81,14 @@ class ProductController extends SuperController
$product->file->setAttribute('full_src', $product->file->src);
}
if ($representativeId !== null && (int) $product->id === $representativeId) {
// کیف محصول فقط نقش هندل را دارد؛ موجودی واقعی از کیف اصلی است
$walletInterface = new WalletInterface($user->id, $user->type, null, $product->id);
$handleWallet = $walletInterface->wallet;
$handleWallet->gold = $mainWalletGold;
$product->setRelation('wallet', $handleWallet);
}
return $product;
});
@@ -103,7 +142,22 @@ class ProductController extends SuperController
}
public function show($id){
$query = Product::/*with(['walletMy as wallet'])->*/findOrFail($id);
$query['wallet']= $query->walletMy;
$wallet = $query->walletMy;
// محصول متصل به کیف اصلی: موجودی از کیف اصلی خوانده می‌شود
if (self::isMainWalletProduct((int) $id)) {
$mainWallet = Wallet::where('owner', auth('customer')->id())
->whereNull('fund_id')
->whereNull('product_id')
->first();
if ($wallet) {
$wallet->gold = (float) ($mainWallet->gold ?? 0);
} else {
$wallet = $mainWallet;
}
}
$query['wallet'] = $wallet;
return response()->json($query);
}