public static IList <CustomItemInfo> BuildCustomLines(IList <DTOOrderItem> items, double orderWeightOz, string defaultCustomType, bool oneLine = false) { var nonAdjustedWeight = items.Sum(i => i.Weight * i.Quantity); var weightDelta = nonAdjustedWeight - orderWeightOz; var itemCount = items.Sum(i => i.Quantity); var adjustingWeight = weightDelta / itemCount; //1 / (float)items.Count; //Adjustment items weight / convert prices foreach (var item in items) { item.ItemPrice = PriceHelper.RougeConvertToUSD(item.ItemPriceCurrency, item.ItemPrice); item.Weight -= adjustingWeight; } ShippingUtils.FixUpItemPrices(items); var lines = CustomsHelper.PrepareLines(items); var results = lines.Select(i => new CustomItemInfo { Title = StringHelper.GetFirstNotEmpty(i.ItemStyle, defaultCustomType), Quantity = i.Quantity, ItemPrice = i.ItemPrice, PerItemPrice = i.Quantity != 0 ? PriceHelper.RoundToTwoPrecision(i.ItemPrice / i.Quantity) : 0, Weight = (decimal)i.Weight, SKU = i.SKU, Country = "US", }).ToArray(); if (oneLine) { results = results.GroupBy(r => r.Title).Select(r => new CustomItemInfo() { Title = r.Key, Quantity = r.Sum(i => i.Quantity), ItemPrice = r.Sum(i => i.ItemPrice), PerItemPrice = r.Sum(i => i.Quantity) != 0 ? PriceHelper.RoundToTwoPrecision(r.Sum(i => i.ItemPrice) / r.Sum(i => i.Quantity)) : 0, Weight = r.Sum(i => i.Weight), SKU = r.FirstOrDefault()?.SKU, Country = r.FirstOrDefault()?.Country }).ToArray(); } return(results); }
public static void FixUpItemPrices(IList <DTOOrderItem> items) { var totalPrice = items.Sum(i => i.ItemPrice * i.Quantity); var totalQuantity = items.Sum(i => i.Quantity); //TASK: max $18 + $6 x item (for each item more then 3) //TASK: #2 taxes when merchandise is over 20 CAD. Right now we make sure it’s less 20 USD. //We need to change it.Assume that 1US = 1.4, so max value on invoice should be 14.3 USD. var random = new Random(DateTime.Now.Millisecond); var maxBasePrice = PriceHelper.RoundToTwoPrecision(14.3M - (decimal)random.NextDouble() / 3M); var maxTotalPrice = maxBasePrice + Math.Max(0, totalQuantity - 3) * 6; if (totalPrice > maxTotalPrice) { decimal priceCorrection = maxTotalPrice / totalPrice; foreach (var item in items) { item.ItemPrice = PriceHelper.RoundToTwoPrecision(priceCorrection * item.ItemPrice); } } }