Exemple #1
0
        public static string GenerateHtmlOrderTable(IList<OrderItem> orderItems, Currency currency, float productsPrice, float orderDiscountPercent, OrderCoupon coupon, OrderCertificate certificate,
            float totalDiscount, float shippingPrice, float paymentPrice, float taxesTotal, CustomerContact billingContact, CustomerContact shippingContact)
        {
            var htmlOrderTable = new StringBuilder();

            htmlOrderTable.Append("<table width=\'100%\' border=\'0\' cellspacing=\'1\' cellpadding=\'2\'>");
            htmlOrderTable.Append("<tr>");
            htmlOrderTable.AppendFormat("<td style=\'width:100px; text-align: center;\'>&nbsp;</td>");
            htmlOrderTable.AppendFormat("<td style=\'width:100px; text-align: center;\'>{0}</td>", Resource.Client_OrderConfirmation_SKU);
            htmlOrderTable.AppendFormat("<td>{0}</td>", Resource.Client_OrderConfirmation_Name);
            htmlOrderTable.AppendFormat("<td>{0}</td>", Resource.Client_OrderConfirmation_CustomOptions);
            htmlOrderTable.AppendFormat("<td style=\'width:200px;\'>{0}</td>", Resource.Client_OrderConfirmation_Price);
            htmlOrderTable.AppendFormat("<td style=\'width:240px; text-align: center;\' >{0}</td>", Resource.Client_OrderConfirmation_Count);
            htmlOrderTable.AppendFormat("<td style=\'width:200px;\'>{0}</td>", Resource.Client_OrderConfirmation_Cost);
            htmlOrderTable.Append("</tr>");

            // Добавление заказанных товаров
            foreach (var item in orderItems)
            {
                if (item.ProductID.HasValue)
                {
                    htmlOrderTable.Append("<tr>");
                    if (item.ProductID != null)
                    {
                        var p = ProductService.GetProduct((int)item.ProductID);
                        if (p != null && p.Photo.IsNotEmpty())
                        {
                            htmlOrderTable.AppendFormat("<td style=\'text-align: center;\'><img src='{0}' /></td>",
                                                        SettingsMain.SiteUrl.Trim('/') + '/' + FoldersHelper.GetImageProductPath(ProductImageType.Small, p.Photo, false));
                        }
                        else
                        {
                            htmlOrderTable.AppendFormat("<td>&nbsp;</td>");
                        }
                    }
                    htmlOrderTable.AppendFormat("<td style=\'text-align: center;\' >{0}</td>", item.ArtNo);
                    htmlOrderTable.AppendFormat("<td>{0}</td>", item.Name);
                    htmlOrderTable.AppendFormat("<td>{0}{1}{2}</td>",
                                                item.Color != null ? SettingsCatalog.ColorsHeader + ": " + item.Color + "<br/>" : "",
                                                item.Size != null ? SettingsCatalog.SizesHeader + ": " + item.Size + "<br/>" : "",
                                                RenderSelectedOptions(item.SelectedOptions));
                    htmlOrderTable.AppendFormat("<td style=\'width:200px;\'>{0}</td>", CatalogService.GetStringPrice(item.Price, currency));
                    htmlOrderTable.AppendFormat("<td style=\'width:240px; text-align: center;\' >{0}</td>", item.Amount);
                    htmlOrderTable.AppendFormat("<td style=\'width:200px;\'>{0}</td>", CatalogService.GetStringPrice(item.Price * item.Amount, currency));
                    htmlOrderTable.Append("</tr>");
                }
            }

            // Стоимость заказа
            htmlOrderTable.Append("<tr>");
            htmlOrderTable.AppendFormat("<td colspan=\'6\' style=\'text-align:right;\'><b>{0}:</b></td>", Resource.Client_OrderConfirmation_OrderCost);
            htmlOrderTable.AppendFormat("<td style=\'width:200px\'><b>{0}</b></td>", CatalogService.GetStringPrice(productsPrice, currency));
            htmlOrderTable.Append("</tr>");

            if (orderDiscountPercent != 0)
            {
                htmlOrderTable.Append("<tr>");
                htmlOrderTable.AppendFormat("<td colspan=\'6\' style=\'text-align:right;\'><b>{0}:</b></td>", Resource.Client_OrderConfirmation_Discount);
                htmlOrderTable.AppendFormat("<td style=\'width:200px\'><b>-{0}</b></td>", CatalogService.GetStringDiscountPercent(productsPrice, orderDiscountPercent, false));
                htmlOrderTable.Append("</tr>");
            }
            if (certificate != null)
            {
                htmlOrderTable.Append("<tr>");
                htmlOrderTable.AppendFormat("<td colspan=\'6\' style=\'text-align:right;\'><b>{0}:</b></td>", Resource.Client_OrderConfirmation_Certificate);
                htmlOrderTable.AppendFormat("<td style=\'width:200px\'><b>-{0}</b></td>", CatalogService.GetStringPrice(certificate.Price, currency));
                htmlOrderTable.Append("</tr>");
            }
            if (coupon != null)
            {
                htmlOrderTable.Append("<tr>");
                htmlOrderTable.AppendFormat("<td colspan=\'6\' style=\'text-align:right;\'><b>{0}:</b></td>",
                                            Resource.Client_OrderConfirmation_Coupon);
                if (coupon.Type == CouponType.Fixed)
                {
                    htmlOrderTable.AppendFormat("<td style=\'width:200px\'><b>-{0}</b></td>",
                                                CatalogService.GetStringPrice(coupon.Value, currency));
                }
                else
                {
                    htmlOrderTable.AppendFormat("<td style=\'width:200px\'><b>-{0}%</b></td>", CatalogService.FormatPriceInvariant(coupon.Value));
                }
                htmlOrderTable.Append("</tr>");
            }

            // Стоимость доставки
            htmlOrderTable.Append("<tr>");
            htmlOrderTable.AppendFormat("<td colspan=\'6\' style=\'text-align:right;\'><b>{0}:</b></td>", Resource.Client_OrderConfirmation_DeliveryCost);
            htmlOrderTable.AppendFormat("<td style=\'width:200px\'><b>{0}</b></td>", CatalogService.GetStringPrice(shippingPrice, currency));
            htmlOrderTable.Append("</tr>");

            if (paymentPrice != 0)
            {
                htmlOrderTable.Append("<tr>");
                htmlOrderTable.AppendFormat("<td colspan=\'6\' style=\'text-align:right;\'><b>{0}:</b></td>", paymentPrice > 0 ? Resource.Client_OrderConfirmation_PaymentCost : Resource.Client_OrderConfirmation_PaymentDiscount);
                htmlOrderTable.AppendFormat("<td style=\'width:200px\'><b>{0}</b></td>", CatalogService.GetStringPrice(paymentPrice, currency));
                htmlOrderTable.Append("</tr>");
            }

            // Налоги
            var taxTotal = TaxServices.GetTaxItems(orderItems, shippingContact, billingContact, orderDiscountPercent);

            if (taxTotal.Any())
            {
                foreach (TaxElement tax in taxTotal.Keys)
                {
                    htmlOrderTable.Append("<tr>");
                    htmlOrderTable.AppendFormat("<td colspan=\'6\' style=\'text-align:right;\'><b>{0}:</b></td>",
                                                (tax.ShowInPrice ? Resource.Core_TaxServices_Include_Tax : "") + " " + tax.Name);
                    htmlOrderTable.AppendFormat("<td style=\'width:200px\'><b>{0}</b></td>",
                                                (tax.ShowInPrice ? "" : "+") +
                                                CatalogService.GetStringPrice(taxTotal[tax], currency));
                    htmlOrderTable.Append("</tr>");
                }
            }

            var total = productsPrice - totalDiscount + shippingPrice + paymentPrice + taxesTotal;
            if (total < 0) total = 0;

            // Итого
            htmlOrderTable.Append("<tr>");
            htmlOrderTable.AppendFormat("<td colspan=\'6\' style=\'text-align:right;\'><b>{0}:</b></td>", Resource.Client_OrderConfirmation_Total);
            htmlOrderTable.AppendFormat("<td style=\'width:200px\'><b>{0}</b></td>", CatalogService.GetStringPrice(total, currency));
            htmlOrderTable.Append("</tr>");

            htmlOrderTable.Append("</table>");

            return htmlOrderTable.ToString();
        }
        protected void btnBuyInOneClick_OnClick(object sender, EventArgs e)
        {
            if (txtName.Text.IsNullOrEmpty() || txtPhone.Text.IsNullOrEmpty())
            {
                errorMsg.Text = Resources.Resource.Client_OneClick_WrongData;
                return;
            }

            var shoppingCart = ShoppingCartService.CurrentShoppingCart;
            float totalPrice = shoppingCart.TotalPrice;
            float discountPercentOnTotalPrice = shoppingCart.DiscountPercentOnTotalPrice;

            if (shoppingCart.TotalPrice < AdvantShop.Configuration.SettingsOrderConfirmation.MinimalOrderPrice)
            {
                errorMsg.Text = string.Format(Resources.Resource.Client_ShoppingCart_MinimalOrderPrice,
                                              CatalogService.GetStringPrice(
                                                  AdvantShop.Configuration.SettingsOrderConfirmation
                                                            .MinimalOrderPrice),
                                              CatalogService.GetStringPrice(
                                                  AdvantShop.Configuration.SettingsOrderConfirmation
                                                            .MinimalOrderPrice - totalPrice));
                return;
            }

            var orderItems = shoppingCart.Select(item => (OrderItem) item).ToList();

            OrderCertificate orderCertificate = null;
            OrderCoupon orderCoupon = null;

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

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

            var orderContact = new OrderContact
            {
                Address = string.Empty,
                City = string.Empty,
                Country = string.Empty,
                Name = string.Empty,
                Zip = string.Empty,
                Zone = string.Empty
            };

            var customerGroup = CustomerSession.CurrentCustomer.CustomerGroup;
            var order = new Order
            {
                CustomerComment = txtComment.Text,
                OrderDate = DateTime.Now,
                OrderCurrency = new OrderCurrency
                {
                    CurrencyCode = CurrencyService.CurrentCurrency.Iso3,
                    CurrencyNumCode = CurrencyService.CurrentCurrency.NumIso3,
                    CurrencySymbol = CurrencyService.CurrentCurrency.Symbol,
                    CurrencyValue = CurrencyService.CurrentCurrency.Value,
                    IsCodeBefore = CurrencyService.CurrentCurrency.IsCodeBefore
                },
                OrderCustomer = new OrderCustomer
                {
                    CustomerID = CustomerSession.CurrentCustomer.Id,
                    Email = CustomerSession.CurrentCustomer.EMail,
                    FirstName = txtName.Text,
                    LastName = string.Empty,
                    MobilePhone = txtPhone.Text,
                    CustomerIP = HttpContext.Current.Request.UserHostAddress
                },

                OrderStatusId = OrderService.DefaultOrderStatus,
                AffiliateID = 0,
                GroupName = customerGroup.GroupName,
                GroupDiscount = customerGroup.GroupDiscount,

                OrderItems = orderItems,
                OrderDiscount = discountPercentOnTotalPrice,
                Number = OrderService.GenerateNumber(1),
                ShippingContact = orderContact,
                BillingContact = orderContact,
                Certificate = orderCertificate,
                Coupon = orderCoupon,
                AdminOrderComment = Resources.Resource.Client_BuyInOneClick_Header
            };

            order.OrderID = OrderService.AddOrder(order);
            order.Number = OrderService.GenerateNumber(order.OrderID);
            OrderService.UpdateNumber(order.OrderID, order.Number);
            OrderService.ChangeOrderStatus(order.OrderID, OrderService.DefaultOrderStatus);

            if (order.OrderID != 0)
            {
               string message = SendMail.BuildMail(new ClsMailParamOnBuyInOneClick
                    {
                        Name = HttpUtility.HtmlEncode(txtName.Text),
                        Phone = HttpUtility.HtmlEncode(txtPhone.Text),
                        Comment = HttpUtility.HtmlEncode(txtComment.Text),
                        OrderTable = OrderService.GenerateHtmlOrderTable(
                            order.OrderItems,
                            CurrencyService.CurrentCurrency,
                            totalPrice,
                            discountPercentOnTotalPrice,
                            orderCoupon,
                            orderCertificate,
                            discountPercentOnTotalPrice > 0 ? discountPercentOnTotalPrice * totalPrice / 100 : 0,
                            0,
                            0,
                            0,
                            CustomerSession.CurrentCustomer.Contacts.Count > 0
                                ? CustomerSession.CurrentCustomer.Contacts[0]
                                : new CustomerContact(),
                            CustomerSession.CurrentCustomer.Contacts.Count > 0
                                ? CustomerSession.CurrentCustomer.Contacts[0]
                                : new CustomerContact()),
                    });

                    SendMail.SendMailNow(AdvantShop.Configuration.SettingsMail.EmailForOrders,
                                         AdvantShop.Configuration.SettingsMain.SiteUrl + " - " +
                                         Resources.Resource.Client_BuyInOneClick_Header, message, true);
            }

            ModulesRenderer.OrderAdded(order.OrderID);

            if (order.OrderID != 0)
            {
                ShoppingCartService.ClearShoppingCart(ShoppingCartType.ShoppingCart, CustomerSession.CustomerId);
                Response.Redirect("~/orderconfirmation.aspx?step=FinalTab");
            }
            else
            {
                errorMsg.Text = Resources.Resource.Client_ShoppingCart_Error;
            }
        }