fixed kilo in metals

This commit is contained in:
2026-06-23 21:04:29 +03:30
parent 147a968179
commit 0bc690591f
4 changed files with 301 additions and 9 deletions
@@ -16,6 +16,7 @@ use Rooyesh\Wallet\interfaces\WalletInterface;
use Rooyesh\Wallet\models\Transaction;
use Rooyesh\Wallet\models\Wallet;
use App\Models\Setting;
use App\Services\BalanceReportService;
use App\Services\ConfigService;
use App\Services\WalletBalanceService;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
@@ -133,19 +134,27 @@ class CustomerController extends SuperController
return response()->json(true);
}
public function tableWallets(Request $request){
$query_table = Customer::with(['wallet','referral'])->where("is_archive",false)/*->where('type',1)*/;
$query_table = Customer::with(['wallet.product:id,metal_id', 'referral'])->where("is_archive",false)/*->where('type',1)*/;
TableEngine::create($query_table, $request)
->initSearch($this->search)
->select(['*',Customer::$SELECT_TYPE_STR])
->export($query_table);
$balanceService = app(WalletBalanceService::class);
$mainWalletProductIds = app(ConfigService::class)->mainWalletProductIds();
$balanceReport = app(BalanceReportService::class);
$catalogMetalIds = $balanceService->catalogMetalIds();
$query_table['catalogMetals'] = $balanceReport->catalogMetalsMeta();
if (isset($query_table['rows'])) {
$query_table['rows'] = collect($query_table['rows'])->map(function ($row) use ($balanceService, $mainWalletProductIds) {
$query_table['rows'] = collect($query_table['rows'])->map(function ($row) use ($balanceService, $catalogMetalIds) {
$wallets = collect($row['wallet'] ?? []);
$row['wallet_cash'] = $balanceService->mainWalletCash($wallets);
$row['wallet_gold'] = $balanceService->totalGoldWeight($wallets, $mainWalletProductIds);
$metalMap = $balanceService->buildCustomerWalletMetalDisplayMap($wallets, $catalogMetalIds);
$row['wallet_metal_gold'] = $metalMap;
foreach ($catalogMetalIds as $metalId) {
$row['wallet_metal_gold_' . $metalId] = $metalMap[(string) $metalId] ?? 0.0;
}
$row['wallet_gold'] = array_sum($metalMap);
return $row;
})->values();
@@ -20,6 +20,7 @@ use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use App\Http\Controllers\Admin\TahesabController;
use App\Models\Setting;
use App\Models\User;
use App\Services\WalletBalanceService;
use Hekmatinasser\Verta\Verta;
@@ -301,28 +302,42 @@ class WalletController extends Controller
public function indexWallet(Request $request)
{
$lastPrice = (float) (Price::latest()->first()?->price ?? 0);
$balanceService = app(WalletBalanceService::class);
$catalogMetalIds = $balanceService->catalogMetalIds();
$mainWalletMetalId = $balanceService->resolveMainWalletMetalId();
// همان فیلتر/جستجوی جدول — بدون صفحه‌بندی — برای مجموع ردیف‌های قابل مشاهده
$sumQuery = Wallet::query();
TableEngine::create($sumQuery, $request->all())
->initSearch(['customer.name']);
$totalGold = (float) (clone $sumQuery)->sum('gold');
$sumRows = (clone $sumQuery)->with('product:id,metal_id')->get();
$metalBalances = $balanceService->buildWalletMetalSums($sumRows, $catalogMetalIds);
$totalGold = array_sum(array_column($metalBalances, 'gold'));
$totalGoldToCash = array_sum(array_column($metalBalances, 'goldToCash'));
$totalCash = (float) (clone $sumQuery)->sum('cash');
$sums = [
'gold' => round($totalGold, 2),
'goldToCash' => round($totalGold * $lastPrice, 2),
'goldToCash' => round($totalGoldToCash, 2),
'cash' => round($totalCash, 2),
'cashToGold' => $lastPrice > 0 ? round($totalCash / $lastPrice, 2) : 0,
'metalBalances' => $metalBalances,
];
$records = Wallet::with('customer');
$records = Wallet::with(['customer', 'product:id,metal_id']);
TableEngine::create($records, $request->all())
->initSearch(['customer.name'])
->select(['*'])
->export($records);
if (isset($records['rows'])) {
foreach ($records['rows'] as &$row) {
$balanceService->enrichWalletTableRow($row, $catalogMetalIds, $mainWalletMetalId);
}
unset($row);
}
$records['sums'] = $sums;
return response()->json($records);