From 028c5d59385c6c325ddf304774140ffc751b231e Mon Sep 17 00:00:00 2001 From: mohammad sedighi Date: Fri, 10 Jul 2026 22:52:28 +0330 Subject: [PATCH] change transition structure --- .../Single/SingleOrderController.php | 113 +++++++----------- app/Services/AssetSummaryService.php | 7 ++ 2 files changed, 51 insertions(+), 69 deletions(-) diff --git a/app/Http/Controllers/Single/SingleOrderController.php b/app/Http/Controllers/Single/SingleOrderController.php index 72fa3c5..7c7b090 100644 --- a/app/Http/Controllers/Single/SingleOrderController.php +++ b/app/Http/Controllers/Single/SingleOrderController.php @@ -247,49 +247,42 @@ class SingleOrderController extends Controller $response = []; $config = app(ConfigService::class); - $feeFromGold = $config->feeDeductFromGold(); $targetCash = $this->resolveTargetCash($data); + // منطق واحد و مستقل از fee_deduct_from_gold: + // • کاربر مبلغ ریالی زده (rialLocked) → دقیقاً همان مبلغ پرداخت می‌شود و کارمزد از طلا کم می‌شود. + // • کاربر وزن زده (gramLocked) → دقیقاً همان وزن دریافت می‌شود و کارمزد به ریال قابل پرداخت اضافه می‌شود. + $rialLocked = $targetCash !== null; + $feeRate = isset($data['fee_free']) && $data['fee_free'] === true + ? 0.0 + : (float) ($data['service_charge_percentage'] ?? 0); $data['price'] += $data['difference']; - if ($targetCash !== null) { - $data['amount'] = $this->resolveBuyAmountFromTargetCash( - $targetCash, - (float) $data['price'], - (float) ($data['service_charge_percentage'] ?? 0), - $feeFromGold, - isset($data['fee_free']) && $data['fee_free'] === true - ); + if ($rialLocked) { + // مبلغ ریالی دقیق — کارمزد از طلای دریافتی کسر می‌شود + $unitPrice = (float) $data['price']; + $grossGold = $unitPrice > 0 ? $targetCash / $unitPrice : 0.0; + $data['service_charge'] = $this->normalizeGoldAmount($feeRate * $grossGold); + $data['amount'] = $this->normalizeGoldAmount($grossGold - $data['service_charge']); + $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; + $data['total_price'] = $data['amount'] * $data['price']; + $data['payment_amount'] = $targetCash; } else { + // وزن دقیق — کارمزد به مبلغ ریالی پرداختی اضافه می‌شود $data['amount'] = $this->normalizeGoldAmount((float) ($data['amount'] ?? 0)); + $data['service_charge'] = $this->normalizeGoldAmount($feeRate * $data['amount']); + $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; + $data['total_price'] = $data['amount'] * $data['price']; + $data['payment_amount'] = $data['total_price'] + $data['service_charge_price']; } - $data['total_price'] = $data['amount'] * $data['price']; - $this->enforceProductOrderLimits($data); if (!empty($data['first_buy']) && ($minFirstBuy = Setting::where('is_active', 1)->where('key', 'min_first_buy')->value('value'))) { - if ($data['total_price'] < (int) $minFirstBuy) { + if ($data['payment_amount'] < (int) $minFirstBuy) { throw new UnprocessableEntityHttpException('حداقل سفارش اول ' . number_format((int) $minFirstBuy) . ' ریال میباشد.'); } } - $data['service_charge'] = isset($data['fee_free']) && $data['fee_free'] === true ? 0 : $data['service_charge_percentage'] * $data['amount']; - $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; - if ($feeFromGold) { - $data['payment_amount'] = $data['total_price'] + $data['service_charge_price']; - } else { - $data['payment_amount'] = $data['total_price']; - $data['amount'] -= $data['service_charge']; - $data['amount'] = $this->normalizeGoldAmount((float) $data['amount']); - } - - if ($targetCash !== null) { - $data['payment_amount'] = $targetCash; - if (!$feeFromGold) { - $data['total_price'] = $targetCash; - } - } - if ($wallet->cash < $data['payment_amount']) { throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است'); } @@ -490,37 +483,39 @@ class SingleOrderController extends Controller { $response = []; $config = app(ConfigService::class); - $feeFromGold = $config->feeDeductFromGold(); $targetCash = $this->resolveTargetCash($data); + // منطق واحد و مستقل از fee_deduct_from_gold: + // • کاربر مبلغ ریالی زده (rialLocked) → دقیقاً همان مبلغ واریز می‌شود و کارمزد از طلای کیف کسر می‌شود. + // • کاربر وزن زده (gramLocked) → دقیقاً همان وزن فروخته می‌شود و کارمزد از مبلغ ریالی دریافتی کم می‌شود. + $rialLocked = $targetCash !== null; + $feeRate = isset($data['fee_free']) && $data['fee_free'] === true + ? 0.0 + : (float) ($data['service_charge_percentage'] ?? 0); $data['price'] -= $data['difference']; - if ($targetCash !== null && $feeFromGold) { - $price = (float) $data['price']; - $data['amount'] = $price > 0 - ? $this->normalizeGoldAmount($targetCash / $price) + if ($rialLocked) { + // مبلغ ریالی دقیق دریافتی — کارمزد از طلای کیف (کسر اضافه‌تر) تأمین می‌شود + $unitPrice = (float) $data['price']; + $data['amount'] = $unitPrice > 0 + ? $this->normalizeGoldAmount($targetCash / $unitPrice) : 0.0; + $data['service_charge'] = $this->normalizeGoldAmount($feeRate * $data['amount']); + $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; + $data['total_price'] = $targetCash; + $data['payment_amount'] = $targetCash; + $goldDebit = $this->normalizeGoldAmount($data['amount'] + $data['service_charge']); } else { + // وزن دقیق فروش — کارمزد از مبلغ ریالی دریافتی کم می‌شود $data['amount'] = $this->normalizeGoldAmount((float) ($data['amount'] ?? 0)); - } - - $grossTotal = $data['amount'] * $data['price']; - $this->enforceProductOrderLimits($data, (float) $grossTotal); - $data['service_charge'] = isset($data['fee_free']) && $data['fee_free'] === true ? 0 : $data['service_charge_percentage'] * $data['amount']; - $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; - if ($feeFromGold) { - $data['total_price'] = $data['amount'] * $data['price']; - $data['payment_amount'] = $data['total_price']; - $goldDebit = $data['amount'] + $data['service_charge']; - } else { + $data['service_charge'] = $this->normalizeGoldAmount($feeRate * $data['amount']); + $data['service_charge_price'] = $data['service_charge'] * $data['price_original']; $data['total_price'] = $data['amount'] * $data['price'] - $data['service_charge_price']; $data['payment_amount'] = $data['total_price']; $goldDebit = $data['amount']; } - if ($targetCash !== null) { - $data['payment_amount'] = $targetCash; - $data['total_price'] = $targetCash; - } + $grossTotal = $data['amount'] * $data['price']; + $this->enforceProductOrderLimits($data, (float) $grossTotal); $abshode = $this->resolveAbshodeProductContext($data); $productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0); @@ -1048,26 +1043,6 @@ class SingleOrderController extends Controller return round($amount, 10); } - private function resolveBuyAmountFromTargetCash( - int $targetCash, - float $unitPrice, - float $serviceChargePercentage, - bool $feeFromGold, - bool $feeFree - ): float { - if ($unitPrice <= 0 || $targetCash <= 0) { - return 0.0; - } - - $feeRate = $feeFree ? 0.0 : $serviceChargePercentage; - - if ($feeFromGold) { - return $this->normalizeGoldAmount($targetCash / ($unitPrice * (1 + $feeRate))); - } - - return $this->normalizeGoldAmount($targetCash / $unitPrice); - } - private function resolveGoldPaymentProductId(array $data): ?int { if (!empty($data['payment_product_id'])) { diff --git a/app/Services/AssetSummaryService.php b/app/Services/AssetSummaryService.php index aa748db..41e3cba 100644 --- a/app/Services/AssetSummaryService.php +++ b/app/Services/AssetSummaryService.php @@ -105,6 +105,7 @@ class AssetSummaryService * cost_basis_rial: float|null, * unrealized_profit_rial: float|null, * unrealized_profit_percent: float|null, + * unrealized_profit_gold: float|null, * live_gold_price: float * } */ @@ -126,6 +127,7 @@ class AssetSummaryService $costBasisRial = null; $unrealizedProfitRial = null; $unrealizedProfitPercent = null; + $unrealizedProfitGold = null; if ($goldWeight > 0) { $buyOrders = Order::query() @@ -145,6 +147,10 @@ class AssetSummaryService if ($costBasisRial > 0) { $unrealizedProfitPercent = round(($unrealizedProfitRial / $costBasisRial) * 100, 2); } + // سود طلایی: همان سود ریالی تبدیل‌شده به وزن طلا با قیمت زندهٔ امروز + if ($liveGoldPrice > 0) { + $unrealizedProfitGold = round($unrealizedProfitRial / $liveGoldPrice, 4); + } } } @@ -158,6 +164,7 @@ class AssetSummaryService 'cost_basis_rial' => $costBasisRial, 'unrealized_profit_rial' => $unrealizedProfitRial, 'unrealized_profit_percent' => $unrealizedProfitPercent, + 'unrealized_profit_gold' => $unrealizedProfitGold, 'live_gold_price' => round($liveGoldPrice, 0), ]; }