public void TestGetOneRateRates(string orderId)
        {
            var weightService  = new WeightService();
            var companyAddress = new CompanyAddressService(_company);

            using (var db = _dbFactory.GetRWDb())
            {
                var order                 = db.ItemOrderMappings.GetOrderWithItems(weightService, orderId, unmaskReferenceStyle: false, includeSourceItems: true);
                var shippingService       = ShippingUtils.InitialShippingServiceIncludeUpgrade(order.InitialServiceType, order.UpgradeLevel); //order.ShippingService
                var orderItemInfoes       = OrderHelper.BuildAndGroupOrderItems(order.Items);
                var sourceOrderItemInfoes = OrderHelper.BuildAndGroupOrderItems(order.SourceItems);

                var serviceFactory = new ServiceFactory();

                var rateProviders = serviceFactory.GetShipmentProviders(_log,
                                                                        _time,
                                                                        _dbFactory,
                                                                        weightService,
                                                                        _company.ShipmentProviderInfoList,
                                                                        AppSettings.DefaultCustomType,
                                                                        AppSettings.LabelDirectory,
                                                                        AppSettings.LabelDirectory,
                                                                        AppSettings.LabelDirectory);

                var fedexRateProvider = rateProviders.FirstOrDefault(r => r.Type == ShipmentProviderType.FedexOneRate);

                var rates = fedexRateProvider.GetLocalRate(
                    companyAddress.GetReturnAddress(order.GetMarketId()),
                    companyAddress.GetPickupAddress(order.GetMarketId()),
                    order.GetAddressDto(),
                    _time.GetAppNowTime(),
                    order.WeightD,
                    null,
                    order.IsInsured ? order.TotalPrice : 0,
                    order.IsSignConfirmation,
                    new OrderRateInfo()
                {
                    ShippingService    = shippingService,
                    InitialServiceType = order.InitialServiceType,
                    OrderNumber        = order.OrderId,
                    Items       = orderItemInfoes,
                    SourceItems = sourceOrderItemInfoes,
                    TotalPrice  = order.TotalPrice,
                    Currency    = order.TotalPriceCurrency,
                },
                    RetryModeType.Normal);


                _log.Info("Rates: " + rates.Rates.Count);
            }
        }
        public void PurchaseAmazonNextDay()
        {
            using (var db = _dbFactory.GetRWDb())
            {
                var toPrintNextDayOrderIds = (from o in db.Orders.GetAll()
                                              join sh in db.OrderShippingInfos.GetAll()
                                              on o.Id equals sh.OrderId
                                              where o.OrderStatus == OrderStatusEnumEx.Unshipped &&
                                              o.Market == (int)MarketType.Amazon &&
                                              !o.BatchId.HasValue &&
                                              o.InitialServiceType == ShippingUtils.NextDayServiceName
                                              //&& sh.IsActive
                                              && sh.ShippingMethodId == (int)ShippingUtils.AmazonExpressFlatShippingMethodId
                                              select o.Id).ToList();

                _log.Info("PurchaseAmazonNextDay. Amazon NextDay orders, count=" + toPrintNextDayOrderIds.Count);

                if (toPrintNextDayOrderIds.Count > 0) //NOTE: print only when a lot of overdue
                {
                    var orderIdList = toPrintNextDayOrderIds.Distinct().ToList();

                    foreach (var orderId in orderIdList)
                    {
                        var order           = db.ItemOrderMappings.GetOrderWithItems(_weightService, orderId, false, false, false);
                        var orderItemInfoes = OrderHelper.BuildAndGroupOrderItems(order.Items);
                        var isSupportFlat   = ShippingServiceUtils.IsSupportFlatEnvelope(orderItemInfoes);

                        if (!isSupportFlat)
                        {
                            _log.Info("Not support flat");
                            continue;
                        }

                        var orderShippings = db.OrderShippingInfos.GetAll().Where(o => o.OrderId == orderId).ToList();
                        if (orderShippings.Where(sh => sh.ShippingMethodId == ShippingUtils.AmazonExpressFlatShippingMethodId &&
                                                 sh.IsActive).Count() == 0)
                        {
                            _log.Info("Set active shipping method to USPS Express Flat");
                            orderShippings.ForEach(sh =>
                            {
                                sh.IsActive  = sh.ShippingMethodId == ShippingUtils.AmazonExpressFlatShippingMethodId;
                                sh.IsVisible = sh.ShippingMethodId == ShippingUtils.AmazonExpressFlatShippingMethodId;
                            });
                            db.Commit();
                        }

                        _emailService.SendSystemEmailToAdmin("Auto-print Amazon Next Day order " + orderId, "");

                        var result = _labelBatchService.PrintLabel(orderId, _companyId, null);
                        if (result.Success)
                        {
                            _log.Info("IsForceVisible=true");
                            var dbOrder = db.Orders.Get(orderId);
                            dbOrder.IsForceVisible = true;
                            db.Commit();
                        }
                        else
                        {
                            _emailService.SendSystemEmailToAdmin("Issue with auto-print Amazon NextDay order " + orderId, "");
                        }

                        _log.Info("PurchaseAmazonNextDay, printed orderId=" + orderId + ", result=" + result.Success);
                    }
                }
            }
        }
        public void TestGetLabel(string orderId)
        {
            var weightService  = new WeightService();
            var companyAddress = new CompanyAddressService(_company);

            using (var db = _dbFactory.GetRWDb())
            {
                var order                 = db.ItemOrderMappings.GetOrderWithItems(weightService, orderId, unmaskReferenceStyle: false, includeSourceItems: true);
                var shippingService       = ShippingUtils.InitialShippingServiceIncludeUpgrade(order.InitialServiceType, order.UpgradeLevel); //order.ShippingService
                var orderItemInfoes       = OrderHelper.BuildAndGroupOrderItems(order.Items);
                var sourceOrderItemInfoes = OrderHelper.BuildAndGroupOrderItems(order.SourceItems);

                var serviceFactory = new ServiceFactory();

                var rateProviders = serviceFactory.GetShipmentProviders(_log,
                                                                        _time,
                                                                        _dbFactory,
                                                                        weightService,
                                                                        _company.ShipmentProviderInfoList,
                                                                        AppSettings.DefaultCustomType,
                                                                        AppSettings.LabelDirectory,
                                                                        AppSettings.LabelDirectory,
                                                                        AppSettings.LabelDirectory);

                var fimsProvider = rateProviders.FirstOrDefault(r => r.Type == ShipmentProviderType.FIMS);

                //var rates = fedexRateProvider.GetLocalRate(
                //        companyAddress.GetReturnAddress(order.GetMarketId()),
                //        companyAddress.GetPickupAddress(order.GetMarketId()),
                //        order.GetAddressDto(),
                //        _time.GetAppNowTime(),
                //        order.WeightD,
                //        order.IsInsured ? order.TotalPrice : 0,
                //        order.IsSignConfirmation,
                //        new OrderRateInfo()
                //        {
                //            ShippingService = shippingService,
                //            InitialServiceType = order.InitialServiceType,
                //            OrderNumber = order.OrderId,
                //            Items = orderItemInfoes,
                //            SourceItems = sourceOrderItemInfoes,
                //            TotalPrice = order.TotalPrice,
                //            Currency = order.TotalPriceCurrency,
                //        },
                //        RetryModeType.Normal);

                var shipmentInfo = new OrderShippingInfoDTO()
                {
                    OrderAmazonId      = order.OrderId,
                    WeightD            = order.WeightD,
                    IsInsured          = order.IsInsured,
                    TotalPrice         = order.TotalPrice,
                    TotalPriceCurrency = order.TotalPriceCurrency,

                    IsSignConfirmation = order.IsSignConfirmation,

                    Items = orderItemInfoes.Select(oi => new DTOOrderItem()
                    {
                        ItemOrderId = oi.ItemOrderId,
                        ItemPrice   = oi.ItemPrice,
                        Weight      = oi.Weight,
                        Quantity    = oi.Quantity,
                    }).ToList(),
                    ShippingMethod = new ShippingMethodDTO()
                    {
                    },
                };

                var shipDate = db.Dates.GetOrderShippingDate(null);

                var boughtInTheCountry = MarketBaseHelper.GetMarketCountry((MarketType)order.Market, order.MarketplaceId);
                //var callResult = labelProvider.CreateShipment(shipmentInfo,
                //    returnAddress,
                //    pickupAddress,
                //    toAddress,
                //    shipDate.Date,
                //    model.Notes,
                //    !model.ShippingMethod.IsSupportReturnToPOBox,
                //    sampleMode,
                //    fromUI: true);


                var labels = fimsProvider.CreateShipment(
                    shipmentInfo,
                    companyAddress.GetReturnAddress(order.GetMarketId()),
                    companyAddress.GetPickupAddress(order.GetMarketId()),
                    order.GetAddressDto(),
                    boughtInTheCountry,
                    shipDate,
                    "",
                    false,
                    false,
                    false);


                _log.Info("Labels: " + labels.Data.LabelFileList.Count);
            }
        }
