diff --git a/app/Http/Controllers/Admin/OrderController.php b/app/Http/Controllers/Admin/OrderController.php index 80e5693..280d70c 100644 --- a/app/Http/Controllers/Admin/OrderController.php +++ b/app/Http/Controllers/Admin/OrderController.php @@ -6,12 +6,14 @@ use App\DB\Te\TableEngine; use App\Http\Controllers\Controller; use App\Models\Order; use App\Services\BalanceReportService; +use App\Services\WalletBalanceService; use Illuminate\Http\Request; class OrderController extends Controller { public function table(Request $request){ $balanceReport = app(BalanceReportService::class); + $catalogMetalIds = app(WalletBalanceService::class)->catalogMetalIds(); $records = Order::with([ 'customer', @@ -25,16 +27,19 @@ class OrderController extends Controller if (isset($records['rows'])) { foreach ($records['rows'] as &$row) { - $balanceReport->enrichOrderTableRow($row); + $balanceReport->enrichOrderTableRow($row, $catalogMetalIds); } unset($row); } + $records['catalogMetals'] = $balanceReport->catalogMetalsMeta(); + return response()->json($records); } public function singleOrderTable(Request $request){ $balanceReport = app(BalanceReportService::class); + $catalogMetalIds = app(WalletBalanceService::class)->catalogMetalIds(); $records = Order::with([ 'customer', @@ -48,11 +53,13 @@ class OrderController extends Controller if (isset($records['rows'])) { foreach ($records['rows'] as &$row) { - $balanceReport->enrichOrderTableRow($row); + $balanceReport->enrichOrderTableRow($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 fe07e26..0419443 100644 --- a/app/Services/BalanceReportService.php +++ b/app/Services/BalanceReportService.php @@ -355,23 +355,61 @@ class BalanceReportService return null; } - public function enrichOrderTableRow(mixed &$row): void + public function resolveOrderMetalId(mixed $order): int + { + $product = $this->resolveOrderProduct($order); + if ($product !== null) { + $metalId = (int) ($product->metal_id ?? 0); + + return $metalId > 0 ? $metalId : 1; + } + + return 1; + } + + /** + * @param list $catalogMetalIds + */ + public function enrichOrderTableRow(mixed &$row, array $catalogMetalIds = []): void { $product = $this->resolveOrderProduct($row); $unit = $this->resolveOrderAmountUnit($product); $amount = (float) ($this->lineValue($row, 'amount') ?? 0); $displayAmount = $this->resolveOrderDisplayAmount($amount, $product); + $metalId = $this->resolveOrderMetalId($row); + $metalLabel = $this->metalLabel($metalId); + $price = (float) ($this->lineValue($row, 'price') ?? 0); + + $metalPrices = []; + foreach ($catalogMetalIds as $mid) { + $mid = (int) $mid; + $metalPrices[(string) $mid] = $mid === $metalId ? $price : 0.0; + } + + $payload = [ + 'amount_unit' => $unit, + 'amount_display' => $displayAmount, + 'order_metal_id' => $metalId, + 'order_metal_label' => $metalLabel, + 'metal_prices' => $metalPrices, + ]; + + foreach ($catalogMetalIds as $mid) { + $payload['metal_price_' . (int) $mid] = $metalPrices[(string) (int) $mid] ?? 0.0; + } if (is_array($row)) { - $row['amount_unit'] = $unit; - $row['amount_display'] = $displayAmount; + foreach ($payload as $key => $value) { + $row[$key] = $value; + } return; } if (is_object($row)) { - $row->amount_unit = $unit; - $row->amount_display = $displayAmount; + foreach ($payload as $key => $value) { + $row->{$key} = $value; + } } }