using System; namespace Электронная_Фармация.Classes { public static class MarkupHelper { public const decimal FallbackMarkupPercent = 0m; /// /// Сумма доступных скидок. null / отрицательные значения считаются 0. /// public static decimal SumDiscountPercents(params decimal?[] parts) { decimal total = 0m; if (parts == null) { return total; } foreach (var part in parts) { if (part.HasValue && part.Value > 0m) { total += part.Value; } } return total; } public static decimal ResolveMarkupPercent(decimal? itemMarkup, decimal? clientMarkup, decimal fallbackMarkup) { var total = SumDiscountPercents(itemMarkup, clientMarkup); if (total > 0m) { return total; } return fallbackMarkup > 0m ? fallbackMarkup : FallbackMarkupPercent; } public static decimal ApplyMarkup(decimal basePrice, decimal markupPercent) { return ApplyDiscount(basePrice, markupPercent); } /// /// итоговая = base * (1 - discount/100), минимум 0. Без округления. /// public static decimal ApplyDiscount(decimal basePrice, decimal discountPercent) { var effectiveDiscount = Math.Max(0m, discountPercent); var result = basePrice * (1m - effectiveDiscount / 100m); return result < 0m ? 0m : result; } public static bool TryParseMarkup(object value, out decimal markupPercent) { markupPercent = 0m; if (value == null || value == DBNull.Value) { return false; } if (value is decimal decimalValue) { markupPercent = decimalValue; return true; } if (value is double doubleValue) { markupPercent = (decimal)doubleValue; return true; } if (value is float floatValue) { markupPercent = (decimal)floatValue; return true; } if (value is int intValue) { markupPercent = intValue; return true; } var text = value.ToString()?.Trim().Replace(',', '.'); return !string.IsNullOrWhiteSpace(text) && decimal.TryParse( text, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out markupPercent); } } }