public static OrderHistoryViewModel GetByOrderId(IUnitOfWork db,
                                                         ILogService log,
                                                         IWeightService weightService,
                                                         string orderId)
        {
            DTOOrder order = null;

            if (!String.IsNullOrEmpty(orderId))
            {
                orderId = orderId.RemoveWhitespaces();
                var orderNumber = OrderHelper.RemoveOrderNumberFormat(orderId);
                var filter      = new OrderSearchFilter()
                {
                    EqualOrderNumber = orderNumber,
                    IncludeMailInfos = true,
                    IncludeNotify    = false,

                    UnmaskReferenceStyles = false,
                };

                order = db.ItemOrderMappings
                        .GetFilteredOrdersWithItems(weightService, filter)
                        .FirstOrDefault();
            }

            if (order == null)
            {
                return(null);
            }
            else
            {
                var anotherBuyerOrders = new List <OrderLinkViewModel>();
                if (!String.IsNullOrEmpty(order.BuyerEmail))
                {
                    anotherBuyerOrders = db.Orders.GetAll().Where(o => o.BuyerEmail == order.BuyerEmail)
                                         .ToList()
                                         .Where(o => o.Id != order.Id)
                                         .OrderByDescending(o => o.OrderDate)
                                         .Select(o => new OrderLinkViewModel()
                    {
                        OrderId       = o.Id,
                        OrderNumber   = o.AmazonIdentifier,
                        OrderDate     = o.OrderDate,
                        OrderStatus   = o.OrderStatus,
                        Market        = o.Market,
                        MarketplaceId = o.MarketplaceId
                    })
                                         .ToList();
                }

                var comments = db.OrderComments.GetByOrderIdDto(order.Id).ToList();

                var emails = db.Emails.GetAllWithOrder(new EmailSearchFilter()
                {
                    OrderId = orderId
                }).ToList();

                var returnRequests = db.ReturnRequests.GetAll()
                                     .OrderByDescending(r => r.CreateDate)
                                     .Select(r => new ReturnRequestDTO()
                {
                    OrderNumber      = r.OrderNumber,
                    ReceiveDate      = r.ReceiveDate,
                    ItemName         = r.ItemName,
                    CustomerComments = r.CustomerComments,
                    Details          = r.Details,
                    CreateDate       = r.CreateDate,
                    Reason           = r.Reason
                })
                                     .Where(r => r.OrderNumber == order.OrderId)
                                     .ToList();

                var refundRequests = RefundViewModel.GetByOrderId(db, order.OrderId);

                var labels = order.ShippingInfos
                             .Where(i => !String.IsNullOrEmpty(i.LabelPath)
                                    //|| i.LabelPurchaseResult != null //NOTE: in case when bag system shows user issue (keeped twice shippings with labels)
                                    )
                             .ToList();
                labels.AddRange(order.MailInfos);


                var address = order.GetAddressDto();

                var changes = db.OrderChangeHistories.GetByOrderIdDto(order.Id)
                              .ToList()
                              .OrderByDescending(ch => ch.ChangeDate)
                              .Select(ch => new OrderChangeViewModel(ch, emails))
                              .Where(ch => ch.ChangeType != OrderChangeTypes.None) //NOTE: Skipped empty
                              .ToList();

                changes.Add(OrderChangeViewModel.BuildCreateOrderChange(order));
                changes.AddRange(comments.Select(c => new OrderChangeViewModel(c)).ToList());
                changes.AddRange(emails.Select(e => new OrderChangeViewModel(e)).ToList());
                changes.AddRange(labels.SelectMany(l => OrderChangeViewModel.BuildChanges(l)).ToList());
                changes.AddRange(refundRequests.Select(r => new OrderChangeViewModel(r)).ToList());
                changes.AddRange(returnRequests.Select(r => new OrderChangeViewModel(r)).ToList());

                return(new OrderHistoryViewModel
                {
                    //Notes =  string.Format("{0} {1}", order.OrderId, itemsNotes),
                    OrderID = order.OrderId,
                    OrderEntityId = order.Id,
                    OrderDate = order.OrderDate,
                    Market = (MarketType)order.Market,
                    MarketplaceId = order.MarketplaceId,

                    WeightLb = (int)Math.Floor(order.WeightD / 16),
                    WeightOz = order.WeightD % 16,
                    TotalPrice = order.TotalPrice,
                    PriceCurrency = PriceHelper.FormatCurrency(order.TotalPriceCurrency),

                    Items = order.Items.Select(i => new OrderItemViewModel(i, false, false)).ToList(),
                    Changes = changes.OrderByDescending(c => c.ChangeDate).ToList(),

                    AnotherOrders = anotherBuyerOrders,

                    ToAddress = new AddressViewModel(address),
                });
            }
        }
