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
@@ -51,10 +51,13 @@ class CustomerController extends Controller
->whereNull('fund_id')
->first() ?? new Wallet();
// کیف‌های قدیمیِ محصولات متصل به کیف اصلی (main_wallet_products) در محاسبات نمی‌آیند؛ موجودی‌شان همان gold کیف اصلی است
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
$treasureGrams = 0;
$wallet->wp = Wallet::with('product')->where('owner', $user->id)->get();
foreach ($wallet->wp as $row) {
if ($row->product_id != null) {
if ($row->product_id != null && !in_array((int) $row->product_id, $mainWalletProductIds, true)) {
$treasureGrams += self::productLineWeightToGrams($row->product, $row->gold);
}
}
@@ -80,6 +83,9 @@ class CustomerController extends Controller
$count_product = 0;
foreach ($user->wallets as $walletSingle) {
if ($walletSingle->product_id) {
if (in_array((int) $walletSingle->product_id, $mainWalletProductIds, true)) {
continue;
}
if ($walletSingle->product->metal_id == 1) {
if (!($walletSingle->product->group_type == "سکه")) {
@@ -128,6 +134,10 @@ class CustomerController extends Controller
$user->pwallet = Wallet::with("product")->where('owner', $user->id)
->where('owner_type', $user->type) // Ensures the wallet's owner type matches the user's type
->whereNotNull('product_id') // Filters wallets that have a non-null product_id
->when($mainWalletProductIds !== [], function ($q) use ($mainWalletProductIds) {
// کیف قدیمی محصولات متصل به کیف اصلی نمایش داده نمی‌شود
$q->whereNotIn('product_id', $mainWalletProductIds);
})
->get() ?? new Wallet(); // If no wallet is found, assigns a new Wallet instance to the attribute
$forbiddenWords = ['tahesab', 'token']; // کلمات ممنوعه
@@ -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);
}
@@ -275,7 +275,19 @@ class WalletController extends Controller
if (!$data['wallets'][$key]['wallet']['product']) {
return response()->json(['message' => 'محصولی در ولت مورد نظر یافت نشد.'], 422);
}
if ($data['wallets'][$key]['value'] > $data['wallets'][$key]['wallet']['gold']){
// محصول متصل به کیف اصلی (main_wallet_products): موجودی و تراکنش روی کیف اصلی است
$effectiveProductId = self::effectiveProductWalletId((int) $data['wallets'][$key]['wallet']['product_id']);
$availableGold = $data['wallets'][$key]['wallet']['gold'];
if ($effectiveProductId === null) {
$mainWallet = Wallet::where('owner', auth('customer')->id())
->whereNull('fund_id')
->whereNull('product_id')
->first();
$availableGold = (float) ($mainWallet->gold ?? 0);
}
if ($data['wallets'][$key]['value'] > $availableGold){
return response()->json([
'message' => "شما به اندازه کافی از {$data['wallets'][$key]['wallet']['product']['title']} ندارید"
], 422);
@@ -288,7 +300,7 @@ class WalletController extends Controller
}
$data['wallets'][$key]['wallet_interface'] = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type, null, $data['wallets'][$key]['wallet']["product_id"]);
$data['wallets'][$key]['wallet_interface'] = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type, null, $effectiveProductId);
}
$data['withdraw'] = new Withdraw(); // Create a new instance of the Withdraw model
$data['withdraw']->note = "برداشت فیزیکی"; // 50 for phisicals