change transition structure

This commit is contained in:
2026-07-10 22:52:28 +03:30
parent 49727c5e55
commit 028c5d5938
2 changed files with 51 additions and 69 deletions
@@ -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'])) {
+7
View File
@@ -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),
];
}