Apply available buyer/price-list/region discounts to summary.price on download, keep consignee carts separate, and harden SQLite startup/migrations. Co-authored-by: Cursor <cursoragent@cursor.com>
99 lines
2.9 KiB
C#
99 lines
2.9 KiB
C#
using System;
|
||
|
||
namespace Электронная_Фармация.Classes
|
||
{
|
||
public static class MarkupHelper
|
||
{
|
||
public const decimal FallbackMarkupPercent = 0m;
|
||
|
||
/// <summary>
|
||
/// Сумма доступных скидок. null / отрицательные значения считаются 0.
|
||
/// </summary>
|
||
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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// итоговая = base * (1 - discount/100), минимум 0. Без округления.
|
||
/// </summary>
|
||
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);
|
||
}
|
||
}
|
||
}
|