From 6c15ff18303cf37a2a0a651571f3f2cfaf4f2701 Mon Sep 17 00:00:00 2001 From: adonis Date: Tue, 23 Jun 2026 21:21:04 +0330 Subject: [PATCH] fixed kilo in metals 2 --- .../Controllers/Admin/WalletController.php | 42 +++---- app/Services/BalanceReportService.php | 119 ++++++++++++++++++ 2 files changed, 137 insertions(+), 24 deletions(-) diff --git a/app/Http/Controllers/Admin/WalletController.php b/app/Http/Controllers/Admin/WalletController.php index 1f2049b..3484e4b 100644 --- a/app/Http/Controllers/Admin/WalletController.php +++ b/app/Http/Controllers/Admin/WalletController.php @@ -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\BalanceReportService; use App\Services\WalletBalanceService; use Hekmatinasser\Verta\Verta; @@ -219,35 +220,28 @@ class WalletController extends Controller public function index(Request $request) { - $records = Transaction::with(['customer'])->whereNull('fund_id'); + $balanceReport = app(BalanceReportService::class); + $balanceService = app(WalletBalanceService::class); + $catalogMetalIds = $balanceService->catalogMetalIds(); + + $records = Transaction::with([ + 'customer', + 'product:id,title,metal_id,group_type,unit,amount,trade_by_quantity,direct_price', + ])->whereNull('fund_id'); TableEngine::create($records, $request->all()) ->initSearch(['order_id', 'customer.name']) ->select([ '*', Transaction::$SELECT_CAJ, Transaction::$SELECT_TYPE_STR, Transaction::$SELECT_SAJ, Transaction::$SELECT_EAJ ])->export($records); -// foreach ($records['rows'] as &$record) { -// if (empty($record->title) or $record->title == "تعریف نشده"){ -// $record->title = $record->type_str ; -// $record->unit = "گرم"; -// if (isset($record->wallet)) { -// $wallet = Wallet::with('product')->where('id',$record->wallet)->first(); -// if (isset($wallet->product)) { -// if ($record->gold > 0){ -// $record->title = " خرید " ; -// -// }else{ -// $record->title = " فروش " ; -// -// } -// $record->title .= $wallet->product->title ; -// $record->unit = "عدد"; -// -// } -// } -// $record->save(); -// } -// -// } + + if (isset($records['rows'])) { + foreach ($records['rows'] as &$row) { + $balanceReport->enrichTransactionTableRow($row, $catalogMetalIds); + } + unset($row); + } + + $records['catalogMetals'] = $balanceReport->catalogMetalsMeta(); return response()->json($records); } diff --git a/app/Services/BalanceReportService.php b/app/Services/BalanceReportService.php index 99c16b4..31f9479 100644 --- a/app/Services/BalanceReportService.php +++ b/app/Services/BalanceReportService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Http\Controllers\Controller; use App\Models\Order; use App\Models\Product; @@ -248,4 +249,122 @@ class BalanceReportService $buckets[$metalId]['gold_abs'] += $gold; $buckets[$metalId]['cash_abs'] += $cash; } + + public function resolveTransactionMetalId(mixed $transaction): int + { + $product = $this->transactionProduct($transaction); + if ($product !== null) { + $metalId = (int) ($product->metal_id ?? 0); + + return $metalId > 0 ? $metalId : 1; + } + + return 1; + } + + public function transactionGoldGrams(mixed $transaction): float + { + $gold = (float) ($this->lineValue($transaction, 'gold') ?? 0); + if (abs($gold) < 0.0000001) { + return 0.0; + } + + $product = $this->transactionProduct($transaction); + if ($product !== null) { + return Controller::productLineWeightToGrams($product, $gold); + } + + return Controller::weightToGrams($gold); + } + + /** + * @param list $catalogMetalIds + * @return array + */ + public function buildTransactionMetalDisplayMap(mixed $transaction, array $catalogMetalIds): array + { + $map = []; + foreach ($catalogMetalIds as $metalId) { + $map[(string) (int) $metalId] = 0.0; + } + + $type = (int) ($this->lineValue($transaction, 'type') ?? 0); + if (in_array($type, [3, 4], true)) { + return $map; + } + + $gold = (float) ($this->lineValue($transaction, 'gold') ?? 0); + if (abs($gold) < 0.0000001) { + return $map; + } + + $metalId = $this->resolveTransactionMetalId($transaction); + $grams = abs($this->transactionGoldGrams($transaction)); + $display = $this->gramsToMetalDisplay($grams, $metalId); + if ($gold < 0) { + $display *= -1; + } + + $map[(string) $metalId] = round($display, 3); + + return $map; + } + + /** + * @param list $catalogMetalIds + */ + public function enrichTransactionTableRow(mixed &$row, array $catalogMetalIds): void + { + $metalMap = $this->buildTransactionMetalDisplayMap($row, $catalogMetalIds); + $metalId = $this->resolveTransactionMetalId($row); + $metalLabel = $this->metalLabel($metalId); + + if (is_array($row)) { + $row['metal_gold'] = $metalMap; + $row['transaction_metal_id'] = $metalId; + $row['transaction_metal_label'] = $metalLabel; + foreach ($catalogMetalIds as $mid) { + $row['metal_gold_' . $mid] = $metalMap[(string) $mid] ?? 0.0; + } + + return; + } + + if (is_object($row)) { + $row->metal_gold = $metalMap; + $row->transaction_metal_id = $metalId; + $row->transaction_metal_label = $metalLabel; + foreach ($catalogMetalIds as $mid) { + $row->{'metal_gold_' . $mid} = $metalMap[(string) $mid] ?? 0.0; + } + } + } + + private function transactionProduct(mixed $transaction): ?object + { + if (is_object($transaction) && isset($transaction->product)) { + return $transaction->product; + } + + if (is_array($transaction) && isset($transaction['product'])) { + $product = $transaction['product']; + + return is_object($product) ? $product : (object) $product; + } + + return null; + } + + 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; + } }