change transition structure
This commit is contained in:
@@ -247,49 +247,42 @@ class SingleOrderController extends Controller
|
|||||||
|
|
||||||
$response = [];
|
$response = [];
|
||||||
$config = app(ConfigService::class);
|
$config = app(ConfigService::class);
|
||||||
$feeFromGold = $config->feeDeductFromGold();
|
|
||||||
$targetCash = $this->resolveTargetCash($data);
|
$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'];
|
$data['price'] += $data['difference'];
|
||||||
|
|
||||||
if ($targetCash !== null) {
|
if ($rialLocked) {
|
||||||
$data['amount'] = $this->resolveBuyAmountFromTargetCash(
|
// مبلغ ریالی دقیق — کارمزد از طلای دریافتی کسر میشود
|
||||||
$targetCash,
|
$unitPrice = (float) $data['price'];
|
||||||
(float) $data['price'],
|
$grossGold = $unitPrice > 0 ? $targetCash / $unitPrice : 0.0;
|
||||||
(float) ($data['service_charge_percentage'] ?? 0),
|
$data['service_charge'] = $this->normalizeGoldAmount($feeRate * $grossGold);
|
||||||
$feeFromGold,
|
$data['amount'] = $this->normalizeGoldAmount($grossGold - $data['service_charge']);
|
||||||
isset($data['fee_free']) && $data['fee_free'] === true
|
$data['service_charge_price'] = $data['service_charge'] * $data['price_original'];
|
||||||
);
|
$data['total_price'] = $data['amount'] * $data['price'];
|
||||||
|
$data['payment_amount'] = $targetCash;
|
||||||
} else {
|
} else {
|
||||||
|
// وزن دقیق — کارمزد به مبلغ ریالی پرداختی اضافه میشود
|
||||||
$data['amount'] = $this->normalizeGoldAmount((float) ($data['amount'] ?? 0));
|
$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);
|
$this->enforceProductOrderLimits($data);
|
||||||
|
|
||||||
if (!empty($data['first_buy']) && ($minFirstBuy = Setting::where('is_active', 1)->where('key', 'min_first_buy')->value('value'))) {
|
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) . ' ریال میباشد.');
|
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']) {
|
if ($wallet->cash < $data['payment_amount']) {
|
||||||
throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است');
|
throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است');
|
||||||
}
|
}
|
||||||
@@ -490,37 +483,39 @@ class SingleOrderController extends Controller
|
|||||||
{
|
{
|
||||||
$response = [];
|
$response = [];
|
||||||
$config = app(ConfigService::class);
|
$config = app(ConfigService::class);
|
||||||
$feeFromGold = $config->feeDeductFromGold();
|
|
||||||
$targetCash = $this->resolveTargetCash($data);
|
$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'];
|
$data['price'] -= $data['difference'];
|
||||||
|
|
||||||
if ($targetCash !== null && $feeFromGold) {
|
if ($rialLocked) {
|
||||||
$price = (float) $data['price'];
|
// مبلغ ریالی دقیق دریافتی — کارمزد از طلای کیف (کسر اضافهتر) تأمین میشود
|
||||||
$data['amount'] = $price > 0
|
$unitPrice = (float) $data['price'];
|
||||||
? $this->normalizeGoldAmount($targetCash / $price)
|
$data['amount'] = $unitPrice > 0
|
||||||
|
? $this->normalizeGoldAmount($targetCash / $unitPrice)
|
||||||
: 0.0;
|
: 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 {
|
} else {
|
||||||
|
// وزن دقیق فروش — کارمزد از مبلغ ریالی دریافتی کم میشود
|
||||||
$data['amount'] = $this->normalizeGoldAmount((float) ($data['amount'] ?? 0));
|
$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'];
|
||||||
$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['total_price'] = $data['amount'] * $data['price'] - $data['service_charge_price'];
|
$data['total_price'] = $data['amount'] * $data['price'] - $data['service_charge_price'];
|
||||||
$data['payment_amount'] = $data['total_price'];
|
$data['payment_amount'] = $data['total_price'];
|
||||||
$goldDebit = $data['amount'];
|
$goldDebit = $data['amount'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($targetCash !== null) {
|
$grossTotal = $data['amount'] * $data['price'];
|
||||||
$data['payment_amount'] = $targetCash;
|
$this->enforceProductOrderLimits($data, (float) $grossTotal);
|
||||||
$data['total_price'] = $targetCash;
|
|
||||||
}
|
|
||||||
|
|
||||||
$abshode = $this->resolveAbshodeProductContext($data);
|
$abshode = $this->resolveAbshodeProductContext($data);
|
||||||
$productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0);
|
$productId = (int) ($data['product_id'] ?? $abshode['id'] ?? 0);
|
||||||
@@ -1048,26 +1043,6 @@ class SingleOrderController extends Controller
|
|||||||
return round($amount, 10);
|
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
|
private function resolveGoldPaymentProductId(array $data): ?int
|
||||||
{
|
{
|
||||||
if (!empty($data['payment_product_id'])) {
|
if (!empty($data['payment_product_id'])) {
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ class AssetSummaryService
|
|||||||
* cost_basis_rial: float|null,
|
* cost_basis_rial: float|null,
|
||||||
* unrealized_profit_rial: float|null,
|
* unrealized_profit_rial: float|null,
|
||||||
* unrealized_profit_percent: float|null,
|
* unrealized_profit_percent: float|null,
|
||||||
|
* unrealized_profit_gold: float|null,
|
||||||
* live_gold_price: float
|
* live_gold_price: float
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
@@ -126,6 +127,7 @@ class AssetSummaryService
|
|||||||
$costBasisRial = null;
|
$costBasisRial = null;
|
||||||
$unrealizedProfitRial = null;
|
$unrealizedProfitRial = null;
|
||||||
$unrealizedProfitPercent = null;
|
$unrealizedProfitPercent = null;
|
||||||
|
$unrealizedProfitGold = null;
|
||||||
|
|
||||||
if ($goldWeight > 0) {
|
if ($goldWeight > 0) {
|
||||||
$buyOrders = Order::query()
|
$buyOrders = Order::query()
|
||||||
@@ -145,6 +147,10 @@ class AssetSummaryService
|
|||||||
if ($costBasisRial > 0) {
|
if ($costBasisRial > 0) {
|
||||||
$unrealizedProfitPercent = round(($unrealizedProfitRial / $costBasisRial) * 100, 2);
|
$unrealizedProfitPercent = round(($unrealizedProfitRial / $costBasisRial) * 100, 2);
|
||||||
}
|
}
|
||||||
|
// سود طلایی: همان سود ریالی تبدیلشده به وزن طلا با قیمت زندهٔ امروز
|
||||||
|
if ($liveGoldPrice > 0) {
|
||||||
|
$unrealizedProfitGold = round($unrealizedProfitRial / $liveGoldPrice, 4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,6 +164,7 @@ class AssetSummaryService
|
|||||||
'cost_basis_rial' => $costBasisRial,
|
'cost_basis_rial' => $costBasisRial,
|
||||||
'unrealized_profit_rial' => $unrealizedProfitRial,
|
'unrealized_profit_rial' => $unrealizedProfitRial,
|
||||||
'unrealized_profit_percent' => $unrealizedProfitPercent,
|
'unrealized_profit_percent' => $unrealizedProfitPercent,
|
||||||
|
'unrealized_profit_gold' => $unrealizedProfitGold,
|
||||||
'live_gold_price' => round($liveGoldPrice, 0),
|
'live_gold_price' => round($liveGoldPrice, 0),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user