public void LoadPhotos(BasketItemEntity entity) { if (!DataBaseContext.Entry(entity).Collection(c => c.CatalogItem.Photos).IsLoaded) { DataBaseContext.Entry(entity).Collection(c => c.CatalogItem.Photos).Load(); } }
private BasketItemEntity GetBasketEntity(CatalogItemEntity entity) { LoadBasket(entity); BasketItemEntity basketItem = entity.BasketItems.FirstOrDefault(x => x.Order == null); return(basketItem); }
private bool ReadNextBasketItem() { // Initialize if first time if (this.xmlBasketReader == null) { InitializeXmlReader(); } // Return null if we reached the end if (!this.xmlBasketReader.ReadToFollowing("item")) { this.currentBasketItem = null; return(false); } BasketItemEntity basketItem = new BasketItemEntity(); // Set the SKU this.xmlBasketReader.MoveToFirstAttribute(); basketItem.Sku = this.xmlBasketReader.Value; // Set the qty this.xmlBasketReader.MoveToNextAttribute(); decimal qty = 0; decimal.TryParse(this.xmlBasketReader.Value, out qty); basketItem.Qty = qty; // Update the current basket item this.currentBasketItem = basketItem; return(true); }
public BasketItem(BasketItemEntity entity, IDataService databaseService, IImageService imageService) { Entity = entity; this.databaseService = databaseService; this.imageService = imageService; CalculateSum(); }
public void CalculateOrderSum(BasketItemEntity basketItem) { OrderEntity order = basketItem?.Order; if (order != null) { LoadBasket(order); order.Sum = order.BasketItems.Sum(x => x.Count * x.CatalogItem.Price); DataBaseContext.SaveChanges(); } }
public LineItem GenerateDiscountLineItem(SkuEntity skuEntity, BasketItemEntity basketItem) { if (Utils.IsCurrentDateTimeWithinRange(skuEntity.SimpleDiscountEntity.EffectiveFromDate, skuEntity.SimpleDiscountEntity.EffectiveToDate)) { return(new LineItem { LineItemType = DiscountTypes.simpleDiscount, Label = skuEntity.SimpleDiscountEntity.Label, Sku = skuEntity.Id, Total = (skuEntity.SimpleDiscountEntity.DiscountedPrice - skuEntity.Price) * basketItem.Qty }); } return(null); }
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { this.xmlBasketReader.Dispose(); } this.xmlBasketReader = null; this.currentBasketItem = null; disposedValue = true; } }
/// <summary> /// Main basket items processing method. It's idemopotent. /// </summary> public void Checkout() { SkuEntity skuEntity; IBasketParser basketParser = BasketParser.Create(basketFilePath); receiptLineItems = new List <Entities.LineItem>(); // Iterate through each item in the basket while (basketParser.MoveNext()) { basketItem = basketParser.Current; skuEntity = this.catalogModel.Find(basketItem.Sku); if (skuEntity != null) { // Add regular line item receiptLineItems.Add(new LineItem { LineItemType = DiscountTypes.noDiscount, Label = skuEntity.Label, Price = skuEntity.Price, Qty = basketItem.Qty, Sku = skuEntity.Id, Total = basketItem.Qty * skuEntity.Price }); // Add discount line item (automatically determined by the appropriate discount strategy for the item) IDiscountStrategy discountStrategy = DiscountStrategy.Instance.Create(skuEntity.DiscountType); // Create the discount line item if any LineItem discountLineItem = discountStrategy.GenerateDiscountLineItem(skuEntity, basketItem); if (discountLineItem != null) { receiptLineItems.Add(discountLineItem); } } } // Reset discount strategies to get them ready for next basket checkout DiscountStrategy.Instance.Reset(); }
public LineItem GenerateDiscountLineItem(SkuEntity skuEntity, BasketItemEntity basketItem) { if (Utils.IsCurrentDateTimeWithinRange(skuEntity.GroupDiscountEntity.EffectiveFromDate, skuEntity.GroupDiscountEntity.EffectiveToDate)) { // We already gave the discount as customer already bought more than the group count. // This could change based on future requirements and business rules if (skuCounter.ContainsKey(skuEntity.Id)) { // First check if the group discount was already given for this item if (skuCounter[skuEntity.Id] == discountAlreadyGivenFlag) { return(null); } // Keep a count of items skuCounter[skuEntity.Id] = skuCounter[skuEntity.Id] + basketItem.Qty; } else { // Add the item for the first time to start tracking it its count skuCounter.Add(skuEntity.Id, basketItem.Qty); } // Now check if we've reached the group count required to give a discount. This should happen only once once. We don't want to keep giving discounts after it goes over 3 for example if (skuCounter[skuEntity.Id] >= skuEntity.GroupDiscountEntity.GroupCount) { skuCounter[skuEntity.Id] = discountAlreadyGivenFlag; return(new LineItem { LineItemType = DiscountTypes.groupDiscount, Label = skuEntity.GroupDiscountEntity.Label, Sku = skuEntity.Id, Total = (skuEntity.GroupDiscountEntity.GroupPrice - (skuEntity.GroupDiscountEntity.GroupCount * skuEntity.Price)) }); } } return(null); }
private void MergeItems(List <BasketItemEntity> items) { if (items != null && items.Any()) { int index = 0; while (index < items.Count) { BasketItemEntity item = items[index]; List <BasketItemEntity> dublicate = items.Where(t => t != item && t.CatalogItem.Id == item.CatalogItem.Id).ToList(); if (dublicate.Any()) { decimal commonCount = item.Count + dublicate.Sum(c => c.Count); item.Count = commonCount; items.RemoveAll(x => dublicate.Contains(x)); dublicate.ForEach(d => DataService.Delete(d)); } index++; } } }
public void SetCount(CatalogItemEntity entity, decimal count) { BasketItemEntity basketItem = GetBasketEntity(entity); if (count == 0) { if (basketItem != null) { Delete(basketItem); } } else { if (basketItem == null) { basketItem = new BasketItemEntity(); entity.BasketItems.Add(basketItem); } basketItem.Count = count; basketItem.DateAction = DateTimeOffset.Now; DataBaseContext.SaveChanges(); } }
public LineItem GenerateDiscountLineItem(SkuEntity skuEntity, BasketItemEntity basketItem) { return(null); }
public LineItem GenerateDiscountLineItem(SkuEntity skuEntity, BasketItemEntity basketItem) { if (Utils.IsCurrentDateTimeWithinRange(skuEntity.AddonDiscountEntity.EffectiveFromDate, skuEntity.AddonDiscountEntity.EffectiveToDate)) { decimal lastRemainderQtyNotGivenDiscount = 0; // Keep a tally on the remainder qty that a discount wasn't applied to because it wasn't sufficient if (skuCounter.ContainsKey(skuEntity.Id)) { lastRemainderQtyNotGivenDiscount = skuCounter[skuEntity.Id]; } else { skuCounter.Add(skuEntity.Id, 0); } // Add the total qty of the minimum to defined to qualify for a discount decimal totalAddonAndBase = skuEntity.AddonDiscountEntity.AddonCount + skuEntity.AddonDiscountEntity.BaseCount; // Calculate the ratio to apply the discount to decimal discountQtyApplyRatio = (skuEntity.AddonDiscountEntity.AddonCount / totalAddonAndBase); // Calculate the qty to apply the addon discount to decimal qualifyingQtyForDiscount = basketItem.Qty + lastRemainderQtyNotGivenDiscount; // Now apply the discount ratio to the qualified quantity decimal addonQtyToBeDiscounted = 0; decimal regularPrice = 0; decimal discountAmount = 0; decimal remainderNonQualified = 0; if (qualifyingQtyForDiscount >= totalAddonAndBase) { // Calculate the discount amount regularPrice = qualifyingQtyForDiscount * skuEntity.Price; addonQtyToBeDiscounted = discountQtyApplyRatio * qualifyingQtyForDiscount; discountAmount = -(addonQtyToBeDiscounted * skuEntity.Price * skuEntity.AddonDiscountEntity.AddonDiscountPercent / 100); // Remainder quantity not given discount so far so we count it on the next call remainderNonQualified = qualifyingQtyForDiscount - addonQtyToBeDiscounted; return(new LineItem { LineItemType = DiscountTypes.addonDiscount, Label = skuEntity.AddonDiscountEntity.Label, Sku = skuEntity.Id, Total = discountAmount }); } else { remainderNonQualified = qualifyingQtyForDiscount; } // Store the remainder that could not qualify for next time to accumulate and hopefully qualify again skuCounter[skuEntity.Id] = remainderNonQualified; } return(null); }