Beispiel #1
0
        private decimal?CalculatePrice(double?weight,
                                       string shippingSize,
                                       decimal usListingPrice,
                                       IList <RateByCountryDTO> allRates,
                                       MarketType forMarket,
                                       string forMarketplaceId)
        {
            var usShipping = RateService.GetMarketShippingAmount(MarketType.Amazon,
                                                                 MarketplaceKeeper.AmazonComMarketplaceId); // 4.49M;
            var currency   = PriceHelper.GetCurrencyAbbr(forMarket, forMarketplaceId);
            var caShipping = PriceHelper.ConvertToUSD(RateService.GetMarketShippingAmount(forMarket, forMarketplaceId), currency);

            var item = new OrderItemRateInfo()
            {
                Quantity     = 1,
                ShippingSize = shippingSize
            };

            IList <RateByCountryDTO> rates = null;

            if (weight.HasValue && weight > 0)
            {
                rates = allRates.Where(r => r.Weight == Math.Floor(weight.Value)).ToList();
            }

            var localPackageType = PackageTypeCode.Flat;

            if (!String.IsNullOrEmpty(shippingSize))
            {
                localPackageType = ShippingServiceUtils.IsSupportFlatEnvelope(new List <OrderItemRateInfo>()
                {
                    item
                })
                        ? PackageTypeCode.Flat
                        : PackageTypeCode.Regular;
            }

            decimal?caRateActualCostRegular = null;
            decimal?usRateActualCost        = null;

            if (rates != null && rates.Any())
            {
                var usPackageType = localPackageType.ToString();

                caRateActualCostRegular = rates.FirstOrDefault(r => r.Country == "CA" && r.PackageType == "Regular")?.Cost;
                usRateActualCost        = rates.FirstOrDefault(r => r.Country == "US" && r.PackageType == usPackageType)?.Cost;
            }
            if (!caRateActualCostRegular.HasValue ||
                !usRateActualCost.HasValue)
            {
                return(null);
            }

            var usIncome = usListingPrice + usShipping - usRateActualCost.Value;
            var newPrice = usIncome - (caShipping - caRateActualCostRegular.Value);

            newPrice = PriceHelper.ConvertFromUSD(newPrice, currency);

            return(newPrice);
        }
        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);
                    }
                }
            }
        }