wallet handling
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* محاسبهٔ موجودی کیف — منبع واحد برای ادمین و APIها.
|
||||
*
|
||||
* کیف اصلی: fund_id=null و product_id=null
|
||||
* main_wallet_products: طلای این محصولات روی کیف اصلی است؛ کیف legacy محصول نادیده گرفته میشود.
|
||||
*/
|
||||
class WalletBalanceService
|
||||
{
|
||||
public function __construct(private ConfigService $configService)
|
||||
{
|
||||
}
|
||||
|
||||
public function mainWalletProductIds(): array
|
||||
{
|
||||
return $this->configService->mainWalletProductIds();
|
||||
}
|
||||
|
||||
/** @param Collection|array<int, mixed>|null $wallets */
|
||||
public function mainWalletCash(Collection|array|null $wallets): float
|
||||
{
|
||||
$line = $this->mainWalletLine($wallets);
|
||||
|
||||
return (float) ($this->lineValue($line, 'cash') ?? 0);
|
||||
}
|
||||
|
||||
/** @param Collection|array<int, mixed>|null $wallets */
|
||||
public function mainWalletGold(Collection|array|null $wallets): float
|
||||
{
|
||||
$line = $this->mainWalletLine($wallets);
|
||||
|
||||
return (float) ($this->lineValue($line, 'gold') ?? 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* جمع طلای قابلنمایش: کیف اصلی + کیف محصولات + قلکها.
|
||||
* کیفهای legacy محصولات main_wallet_products شمرده نمیشوند.
|
||||
*
|
||||
* @param Collection|array<int, mixed>|null $wallets
|
||||
*/
|
||||
public function totalGoldWeight(
|
||||
Collection|array|null $wallets,
|
||||
?array $mainWalletProductIds = null
|
||||
): float {
|
||||
$mainWalletProductIds ??= $this->mainWalletProductIds();
|
||||
$sum = 0.0;
|
||||
|
||||
foreach ($this->walletLines($wallets) as $line) {
|
||||
$fundId = $this->lineValue($line, 'fund_id');
|
||||
$productId = (int) ($this->lineValue($line, 'product_id') ?? 0);
|
||||
$gold = (float) ($this->lineValue($line, 'gold') ?? 0);
|
||||
|
||||
if ($gold <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($fundId !== null) {
|
||||
$sum += $gold;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($productId === 0) {
|
||||
$sum += $gold;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($mainWalletProductIds !== [] && in_array($productId, $mainWalletProductIds, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sum += $gold;
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/** @param Collection|array<int, mixed>|null $wallets */
|
||||
private function mainWalletLine(Collection|array|null $wallets): mixed
|
||||
{
|
||||
foreach ($this->walletLines($wallets) as $line) {
|
||||
if ($this->lineValue($line, 'fund_id') === null && $this->lineValue($line, 'product_id') === null) {
|
||||
return $line;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @param Collection|array<int, mixed>|null $wallets */
|
||||
private function walletLines(Collection|array|null $wallets): array
|
||||
{
|
||||
if ($wallets === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($wallets instanceof Collection) {
|
||||
return $wallets->all();
|
||||
}
|
||||
|
||||
return is_array($wallets) ? $wallets : [];
|
||||
}
|
||||
|
||||
private function lineValue(mixed $line, string $key): mixed
|
||||
{
|
||||
if (is_array($line)) {
|
||||
return $line[$key] ?? null;
|
||||
}
|
||||
|
||||
if (is_object($line)) {
|
||||
return $line->{$key} ?? null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user