fix some bug

This commit is contained in:
2026-07-01 20:59:24 +03:30
parent e3a883d224
commit a69316e0de
10 changed files with 226 additions and 14 deletions
+70
View File
@@ -242,12 +242,16 @@ class WalletBalanceService
public function enrichWalletTableRow(mixed &$row, array $catalogMetalIds, int $mainWalletMetalId): void
{
$metalMap = $this->walletLineMetalGoldMap($row, $catalogMetalIds, $mainWalletMetalId);
$meta = $this->buildWalletTableRowMeta($row, $mainWalletMetalId);
if (is_array($row)) {
$row['metal_gold'] = $metalMap;
foreach ($catalogMetalIds as $metalId) {
$row['metal_gold_' . $metalId] = $metalMap[(string) $metalId] ?? 0.0;
}
foreach ($meta as $key => $value) {
$row[$key] = $value;
}
return;
}
@@ -257,9 +261,60 @@ class WalletBalanceService
foreach ($catalogMetalIds as $metalId) {
$row->{'metal_gold_' . $metalId} = $metalMap[(string) $metalId] ?? 0.0;
}
foreach ($meta as $key => $value) {
$row->{$key} = $value;
}
}
}
/**
* @return array<string, mixed>
*/
private function buildWalletTableRowMeta(mixed $row, int $mainWalletMetalId): array
{
$fundId = $this->lineValue($row, 'fund_id');
$productId = $this->lineValue($row, 'product_id');
$product = $this->lineProduct($row);
$fund = $this->lineFund($row);
$cash = (float) ($this->lineValue($row, 'cash') ?? 0);
if ($fundId !== null) {
$kind = 'fund';
$kindLabel = 'قلک (صندوق)';
$fundTitle = trim((string) ($fund?->title ?? ''));
$subject = $fundTitle !== '' ? "قلک «{$fundTitle}»" : 'قلک بدون عنوان';
} elseif ($productId === null) {
$kind = 'main';
$kindLabel = 'کیف اصلی';
$subject = 'کیف اصلی — ریال و طلای آبشده';
} else {
$kind = 'product';
$kindLabel = 'کیف محصول';
$title = trim((string) ($product?->title ?? ''));
$subject = $title !== '' ? "محصول «{$title}»" : "محصول کد {$productId}";
}
$metalId = $this->resolveWalletLineMetalId($row, $mainWalletMetalId);
$goldGrams = $this->walletLineGoldGrams($row);
$goldToCash = $goldGrams > 0 ? (int) round($goldGrams * $this->livePricePerGram($metalId)) : 0;
return [
'wallet_kind' => $kind,
'wallet_kind_label' => $kindLabel,
'wallet_subject' => $subject,
'product_title' => $product?->title ?? null,
'product_group_type' => $product?->group_type ?? null,
'product_unit' => $product?->unit ?? null,
'product_amount' => $product?->amount ?? null,
'fund_title' => $fund?->title ?? null,
'wallet_metal_id' => $metalId,
'wallet_metal_label' => $this->balanceReportService->metalLabel($metalId),
'gold_to_cash' => $goldToCash,
'gold_grams' => round($goldGrams, 3),
'total_rial_value' => (int) round($cash + $goldToCash),
];
}
private function livePricePerGram(int $metalId): float
{
if ($metalId === 1) {
@@ -303,6 +358,21 @@ class WalletBalanceService
return null;
}
private function lineFund(mixed $line): ?object
{
if (is_object($line) && isset($line->fund)) {
return $line->fund;
}
if (is_array($line) && isset($line['fund'])) {
$fund = $line['fund'];
return is_object($fund) ? $fund : (object) $fund;
}
return null;
}
private function walletLineGoldGrams(mixed $line): float
{
$gold = (float) ($this->lineValue($line, 'gold') ?? 0);