Exemple #2
0
        public static OrderQuickSummaryViewModel GetByOrderId(IUnitOfWork db,
                                                              ILogService log,
                                                              IWeightService weightService,
                                                              string orderId)
        {
            DTOOrder order = null;

            if (!String.IsNullOrEmpty(orderId))
            {
                orderId = orderId.RemoveWhitespaces();
                var orderNumber = OrderHelper.RemoveOrderNumberFormat(orderId);
                var filter      = new OrderSearchFilter()
                {
                    EqualOrderNumber = orderNumber,
                    IncludeMailInfos = true,
                    IncludeNotify    = false,

                    UnmaskReferenceStyles = false,
                };

                order = db.ItemOrderMappings
                        .GetFilteredOrdersWithItems(weightService, filter)
                        .FirstOrDefault();
            }

            if (order == null)
            {
                return(null);
            }
            else
            {
                var refundRequestList = RefundViewModel.GetByOrderId(db, order.OrderId);

                var shippingPrice = 0M;

                var activeShippings = order.ShippingInfos.Where(i => i.IsActive).ToList();
                activeShippings.AddRange(order.MailInfos);
                activeShippings = activeShippings.OrderBy(sh => sh.LabelPurchaseDate).ToList();
                //if (order.MailInfos.Any(m => !m.LabelCanceled)
                //    && order.ShippingInfos.All(sh => String.IsNullOrEmpty(sh.LabelPath) || sh.LabelCanceled))
                //{
                //    var mailShipping = order.MailInfos.OrderBy(m => m.LabelPurchaseDate).FirstOrDefault(l => !l.LabelCanceled);
                //    activeShippings = new List<OrderShippingInfoDTO>() { mailShipping };
                //}
                //var mainActiveShipping = activeShippings.FirstOrDefault();
                //var shipmentProviderType = mainActiveShipping != null ? mainActiveShipping.ShipmentProviderType : (int)Core.Models.Settings.ShipmentProviderType.Stamps;
                var address = order.GetAddressDto();

                return(new OrderQuickSummaryViewModel()
                {
                    OrderID = order.OrderId,
                    OrderEntityId = order.Id,
                    OrderDate = order.OrderDate,
                    OrderStatus = order.OrderStatus,
                    Market = (MarketType)order.Market,
                    MarketplaceId = order.MarketplaceId,

                    OrderIsOnHold = order.OnHold,

                    WeightLb = (int)Math.Floor(order.WeightD / 16),
                    WeightOz = order.WeightD % 16,

                    TrackingNumbers = activeShippings.Where(t => !String.IsNullOrEmpty(t.TrackingNumber)).Select(t => new LabelViewModel()
                    {
                        TrackingNumber = t.TrackingNumber,
                        Carrier = t.ShippingMethod.CarrierName,
                        FromType = (LabelFromType)t.LabelFromType,
                        TrackingStatusSource = t.TrackingStateSource,
                        ShippingDate = t.ShippingDate,
                        EstDeliveryDate = t.EstimatedDeliveryDate,
                        ActualDeliveryDate = t.ActualDeliveryDate,
                        LastTrackingStateUpdateDate = t.LastTrackingRequestDate,
                        IsCanceled = t.CancelLabelRequested || t.LabelCanceled,
                        DeliveryStatusMessage = GetDeliveryStatus(order.LatestDeliveryDate,
                                                                  t.ActualDeliveryDate,
                                                                  t.TrackingStateDate,
                                                                  t.DeliveredStatus == (int)DeliveredStatusEnum.DeliveredToSender),
                        ShippingMethodName = t.ShippingMethod != null
                            ? ShippingUtils.PrepareMethodNameToDisplay(t.ShippingMethod.Name, t.DeliveryDaysInfo) : string.Empty,
                        ShippingProviderName = ShipmentProviderHelper.GetName((ShipmentProviderType)t.ShipmentProviderType),
                    }).ToList(),

                    PriceCurrency = PriceHelper.FormatCurrency(order.TotalPriceCurrency),

                    ExpectedShipDate = order.LatestShipDate,
                    ExpectedDeliveryDate = order.LatestDeliveryDate,

                    //ShippingMethodName = mainActiveShipping != null && mainActiveShipping.ShippingMethod != null
                    //    ? ShippingUtils.PrepareMethodNameToDisplay(mainActiveShipping.ShippingMethod.Name, mainActiveShipping.DeliveryDaysInfo) : string.Empty,
                    //ShippingProviderName = ShipmentProviderHelper.GetName((ShipmentProviderType)shipmentProviderType),
                    //ShippingDate = activeShippings.Where(i => i.IsActive).Max(i => i.ShippingDate),

                    //DeliveryDate = mainActiveShipping != null ? mainActiveShipping.ActualDeliveryDate : null,
                    //DeliveryStatus = GetDeliveryStatus(order.LatestDeliveryDate,
                    //    mainActiveShipping != null ? mainActiveShipping.ActualDeliveryDate : null,
                    //    mainActiveShipping != null ? mainActiveShipping.TrackingStateDate : null,
                    //    mainActiveShipping != null ? mainActiveShipping.DeliveredStatus == (int)DeliveredStatusEnum.DeliveredToSender : false),

                    Refunds = refundRequestList,

                    Items = order.Items.Select(i => new OrderItemViewModel(i, false, false)).ToList(),

                    ToAddress = address,
                });
            }
        }