private Order CreateOrder(ShoppingCart shoppingCart)
        {
            var orderConfirmData = PageData.OrderConfirmationData;
            var customerGroup    = CustomerContext.CurrentCustomer.CustomerGroup;

            var baseCurrency = CurrencyService.BaseCurrency;

            var ord = new Order
            {
                OrderCustomer = new OrderCustomer
                {
                    CustomerIP  = Request.UserHostAddress,
                    CustomerID  = orderConfirmData.Customer.Id,
                    FirstName   = orderConfirmData.Customer.FirstName,
                    LastName    = orderConfirmData.Customer.LastName,
                    Patronymic  = orderConfirmData.Customer.Patronymic,
                    Email       = orderConfirmData.Customer.EMail,
                    MobilePhone = orderConfirmData.Customer.Phone,
                },
                OrderCurrency = new OrderCurrency
                {
                    //CurrencyCode = CurrencyService.CurrentCurrency.Iso3,
                    //CurrencyValue = CurrencyService.CurrentCurrency.Value,
                    //CurrencySymbol = CurrencyService.CurrentCurrency.Symbol,
                    //CurrencyNumCode = CurrencyService.CurrentCurrency.NumIso3,
                    //IsCodeBefore = CurrencyService.CurrentCurrency.IsCodeBefore
                    CurrencyCode    = baseCurrency.Iso3,
                    CurrencyValue   = baseCurrency.Value,
                    CurrencySymbol  = baseCurrency.Symbol,
                    CurrencyNumCode = baseCurrency.NumIso3,
                    IsCodeBefore    = baseCurrency.IsCodeBefore
                },
                OrderStatusId   = OrderService.DefaultOrderStatus,
                AffiliateID     = 0,
                OrderDate       = DateTime.Now,
                CustomerComment = orderConfirmData.CustomerComment,
                ShippingContact = new OrderContact
                {
                    Name         = orderConfirmData.ShippingContact.Name,
                    Country      = orderConfirmData.ShippingContact.Country,
                    Zone         = orderConfirmData.ShippingContact.RegionName,
                    City         = orderConfirmData.ShippingContact.City,
                    Zip          = orderConfirmData.ShippingContact.Zip,
                    Address      = orderConfirmData.ShippingContact.Address,
                    CustomField1 = orderConfirmData.ShippingContact.CustomField1,
                    CustomField2 = orderConfirmData.ShippingContact.CustomField2,
                    CustomField3 = orderConfirmData.ShippingContact.CustomField3
                },

                GroupName     = customerGroup.GroupName,
                GroupDiscount = customerGroup.GroupDiscount,
                OrderDiscount = shoppingCart.DiscountPercentOnTotalPrice
            };

            foreach (var orderItem in shoppingCart.Select(item => (OrderItem)item))
            {
                ord.OrderItems.Add(orderItem);
            }

            if (!orderConfirmData.BillingIsShipping)
            {
                ord.BillingContact = new OrderContact
                {
                    Name         = orderConfirmData.BillingContact.Name,
                    Country      = orderConfirmData.BillingContact.Country,
                    Zone         = orderConfirmData.BillingContact.RegionName,
                    City         = orderConfirmData.BillingContact.City,
                    Zip          = orderConfirmData.BillingContact.Zip,
                    Address      = orderConfirmData.BillingContact.Address,
                    CustomField1 = orderConfirmData.BillingContact.CustomField1,
                    CustomField2 = orderConfirmData.BillingContact.CustomField2,
                    CustomField3 = orderConfirmData.BillingContact.CustomField3
                };
            }

            ord.ShippingMethodId = orderConfirmData.SelectedShippingItem.MethodId;
            ord.PaymentMethodId  = orderConfirmData.SelectedPaymentItem.PaymenMethodtId;

            ord.ArchivedShippingName = orderConfirmData.SelectedShippingItem.MethodNameRate;
            ord.ArchivedPaymentName  = orderConfirmData.SelectedPaymentItem.Name;

            ord.PaymentDetails = orderConfirmData.PaymentDetails;

            if (orderConfirmData.SelectedShippingItem.Ext != null &&
                orderConfirmData.SelectedShippingItem.Ext.PickpointAddress.IsNotEmpty())
            {
                ord.OrderPickPoint = new OrderPickPoint
                {
                    PickPointId      = orderConfirmData.SelectedShippingItem.Ext.PickpointId,
                    PickPointAddress = orderConfirmData.SelectedShippingItem.Ext.PickpointAddress,
                    AdditionalData   = orderConfirmData.SelectedShippingItem.Ext.AdditionalData
                };
            }
            else if (orderConfirmData.SelectedShippingItem.Ext != null &&
                     orderConfirmData.SelectedShippingItem.Ext.Type == ExtendedType.Cdek &&
                     orderConfirmData.SelectedShippingItem.Ext.PickpointAddress.IsNullOrEmpty())
            {
                ord.OrderPickPoint = new OrderPickPoint
                {
                    PickPointId      = orderConfirmData.SelectedShippingItem.Ext.PickpointId,
                    PickPointAddress = ord.ShippingContact.Address,
                    AdditionalData   = orderConfirmData.SelectedShippingItem.Ext.AdditionalData
                };
            }
            else if (orderConfirmData.SelectedShippingItem.Type == ShippingType.CheckoutRu &&
                     orderConfirmData.SelectedShippingItem.Ext != null)
            {
                ord.OrderPickPoint = new OrderPickPoint
                {
                    PickPointId      = orderConfirmData.SelectedShippingItem.Ext.PickpointId,
                    PickPointAddress = orderConfirmData.SelectedShippingItem.Ext.Type != ExtendedType.CashOnDelivery
                        ? ord.ShippingContact.Address
                        : string.Empty,
                    AdditionalData = orderConfirmData.SelectedShippingItem.Ext.AdditionalData
                };
            }

            var certificate = shoppingCart.Certificate;
            var coupon      = shoppingCart.Coupon;

            if (certificate != null)
            {
                ord.Certificate = new OrderCertificate()
                {
                    Code  = certificate.CertificateCode,
                    Price = certificate.Sum
                };
            }
            if (coupon != null && shoppingCart.TotalPrice >= coupon.MinimalOrderPrice)
            {
                ord.Coupon = new OrderCoupon()
                {
                    Code  = coupon.Code,
                    Type  = coupon.Type,
                    Value = coupon.Value
                };
            }

            var shippingPrice = orderConfirmData.SelectedPaymentItem.Type == PaymentType.CashOnDelivery && (orderConfirmData.SelectedShippingItem.Type != ShippingType.Cdek || orderConfirmData.SelectedShippingItem.Type != ShippingType.CheckoutRu)
                                ? orderConfirmData.SelectedShippingItem.Ext != null
                                      ? orderConfirmData.SelectedShippingItem.Ext.PriceCash
                                      : 0
                                : orderConfirmData.SelectedShippingItem.Rate;

            BonusCard bonusCard = null;

            if (BonusSystem.IsActive)
            {
                bonusCard = BonusSystemService.GetCard(orderConfirmData.Customer.BonusCardNumber);
                if (bonusCard != null && orderConfirmData.UseBonuses && bonusCard.BonusAmount > 0)
                {
                    ord.BonusCost =
                        BonusSystemService.GetBonusCost(
                            shoppingCart.TotalPrice - shoppingCart.TotalDiscount + shippingPrice,
                            shoppingCart.TotalPrice - shoppingCart.TotalDiscount, bonusCard.BonusAmount);
                }
            }

            var taxTotal =
                AdvantShop.Taxes.TaxServices.CalculateTaxes(shoppingCart.TotalPrice - shoppingCart.TotalDiscount +
                                                            shippingPrice - ord.BonusCost).Where(tax => !tax.Key.ShowInPrice).Sum(tax => tax.Value);

            var paymentPrice = orderConfirmData.SelectedPaymentItem.ExtrachargeType == ExtrachargeType.Percent
                                    ? (shoppingCart.TotalPrice - shoppingCart.TotalDiscount + shippingPrice - ord.BonusCost + taxTotal) * orderConfirmData.SelectedPaymentItem.Extracharge / 100
                                    : orderConfirmData.SelectedPaymentItem.Extracharge;

            ord.ShippingCost = shippingPrice;
            ord.PaymentCost  = paymentPrice;


            ord.Number = OrderService.GenerateNumber(1); // For crash protection

            ord.OrderID = OrderService.AddOrder(ord);
            ord.Number  = OrderService.GenerateNumber(ord.OrderID); // new number
            OrderService.UpdateNumber(ord.OrderID, ord.Number);

            ModulesRenderer.OrderAdded(ord.OrderID);

            OrderService.ChangeOrderStatus(ord.OrderID, OrderService.DefaultOrderStatus);

            if (BonusSystem.IsActive && bonusCard != null)
            {
                var sumPrice = BonusSystem.BonusType == BonusSystem.EBonusType.ByProductsCostWithShipping
                       ? shoppingCart.TotalPrice - shoppingCart.TotalDiscount + shippingPrice
                       : shoppingCart.TotalPrice - shoppingCart.TotalDiscount;

                BonusSystemService.MakeBonusPurchase(bonusCard.CardNumber, sumPrice, ord.BonusCost, ord.Number, ord.OrderID);
            }



            TrialService.TrackEvent(
                ord.OrderItems.Any(item => item.Name.Contains("SM-G900F"))
                    ? TrialEvents.BuyTheProduct
                    : TrialEvents.CheckoutOrder, string.Empty);

            return(ord);
        }
