| | | 1 | | using System.Globalization; |
| | | 2 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 3 | | |
| | | 4 | | namespace Services.ApiSupplierSettingsServices; |
| | | 5 | | |
| | | 6 | | /// <summary>Применяет коэффициент из настроек API-поставщика к себестоимости предложения. |
| | | 7 | | /// Исходная цена закупки поставщика (<see cref="PreOrderItem.Price"/>) и исходная цена API (<see cref="PreOrderItem.Sou |
| | | 8 | | /// намеренно остаются неизменными.</summary> |
| | | 9 | | public static class SupplierQuotationPriceMultiplier |
| | | 10 | | { |
| | 0 | 11 | | public const decimal MinValue = 0.0001m; |
| | 0 | 12 | | public const decimal MaxValue = 100m; |
| | | 13 | | |
| | 9 | 14 | | public static decimal Clamp(decimal value) => Math.Clamp(value, MinValue, MaxValue); |
| | | 15 | | |
| | | 16 | | public static void Apply(IEnumerable<PreOrderItem> items, decimal multiplier) |
| | | 17 | | { |
| | 20 | 18 | | foreach (var item in items) Apply(item, multiplier); |
| | 4 | 19 | | } |
| | | 20 | | |
| | | 21 | | public static void Apply(PreOrderItem item, decimal multiplier) |
| | | 22 | | { |
| | 5 | 23 | | var normalized = Clamp(multiplier); |
| | 5 | 24 | | item.PriceMultiplier = normalized; |
| | 8 | 25 | | if (normalized == 1m) return; |
| | | 26 | | |
| | 2 | 27 | | item.CostPrice = Multiply(item.CostPrice, normalized); |
| | | 28 | | // Форматированное значение часто дублирует CostPrice; сбрасываем его, чтобы UI показал |
| | | 29 | | // уже пересчитанное число, а не старую строку поставщика. |
| | 2 | 30 | | item.CostPriceFormatted = null; |
| | 2 | 31 | | } |
| | | 32 | | |
| | | 33 | | private static string? Multiply(string? value, decimal multiplier) |
| | | 34 | | { |
| | 2 | 35 | | if (!decimal.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out var parsed)) |
| | 0 | 36 | | return value; |
| | | 37 | | |
| | 2 | 38 | | return (parsed * multiplier).ToString("0.####", CultureInfo.InvariantCulture); |
| | | 39 | | } |
| | | 40 | | } |