public static TaxLine ToViewModel(this DataContracts.TaxDetail taxDetail)
        {
            var taxLineModel = new TaxLine();

            taxLineModel.Price = taxDetail.Amount;
            taxLineModel.Rate = taxDetail.Rate;
            taxLineModel.Title = taxDetail.Name;

            return taxLineModel;
        }
        public static Checkout AsCheckoutWebModel(
            this Data.ShoppingCart cart, VirtoCommerce.ApiClient.DataContracts.Orders.CustomerOrder order = null)
        {
            var checkoutModel = new Checkout();

            checkoutModel.TaxLines = new List<TaxLine>();

            if (cart.Addresses != null)
            {
                var billingAddress = cart.Addresses.FirstOrDefault(a => a.Type == AddressType.Billing);
                if (billingAddress != null)
                {
                    checkoutModel.BillingAddress = billingAddress.AsCartWebModel();
                    checkoutModel.Email = billingAddress.Email;
                }

                var shippingAddress = cart.Addresses.FirstOrDefault(a => a.Type == AddressType.Shipping);
                if (shippingAddress != null)
                {
                    checkoutModel.ShippingAddress = shippingAddress.AsCartWebModel();
                    checkoutModel.Email = shippingAddress.Email;
                }
            }

            checkoutModel.BuyerAcceptsMarketing = true;
            checkoutModel.Currency = cart.Currency;
            checkoutModel.CustomerId = cart.CustomerId;

            if (cart.Discounts != null)
            {
                checkoutModel.Discounts = new List<VirtoCommerce.Web.Models.Discount>();

                foreach (var discount in cart.Discounts)
                {
                    checkoutModel.Discounts.Add(new VirtoCommerce.Web.Models.Discount
                    {
                        Amount = (decimal)discount.DiscountAmount,
                        Code = discount.PromotionId,
                        Id = discount.Id
                    });
                }
            }

            checkoutModel.Email = "";

            // TODO: Gift cards

            checkoutModel.Id = cart.Id;

            if (cart.Items != null)
            {
                checkoutModel.LineItems = new List<LineItem>();

                foreach (var item in cart.Items)
                {
                    checkoutModel.LineItems.Add(item.AsWebModel());
                }

                var taxableItems = cart.Items;//.Where(i => i.TaxIncluded);
                if (taxableItems.Count() > 0)
                {
                    var lineItemsTax = new TaxLine
                    {
                        Title = "Line items",
                        Price = taxableItems.Sum(i => i.TaxTotal),
                        Rate = taxableItems.Where(i => i.TaxDetails != null).Sum(i => i.TaxDetails.Sum(td => td.Rate))
                    };

                    if (checkoutModel.TaxLines == null)
                    {
                        checkoutModel.TaxLines = new List<TaxLine>();
                    }

                    checkoutModel.TaxLines.Add(lineItemsTax);
                }
            }

            checkoutModel.Name = cart.Name;
            //checkoutModel.Note = cart.Note;

            if (order != null)
            {
                checkoutModel.Order = order.AsWebModel();
            }

            if (cart.Payments != null)
            {
                var payment = cart.Payments.FirstOrDefault(); // Remake for multipayment

                if (payment != null)
                {
                    checkoutModel.PaymentMethod = new Models.PaymentMethod
                    {
                        Code = payment.PaymentGatewayCode
                    };
                }
            }

            if (cart.Shipments != null)
            {
                var shipment = cart.Shipments.FirstOrDefault(); // Remake for multishipment

                if (shipment != null)
                {
                    checkoutModel.ShippingMethod = new ShippingMethod
                    {
                        Handle = shipment.ShipmentMethodCode,
                        Price = shipment.ShippingPrice,
                        Title = shipment.ShipmentMethodCode,
                        TaxTotal = shipment.TaxTotal,
                        TaxType = shipment.TaxType
                    };
                }

                var taxableShipments = cart.Shipments;//.Where(s => s.TaxIncluded);
                if (taxableShipments.Count() > 0)
                {
                    var shippingTax = new TaxLine
                    {
                        Title = "Shipping",
                        Price = cart.Shipments.Sum(s => s.TaxTotal),
                        Rate = taxableShipments.Where(s => s.TaxDetails != null).Sum(i => i.TaxDetails.Sum(td => td.Rate))
                    };

                    if (checkoutModel.TaxLines == null)
                    {
                        checkoutModel.TaxLines = new List<TaxLine>();
                    }

                    checkoutModel.TaxLines.Add(shippingTax);
                }
            }

            // Transactions

            return checkoutModel;
        }