fix some bugs 5

This commit is contained in:
2026-07-21 22:23:09 +03:30
parent 55ce5e83b0
commit 3f8c47b1fc
6 changed files with 215 additions and 36 deletions
@@ -129,9 +129,17 @@ class CustomerController extends SuperController
public function funds(Request $request,$customer_id)
{
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
$balanceService = app(WalletBalanceService::class);
$catalogMetalIds = $balanceService->catalogMetalIds();
$mainWalletMetalId = $balanceService->resolveMainWalletMetalId();
$productCols = implode(',', self::WALLET_PRODUCT_COLUMNS);
// فقط کیف محصول‌ها (نه کیف اصلی) — هر ردیف = موجودی واقعی یک محصول/فلز
$query_table = Wallet::with(['product:id,title,metal_id,unit'])
$query_table = Wallet::with([
'product' => static function ($q) use ($productCols) {
$q->withTrashed()->select(explode(',', $productCols));
},
])
->where('owner', $customer_id)
->whereNotNull('product_id');
if ($mainWalletProductIds !== []) {
@@ -147,7 +155,9 @@ class CustomerController extends SuperController
if ($mainWalletProductIds !== []) {
$representative = null;
foreach ($mainWalletProductIds as $pid) {
$representative = \App\Models\Product::select(['id', 'title', 'metal_id', 'unit'])->find($pid);
$representative = \App\Models\Product::withTrashed()
->select(self::WALLET_PRODUCT_COLUMNS)
->find($pid);
if ($representative) {
break;
}
@@ -176,6 +186,31 @@ class CustomerController extends SuperController
}
}
if (isset($query_table['rows'])) {
foreach ($query_table['rows'] as &$row) {
$balanceService->enrichWalletTableRow($row, $catalogMetalIds, $mainWalletMetalId);
// اگر eager-load محصول نیامده بود، از lookup پر کن
$productId = (int) (is_array($row) ? ($row['product_id'] ?? 0) : ($row->product_id ?? 0));
$hasProduct = is_array($row)
? ! empty($row['product'])
: ! empty($row->product);
if ($productId > 0 && ! $hasProduct) {
$product = \App\Models\Product::withTrashed()
->select(self::WALLET_PRODUCT_COLUMNS)
->find($productId);
if ($product) {
if (is_array($row)) {
$row['product'] = $product;
} else {
$row->setRelation('product', $product);
}
$balanceService->enrichWalletTableRow($row, $catalogMetalIds, $mainWalletMetalId);
}
}
}
unset($row);
}
return response()->json($query_table);
}
public function archive($id){
+12 -4
View File
@@ -17,8 +17,12 @@ class OrderController extends Controller
$records = Order::with([
'customer',
'product:id,title,unit,metal_id,group_type,amount,trade_by_quantity,direct_price',
'transactions.product:id,title,unit,metal_id,group_type,amount,trade_by_quantity,direct_price',
'product' => static function ($q) {
$q->withTrashed()->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price']);
},
'transactions.product' => static function ($q) {
$q->withTrashed()->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price']);
},
]);
TableEngine::create($records, $request->all())
->initSearch(['customer.name'])
@@ -44,8 +48,12 @@ class OrderController extends Controller
$records = Order::with([
'customer',
'product:id,title,unit,metal_id,group_type,amount,trade_by_quantity,direct_price',
'transactions.product:id,title,unit,metal_id,group_type,amount,trade_by_quantity,direct_price',
'product' => static function ($q) {
$q->withTrashed()->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price']);
},
'transactions.product' => static function ($q) {
$q->withTrashed()->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price']);
},
])->where('type',2);
TableEngine::create($records, $request->all())
->initSearch(['customer.name'])
@@ -476,8 +476,12 @@ class WalletController extends Controller
$records = Transaction::with([
'customer',
'wallet.product:id,title,metal_id',
'product:id,title,metal_id,group_type,unit,amount,trade_by_quantity,direct_price',
'wallet.product' => static function ($q) {
$q->withTrashed()->select(['id', 'title', 'metal_id', 'group_type', 'unit', 'amount', 'trade_by_quantity', 'direct_price']);
},
'product' => static function ($q) {
$q->withTrashed()->select(['id', 'title', 'metal_id', 'group_type', 'unit', 'amount', 'trade_by_quantity', 'direct_price']);
},
])->whereNull('fund_id');
TableEngine::create($records, $request->all())
->initSearch(['order_id', 'customer.name'])