diff --git a/app/Http/Controllers/Customer/WalletController.php b/app/Http/Controllers/Customer/WalletController.php index 60280d8..7a334a7 100644 --- a/app/Http/Controllers/Customer/WalletController.php +++ b/app/Http/Controllers/Customer/WalletController.php @@ -7,6 +7,7 @@ use App\Http\Controllers\Controller; use App\Models\Address; use App\Models\Customer; use App\Models\Order; +use App\Models\Product; use App\Models\User; use App\Models\Withdraw; use App\Models\BankAccount; @@ -75,7 +76,8 @@ class WalletController extends Controller 'withdraw' => function ($q) { $q->select(['*', Withdraw::$SELECT_STS_STR]); }, - 'wallet.product' + 'wallet.product', + 'product', ])->whereIn('wallet', $wallet->pluck('id'))->whereNull('fund_id'); TableEngine::create($records, $request->all()) @@ -88,6 +90,7 @@ class WalletController extends Controller foreach ($records['rows'] as &$record) { if ($this->isShopGoldTransaction($record)) { $this->applyShopTransactionPresentation($record); + $this->applyAbshodeProductTransactionTitle($record); $this->applyMetalTypeTransactionTitle($record); $this->applyProductTransactionTitle($record); $this->applyProductOrderDisplayFields($record); @@ -128,6 +131,7 @@ class WalletController extends Controller $record->save(); } + $this->applyAbshodeProductTransactionTitle($record); $this->applyMetalTypeTransactionTitle($record); $this->applyProductTransactionTitle($record); $this->applyProductOrderDisplayFields($record); @@ -357,8 +361,64 @@ class WalletController extends Controller } } + private function applyAbshodeProductTransactionTitle($record): void + { + if (self::isCashTransactionTitle($record->title ?? null)) { + return; + } + + $productId = $this->resolveAbshodeProductId($record); + if (!$productId) { + return; + } + + $productTitle = $record->product->title + ?? Product::query()->whereKey($productId)->value('title'); + if (!$productTitle) { + return; + } + + $direction = (int) ($record->order->direction ?? 0); + if ($direction === 1 || ($direction === 0 && (float) ($record->gold ?? 0) > 0)) { + $record->title = self::productBuyTitle($productTitle); + return; + } + + if ($direction === 2 || ($direction === 0 && (float) ($record->gold ?? 0) < 0)) { + $record->title = self::productSellTitle($productTitle); + } + } + + private function resolveAbshodeProductId($record): ?int + { + if (!empty($record->product_id)) { + return (int) $record->product_id; + } + + $note = (string) ($record->note ?? ''); + if ($note !== '' && preg_match('/(?:^|;)product_id:(\d+)/', $note, $matches)) { + return (int) $matches[1]; + } + + if (empty($record->order_id)) { + return null; + } + + $siblingProductId = Transaction::query() + ->where('order_id', $record->order_id) + ->whereNotNull('product_id') + ->where('product_id', '>', 0) + ->value('product_id'); + + return $siblingProductId ? (int) $siblingProductId : null; + } + private function applyMetalTypeTransactionTitle($record): void { + if ($this->resolveAbshodeProductId($record)) { + return; + } + if (!empty($record->product_id)) { return; } @@ -389,39 +449,36 @@ class WalletController extends Controller private function applyProductTransactionTitle($record): void { - if (empty($record->product_id)) { - return; - } - if (self::isCashTransactionTitle($record->title)) { return; } - $productTitle = $record->wallet->product->title ?? $record->product->title ?? null; + $productId = $this->resolveAbshodeProductId($record); + if (!$productId) { + return; + } + + $productTitle = $record->product->title + ?? $record->wallet->product->title + ?? Product::query()->whereKey($productId)->value('title'); if (!$productTitle) { return; } $expectedBuy = self::productBuyTitle($productTitle); $expectedSell = self::productSellTitle($productTitle); - $metalBuy = self::mainMetalBuyTitle(); - $metalSell = self::mainMetalSellTitle(); - if (in_array($record->title, [$expectedBuy, $expectedSell, $metalBuy, $metalSell], true)) { + if (in_array($record->title, [$expectedBuy, $expectedSell], true)) { return; } - if (!self::isGenericMetalTransactionTitle($record->title)) { - return; - } - - $direction = $record->order->direction ?? null; - if ((int) $direction === 1) { + $direction = (int) ($record->order->direction ?? 0); + if ($direction === 1 || ($direction === 0 && (float) ($record->gold ?? 0) > 0)) { $record->title = $expectedBuy; return; } - if ((int) $direction === 2) { + if ($direction === 2 || ($direction === 0 && (float) ($record->gold ?? 0) < 0)) { $record->title = $expectedSell; } } diff --git a/app/Http/Controllers/Single/SingleOrderController.php b/app/Http/Controllers/Single/SingleOrderController.php index 1ce3d90..55c8ce6 100644 --- a/app/Http/Controllers/Single/SingleOrderController.php +++ b/app/Http/Controllers/Single/SingleOrderController.php @@ -241,6 +241,7 @@ class SingleOrderController extends Controller throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است'); } $data['order'] = $order = Order::create($data); + $abshode = $this->resolveAbshodeProductContext($data); $buyTransaction = [ "cash" => $data['payment_amount'], "gold" => $data['amount'], @@ -248,10 +249,13 @@ class SingleOrderController extends Controller 'active' => true, 'order_id' => $order->id, 'start_at' => now(), - 'title' => $this->meltedOrProductBuyTitle($data), + 'title' => $abshode['title'] !== '' + ? self::productBuyTitle($abshode['title']) + : self::meltedGoldBuyTitle(), ]; - if (!empty($data['product_id'])) { - $buyTransaction['product_id'] = $data['product_id']; + if ($abshode['id'] > 0) { + $buyTransaction['product_id'] = $abshode['id']; + $buyTransaction['note'] = 'product_id:' . $abshode['id']; } new TransactionInterface($wallet_interface, $buyTransaction, 1, true, false); $response['1'] = 2; @@ -372,16 +376,20 @@ class SingleOrderController extends Controller throw new UnprocessableEntityHttpException($this->insufficientGoldBalanceMessage($data)); } $data['order'] = $order = Order::create($data); + $abshode = $this->resolveAbshodeProductContext($data); $sellTransaction = [ "gold" => $data['amount'], "cash" => $data['total_price'], 'expire_at' => now(), 'active' => true, 'order_id' => $order->id, - 'title' => $this->meltedOrProductSellTitle($data), + 'title' => $abshode['title'] !== '' + ? self::productSellTitle($abshode['title']) + : self::meltedGoldSellTitle(), ]; - if (!empty($data['product_id'])) { - $sellTransaction['product_id'] = $data['product_id']; + if ($abshode['id'] > 0) { + $sellTransaction['product_id'] = $abshode['id']; + $sellTransaction['note'] = 'product_id:' . $abshode['id']; } new TransactionInterface($wallet_interface, $sellTransaction, 2, false, true); @@ -757,6 +765,34 @@ class SingleOrderController extends Controller return null; } + private function resolveAbshodeProductContext(array $data): array + { + $productId = (int) ($data['product_id'] ?? $data['abshode_id'] ?? 0); + $product = null; + + if (!empty($data['product'])) { + $candidate = $data['product']; + if ($candidate instanceof Product) { + $product = $candidate; + } elseif (is_array($candidate) && !empty($candidate['id'])) { + $product = Product::find($candidate['id']); + } + } + + if (!$product && $productId > 0) { + $product = Product::find($productId); + } + + if (!$product) { + return ['id' => 0, 'title' => '']; + } + + return [ + 'id' => (int) $product->id, + 'title' => trim((string) $product->title), + ]; + } + private function productTitleFromData(array $data): ?string { $product = $data['product'] ?? null;