public void Can_run_activity_recordpromotionusage() { var orderGroup = CreateCart(); var promotionId = Guid.NewGuid().ToString(); var memberId = Guid.NewGuid().ToString(); const string couponCode = "Test123"; const PromotionUsageStatus testStatus = PromotionUsageStatus.Reserved; var lineItemSkuReward = orderGroup.OrderForms[0].LineItems[0]; lineItemSkuReward.Discounts.Add(new LineItemDiscount() { DiscountAmount = 100, PromotionId = promotionId, DiscountCode = couponCode, LineItemId = lineItemSkuReward.LineItemId }); var customerSession = new Mock <ICustomerSessionService>(); customerSession.Setup(x => x.CustomerSession).Returns(() => new CustomerSession { CustomerId = memberId }); customerSession.SetupAllProperties(); var usages = new List <PromotionUsage>(); var mockUnitOfWork = new Mock <IUnitOfWork>(); var repository = new Mock <IMarketingRepository>(); repository.Setup(x => x.PromotionUsages).Returns(usages.AsQueryable); repository.Setup(x => x.UnitOfWork).Returns(mockUnitOfWork.Object); repository.Setup(x => x.Add(It.IsAny <PromotionUsage>())).Callback <PromotionUsage>(usages.Add); repository.SetupAllProperties(); var activity = new RecordPromotionUsageActivity(customerSession.Object, repository.Object) { UsageStatus = testStatus }; InvokeActivity(activity, orderGroup); var addedUsage = repository.Object.PromotionUsages.First(x => x.PromotionId == promotionId); Assert.True(addedUsage.Status == (int)testStatus, "Wrong promotion usage status"); Assert.True(addedUsage.CouponCode.Equals(couponCode), "Wrong promotion usage status"); }
/// <summary> /// Records the promotions. /// /// Step 1: Load the existing usage that is related to the current order (if any). /// Step 2: Record/update the usage of lineitem, order and shipment level discounts. /// /// The CustomerId can be taken from the Current Order.CustomerId. /// </summary> private void RecordPromotions() { List <Discount> discounts = new List <Discount>(); OrderGroup group = this.OrderGroup; // if the order has been just added, skip recording the discounts if (group.ObjectState == Mediachase.MetaDataPlus.MetaObjectState.Added) { return; } PromotionUsageStatus status = this.UsageStatus; foreach (OrderForm form in group.OrderForms) { // Add order level discounts foreach (Discount discount in form.Discounts) { discounts.Add(discount); } // Add lineitem discounts foreach (LineItem item in form.LineItems) { foreach (Discount discount in item.Discounts) { discounts.Add(discount); } } // Add shipping discounts foreach (Shipment shipment in form.Shipments) { foreach (Discount discount in shipment.Discounts) { discounts.Add(discount); } } } // Load existing usage Dto for the current order PromotionUsageDto usageDto = PromotionManager.GetPromotionUsageDto(0, Guid.Empty, group.OrderGroupId); // Clear all old items first if (usageDto.PromotionUsage.Count > 0) { foreach (PromotionUsageDto.PromotionUsageRow row in usageDto.PromotionUsage) { row.Delete(); } } // Now process the discounts foreach (Discount discount in discounts) { // we only record real discounts that exist in our database if (discount.DiscountId <= 0) { continue; } PromotionUsageDto.PromotionUsageRow row = usageDto.PromotionUsage.NewPromotionUsageRow(); row.CustomerId = group.CustomerId; row.LastUpdated = DateTime.UtcNow; row.OrderGroupId = group.OrderGroupId; row.PromotionId = discount.DiscountId; row.Status = status.GetHashCode(); row.Version = 1; // for now version is always 1 usageDto.PromotionUsage.AddPromotionUsageRow(row); } // Save the promotion usage PromotionManager.SavePromotionUsage(usageDto); }