fixed عدهف

This commit is contained in:
2026-06-22 21:29:25 +03:30
parent 6ebd02beb8
commit e8d17c1c04
4 changed files with 136 additions and 20 deletions
+81 -1
View File
@@ -416,6 +416,86 @@ class Controller extends BaseController
return in_array($group, ['سکه', 'گوی', 'مکعب', 'شمش', 'سکه پارسیان'], true);
}
public static function productUnitPayload($product): array
{
if (!$product) {
return [];
}
if (is_array($product)) {
return $product;
}
return [
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
'group_type' => $product->group_type ?? null,
'amount' => $product->amount ?? null,
'trade_by_quantity' => $product->trade_by_quantity ?? null,
];
}
public static function convertWeight(float $value, ?string $fromUnit, ?string $toUnit): float
{
$from = trim((string) ($fromUnit ?? ''));
$to = trim((string) ($toUnit ?? ''));
if ($from === '' || $to === '' || $from === $to) {
return $value;
}
$grams = self::weightToGrams($value, $from);
return $to === 'کیلوگرم' ? $grams / 1000 : $grams;
}
/**
* مقدار و واحد نمایشی تراکنش — منبع حقیقت: unit محصول در ادمین.
* معامله مقداری + وزن دارد: تعداد ذخیره‌شده × وزن هر واحد.
*/
public static function resolveTransactionDisplayGoldAndUnit(?array $payload, float $gold, bool $onMainWallet = false): array
{
if ($payload === []) {
return [$gold, self::tradeWeightUnit()];
}
$sign = $gold < 0 ? -1 : 1;
$absGold = abs($gold);
if (
self::usesQuantityTrade($payload)
&& self::hasProductUnitWeight($payload)
&& self::isWeightBasedProductUnit($payload['unit'] ?? null)
) {
$factor = (float) ($payload['amount'] ?? 1);
return [
round($absGold * $factor, 3) * $sign,
trim((string) $payload['unit']),
];
}
$unit = self::productQuantityUnit($payload);
$productWeightUnit = self::isWeightBasedProductUnit($payload['unit'] ?? null)
? trim((string) $payload['unit'])
: null;
if ($onMainWallet && $productWeightUnit) {
$tradeUnit = self::tradeWeightUnit();
if ($productWeightUnit !== $tradeUnit) {
return [
round(self::convertWeight($absGold, $tradeUnit, $productWeightUnit), 3) * $sign,
$productWeightUnit,
];
}
return [$gold, $productWeightUnit];
}
return [$gold, $unit];
}
public static function productQuantityUnit(?array $product): string
{
$unit = trim((string) ($product['unit'] ?? ''));
@@ -427,7 +507,7 @@ class Controller extends BaseController
return 'عدد';
}
return 'گرم';
return self::tradeWeightUnit();
}
/** مطابق ادمین: وزن «دارد» => amount > 0 ؛ «ندارد» => amount = 0 */