added gholak

This commit is contained in:
2026-08-01 17:30:14 +03:30
parent 9642e603e7
commit eaff9dead2
12 changed files with 538 additions and 75 deletions
@@ -204,14 +204,16 @@ class CustomerController extends SuperController
$catalogMetalIds = $balanceService->catalogMetalIds();
$mainWalletMetalId = $balanceService->resolveMainWalletMetalId();
$productCols = implode(',', self::WALLET_PRODUCT_COLUMNS);
$ownerType = (int) (Customer::find($customer_id)?->type ?? 0);
// فقط کیف محصول‌ها (نه کیف اصلی) — هر ردیف = موجودی واقعی یک محصول/فلز
// فقط کیف محصول آزاد (نه کیف اصلی، نه قلک) — هر ردیف = یک دارایی
$query_table = Wallet::with([
'product' => static function ($q) use ($productCols) {
$q->withTrashed()->select(explode(',', $productCols));
},
])
->where('owner', $customer_id)
->whereNull('fund_id')
->whereNotNull('product_id');
if ($mainWalletProductIds !== []) {
// کیف‌های قدیمیِ محصولات متصل به کیف اصلی نمایش داده نمی‌شوند
@@ -240,11 +242,15 @@ class CustomerController extends SuperController
->first();
if ($representative && $mainWallet) {
$freeGold = $balanceService->effectiveMainWalletGold((int) $customer_id, $ownerType);
$fundGold = $balanceService->fundGoldForMainWalletProducts((int) $customer_id, $ownerType);
$mainRow = [
'id' => $mainWallet->id,
'owner' => (int) $customer_id,
'cash' => 0,
'gold' => (float) $mainWallet->gold,
'gold' => $freeGold + $fundGold,
'free_gold' => $freeGold,
'fund_gold' => $fundGold,
'fund_id' => null,
'product_id' => (int) $representative->id,
'created_at' => $mainWallet->created_at,
@@ -260,7 +266,6 @@ 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'])
@@ -278,12 +283,149 @@ class CustomerController extends SuperController
$balanceService->enrichWalletTableRow($row, $catalogMetalIds, $mainWalletMetalId);
}
}
// جمع دارایی = آزاد + قلک‌های همان محصول (برای ردیف اصلی قبلاً ست شده)
$isMain = is_array($row)
? ! empty($row['is_main_wallet'])
: ! empty($row->is_main_wallet);
if ($productId > 0 && ! $isMain) {
$freeGold = $balanceService->freeProductGold((int) $customer_id, $productId, $ownerType);
$fundGold = $balanceService->fundGoldForProduct((int) $customer_id, $productId, $ownerType);
if (is_array($row)) {
$row['free_gold'] = $freeGold;
$row['fund_gold'] = $fundGold;
$row['gold'] = $freeGold + $fundGold;
} else {
$row->free_gold = $freeGold;
$row->fund_gold = $fundGold;
$row->gold = $freeGold + $fundGold;
}
}
}
unset($row);
}
return response()->json($query_table);
}
/** جدول قلک‌های واقعی مشتری (مدل Fund) */
public function piggyFunds(Request $request, $customer_id)
{
$query_table = \App\Models\Fund::with(['wallet', 'product'])
->where('customer_id', $customer_id);
TableEngine::create($query_table, $request)
->initSearch(['title'])
->select(['*'])
->export($query_table);
if (isset($query_table['rows'])) {
foreach ($query_table['rows'] as &$row) {
$wallets = is_array($row)
? collect($row['wallet'] ?? [])
: collect($row->wallet ?? []);
$gold = (float) $wallets->sum('gold');
if (is_array($row)) {
$row['gold'] = $gold;
$row['product_title'] = $row['product']['title'] ?? ($row['product']->title ?? null);
} else {
$row->gold = $gold;
$row->product_title = $row->product?->title;
}
}
unset($row);
}
return response()->json($query_table);
}
/** قلک‌های یک دارایی (محصول) */
public function assetPiggyFunds(Request $request, $customer_id, $product_id)
{
$request->merge(['product_id' => (int) $product_id]);
$query_table = \App\Models\Fund::with(['wallet', 'product'])
->where('customer_id', $customer_id)
->where('product_id', (int) $product_id);
TableEngine::create($query_table, $request)
->initSearch(['title'])
->select(['*'])
->export($query_table);
if (isset($query_table['rows'])) {
foreach ($query_table['rows'] as &$row) {
$wallets = is_array($row)
? collect($row['wallet'] ?? [])
: collect($row->wallet ?? []);
$gold = (float) $wallets->sum('gold');
if (is_array($row)) {
$row['gold'] = $gold;
} else {
$row->gold = $gold;
}
}
unset($row);
}
return response()->json($query_table);
}
/** تراکنش‌های مربوط به یک دارایی (کیف آزاد + قلک‌های همان محصول) */
public function assetTransactions(Request $request, $customer_id, $product_id)
{
$config = app(ConfigService::class);
$productId = (int) $product_id;
$walletIds = Wallet::query()
->where('owner', $customer_id)
->where(function ($q) use ($config, $productId) {
if ($config->isMainWalletProduct($productId)) {
$q->where(function ($inner) {
$inner->whereNull('fund_id')->whereNull('product_id');
})->orWhere(function ($inner) use ($productId) {
$inner->whereNotNull('fund_id')
->where(function ($f) use ($productId) {
$f->where('product_id', $productId)->orWhereNull('product_id');
});
});
} else {
$q->where('product_id', $productId);
}
})
->pluck('id');
$records = \Rooyesh\Wallet\models\Transaction::whereIn('wallet', $walletIds);
TableEngine::create($records, $request->all())
->initSearch([])
->select([
'*',
\Rooyesh\Wallet\models\Transaction::$SELECT_CAJ,
\Rooyesh\Wallet\models\Transaction::$SELECT_TYPE_STR,
])->export($records);
return response()->json($records);
}
/** تراکنش‌های یک قلک */
public function piggyFundTransactions(Request $request, $customer_id, $fund_id)
{
$fund = \App\Models\Fund::where('customer_id', $customer_id)->findOrFail($fund_id);
$walletIds = Wallet::query()
->where('owner', $customer_id)
->where('fund_id', $fund->id)
->pluck('id');
$records = \Rooyesh\Wallet\models\Transaction::whereIn('wallet', $walletIds);
TableEngine::create($records, $request->all())
->initSearch([])
->select([
'*',
\Rooyesh\Wallet\models\Transaction::$SELECT_CAJ,
\Rooyesh\Wallet\models\Transaction::$SELECT_TYPE_STR,
])->export($records);
return response()->json($records);
}
public function archive($id){
$query = Customer::findOrFail($id);
$query->update([