Exemple #1
0
        protected override DocumentInfo ComposeDocument(IUnitOfWork db,
                                                        long companyId,
                                                        MarketType market,
                                                        string marketplaceId,
                                                        IList <string> asinList)
        {
            Log.Info("Get orders");
            var shippingMethodList = db.ShippingMethods.GetAllAsDto().ToList();
            var fromDate           = Time.GetAppNowTime().AddDays(-7);

            var shippingLabels = db.OrderShippingInfos.GetOrdersToFulfillAsDTO(market, marketplaceId)
                                 .Where(sh => sh.ShippingDate > fromDate).ToList();
            var shippingInfoIds = shippingLabels.Select(i => i.Id).ToList();

            Shippings      = db.OrderShippingInfos.GetFiltered(sh => shippingInfoIds.Contains(sh.Id)).ToList();
            shippingLabels = PrepareOrderShippings(db, shippingLabels);

            var mailLabels = db.MailLabelInfos.GetInfosToFulfillAsDTO(market, marketplaceId)
                             .Where(sh => sh.ShippingDate > fromDate).ToList();
            var mailInfoIds = mailLabels.Select(i => i.Id).ToList();

            MailInfoes = db.MailLabelInfos.GetFiltered(m => mailInfoIds.Contains(m.Id)).ToList();
            mailLabels = PrepareMailShippings(db, mailLabels);

            var allShippings = shippingLabels;

            allShippings.AddRange(mailLabels);

            if (asinList != null)
            {
                allShippings = allShippings.Where(sh => asinList.Contains(sh.AmazonIdentifier)).ToList();
            }

            if (allShippings.Any())
            {
                var orderMessages = new List <XmlElement>();
                var index         = 0;
                var merchant      = db.Companies.Get(companyId).AmazonFeedMerchantIdentifier;
                foreach (var shipping in allShippings)
                {
                    index++;
                    Log.Info("add order " + index + ", shippingId=" + shipping.Id);

                    var shippingMethod = shippingMethodList.FirstOrDefault(m => m.Id == shipping.ShippingMethodId);

                    string shippingService = String.Empty;
                    if (string.IsNullOrEmpty(shipping.ShippingService)) //NOTE: this options using Mail Labels, todo: make join
                    {
                        if (shippingMethod != null)
                        {
                            shippingService = shippingMethod.Name;
                        }
                        Log.Info("shipping service=" + shippingService);
                    }
                    else
                    {
                        shippingService = shipping.ShippingService;
                    }

                    OrderHelper.PrepareSourceItemOrderId(shipping.Items);
                    shipping.Items = OrderHelper.GroupBySourceItemOrderId(shipping.Items);

                    var carrier = shippingMethod?.CarrierName;
                    if (!String.IsNullOrEmpty(shipping.CustomCurrier))
                    {
                        carrier = shipping.CustomCurrier;
                    }

                    orderMessages.Add(FeedHelper.ComposeOrderFulfillmentMessage(index,
                                                                                shipping.AmazonIdentifier,
                                                                                shipping.ShippingDate,
                                                                                shipping.OrderDate,
                                                                                ShippingUtils.FormattedToMarketShippingService(shippingService, shippingMethod.IsInternational),
                                                                                shippingMethod != null ? ShippingUtils.FormattedToMarketCurrierName(carrier, shippingMethod.IsInternational, market) : String.Empty,
                                                                                shipping.TrackingNumber,
                                                                                shipping.Items));

                    if (shipping.IsFromMailPage)
                    {
                        MailInfoes.First(m => m.Id == shipping.Id).MessageIdentifier = index;
                    }
                    else
                    {
                        Shippings.First(o => o.Id == shipping.Id).MessageIdentifier = index;
                    }
                }
                Log.Info("Compose feed");
                var document = FeedHelper.ComposeFeed(orderMessages, merchant, Type.ToString());
                return(new DocumentInfo
                {
                    XmlDocument = document,
                    NodesCount = index
                });
            }
            return(null);
        }