fix zarin duplicate hesab and convert mesghal unit price to gram before cash/weight.

Reuse an existing Zarin hesab instead of a second addhesab, and stop rial-locked orders from treating mesghal as per-gram.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-29 13:50:12 +03:30
co-authored by Cursor
parent 6b5b490e2b
commit c1e2bb5e9b
3 changed files with 171 additions and 17 deletions
@@ -99,6 +99,7 @@ class SingleOrderController extends Controller
} else if (auth('user')->id()) {//admin
$data['service_charge_percentage'] = 0;
}
$this->ensureOrderUnitPriceIsPerGram($data);
if($data['price'] === 0){
throw new UnprocessableEntityHttpException(' سفارشات بسته میباشد.');
}
@@ -320,6 +321,7 @@ class SingleOrderController extends Controller
$feeRate = isset($data['fee_free']) && $data['fee_free'] === true
? 0.0
: (float) ($data['service_charge_percentage'] ?? 0);
$this->ensureOrderUnitPriceIsPerGram($data);
$data['price'] += $data['difference'];
if ($rialLocked) {
@@ -586,6 +588,7 @@ class SingleOrderController extends Controller
$feeRate = isset($data['fee_free']) && $data['fee_free'] === true
? 0.0
: (float) ($data['service_charge_percentage'] ?? 0);
$this->ensureOrderUnitPriceIsPerGram($data);
$data['price'] -= $data['difference'];
if ($rialLocked) {
@@ -1330,6 +1333,44 @@ class SingleOrderController extends Controller
return round($amount, 10);
}
/** مظنهٔ زندهٔ هر گرم (ریال) — برای تشخیص قیمت مثقال به‌جای گرم */
private function liveGramQuoteRial(): float
{
$base = (float) app(ProductDisplayPriceService::class)->resolveAppBasePrice();
if ($base > 0) {
return $base;
}
$montzar = (float) app(ConfigService::class)->get('montzar_live_price', 0);
if ($montzar > 0) {
return $montzar;
}
return (float) (Price::latest()->first()?->price ?? 0);
}
/**
* اگر قیمت واحد سفارش حدود قیمت مثقال باشد، به قیمت هر گرم برگردان.
* جلوگیری از ۱۰ میلیون تومان → ۱۰۵ سوت وقتی مثقال در فیلد «هر گرم» نشسته.
*/
private function ensureOrderUnitPriceIsPerGram(array &$data): void
{
$live = $this->liveGramQuoteRial();
$config = app(ConfigService::class);
$price = $config->gramUnitPriceIfMesghalQuoted((float) ($data['price'] ?? 0), $live);
if ($price <= 0 || abs($price - (float) ($data['price'] ?? 0)) < 1) {
return;
}
$data['price'] = $price;
if (isset($data['price_original'])) {
$data['price_original'] = $config->gramUnitPriceIfMesghalQuoted(
(float) $data['price_original'],
$live
);
}
}
private function resolveGoldPaymentProductId(array $data): ?int
{
if (!empty($data['payment_product_id'])) {