From f271df9cd679392426bfe024eb3a1b81f848d9fe Mon Sep 17 00:00:00 2001 From: adonis Date: Tue, 23 Jun 2026 21:28:49 +0330 Subject: [PATCH] fixed kilo in metals 3 --- .../Controllers/Admin/OrderController.php | 32 ++++++++- app/Services/BalanceReportService.php | 70 +++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Admin/OrderController.php b/app/Http/Controllers/Admin/OrderController.php index e90db91..80e5693 100644 --- a/app/Http/Controllers/Admin/OrderController.php +++ b/app/Http/Controllers/Admin/OrderController.php @@ -5,26 +5,54 @@ namespace App\Http\Controllers\Admin; use App\DB\Te\TableEngine; use App\Http\Controllers\Controller; use App\Models\Order; +use App\Services\BalanceReportService; use Illuminate\Http\Request; class OrderController extends Controller { public function table(Request $request){ - $records = Order::with(['customer','transactions']); + $balanceReport = app(BalanceReportService::class); + + $records = Order::with([ + 'customer', + 'transactions.product:id,title,unit,metal_id,group_type,amount,trade_by_quantity,direct_price', + ]); TableEngine::create($records, $request->all()) ->initSearch(['customer.name']) ->select([ '*',Order::$SELECT_STS_STR ])->export($records); + + if (isset($records['rows'])) { + foreach ($records['rows'] as &$row) { + $balanceReport->enrichOrderTableRow($row); + } + unset($row); + } + return response()->json($records); } + public function singleOrderTable(Request $request){ - $records = Order::with(['customer'])->where('type',2); + $balanceReport = app(BalanceReportService::class); + + $records = Order::with([ + 'customer', + 'transactions.product:id,title,unit,metal_id,group_type,amount,trade_by_quantity,direct_price', + ])->where('type',2); TableEngine::create($records, $request->all()) ->initSearch(['customer.name']) ->select([ '*',Order::$SELECT_STS_STR ])->export($records); + + if (isset($records['rows'])) { + foreach ($records['rows'] as &$row) { + $balanceReport->enrichOrderTableRow($row); + } + unset($row); + } + return response()->json($records); } } diff --git a/app/Services/BalanceReportService.php b/app/Services/BalanceReportService.php index 31f9479..fe07e26 100644 --- a/app/Services/BalanceReportService.php +++ b/app/Services/BalanceReportService.php @@ -355,6 +355,76 @@ class BalanceReportService return null; } + public function enrichOrderTableRow(mixed &$row): void + { + $product = $this->resolveOrderProduct($row); + $unit = $this->resolveOrderAmountUnit($product); + $amount = (float) ($this->lineValue($row, 'amount') ?? 0); + $displayAmount = $this->resolveOrderDisplayAmount($amount, $product); + + if (is_array($row)) { + $row['amount_unit'] = $unit; + $row['amount_display'] = $displayAmount; + + return; + } + + if (is_object($row)) { + $row->amount_unit = $unit; + $row->amount_display = $displayAmount; + } + } + + public function resolveOrderAmountUnit(?object $product): string + { + if ($product === null) { + return Controller::tradeWeightUnit(); + } + + return Controller::productQuantityUnit(Controller::productUnitPayload($product)); + } + + private function resolveOrderDisplayAmount(float $amount, ?object $product): float + { + if ($product === null || abs($amount) < 0.0000001) { + return round($amount, 3); + } + + $payload = Controller::productUnitPayload($product); + $productUnit = trim((string) ($payload['unit'] ?? '')); + if (!Controller::isWeightBasedProductUnit($productUnit)) { + return round($amount, 3); + } + + $tradeUnit = Controller::tradeWeightUnit(); + if ($productUnit !== $tradeUnit) { + return round(Controller::convertWeight($amount, $tradeUnit, $productUnit), 3); + } + + return round($amount, 3); + } + + private function resolveOrderProduct(mixed $order): ?object + { + $transactions = $this->lineValue($order, 'transactions'); + if ($transactions instanceof \Illuminate\Support\Collection) { + $transactions = $transactions->all(); + } + + if (!is_array($transactions)) { + return null; + } + + foreach ($transactions as $tx) { + $product = is_object($tx) ? ($tx->product ?? null) : ($tx['product'] ?? null); + if ($product) { + return is_object($product) ? $product : (object) $product; + } + } + + return null; + } + private function lineValue(mixed $line, string $key): mixed { if (is_array($line)) {