Esempio n. 2
0
    private Order CreateOrder(ShoppingCart shoppingCart)
    {
        var customerGroup = CustomerSession.CurrentCustomer.CustomerGroup;
        var ord           = new Order
        {
            OrderCustomer = new OrderCustomer
            {
                CustomerIP  = Request.UserHostAddress,
                CustomerID  = PageData.OrderConfirmationData.Customer.Id,
                FirstName   = PageData.OrderConfirmationData.Customer.FirstName,
                LastName    = PageData.OrderConfirmationData.Customer.LastName,
                Email       = PageData.OrderConfirmationData.Customer.EMail,
                MobilePhone = PageData.OrderConfirmationData.Customer.Phone,
            },
            OrderCurrency = new OrderCurrency
            {
                CurrencyCode    = CurrencyService.CurrentCurrency.Iso3,
                CurrencyValue   = CurrencyService.CurrentCurrency.Value,
                CurrencySymbol  = CurrencyService.CurrentCurrency.Symbol,
                CurrencyNumCode = CurrencyService.CurrentCurrency.NumIso3,
                IsCodeBefore    = CurrencyService.CurrentCurrency.IsCodeBefore
            },
            OrderStatusId = OrderService.DefaultOrderStatus,
            AffiliateID   = 0,
            ShippingCost  = PageData.OrderConfirmationData.SelectedPaymentType == PaymentType.CashOnDelivery
                                                                              ? PageData.OrderConfirmationData.ShippingOptionEx != null ? PageData.OrderConfirmationData.ShippingOptionEx.PriceCash : 0
                                                                              : PageData.OrderConfirmationData.SelectShippingRate,
            OrderDate       = DateTime.Now,
            CustomerComment = PageData.OrderConfirmationData.CustomerComment,
            ShippingContact = new OrderContact
            {
                Name    = PageData.OrderConfirmationData.ShippingContact.Name,
                Country = PageData.OrderConfirmationData.ShippingContact.Country,
                Zone    = PageData.OrderConfirmationData.ShippingContact.RegionName,
                City    = PageData.OrderConfirmationData.ShippingContact.City,
                Zip     = PageData.OrderConfirmationData.ShippingContact.Zip,
                Address = PageData.OrderConfirmationData.ShippingContact.Address,
            },

            GroupName     = customerGroup.GroupName,
            GroupDiscount = customerGroup.GroupDiscount,
            OrderDiscount = shoppingCart.DiscountPercentOnTotalPrice
        };



        foreach (var orderItem in shoppingCart.Select(item => (OrderItem)item))
        {
            ord.OrderItems.Add(orderItem);
        }

        if (!PageData.OrderConfirmationData.BillingIsShipping)
        {
            ord.BillingContact = new OrderContact
            {
                Name    = PageData.OrderConfirmationData.BillingContact.Name,
                Country = PageData.OrderConfirmationData.BillingContact.Country,
                Zone    = PageData.OrderConfirmationData.BillingContact.RegionName,
                City    = PageData.OrderConfirmationData.BillingContact.City,
                Zip     = PageData.OrderConfirmationData.BillingContact.Zip,
                Address = PageData.OrderConfirmationData.BillingContact.Address,
            };
        }

        ord.ShippingMethodId = PageData.OrderConfirmationData.SelectShippingId;
        ord.ShippingMethodId = shoppingCart.Count(item => item.ItemType == EnumItemType.Product) == 0 ? 0 : PageData.OrderConfirmationData.SelectShippingId;
        ord.PaymentMethodId  = PageData.OrderConfirmationData.SelectPaymentId;

        ord.ArchivedShippingName = shoppingCart.Count(item => item.ItemType == EnumItemType.Product) == 0 ? Resource.Client_OrderConfirmation_FreeShipping : PageData.OrderConfirmationData.SelectShippingName;
        ord.ArchivedPaymentName  = PageData.OrderConfirmationData.SelectPaymentName;

        ord.PaymentDetails = PageData.OrderConfirmationData.PaymentDetails;

        if (PageData.OrderConfirmationData.ShippingOptionEx != null && !string.IsNullOrEmpty(PageData.OrderConfirmationData.ShippingOptionEx.Pickpointmap))
        {
            ord.OrderPickPoint = new OrderPickPoint
            {
                PickPointId      = PageData.OrderConfirmationData.ShippingOptionEx.PickpointId,
                PickPointAddress =
                    PageData.OrderConfirmationData.ShippingOptionEx.PickpointAddress
            }
        }
        ;

        ord.Number = OrderService.GenerateNumber(1); // For crash protection

        GiftCertificate certificate = shoppingCart.Certificate;
        Coupon          coupon      = shoppingCart.Coupon;

        if (certificate != null)
        {
            ord.Certificate = new OrderCertificate()
            {
                Code  = certificate.CertificateCode,
                Price = certificate.Sum
            };
        }
        if (coupon != null && shoppingCart.TotalPrice >= coupon.MinimalOrderPrice)
        {
            ord.Coupon = new OrderCoupon()
            {
                Code  = coupon.Code,
                Type  = coupon.Type,
                Value = coupon.Value
            };
        }



        ord.OrderID = OrderService.AddOrder(ord);
        ord.Number  = OrderService.GenerateNumber(ord.OrderID); // new number
        OrderService.UpdateNumber(ord.OrderID, ord.Number);
        OrderService.ChangeOrderStatus(ord.OrderID, OrderService.DefaultOrderStatus);

        ModulesRenderer.OrderAdded(ord.OrderID);
        return(ord);
    }