| | | 1 | | |
| | | 2 | | |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | |
| | | 5 | | public class ProductPriceItem |
| | | 6 | | { |
| | | 7 | | [JsonProperty("auto_action_enabled")] |
| | 4702 | 8 | | public string? AutoActionEnabled { get; set; } = "UNKNOWN"; |
| | | 9 | | |
| | | 10 | | [JsonProperty("auto_add_to_ozon_actions_list_enabled")] |
| | 4702 | 11 | | public string? AutoAddToOzonActionsListEnabled { get; set; } = "UNKNOWN"; |
| | | 12 | | |
| | | 13 | | [JsonProperty("currency_code")] |
| | 4702 | 14 | | public string? CurrencyCode { get; set; } = "RUB"; |
| | | 15 | | |
| | | 16 | | [JsonProperty("min_price", NullValueHandling = NullValueHandling.Ignore)] |
| | | 17 | | public string? MinPrice |
| | | 18 | | { |
| | 2351 | 19 | | get => _minPrice; |
| | 1 | 20 | | set => _minPrice = NormalizePriceString(value); |
| | | 21 | | } |
| | | 22 | | private string? _minPrice; |
| | | 23 | | |
| | | 24 | | [JsonProperty("min_price_for_auto_actions_enabled")] |
| | 2350 | 25 | | public bool MinPriceForAutoActionsEnabled { get; set; } |
| | | 26 | | |
| | | 27 | | [JsonProperty("net_price", NullValueHandling = NullValueHandling.Ignore)] |
| | | 28 | | public string? NetPrice |
| | | 29 | | { |
| | 2350 | 30 | | get => _netPrice; |
| | 0 | 31 | | set => _netPrice = NormalizePriceString(value); |
| | | 32 | | } |
| | | 33 | | private string? _netPrice; |
| | | 34 | | |
| | | 35 | | [JsonProperty("offer_id")] |
| | 4700 | 36 | | public string? OfferId { get; set; } |
| | | 37 | | |
| | | 38 | | [JsonProperty("old_price", NullValueHandling = NullValueHandling.Ignore)] |
| | | 39 | | public string? OldPrice |
| | | 40 | | { |
| | 2351 | 41 | | get => _oldPrice; |
| | 1 | 42 | | set => _oldPrice = NormalizePriceString(value); |
| | | 43 | | } |
| | | 44 | | private string? _oldPrice; |
| | | 45 | | |
| | | 46 | | [JsonProperty("price")] |
| | | 47 | | public string? Price |
| | | 48 | | { |
| | 2352 | 49 | | get => _price; |
| | 2352 | 50 | | set => _price = NormalizePriceString(value); |
| | | 51 | | } |
| | | 52 | | private string? _price; |
| | | 53 | | |
| | | 54 | | [JsonProperty("price_strategy_enabled")] |
| | 4702 | 55 | | public string? PriceStrategyEnabled { get; set; } = "UNKNOWN"; |
| | | 56 | | |
| | | 57 | | [JsonProperty("product_id")] |
| | 2350 | 58 | | public long ProductId { get; set; } |
| | | 59 | | |
| | | 60 | | [JsonProperty("quant_size")] |
| | 4702 | 61 | | public int QuantSize { get; set; } = 1; |
| | | 62 | | |
| | | 63 | | [JsonProperty("vat", NullValueHandling = NullValueHandling.Ignore)] |
| | | 64 | | public string? Vat |
| | | 65 | | { |
| | 2352 | 66 | | get => _vat; |
| | 2 | 67 | | set => _vat = NormalizeVat(value); |
| | | 68 | | } |
| | | 69 | | private string? _vat; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Нормализует строковое значение цены: обрезает пробелы, пустые строки переводит в null, |
| | | 73 | | /// заменяет отрицательные цены на null во избежание regex validation error в Ozon API. |
| | | 74 | | /// </summary> |
| | | 75 | | public static string? NormalizePriceString(string? val) |
| | | 76 | | { |
| | 2354 | 77 | | if (string.IsNullOrWhiteSpace(val)) return null; |
| | 2354 | 78 | | val = val.Trim(); |
| | | 79 | | // Если значение не парсится как неотрицательное число — отбрасываем |
| | 2354 | 80 | | if (double.TryParse(val.Replace(',', '.'), System.Globalization.NumberStyles.Float, System.Globalization.Culture |
| | | 81 | | { |
| | 2354 | 82 | | if (num < 0) return null; |
| | 2352 | 83 | | return num.ToString(System.Globalization.CultureInfo.InvariantCulture); |
| | | 84 | | } |
| | 1 | 85 | | return null; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Нормализует ставку НДС для Ozon API: допускаются только числа (regex "^\d+([\.,]\d+)?$"). |
| | | 90 | | /// При null, пустой строке, "null", "none" или процентах — приводит к корректному формату Ozon. |
| | | 91 | | /// </summary> |
| | | 92 | | public static string? NormalizeVat(string? vat) |
| | | 93 | | { |
| | 2 | 94 | | if (string.IsNullOrWhiteSpace(vat)) return null; |
| | 2 | 95 | | vat = vat.Trim().TrimEnd('%').Trim(); |
| | 2 | 96 | | if (vat.Equals("null", StringComparison.OrdinalIgnoreCase) || |
| | 2 | 97 | | vat.Equals("none", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 98 | | return null; |
| | | 99 | | |
| | 2 | 100 | | if (double.TryParse(vat.Replace(',', '.'), System.Globalization.NumberStyles.Float, System.Globalization.Culture |
| | | 101 | | { |
| | 2 | 102 | | if (num < 0) return null; |
| | | 103 | | // Ozon ставки: 0 -> "0", 10/20 -> "0.1"/"0.2" или исходный процент |
| | 3 | 104 | | if (num == 20) return "0.2"; |
| | 1 | 105 | | if (num == 10) return "0.1"; |
| | 1 | 106 | | return num.ToString(System.Globalization.CultureInfo.InvariantCulture); |
| | | 107 | | } |
| | 0 | 108 | | return null; |
| | | 109 | | } |
| | | 110 | | } |