From c0c84a9b390c868512bd487c6049b1fc87bbda5d Mon Sep 17 00:00:00 2001 From: adonis Date: Thu, 9 Jul 2026 17:47:27 +0330 Subject: [PATCH] fix buy and sell information 2 --- .../Single/SingleOrderController.php | 116 +++++++++++++----- 1 file changed, 88 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Single/SingleOrderController.php b/app/Http/Controllers/Single/SingleOrderController.php index 4f8414c..56aa6b5 100644 --- a/app/Http/Controllers/Single/SingleOrderController.php +++ b/app/Http/Controllers/Single/SingleOrderController.php @@ -305,20 +305,45 @@ 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'], - 'expire_at' => now(), - 'active' => true, - 'order_id' => $order->id, - 'start_at' => now(), - 'title' => $this->meltedOrProductBuyTitle($data), - ]; - if ($productId > 0) { - $buyTransaction['product_id'] = $productId; - $buyTransaction['note'] = 'product_id:' . $productId; + $usesProductWallet = $productId > 0 && !$config->isMainWalletProduct($productId); + + if ($usesProductWallet) { + new TransactionInterface($wallet_interface, [ + "cash" => $data['payment_amount'], + "product_id" => $productId, + 'expire_at' => now(), + 'active' => true, + 'order_id' => $order->id, + 'start_at' => now(), + 'title' => 'برداشت وجه نقد', + ], 12, false, false); + $wallet_interface_product = $this->productWalletInterface($productId); + $this->normalizeProductWalletGold($wallet_interface_product); + new TransactionInterface($wallet_interface_product, [ + "gold" => $data['amount'], + "product_id" => $productId, + 'expire_at' => now(), + 'active' => true, + 'order_id' => $order->id, + 'start_at' => now(), + 'title' => $this->meltedOrProductBuyTitle($data), + ], 1, true, false); + } else { + $buyTransaction = [ + "cash" => $data['payment_amount'], + "gold" => $data['amount'], + 'expire_at' => now(), + 'active' => true, + 'order_id' => $order->id, + 'start_at' => now(), + 'title' => $this->meltedOrProductBuyTitle($data), + ]; + if ($productId > 0) { + $buyTransaction['product_id'] = $productId; + $buyTransaction['note'] = 'product_id:' . $productId; + } + new TransactionInterface($wallet_interface, $buyTransaction, 1, true, false); } - new TransactionInterface($wallet_interface, $buyTransaction, 1, true, false); $this->finalizeProviderOrder($data); $response['1'] = 2; @@ -503,28 +528,63 @@ class SingleOrderController extends Controller $data['total_price'] = $targetCash; } - if ($wallet->gold < $goldDebit) { + $abshode = $this->resolveAbshodeProductContext($data); + $productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0); + $usesProductWallet = $productId > 0 && !$config->isMainWalletProduct($productId); + $goldWallet = $wallet; + $wallet_interface_product = null; + + if ($usesProductWallet) { + $wallet_interface_product = $this->productWalletInterface($productId); + $goldWallet = $wallet_interface_product->wallet; + if (!$goldWallet) { + throw new UnprocessableEntityHttpException('کیف پول محصول یافت نشد.'); + } + } + + if ($goldWallet->gold < $goldDebit) { throw new UnprocessableEntityHttpException($this->insufficientGoldBalanceMessage($data)); } $this->assertProviderOrderAccepted($data); $data['order'] = $order = Order::create($data); - $abshode = $this->resolveAbshodeProductContext($data); - $productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0); - $sellTransaction = [ - "gold" => $goldDebit, - "cash" => $data['total_price'], - 'expire_at' => now(), - 'active' => true, - 'order_id' => $order->id, - 'title' => $this->meltedOrProductSellTitle($data), - ]; - if ($productId > 0) { - $sellTransaction['product_id'] = $productId; - $sellTransaction['note'] = 'product_id:' . $productId; + + if ($usesProductWallet) { + $this->normalizeProductWalletGold($wallet_interface_product); + new TransactionInterface($wallet_interface_product, [ + "gold" => $goldDebit, + "product_id" => $productId, + 'expire_at' => now(), + 'active' => true, + 'order_id' => $order->id, + 'start_at' => now(), + 'title' => $this->meltedOrProductSellTitle($data), + ], 1, false, false); + new TransactionInterface($wallet_interface, [ + "cash" => $data['total_price'], + "product_id" => $productId, + 'expire_at' => now(), + 'active' => true, + 'order_id' => $order->id, + 'start_at' => now(), + 'title' => 'واریز وجه نقد', + ], 12, false, true); + } else { + $sellTransaction = [ + "gold" => $goldDebit, + "cash" => $data['total_price'], + 'expire_at' => now(), + 'active' => true, + 'order_id' => $order->id, + 'title' => $this->meltedOrProductSellTitle($data), + ]; + if ($productId > 0) { + $sellTransaction['product_id'] = $productId; + $sellTransaction['note'] = 'product_id:' . $productId; + } + new TransactionInterface($wallet_interface, $sellTransaction, 2, false, true); } - new TransactionInterface($wallet_interface, $sellTransaction, 2, false, true); $this->finalizeProviderOrder($data); }