예제 #1
0
        protected override DocumentInfo ComposeDocument(IUnitOfWork db,
                                                        long companyId,
                                                        MarketType market,
                                                        string marketplaceId,
                                                        IList <string> asinList)
        {
            var tag     = asinList != null && asinList.Count == 1 ? asinList[0] : null;
            var actions = _actionService.GetUnprocessedByType(db,
                                                              SystemActionType.UpdateOnMarketReturnOrder,
                                                              null,
                                                              tag);

            if (asinList != null)
            {
                actions = actions.Where(a => asinList.Contains(a.Tag)).ToList();
            }

            _actionOutputList = new Dictionary <long, ReturnOrderOutput>();

            if (actions.Any())
            {
                var orderMessages = new List <XmlElement>();
                var index         = 0;
                var merchant      = db.Companies.Get(companyId).AmazonFeedMerchantIdentifier;
                foreach (var action in actions)
                {
                    var data              = JsonConvert.DeserializeObject <ReturnOrderInput>(action.InputData);
                    var order             = db.Orders.GetByOrderNumber(data.OrderNumber);
                    var unsuitableData    = false;
                    var unsuitableMessage = "";
                    if (order == null)
                    {
                        unsuitableMessage = "Can't find orderId";
                        unsuitableData    = true;
                    }

                    if (data.Items == null)
                    {
                        unsuitableMessage = "No data items";
                        unsuitableData    = true;
                    }

                    if (data.Items != null && !data.Items.Any())
                    {
                        unsuitableMessage = "No return items";
                        unsuitableData    = true;
                    }

                    if (data.Items != null && !data.Items.Any(i => i.RefundShippingPrice > 0 || i.RefundItemPrice > 0))
                    {
                        unsuitableMessage = "All refund price = 0";
                        unsuitableData    = true;
                    }

                    if (unsuitableData)
                    {
                        Log.Info(marketplaceId + ": " + unsuitableMessage);
                        _actionService.SetResult(db, action.Id, SystemActionStatus.Suspended, new ReturnOrderOutput()
                        {
                            ResultMessage = unsuitableMessage
                        });
                        continue;
                    }

                    if (order.Market != (int)market ||
                        (!String.IsNullOrEmpty(marketplaceId) &&
                         order.MarketplaceId != marketplaceId))
                    {
                        //Log.Info("skip order=" + data.OrderNumber + ", market=" + order.Market + ", marketplace=" + order.MarketplaceId);
                        continue;
                    }

                    Log.Info(marketplaceId + ": add order " + index + ", actionId=" + action.Id + ", orderId=" + data.OrderNumber);

                    index++;

                    var returnItems = data.Items;
                    var orderItems  = db.OrderItems.GetAll().Where(oi => oi.OrderId == order.Id).ToList();
                    foreach (var returnItem in data.Items)
                    {
                        var existOrderItem = orderItems.FirstOrDefault(i => i.ItemOrderIdentifier == returnItem.ItemOrderId);
                        if (existOrderItem != null && returnItem.RefundItemPrice > 0 && existOrderItem.ItemPaid > 0 && existOrderItem.ItemTax > 0)
                        {
                            returnItem.RefundItemTax = (existOrderItem.ItemTax ?? 0) * returnItem.RefundItemPrice / (existOrderItem.ItemPaid.Value - existOrderItem.ItemTax.Value);
                        }
                    }

                    orderMessages.Add(FeedHelper.ComposeOrderAdjustmentMessage(index,
                                                                               data.OrderNumber,
                                                                               data.IncludeShipping,
                                                                               data.DeductShipping,
                                                                               data.IsDeductPrepaidLabelCost,
                                                                               order.TotalPriceCurrency,
                                                                               returnItems));

                    _actionOutputList.Add(new KeyValuePair <long, ReturnOrderOutput>(action.Id, new ReturnOrderOutput()
                    {
                        Identifier = index
                    }));
                }

                db.Commit(); //NOTE: Save SetResulte changes

                if (orderMessages.Any())
                {
                    Log.Info(marketplaceId + ": Compose feed");
                    var document = FeedHelper.ComposeFeed(orderMessages, merchant, Type.ToString());
                    return(new DocumentInfo
                    {
                        XmlDocument = document,
                        NodesCount = index
                    });
                }
            }
            return(null);
        }