fix some bugs

This commit is contained in:
2026-07-18 15:04:08 +03:30
parent 4c7e4fdfde
commit eedde65be6
4 changed files with 94 additions and 29 deletions
@@ -9,6 +9,7 @@ use App\Models\Customer;
use App\Models\Order;
use App\Models\Price;
use App\Models\Setting;
use App\Services\WalletBalanceService;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
@@ -37,10 +38,7 @@ class OrderController extends Controller
$lastPrice = (float) (Price::latest('created_at')->value('price') ?? 0);
$mainWallet = $customer->wallet->first(
fn ($wallet) => is_null($wallet->product_id) && is_null($wallet->fund_id)
) ?? $customer->wallet->first();
$goldAmount = (float) ($mainWallet->gold ?? 0);
$goldAmount = app(WalletBalanceService::class)->effectiveMainWalletGold((int) $customerId);
$orders = Order::query()
->where('customer_id', $customerId)
@@ -8,6 +8,7 @@ use App\Http\Controllers\SuperController;
use App\Models\Fund;
use App\Models\Product;
use App\Services\AssetSummaryService;
use App\Services\WalletBalanceService;
use Illuminate\Http\Request;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
@@ -27,7 +28,7 @@ class ProductController extends SuperController
$query = Product::with($this->with);
parent::__construct($model, $this->with, $select, null,$query,$search);
}
public function pTable(Request $request, AssetSummaryService $assetSummaryService)
public function pTable(Request $request, AssetSummaryService $assetSummaryService, WalletBalanceService $balanceService)
{
$user = auth('customer')->user();
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
@@ -58,11 +59,7 @@ class ProductController extends SuperController
}
if ($representativeId !== null) {
$mainWallet = Wallet::where('owner', $user->id)
->whereNull('fund_id')
->whereNull('product_id')
->first();
$mainWalletGold = (float) ($mainWallet->gold ?? 0);
$mainWalletGold = $balanceService->effectiveMainWalletGold((int) $user->id);
}
$products = $products->filter(function ($product) use ($mainWalletProductIds, $representativeId) {
@@ -74,7 +71,7 @@ class ProductController extends SuperController
}
// Prepare the response
$user['rows'] = $products->map(function ($product) use ($user, $representativeId, $mainWalletGold, $inventoryByProduct) {
$user['rows'] = $products->map(function ($product) use ($user, $representativeId, $mainWalletGold, $inventoryByProduct, $assetSummaryService) {
$product->unit = self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
@@ -88,13 +85,13 @@ class ProductController extends SuperController
$card = $inventoryByProduct->get((int) $product->id);
$displayQty = $card !== null
? (float) ($card['quantity'] ?? 0)
: null;
: $assetSummaryService->productDisplayQuantity((int) $user->id, (int) $user->type, (int) $product->id);
if ($representativeId !== null && (int) $product->id === $representativeId) {
// کیف محصول فقط نقش هندل را دارد؛ موجودی واقعی از کیف اصلی است
// کیف محصول فقط نقش هندل را دارد؛ موجودی واقعی از کیف اصلی (+ legacy) است
$walletInterface = new WalletInterface($user->id, $user->type, null, $product->id);
$handleWallet = $walletInterface->wallet;
$handleWallet->gold = $displayQty ?? $mainWalletGold;
$handleWallet->gold = $displayQty > 0 ? $displayQty : $mainWalletGold;
$product->setRelation('wallet', $handleWallet);
} elseif ($displayQty !== null) {
if ($product->wallet) {
@@ -158,21 +155,33 @@ class ProductController extends SuperController
], 5, true);
return response()->json(true);
}
public function show($id){
public function show($id, AssetSummaryService $assetSummaryService){
$customer = auth('customer')->user();
$query = Product::/*with(['walletMy as wallet'])->*/findOrFail($id);
$wallet = $query->walletMy;
// محصول متصل به کیف اصلی: موجودی از کیف اصلی خوانده می‌شود
// محصول متصل به کیف اصلی: موجودی مؤثر (کیف اصلی + legacy) — نه فقط ردیف product handle
if (self::isMainWalletProduct((int) $id)) {
$mainWallet = Wallet::where('owner', auth('customer')->id())
->whereNull('fund_id')
->whereNull('product_id')
->first();
$mainWalletGold = $assetSummaryService->productDisplayQuantity(
(int) $customer->id,
(int) $customer->type,
(int) $id
);
if ($wallet) {
$wallet->gold = (float) ($mainWallet->gold ?? 0);
$wallet->gold = $mainWalletGold;
} else {
$wallet = $mainWallet;
$wallet = Wallet::where('owner', $customer->id)
->whereNull('fund_id')
->whereNull('product_id')
->first() ?? new Wallet(['gold' => $mainWalletGold]);
$wallet->gold = $mainWalletGold;
}
} elseif ($wallet) {
$wallet->gold = $assetSummaryService->productDisplayQuantity(
(int) $customer->id,
(int) $customer->type,
(int) $id
);
}
$query['wallet'] = $wallet;