public void CreateShipmentResult(CreateShipmentFile createShipmentFile)
        {
            DataTable dt = ExcelUtility.ConvertExcelFileToDataTable(createShipmentFile.File);

            List <ShipmentItem> shipmentItems = dt.FromDataTableToList <ShipmentItem>()
                                                .Where(w => w.Quantity > 0)
                                                .ToList();

            if (shipmentItems.Any(a => a.QuantityInCase.HasValue) && !shipmentItems.All(a => a.QuantityInCase.HasValue))
            {
                throw new InvalidOperationException("All, or none items in the shipment must have a QuantityInCase set.");
            }

            bool areCasesRequired = shipmentItems.Any(a => a.QuantityInCase.HasValue);

            CreateShipmentRequest createShipmentRequest = new CreateShipmentRequest(
                shipmentItems,
                createShipmentFile.ShipmentName,
                areCasesRequired);

            CreateShipmentResponse createShipmentResponse = m_mwsProductsApi.CreateInboundShipment(createShipmentRequest);

            using (ExcelPackage excel = new ExcelPackage())
            {
                foreach (CreatedShipment createdShipment in createShipmentResponse.CreatedShipments)
                {
                    string worksheetName = createdShipment.ShipmentId;

                    DataTable dtShipmentItems;

                    if (areCasesRequired)
                    {
                        dtShipmentItems = createdShipment.ItemsInShipment
                                          .Select(s => new { s.Name, s.ItemNumber, s.Asin, s.Quantity, s.Cost, s.QuantityInCase, Extension = s.Cost * s.Quantity })
                                          .ToDataTable();
                    }
                    else
                    {
                        dtShipmentItems = createdShipment.ItemsInShipment
                                          .Select(s => new { s.Name, s.ItemNumber, s.Asin, s.Quantity, s.Cost, Extension = s.Cost * s.Quantity })
                                          .ToDataTable();
                    }

                    ExcelWorksheet wsCreatedShipment = excel.Workbook.Worksheets.Add(worksheetName);
                    wsCreatedShipment.Cells["A1"].LoadFromDataTable(dtShipmentItems, true);
                }

                ExcelUtility.CreateExcelResponse(Response, excel, createShipmentFile.ShipmentName);
            }
        }
Ejemplo n.º 2
0
        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);
        }