Esempio n. 4
0
        public void GetIntlRatesTest(string orderId, ShipmentProviderType type)
        {
            using (var db = _dbFactory.GetRWDb())
            {
                var order = db.ItemOrderMappings.GetOrderWithItems(_weightService, orderId, unmaskReferenceStyle: false, includeSourceItems: true);

                var shippingService       = ShippingUtils.InitialShippingServiceIncludeUpgrade(order.InitialServiceType, order.UpgradeLevel); //order.ShippingService
                var orderItemInfoes       = OrderHelper.BuildAndGroupOrderItems(order.Items);
                var sourceOrderItemInfoes = OrderHelper.BuildAndGroupOrderItems(order.SourceItems);

                var providers = GetShipmentProviders(_company);
                var provider  = providers.FirstOrDefault(p => p.Type == type);

                var companyAddress = new CompanyAddressService(_company);

                if (ShippingUtils.IsInternational(order.ShippingCountry))
                {
                    var rates = provider.GetInternationalRates(
                        companyAddress.GetReturnAddress(order.GetMarketId()),
                        companyAddress.GetPickupAddress(order.GetMarketId()),
                        order.GetAddressDto(),
                        _time.GetAppNowTime(),
                        order.WeightD,
                        null,
                        order.IsInsured ? order.TotalPrice : 0,
                        order.IsSignConfirmation,
                        new OrderRateInfo()
                    {
                        ShippingService = shippingService,
                        OrderNumber     = order.OrderId,
                        Items           = orderItemInfoes,
                        SourceItems     = sourceOrderItemInfoes,
                        TotalPrice      = order.TotalPrice,
                        Currency        = order.TotalPriceCurrency,
                    },
                        RetryModeType.Normal);

                    Console.WriteLine(rates.Rates.Count);
                }
                else
                {
                    var rates = provider.GetLocalRate(
                        companyAddress.GetReturnAddress(order.GetMarketId()),
                        companyAddress.GetPickupAddress(order.GetMarketId()),
                        order.GetAddressDto(),
                        _time.GetAppNowTime(),
                        order.WeightD,
                        null,
                        order.IsInsured ? order.TotalPrice : 0,
                        order.IsSignConfirmation,
                        new OrderRateInfo()
                    {
                        ShippingService    = shippingService,
                        InitialServiceType = order.InitialServiceType,
                        OrderNumber        = order.OrderId,
                        Items       = orderItemInfoes,
                        SourceItems = sourceOrderItemInfoes,
                        TotalPrice  = order.TotalPrice,
                        Currency    = order.TotalPriceCurrency,
                    },
                        RetryModeType.Normal);

                    Console.WriteLine(rates.Rates.Count);
                }
            }
        }
