wallet handling
This commit is contained in:
@@ -16,6 +16,8 @@ use Rooyesh\Wallet\interfaces\WalletInterface;
|
|||||||
use Rooyesh\Wallet\models\Transaction;
|
use Rooyesh\Wallet\models\Transaction;
|
||||||
use Rooyesh\Wallet\models\Wallet;
|
use Rooyesh\Wallet\models\Wallet;
|
||||||
use App\Models\Setting;
|
use App\Models\Setting;
|
||||||
|
use App\Services\ConfigService;
|
||||||
|
use App\Services\WalletBalanceService;
|
||||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||||
|
|
||||||
class CustomerController extends SuperController
|
class CustomerController extends SuperController
|
||||||
@@ -136,6 +138,19 @@ class CustomerController extends SuperController
|
|||||||
->initSearch($this->search)
|
->initSearch($this->search)
|
||||||
->select(['*',Customer::$SELECT_TYPE_STR])
|
->select(['*',Customer::$SELECT_TYPE_STR])
|
||||||
->export($query_table);
|
->export($query_table);
|
||||||
|
|
||||||
|
$balanceService = app(WalletBalanceService::class);
|
||||||
|
$mainWalletProductIds = app(ConfigService::class)->mainWalletProductIds();
|
||||||
|
if (isset($query_table['rows'])) {
|
||||||
|
$query_table['rows'] = collect($query_table['rows'])->map(function ($row) use ($balanceService, $mainWalletProductIds) {
|
||||||
|
$wallets = collect($row['wallet'] ?? []);
|
||||||
|
$row['wallet_cash'] = $balanceService->mainWalletCash($wallets);
|
||||||
|
$row['wallet_gold'] = $balanceService->totalGoldWeight($wallets, $mainWalletProductIds);
|
||||||
|
|
||||||
|
return $row;
|
||||||
|
})->values();
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json($query_table);
|
return response()->json($query_table);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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