public ActionResult EvaluateCalculate(ProductProfitabilityRequest productProfitabilityRequest) { if (productProfitabilityRequest.CostPerPoundToShip.HasValue && productProfitabilityRequest.FlatCostToShip.HasValue) { throw new InvalidOperationException("Cannot have both a CostPerPoundToShip and FlatCostToShip"); } Func <decimal, decimal> funcShippingCost = null; if (productProfitabilityRequest.CostPerPoundToShip.HasValue) { funcShippingCost = weight => weight * productProfitabilityRequest.CostPerPoundToShip.Value; } else if (productProfitabilityRequest.FlatCostToShip.HasValue) { funcShippingCost = weight => productProfitabilityRequest.FlatCostToShip.Value; } DimensionContainer dimensionContainer = new DimensionContainer( productProfitabilityRequest.Length, productProfitabilityRequest.Width, productProfitabilityRequest.Height, productProfitabilityRequest.Weight); decimal profitMargin = FeeStructureUtility.CalculateProfitMargin( dimensionContainer, productProfitabilityRequest.FeeCategory, productProfitabilityRequest.LowestPrice, productProfitabilityRequest.PurchasePrice, funcShippingCost); decimal breakEven = FeeStructureUtility.CalculateBreakEven( dimensionContainer, productProfitabilityRequest.FeeCategory, productProfitabilityRequest.PurchasePrice, funcShippingCost); ProductProfitabilityResponse productProfitabilityViewModel = new ProductProfitabilityResponse { BreakEvenPrice = breakEven, ProfitMargin = profitMargin }; return(Json(productProfitabilityViewModel)); }
public void EvaluatePriceList(PriceListFile priceListFile) { DataTable dt = ExcelUtility.ConvertExcelFileToDataTable(priceListFile.File); List <ManufacturerPriceListRowInput> manufacturerPriceListRowsInputs = dt.FromDataTableToList <ManufacturerPriceListRowInput>(); // Price is always required. if (manufacturerPriceListRowsInputs.Any(a => !a.Price.HasValue)) { throw new ArgumentException("Price must be set for all rows."); } // Need either UPC or ASIN. if (manufacturerPriceListRowsInputs.Any(a => a.ASIN == null && a.UPC == null)) { throw new ArgumentException("ASIN or UPC must be set for all rows."); } List <ManufacturerPriceListRowOutput> manufacturerPriceListRowsOutputs = new List <ManufacturerPriceListRowOutput>(); // Only lookup rows where either ASIN or UPC is set. foreach (ManufacturerPriceListRowInput manufacturerPriceListRow in manufacturerPriceListRowsInputs .Where(w => !w.ASIN.IsNullOrEmptyTrimmed() || !w.UPC.IsNullOrEmptyTrimmed())) { LookupProductRequest lookupProductRequest = new LookupProductRequest { SearchIndex = priceListFile.SearchIndex, ResponseGroup = ResponseGroup.Medium }; if (manufacturerPriceListRow.ASIN != null) { lookupProductRequest.IdType = IdType.ASIN; lookupProductRequest.ItemId = manufacturerPriceListRow.ASIN; } else { lookupProductRequest.IdType = IdType.UPC; lookupProductRequest.ItemId = manufacturerPriceListRow.UPC; } LookupProductResponse lookupProductResponse = m_productAdvertisingApi.LookupProduct(lookupProductRequest); if (lookupProductResponse != null) { Product product = lookupProductResponse.Product; Listing listing = m_amazonApiClient.GetAllListingsForAsin(new[] { product.ASIN }).FirstOrDefault(); decimal?breakEven = null; decimal?profitMargin = null; decimal?lowestPrice = null; string sellersRankCategory = null; if (listing != null) { lowestPrice = listing.LowestPrice; sellersRankCategory = listing.SellersRankCategory; } if (product.Length.HasValue && product.Width.HasValue && product.Height.HasValue && product.Weight.HasValue) { DimensionContainer dimensionContainer = new DimensionContainer( product.Length.Value, product.Width.Value, product.Height.Value, product.Weight.Value); breakEven = FeeStructureUtility.CalculateBreakEven( dimensionContainer, priceListFile.FeeCategory, manufacturerPriceListRow.Price.Value); if (lowestPrice.HasValue && lowestPrice.Value > 0) { profitMargin = FeeStructureUtility.CalculateProfitMargin( dimensionContainer, priceListFile.FeeCategory, lowestPrice.Value, manufacturerPriceListRow.Price.Value); } } ManufacturerPriceListRowOutput manufacturerPriceListRowOutput = new ManufacturerPriceListRowOutput { ASIN = product.ASIN, UPC = manufacturerPriceListRow.UPC, ProductName = product.Name, SellersRank = lookupProductResponse.ProductMetadata.SalesRank, SellersRankCategory = sellersRankCategory, Category = lookupProductResponse.ProductMetadata.ProductGroup, Price = manufacturerPriceListRow.Price.Value, BreakEven = breakEven.HasValue ? breakEven.Value.ToString("F", CultureInfo.InvariantCulture) : null, LowestPrice = lowestPrice.HasValue ? lowestPrice.Value.ToString("F", CultureInfo.InvariantCulture) : null, ProfitMargin = profitMargin.HasValue ? string.Format("{0:0.00%}", profitMargin.Value) : null }; manufacturerPriceListRowsOutputs.Add(manufacturerPriceListRowOutput); } } ExcelUtility.WriteExcelFileToResponse(Response, manufacturerPriceListRowsOutputs, c_worksheetName, priceListFile.File.FileName); }