Esempio n. 5
0
        public CallResult <List <ShippingMethodViewModel> > GetShippingOptionsModel(IUnitOfWork db,
                                                                                    ITime time,
                                                                                    ILogService log,
                                                                                    IShipmentApi rateProvider,
                                                                                    IShippingService shippingService,
                                                                                    IWeightService weightService)
        {
            var result = new CallResult <List <ShippingMethodViewModel> >();

            if (String.IsNullOrEmpty(OrderID))
            {
                result.Status  = CallStatus.Fail;
                result.Message = "Empty Order Id";
                return(result);
            }

            var fromAddress = FromAddress.GetAddressDto();
            var toAddress   = ToAddress.GetAddressDto();

            if (AddressHelper.IsEmpty(fromAddress) ||
                AddressHelper.IsEmpty(toAddress))
            {
                result.Status  = CallStatus.Fail;
                result.Message = "Empty from/to address";
            }

            var orders = db.ItemOrderMappings.GetFilteredOrdersWithItems(weightService,
                                                                         new OrderSearchFilter()
            {
                Market                = MarketType.None,
                EqualOrderNumber      = OrderID,
                IgnoreBatchFilter     = true,
                IncludeNotify         = false,
                UnmaskReferenceStyles = false,
                IncludeSourceItems    = true,
            });

            if (!orders.Any())
            {
                result.Status  = CallStatus.Fail;
                result.Message = "Cannot find order Id";
                return(result);
            }

            //NOTE: no need to use Items from model, we always buy label for all items, only needs a custom weight
            var orderItems = OrderHelper.BuildAndGroupOrderItems(orders.SelectMany(o => o.Items).ToList());

            orderItems = OrderHelper.GroupBySourceItemOrderId(orderItems);

            var sourceOrderItems = OrderHelper.BuildAndGroupOrderItems(orders.SelectMany(o => o.SourceItems).ToList());

            sourceOrderItems = OrderHelper.GroupBySourceItemOrderId(sourceOrderItems);

            var shipDate  = db.Dates.GetOrderShippingDate(null);
            var mainOrder = orders.FirstOrDefault();

            result = MailViewModel.GetShippingOptionsWithRate(db,
                                                              log,
                                                              time,
                                                              rateProvider,
                                                              shippingService,
                                                              fromAddress,
                                                              toAddress,
                                                              shipDate,
                                                              WeightLb ?? 0,
                                                              (decimal)(WeightOz ?? 0),
                                                              new ItemPackageDTO()
            {
                PackageLength = PackageLength,
                PackageWidth  = PackageWidth,
                PackageHeight = PackageHeight,
            },
                                                              0,
                                                              new OrderRateInfo()
            {
                OrderNumber = mainOrder.OrderId,

                Items       = orderItems,
                SourceItems = sourceOrderItems,

                EstimatedShipDate = ShippingUtils.AlignMarketDateByEstDayEnd(mainOrder.LatestShipDate, (MarketType)mainOrder.Market),
                TotalPrice        = orders.Sum(o => o.TotalPrice),
                Currency          = mainOrder.TotalPriceCurrency,
            });

            return(result);
        }