fix some bugs

This commit is contained in:
2026-07-29 17:00:51 +03:30
parent dd77e2ce81
commit 16b947adc5
6 changed files with 138 additions and 44 deletions
@@ -392,8 +392,8 @@ class SingleOrderController extends Controller
private function processBuyTransactionProduct(&$data, $wallet, $wallet_interface)
{
$data['price'] = $data['product']['price'] + $data['product']['price_profit'];
$data['price'] += $data['difference'];
// همان قیمت پایهٔ سفارش (price_original / قیمت اپ) — نه ستون کهنهٔ products.price
$data['price'] = $this->resolveProductOrderUnitPrice($data) + (float) ($data['difference'] ?? 0);
$data['amount'] = $data['number'] * $data['product']['amount'];
// محصول تعدادی: کارمزد فقط از ریال (fee_service_charge_percentage)
$feeRate = isset($data['fee_free']) && $data['fee_free'] === true
@@ -662,8 +662,8 @@ class SingleOrderController extends Controller
private function processSellTransactionProduct(&$data, $wallet, $wallet_interface)
{
$data['price'] = $data['product']['price'] + $data['product']['price_profit'];
$data['price'] -= $data['difference'];
// همان قیمت پایهٔ سفارش (price_original / قیمت اپ) — نه ستون کهنهٔ products.price
$data['price'] = max(0.0, $this->resolveProductOrderUnitPrice($data) - (float) ($data['difference'] ?? 0));
$data['amount'] = $data['number'] * $data['product']['amount'];
// محصول تعدادی: کارمزد فقط از ریال
$feeRate = isset($data['fee_free']) && $data['fee_free'] === true
@@ -1118,6 +1118,27 @@ class SingleOrderController extends Controller
);
}
/**
* قیمت پایهٔ هر گرم/واحد برای سفارش محصول — اول price_original (زنده)، بعد DB.
*/
private function resolveProductOrderUnitPrice(array $data): float
{
$base = (float) ($data['price_original'] ?? 0);
if ($base > 0) {
return $base;
}
$product = $data['product'] ?? null;
if ($product instanceof Product) {
return (float) (($product->price ?? 0) + ($product->price_profit ?? 0));
}
if (is_array($product)) {
return (float) (($product['price'] ?? 0) + ($product['price_profit'] ?? 0));
}
return (float) ($data['price'] ?? 0);
}
private function resolveSellOrderCashRial(array $data): ?float
{
$targetCash = $this->resolveTargetCash($data);
@@ -1128,10 +1149,9 @@ class SingleOrderController extends Controller
if (!empty($data['product_id']) && isset($data['number'])) {
$product = $data['product'] ?? Product::find($data['product_id']);
if ($product) {
$unitPrice = (float) ($product->price + $product->price_profit);
$difference = (float) ($data['difference'] ?? 0);
$amount = (float) $data['number'] * (float) $product->amount;
$sellUnitPrice = max(0.0, $unitPrice - $difference);
$sellUnitPrice = max(0.0, $this->resolveProductOrderUnitPrice($data) - $difference);
return $amount * $sellUnitPrice;
}
@@ -1140,9 +1160,8 @@ class SingleOrderController extends Controller
if (isset($data['amount'])) {
$amount = (float) $data['amount'];
if ($amount > 0) {
$price = (float) ($data['price'] ?? 0);
$difference = (float) ($data['difference'] ?? 0);
$sellUnitPrice = max(0.0, $price - $difference);
$sellUnitPrice = max(0.0, $this->resolveProductOrderUnitPrice($data) - $difference);
return $amount * $sellUnitPrice;
}