private WarehouseQuantityChange[] ValidateQuantityInStock(AddressSelectedCommand command, RWarehouse[] warehouses, RWarehouse_Product_Mapping[] warehouseProductMappings, WorkingTime[] logisticsWorkingTimes, Holiday[] logisticsHolidayTimes) { IList <WarehouseQuantityChange> quantityChanges = new List <WarehouseQuantityChange>(); var cartItemsGroups = CartItems.GroupBy(p => $"ProductId:{p.ProductId}, SellPrice:{p.Price}"); var warehouseProductMappingsGroups = warehouseProductMappings.GroupBy(p => $"ProductId:{p.Id}, SellPrice:{p.SellPrice}").ToDictionary(p => p.Key, p => p.ToArray()); foreach (var cartItemsGroup in cartItemsGroups) { int quantityBuy = cartItemsGroup.Count(); EnumDefine.CartStatusEnum status; RWarehouse_Product_Mapping[] warehouseProductMappingsGroup = null; if (warehouseProductMappingsGroups.ContainsKey(cartItemsGroup.Key)) { warehouseProductMappingsGroup = warehouseProductMappingsGroups[cartItemsGroup.Key]; int quantityInStock = warehouseProductMappingsGroup.Sum(p => p.QuantityCanUse); status = quantityBuy > quantityInStock ? EnumDefine.CartStatusEnum.NotEnoughQuantity : EnumDefine.CartStatusEnum.EnoughInventory; } else { status = EnumDefine.CartStatusEnum.NotInStock; } if (status == EnumDefine.CartStatusEnum.EnoughInventory && warehouseProductMappingsGroup != null) { foreach (var warehouseProductMapping in warehouseProductMappingsGroup) { bool isSetShippingTime = false; foreach (var cartItem in cartItemsGroup) { var warehouseSelected = warehouses.FirstOrDefault(p => p.WarehouseId == warehouseProductMapping.WarehouseId); isSetShippingTime = GetShippingTimes(cartItem, warehouseSelected, warehouseProductMapping, command, logisticsWorkingTimes, logisticsHolidayTimes); if (isSetShippingTime) { cartItem.WarehouseSelected(warehouseSelected); } } if (!isSetShippingTime) { continue; } int quantityChange = 0; if (quantityBuy <= warehouseProductMapping.QuantityCanUse) { quantityChange = quantityBuy; quantityChanges.Add(new WarehouseQuantityChange(warehouseProductMapping.WarehouseId, warehouseProductMapping.ProductId, warehouseProductMapping.SellPrice, quantityChange)); break; } else { quantityChange = warehouseProductMapping.QuantityCanUse; quantityChanges.Add(new WarehouseQuantityChange(warehouseProductMapping.WarehouseId, warehouseProductMapping.ProductId, warehouseProductMapping.SellPrice, quantityChange)); } } } } return(quantityChanges.ToArray()); }
private void CreatePo() { var poGroups = CartItems.GroupBy(p => new { p.WarehouseId, p.ProductType }); int i = 0; foreach (var poGroup in poGroups) { foreach (var item in poGroup) { item.SetPoCode(i); } } }
public string Print() { //This business can be moved in a different object or structure but there is no request to make that new object reusable/extensible etc. Can be implemented here for now. //Structure => <categoryTitle , products in this category> var groupedProductsByCategory = CartItems .GroupBy(x => x.Value.Product.Category.Title) .ToDictionary(x => x.Key, x => x.ToList()); var builder = new StringBuilder(); builder.AppendLine($"{FormatInfoField("Category Name")}" + $"{FormatInfoField("Product Name")}" + $"{FormatInfoField("Quantity")}" + $"{FormatInfoField("Unit Price")}" + $"{FormatInfoField("TotalPrice")}" + $"{FormatInfoField("Campaign Discount")}"); if (groupedProductsByCategory.Any()) { foreach (var categoryGroup in groupedProductsByCategory) { var shoppingCartItems = categoryGroup.Value.Select(x => x.Value); foreach (var shoppingCartItem in shoppingCartItems) { builder.AppendLine($"{FormatInfoField(categoryGroup.Key)}" + $"{FormatInfoField(shoppingCartItem.Product.Title)}" + $"{FormatInfoField(shoppingCartItem.OrderQuantity.ToCurrencyString())}" + $"{FormatInfoField(shoppingCartItem.Product.UnitPrice.ToCurrencyString())}" + $"{FormatInfoField(shoppingCartItem.TotalPrice.ToCurrencyString())}" + $"{FormatInfoField(shoppingCartItem.BestCampaignDiscount.DiscountAmount.ToCurrencyString())}"); //In request, its wanted but we cannot add coupon discount here because a coupon can be applied on cart - not on each shopping item } } } else { builder.AppendLine($"{"No item in the cart.",80}"); } builder.AppendLine($"{Environment.NewLine}" + $"{FormatFooterField("Cart Total Amount", GetTotalAmount(), true)}" + $"{FormatFooterField("Cart Net Amount", GetTotalAmountAfterDiscounts(), true)}" + $"{FormatFooterField("Campaign Discounts", GetCampaignDiscount(), true)}" + $"{FormatFooterField("Coupon Discount", GetCouponDiscount(), true)}" + $"{FormatFooterField("Delivery Cost", DeliveryCostCalculator.CalculateFor(this), false)}"); return(builder.ToString()); }