private async Task MergeMattressProtectorsAsync(AbcMattressModel model, ProductAttribute pa, Product product) { var displayOrder = 40; var pam = (await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(product.Id)) .Where(pam => pam.ProductAttributeId == pa.Id) .FirstOrDefault(); var abcMattressEntry = _abcMattressEntryService.GetAbcMattressEntriesByModelId(model.Id) .Where(ame => pa.Name == $"Mattress Protector ({ame.Size})") .FirstOrDefault(); if (abcMattressEntry == null) { return; } var protectors = _abcMattressProtectorService.GetAbcMattressProtectorsBySize(abcMattressEntry.Size); if (pam == null && protectors.Any()) { var sizeAttrs = await GetSizeAttributesAsync(product, abcMattressEntry); pam = new ProductAttributeMapping() { ProductId = product.Id, ProductAttributeId = pa.Id, IsRequired = false, AttributeControlType = AttributeControlType.DropdownList, DisplayOrder = displayOrder, TextPrompt = "Mattress Protector", ConditionAttributeXml = $"<Attributes><ProductAttribute ID=\"{sizeAttrs.pam.Id}\"><ProductAttributeValue><Value>{sizeAttrs.pav.Id}</Value></ProductAttributeValue></ProductAttribute></Attributes>" }; await _productAttributeService.InsertProductAttributeMappingAsync(pam); } else if (pam != null && !protectors.Any()) { await _productAttributeService.DeleteProductAttributeMappingAsync(pam); } else if (pam != null) { await UpdatePamAsync(product, pam, abcMattressEntry); } if (!protectors.Any()) { return; } var existingMattressProtectors = (await _productAttributeService.GetProductAttributeValuesAsync(pam.Id)) .Where(pav => pav.ProductAttributeMappingId == pam.Id ); var newMattressProtectors = protectors.Select(np => np.ToProductAttributeValue( pam.Id )).OrderBy(mp => mp.PriceAdjustment).ToList(); ApplyDisplayOrder(newMattressProtectors); var toBeDeleted = existingMattressProtectors .Where(e => !newMattressProtectors.Any(n => n.Name == e.Name && n.DisplayOrder == e.DisplayOrder && n.PriceAdjustment == e.PriceAdjustment)); var toBeInserted = newMattressProtectors .Where(n => !existingMattressProtectors.Any(e => n.Name == e.Name && n.DisplayOrder == e.DisplayOrder && n.PriceAdjustment == e.PriceAdjustment)); toBeInserted.ToList().ForEach(async n => await _productAttributeService.InsertProductAttributeValueAsync(n)); toBeDeleted.ToList().ForEach(async e => await _productAttributeService.DeleteProductAttributeValueAsync(e)); }
public async Task <IList <YahooDetailRow> > GetYahooDetailRowsAsync(Order order) { var result = new List <YahooDetailRow>(); var pickupLineNumber = 1; var shippingLineNumber = 1; var orderItems = await _customOrderService.GetOrderItemsAsync(order.Id); foreach (var orderItem in orderItems) { int lineNumber = GetLineNumber(ref pickupLineNumber, ref shippingLineNumber, orderItem); var product = await _productService.GetProductByIdAsync(orderItem.ProductId); var productAbcDescription = await _productAbcDescriptionService.GetProductAbcDescriptionByProductIdAsync( orderItem.ProductId ); var storeUrl = (await _storeService.GetStoreByIdAsync(order.StoreId))?.Url; var warranty = await _customOrderService.GetOrderItemWarrantyAsync(orderItem); if (warranty != null) { // adjust price for item orderItem.UnitPriceExclTax -= warranty.PriceAdjustment; } (string code, decimal price)standardItemCodeAndPrice = GetCodeAndPrice(orderItem, product, productAbcDescription); result.Add(new YahooDetailRow( _settings.OrderIdPrefix, orderItem, lineNumber, product.Sku, standardItemCodeAndPrice.code, standardItemCodeAndPrice.price, product.Name, $"{storeUrl}{await _urlRecordService.GetSeNameAsync(product)}", await GetPickupStoreAsync(orderItem) )); lineNumber++; if (warranty != null) { result.Add(new YahooDetailRow( _settings.OrderIdPrefix, orderItem, lineNumber, standardItemCodeAndPrice.code, _warrantyService.GetWarrantySkuByName(warranty.Name), warranty.PriceAdjustment, warranty.Name, "", // no url for warranty line items await GetPickupStoreAsync(orderItem) )); lineNumber++; } var freeGift = orderItem.GetFreeGift(); if (freeGift != null) { var amg = _abcMattressGiftService.GetAbcMattressGiftByDescription(freeGift); if (amg == null) { throw new Exception($"Unable to match free gift named {freeGift}"); } result.Add(new YahooDetailRow( _settings.OrderIdPrefix, orderItem, lineNumber, "", // no item ID associated amg.ItemNo, 0.00M, // free item freeGift, "", // no url for free gifts await GetPickupStoreAsync(orderItem), -1 )); lineNumber++; } var mattressProtector = orderItem.GetMattressProtector(); if (mattressProtector != null) { var size = orderItem.GetMattressSize(); var amp = _abcMattressProtectorService.GetAbcMattressProtectorsBySize(size) .Where(p => p.Name == mattressProtector) .FirstOrDefault(); if (amp == null) { throw new Exception($"Unable to match mattress protector named {mattressProtector}"); } result.Add(new YahooDetailRow( _settings.OrderIdPrefix, orderItem, lineNumber, "", // no item ID associated amp.ItemNo, amp.Price, mattressProtector, "", // no url await GetPickupStoreAsync(orderItem) )); lineNumber++; } lineNumber = await ProcessFrameAsync(orderItem, lineNumber, result); SetLineNumber(ref pickupLineNumber, ref shippingLineNumber, orderItem, lineNumber); } return(result); }