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
+37 -7
View File
@@ -51,10 +51,28 @@ class AssetSummaryService
$metalsWithProductWallet = $this->metalIdsWithProductWallet($userWallets, $catalogById, $mainWalletProductIds, $mainWallet);
$inventoryCards = array_merge(
$this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds, $metalsWithProductWallet, $mainWalletProductIds, $userWallets),
$this->buildMeltedInventoryCards(
$catalog,
$catalogById,
$mainWallet,
$metalIds,
$metalsWithProductWallet,
$mainWalletProductIds,
$userWallets,
$customerId
),
$this->buildInventoryCards($userWallets, $catalogById, $mainWalletProductIds, $mainWallet),
);
$metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById, $metalsWithProductWallet, $mainWalletProductIds);
$metalTotals = $this->buildMetalTotals(
$userWallets,
$mainWallet,
$metalIds,
$weightUnit,
$catalogById,
$metalsWithProductWallet,
$mainWalletProductIds,
$customerId
);
$cashWallet = $this->walletBalanceService->mainWalletCash($userWallets);
$assetsRialValue = $this->sumAssetsRialValue(
$metalTotals,
@@ -199,11 +217,14 @@ class AssetSummaryService
array $metalIds,
array $metalsWithProductWallet = [],
array $mainWalletProductIds = [],
Collection $userWallets
Collection $userWallets,
int $customerId = 0
): array {
$cards = [];
$mainWalletGrams = Controller::weightToGrams(
$this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds)
$customerId > 0
? $this->walletBalanceService->effectiveMainWalletGold($customerId)
: $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds)
);
$assignedMetalIds = [];
$mainProductId = $this->configService->mainProductId();
@@ -215,7 +236,9 @@ class AssetSummaryService
return [];
}
$mainWalletWeight = $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds);
$mainWalletWeight = $customerId > 0
? $this->walletBalanceService->effectiveMainWalletGold($customerId)
: $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds);
if ($mainWalletWeight <= 0) {
return [];
}
@@ -298,6 +321,10 @@ class AssetSummaryService
/** موجودی نمایشی محصول — همسان با inventory_cards */
public function productDisplayQuantity(int $customerId, int $ownerType, int $productId): float
{
if ($this->configService->isMainWalletProduct($productId)) {
return $this->walletBalanceService->effectiveMainWalletGold($customerId);
}
$card = $this->productInventoryCard($customerId, $ownerType, $productId);
if ($card !== null) {
return (float) ($card['quantity'] ?? 0);
@@ -581,14 +608,17 @@ class AssetSummaryService
string $weightUnit,
Collection $catalogById,
array $metalsWithProductWallet = [],
array $mainWalletProductIds = []
array $mainWalletProductIds = [],
int $customerId = 0
): array {
if ($metalIds === []) {
return [];
}
$mainWalletGrams = Controller::weightToGrams(
$this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds)
$customerId > 0
? $this->walletBalanceService->effectiveMainWalletGold($customerId)
: $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds)
);
$totals = [];
+28
View File
@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use App\Models\Price;
use App\Models\Product;
use Illuminate\Support\Collection;
use Rooyesh\Wallet\models\Wallet;
/**
* محاسبهٔ موجودی کیف — منبع واحد برای ادمین و APIها.
@@ -42,6 +43,33 @@ class WalletBalanceService
return (float) ($this->lineValue($line, 'gold') ?? 0);
}
/**
* موجودی مؤثر کیف اصلی برای نمایش آبشده =
* gold کیف اصلی + gold کیف‌های legacy محصولات main_wallet_products.
* owner_type در جمع نادیده گرفته می‌شود تا خریدهای قدیمی از دست نروند.
*/
public function effectiveMainWalletGold(int $ownerId): float
{
$mainGold = (float) Wallet::query()
->where('owner', $ownerId)
->whereNull('fund_id')
->whereNull('product_id')
->sum('gold');
$productIds = $this->mainWalletProductIds();
if ($productIds === []) {
return round($mainGold, 3);
}
$orphanedGold = (float) Wallet::query()
->where('owner', $ownerId)
->whereNull('fund_id')
->whereIn('product_id', $productIds)
->sum('gold');
return round($mainGold + $orphanedGold, 3);
}
/**
* جمع طلای قابل‌نمایش: کیف اصلی + کیف محصولات + قلک‌ها.
* کیف‌های legacy محصولات main_wallet_products شمرده نمی‌شوند.