fix amount

This commit is contained in:
2026-07-04 12:06:56 +03:30
parent 163097f42a
commit 4dea2fb212
2 changed files with 67 additions and 1 deletions
@@ -239,6 +239,19 @@ class SingleOrderController extends Controller
$feeFromGold = $config->feeDeductFromGold(); $feeFromGold = $config->feeDeductFromGold();
$targetCash = $this->resolveTargetCash($data); $targetCash = $this->resolveTargetCash($data);
$data['price'] += $data['difference']; $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
);
} else {
$data['amount'] = $this->normalizeGoldAmount((float) ($data['amount'] ?? 0));
}
$data['total_price'] = $data['amount'] * $data['price']; $data['total_price'] = $data['amount'] * $data['price'];
$this->enforceProductOrderLimits($data, (float) $data['total_price']); $this->enforceProductOrderLimits($data, (float) $data['total_price']);
@@ -256,6 +269,7 @@ class SingleOrderController extends Controller
} else { } else {
$data['payment_amount'] = $data['total_price']; $data['payment_amount'] = $data['total_price'];
$data['amount'] -= $data['service_charge']; $data['amount'] -= $data['service_charge'];
$data['amount'] = $this->normalizeGoldAmount((float) $data['amount']);
} }
if ($targetCash !== null) { if ($targetCash !== null) {
@@ -441,7 +455,18 @@ class SingleOrderController extends Controller
$response = []; $response = [];
$config = app(ConfigService::class); $config = app(ConfigService::class);
$feeFromGold = $config->feeDeductFromGold(); $feeFromGold = $config->feeDeductFromGold();
$targetCash = $this->resolveTargetCash($data);
$data['price'] -= $data['difference']; $data['price'] -= $data['difference'];
if ($targetCash !== null && $feeFromGold) {
$price = (float) $data['price'];
$data['amount'] = $price > 0
? $this->normalizeGoldAmount($targetCash / $price)
: 0.0;
} else {
$data['amount'] = $this->normalizeGoldAmount((float) ($data['amount'] ?? 0));
}
$grossTotal = $data['amount'] * $data['price']; $grossTotal = $data['amount'] * $data['price'];
$this->enforceProductOrderLimits($data, (float) $grossTotal); $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'] = isset($data['fee_free']) && $data['fee_free'] === true ? 0 : $data['service_charge_percentage'] * $data['amount'];
@@ -456,7 +481,6 @@ class SingleOrderController extends Controller
$goldDebit = $data['amount']; $goldDebit = $data['amount'];
} }
$targetCash = $this->resolveTargetCash($data);
if ($targetCash !== null) { if ($targetCash !== null) {
$data['payment_amount'] = $targetCash; $data['payment_amount'] = $targetCash;
$data['total_price'] = $targetCash; $data['total_price'] = $targetCash;
@@ -893,6 +917,31 @@ class SingleOrderController extends Controller
return $target > 0 ? $target : null; return $target > 0 ? $target : null;
} }
private function normalizeGoldAmount(float $amount): float
{
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'])) {
@@ -0,0 +1,17 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
DB::statement('ALTER TABLE `orders` MODIFY `amount` DECIMAL(20,10) UNSIGNED NULL');
}
public function down(): void
{
DB::statement('ALTER TABLE `orders` MODIFY `amount` DOUBLE(15,6) UNSIGNED NULL');
}
};