added gholak
This commit is contained in:
@@ -77,6 +77,95 @@ class WalletBalanceService
|
||||
return round($mainGold + $orphanedGold, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* طلای داخل قلکهای یک محصول (یا همهٔ محصولات main_wallet اگر $productId=null و فقط main).
|
||||
*/
|
||||
public function fundGoldForProduct(int $ownerId, ?int $productId, ?int $ownerType = null): float
|
||||
{
|
||||
$query = Wallet::query()
|
||||
->where('owner', $ownerId)
|
||||
->whereNotNull('fund_id');
|
||||
if ($ownerType !== null) {
|
||||
$query->where('owner_type', $ownerType);
|
||||
}
|
||||
if ($productId === null) {
|
||||
$query->whereNull('product_id');
|
||||
} else {
|
||||
$query->where('product_id', $productId);
|
||||
}
|
||||
|
||||
return round((float) $query->sum('gold'), 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* جمع طلای قلکهای محصولات متصل به کیف اصلی (+ قلکهای legacy بدون product_id).
|
||||
*/
|
||||
public function fundGoldForMainWalletProducts(int $ownerId, ?int $ownerType = null): float
|
||||
{
|
||||
$productIds = $this->mainWalletProductIds();
|
||||
$query = Wallet::query()
|
||||
->where('owner', $ownerId)
|
||||
->whereNotNull('fund_id');
|
||||
if ($ownerType !== null) {
|
||||
$query->where('owner_type', $ownerType);
|
||||
}
|
||||
if ($productIds === []) {
|
||||
$query->whereNull('product_id');
|
||||
} else {
|
||||
$query->where(function ($q) use ($productIds) {
|
||||
$q->whereIn('product_id', $productIds)->orWhereNull('product_id');
|
||||
});
|
||||
}
|
||||
|
||||
return round((float) $query->sum('gold'), 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* موجودی قابلمقایسه با حسابداری برای طلای کیف اصلی =
|
||||
* آزاد (effectiveMain) + قلکهای همان محصولات.
|
||||
* برای معامله استفاده نشود — فقط sync/مانده.
|
||||
*/
|
||||
public function accountingVisibleMainWalletGold(int $ownerId, ?int $ownerType = null): float
|
||||
{
|
||||
return round(
|
||||
$this->effectiveMainWalletGold($ownerId, $ownerType)
|
||||
+ $this->fundGoldForMainWalletProducts($ownerId, $ownerType),
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* موجودی آزاد یک محصول (بدون قلک) — برای معامله و واریز به قلک.
|
||||
*/
|
||||
public function freeProductGold(int $ownerId, int $productId, ?int $ownerType = null): float
|
||||
{
|
||||
if ($this->configService->isMainWalletProduct($productId)) {
|
||||
return $this->effectiveMainWalletGold($ownerId, $ownerType);
|
||||
}
|
||||
|
||||
$query = Wallet::query()
|
||||
->where('owner', $ownerId)
|
||||
->whereNull('fund_id')
|
||||
->where('product_id', $productId);
|
||||
if ($ownerType !== null) {
|
||||
$query->where('owner_type', $ownerType);
|
||||
}
|
||||
|
||||
return round((float) $query->sum('gold'), 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* جمع نمایشی دارایی یک محصول = آزاد + قلکهای همان محصول.
|
||||
*/
|
||||
public function totalProductGold(int $ownerId, int $productId, ?int $ownerType = null): float
|
||||
{
|
||||
return round(
|
||||
$this->freeProductGold($ownerId, $productId, $ownerType)
|
||||
+ $this->fundGoldForProduct($ownerId, $productId, $ownerType),
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* جمع طلای قابلنمایش: کیف اصلی + کیف محصولات + قلکها.
|
||||
* کیفهای legacy محصولات main_wallet_products شمرده نمیشوند.
|
||||
|
||||
Reference in New Issue
Block a user