fix amount 5

This commit is contained in:
2026-06-09 12:43:41 +03:30
parent a8dd0071e2
commit e83afb89d5
4 changed files with 122 additions and 45 deletions
+56 -4
View File
@@ -280,12 +280,64 @@ class Controller extends BaseController
return self::productSellTitle(self::metalTypeLabel());
}
/** واحد وزن معاملات فلز اصلی — مس: کیلوگرم، طلا: گرم */
/** واحد وزن معاملات فلز — از ستینگ unit؛ پیش‌فرض گرم */
public static function tradeWeightUnit(): string
{
$label = self::metalTypeLabel();
return self::resolveTradeWeightUnit(
Setting::where('key', 'unit')->value('value')
);
}
return str_contains($label, 'مس') ? 'کیلوگرم' : 'گرم';
public static function resolveTradeWeightUnit(?string $value): string
{
$unit = trim((string) $value);
if ($unit === 'کیلوگرم' || $unit === 'kilogram' || $unit === 'kg') {
return 'کیلوگرم';
}
return 'گرم';
}
public static function weightToGrams(float $value, ?string $fromUnit = null): float
{
$from = $fromUnit ?? self::tradeWeightUnit();
return $from === 'کیلوگرم' ? $value * 1000 : $value;
}
public static function gramsToTradeUnit(float $grams): float
{
return self::tradeWeightUnit() === 'کیلوگرم' ? $grams / 1000 : $grams;
}
public static function productStorageWeightUnit($product): string
{
$unit = trim((string) (is_object($product) ? ($product->unit ?? '') : ($product['unit'] ?? '')));
if (self::isWeightBasedProductUnit($unit)) {
return $unit;
}
return 'گرم';
}
public static function productLineWeightToGrams($product, $quantity): float
{
$groupType = is_object($product) ? ($product->group_type ?? '') : ($product['group_type'] ?? '');
if ((string) $groupType === 'سکه') {
return 0.0;
}
$qty = (float) $quantity;
$unit = trim((string) (is_object($product) ? ($product->unit ?? '') : ($product['unit'] ?? '')));
$amount = (float) (is_object($product) ? ($product->amount ?? 0) : ($product['amount'] ?? 0));
if (self::isWeightBasedProductUnit($unit)) {
return self::weightToGrams($qty, $unit);
}
return self::weightToGrams($amount * $qty, self::productStorageWeightUnit($product));
}
public static function isWeightBasedProductUnit(?string $unit): bool
@@ -330,7 +382,7 @@ class Controller extends BaseController
}
$group = trim((string) ($product['group_type'] ?? ''));
if (in_array($group, ['مفتول مس', 'کاتد مس'], true)) {
if (in_array($group, ['مفتول مس'], true)) {
return true;
}