From bd36b74dad0a12a55f40130a159b98c6b3525bff Mon Sep 17 00:00:00 2001 From: adonis Date: Mon, 22 Jun 2026 21:42:48 +0330 Subject: [PATCH] fixed product title --- .../Controllers/Customer/WalletController.php | 113 ++++++++++++++---- .../Single/SingleOrderController.php | 49 ++++---- 2 files changed, 113 insertions(+), 49 deletions(-) diff --git a/app/Http/Controllers/Customer/WalletController.php b/app/Http/Controllers/Customer/WalletController.php index 0bafceb..be80518 100644 --- a/app/Http/Controllers/Customer/WalletController.php +++ b/app/Http/Controllers/Customer/WalletController.php @@ -402,17 +402,12 @@ class WalletController extends Controller return; } - $productId = $this->resolveAbshodeProductId($record); - if (!$productId) { - return; - } - - $productTitle = $record->product->title - ?? Product::query()->whereKey($productId)->value('title'); - if (!$productTitle) { + $product = $this->resolveTransactionProduct($record); + if (!$product || trim((string) $product->title) === '') { return; } + $productTitle = trim((string) $product->title); $direction = (int) ($record->order->direction ?? 0); if ($direction === 1 || ($direction === 0 && (float) ($record->gold ?? 0) > 0)) { $record->title = self::productBuyTitle($productTitle); @@ -430,9 +425,14 @@ class WalletController extends Controller return (int) $record->product_id; } - $note = (string) ($record->note ?? ''); - if ($note !== '' && preg_match('/(?:^|;)product_id:(\d+)/', $note, $matches)) { - return (int) $matches[1]; + $noteProductId = $this->productIdFromTransactionNote($record->note ?? null); + if ($noteProductId) { + return $noteProductId; + } + + $wallet = $this->resolveTransactionWallet($record); + if ($wallet && !empty($wallet->product_id)) { + return (int) $wallet->product_id; } if (empty($record->order_id)) { @@ -445,7 +445,33 @@ class WalletController extends Controller ->where('product_id', '>', 0) ->value('product_id'); - return $siblingProductId ? (int) $siblingProductId : null; + if ($siblingProductId) { + return (int) $siblingProductId; + } + + $siblingNotes = Transaction::query() + ->where('order_id', $record->order_id) + ->whereNotNull('note') + ->pluck('note'); + + foreach ($siblingNotes as $note) { + $productId = $this->productIdFromTransactionNote($note); + if ($productId) { + return $productId; + } + } + + return null; + } + + private function productIdFromTransactionNote(?string $note): ?int + { + $note = (string) ($note ?? ''); + if ($note === '' || !preg_match('/(?:^|;)product_id:(\d+)/', $note, $matches)) { + return null; + } + + return (int) $matches[1]; } private function applyMetalTypeTransactionTitle($record): void @@ -458,7 +484,7 @@ class WalletController extends Controller return; } - $wallet = $record->wallet ?? null; + $wallet = $this->resolveTransactionWallet($record); if ($wallet && !empty($wallet->product)) { return; } @@ -488,18 +514,12 @@ class WalletController extends Controller return; } - $productId = $this->resolveAbshodeProductId($record); - if (!$productId) { - return; - } - - $productTitle = $record->product->title - ?? $record->wallet->product->title - ?? Product::query()->whereKey($productId)->value('title'); - if (!$productTitle) { + $product = $this->resolveTransactionProduct($record); + if (!$product || trim((string) $product->title) === '') { return; } + $productTitle = trim((string) $product->title); $expectedBuy = self::productBuyTitle($productTitle); $expectedSell = self::productSellTitle($productTitle); @@ -515,11 +535,28 @@ class WalletController extends Controller if ($direction === 2 || ($direction === 0 && (float) ($record->gold ?? 0) < 0)) { $record->title = $expectedSell; + return; + } + + if (self::isGenericMetalTransactionTitle($record->title)) { + $record->title = $expectedBuy; } } private function resolveMainWalletTransactionTitle($record): string { + $product = $this->resolveTransactionProduct($record); + if ($product && trim((string) $product->title) !== '') { + $productTitle = trim((string) $product->title); + if ((int) $record->type === 1) { + return self::productBuyTitle($productTitle); + } + + if ((int) $record->type === 2) { + return self::productSellTitle($productTitle); + } + } + if ((int) $record->type === 1) { return self::meltedGoldBuyTitle(); } @@ -560,15 +597,39 @@ class WalletController extends Controller } } + private function resolveTransactionWallet($record): ?Wallet + { + if ($record instanceof Transaction) { + $relation = $record->getRelationValue('wallet'); + if ($relation instanceof Wallet) { + return $relation; + } + } + + $walletId = (int) ($record->getAttributes()['wallet'] ?? 0); + if ($walletId <= 0) { + return null; + } + + return Wallet::with('product')->find($walletId); + } + private function resolveTransactionProduct($record): ?Product { - $partial = $record->wallet->product ?? $record->product ?? null; - $productId = (int) ($partial->id ?? $record->product_id ?? 0); + $productId = $this->resolveAbshodeProductId($record) ?? 0; + $wallet = $this->resolveTransactionWallet($record); if ($productId <= 0) { + $partial = $wallet?->product ?? $record->product ?? null; + return $partial instanceof Product ? $partial : null; } + $partial = $record->product ?? $wallet?->product ?? null; + if ($partial instanceof Product && (int) $partial->id === $productId && trim((string) $partial->title) !== '') { + return $partial; + } + return Product::query() ->select([ 'id', @@ -609,7 +670,9 @@ class WalletController extends Controller private function isProductWalletTransaction($record): bool { - return !empty($record->wallet?->product); + $wallet = $this->resolveTransactionWallet($record); + + return !empty($wallet?->product); } /** diff --git a/app/Http/Controllers/Single/SingleOrderController.php b/app/Http/Controllers/Single/SingleOrderController.php index 33ce376..c91244f 100644 --- a/app/Http/Controllers/Single/SingleOrderController.php +++ b/app/Http/Controllers/Single/SingleOrderController.php @@ -249,6 +249,7 @@ class SingleOrderController extends Controller } $data['order'] = $order = Order::create($data); $abshode = $this->resolveAbshodeProductContext($data); + $productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0); $buyTransaction = [ "cash" => $data['payment_amount'], "gold" => $data['amount'], @@ -256,13 +257,11 @@ class SingleOrderController extends Controller 'active' => true, 'order_id' => $order->id, 'start_at' => now(), - 'title' => $abshode['title'] !== '' - ? self::productBuyTitle($abshode['title']) - : self::meltedGoldBuyTitle(), + 'title' => $this->meltedOrProductBuyTitle($data), ]; - if ($abshode['id'] > 0) { - $buyTransaction['product_id'] = $abshode['id']; - $buyTransaction['note'] = 'product_id:' . $abshode['id']; + if ($productId > 0) { + $buyTransaction['product_id'] = $productId; + $buyTransaction['note'] = 'product_id:' . $productId; } new TransactionInterface($wallet_interface, $buyTransaction, 1, true, false); $response['1'] = 2; @@ -316,7 +315,7 @@ class SingleOrderController extends Controller 'active' => true, 'order_id' => $order->id, 'start_at' => now(), - 'title' => self::productBuyTitle($data['product']['title'] ?? ''), + 'title' => $this->meltedOrProductBuyTitle($data), ], 1, true, false); @@ -408,7 +407,7 @@ class SingleOrderController extends Controller 'active' => true, 'order_id' => $order->id, 'start_at' => now(), - 'title' => self::productBuyTitle($data['product']['title'] ?? ''), + 'title' => $this->meltedOrProductBuyTitle($data), ], 1, true, false); } @@ -429,19 +428,18 @@ class SingleOrderController extends Controller } $data['order'] = $order = Order::create($data); $abshode = $this->resolveAbshodeProductContext($data); + $productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0); $sellTransaction = [ "gold" => $data['amount'], "cash" => $data['total_price'], 'expire_at' => now(), 'active' => true, 'order_id' => $order->id, - 'title' => $abshode['title'] !== '' - ? self::productSellTitle($abshode['title']) - : self::meltedGoldSellTitle(), + 'title' => $this->meltedOrProductSellTitle($data), ]; - if ($abshode['id'] > 0) { - $sellTransaction['product_id'] = $abshode['id']; - $sellTransaction['note'] = 'product_id:' . $abshode['id']; + if ($productId > 0) { + $sellTransaction['product_id'] = $productId; + $sellTransaction['note'] = 'product_id:' . $productId; } new TransactionInterface($wallet_interface, $sellTransaction, 2, false, true); @@ -479,7 +477,7 @@ class SingleOrderController extends Controller 'active' => true, 'order_id' => $order->id, 'start_at' => now(), - 'title' => self::productSellTitle($data['product']['title'] ?? ''), + 'title' => $this->meltedOrProductSellTitle($data), ], 1, false, false); $data['transaction'][] = new TransactionInterface($wallet_interface, $_inp = [ "number" => $data['number'], @@ -520,7 +518,7 @@ class SingleOrderController extends Controller 'expire_at' => now(), 'active' => true, 'order_id' => $order->id, - 'title' => self::mainMetalBuyTitle(), + 'title' => $this->meltedOrProductSellTitle($data), ], 12, true, false); $this->normalizeProductWalletGold($wallet_interface_product); new TransactionInterface($wallet_interface_product, [ @@ -530,7 +528,7 @@ class SingleOrderController extends Controller 'active' => true, 'order_id' => $order->id, 'start_at' => now(), - 'title' => self::productSellTitle($data['product']['title'] ?? ''), + 'title' => $this->meltedOrProductSellTitle($data), ], 1, false, false); } @@ -890,17 +888,20 @@ class SingleOrderController extends Controller private function productTitleFromData(array $data): ?string { $product = $data['product'] ?? null; - if (!$product) { - return null; - } + $title = null; - if (is_array($product)) { - $title = $product['title'] ?? null; - } else { - $title = $product->title ?? null; + if ($product) { + if (is_array($product)) { + $title = $product['title'] ?? null; + } else { + $title = $product->title ?? null; + } } $title = trim((string) $title); + if ($title === '' && !empty($data['product_id'])) { + $title = trim((string) Product::query()->whereKey((int) $data['product_id'])->value('title')); + } return $title !== '' ? $title : null; }