Пример #1
0
        protected int CreateCertificateOrder()
        {
            var certificate = new GiftCertificate
            {
                CertificateCode    = GiftCertificateService.GenerateCertificateCode(),
                ToName             = txtTo.Text,
                FromName           = txtFrom.Text,
                Sum                = Convert.ToSingle(txtSum.Text.Trim()),
                CertificateMessage = txtMessage.Text,
                Enable             = true,
                ToEmail            = txtEmail.Text
            };

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

            var taxes = TaxServices.CalculateCertificateTaxes(certificate.Sum);

            var taxOverPay = taxes.Where(tax => !tax.Key.ShowInPrice).Sum(tax => tax.Value);

            float orderSum = certificate.Sum + taxOverPay;

            var   payment      = PaymentService.GetPaymentMethod(hfPaymentMethod.Value.TryParseInt());
            float paymentPrice = payment.Extracharge == 0 ? 0 : (payment.ExtrachargeType == ExtrachargeType.Fixed ? payment.Extracharge : payment.Extracharge / 100 * certificate.Sum + taxOverPay);

            var baseCurrency = CurrencyService.BaseCurrency;

            var order = new Order
            {
                OrderDate     = DateTime.Now,
                OrderCustomer = new OrderCustomer
                {
                    CustomerID = CustomerContext.CurrentCustomer.Id,
                    Email      = txtEmailFrom.Text,
                    FirstName  = CustomerContext.CurrentCustomer.FirstName,
                    LastName   = CustomerContext.CurrentCustomer.LastName,
                    CustomerIP = HttpContext.Current.Request.UserHostAddress
                },
                OrderCurrency = new OrderCurrency
                {
                    //CurrencyCode = CurrencyService.CurrentCurrency.Iso3,
                    //CurrencyNumCode = CurrencyService.CurrentCurrency.NumIso3,
                    //CurrencyValue = CurrencyService.CurrentCurrency.Value,
                    //CurrencySymbol = CurrencyService.CurrentCurrency.Symbol,
                    //IsCodeBefore = CurrencyService.CurrentCurrency.IsCodeBefore
                    CurrencyCode    = baseCurrency.Iso3,
                    CurrencyValue   = baseCurrency.Value,
                    CurrencySymbol  = baseCurrency.Symbol,
                    CurrencyNumCode = baseCurrency.NumIso3,
                    IsCodeBefore    = baseCurrency.IsCodeBefore
                },
                OrderStatusId        = OrderService.DefaultOrderStatus,
                AffiliateID          = 0,
                ArchivedShippingName = Resource.Client_GiftCertificate_DeliveryByEmail,
                PaymentMethodId      = Convert.ToInt32(hfPaymentMethod.Value),
                ArchivedPaymentName  = payment.Name,
                PaymentDetails       = null,
                Sum               = orderSum + paymentPrice,
                PaymentCost       = paymentPrice,
                OrderCertificates = new List <GiftCertificate>
                {
                    certificate
                },
                TaxCost = taxes.Sum(tax => tax.Value),
                Taxes   = taxes.Select(tax => new OrderTax()
                {
                    TaxID = tax.Key.TaxId, TaxName = tax.Key.Name, TaxShowInPrice = tax.Key.ShowInPrice, TaxSum = tax.Value
                }).ToList(),
                ShippingContact = orderContact,
                BillingContact  = orderContact,
                Number          = OrderService.GenerateNumber(1)
            };

            if (order.PaymentMethod.Type == PaymentType.QIWI)
            {
                order.PaymentDetails = new PaymentDetails()
                {
                    Phone = txtPhone.Text
                };
            }

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

            string email = txtEmailFrom.Text;

            string htmlOrderTable = OrderService.GenerateHtmlOrderCertificateTable(order.OrderCertificates,
                                                                                   CurrencyService.CurrentCurrency,
                                                                                   order.PaymentCost, order.TaxCost);

            var orderMailTemplate = new NewOrderMailTemplate(order.OrderID.ToString(), order.Number, email, string.Empty,
                                                             order.ArchivedShippingName,
                                                             order.ArchivedPaymentName, htmlOrderTable,
                                                             CurrencyService.CurrentCurrency.Iso3, order.Sum.ToString(),
                                                             order.CustomerComment,
                                                             OrderService.GetBillingLinkHash(order));

            orderMailTemplate.BuildMail();

            SendMail.SendMailNow(email, orderMailTemplate.Subject, orderMailTemplate.Body, true);
            SendMail.SendMailNow(SettingsMail.EmailForOrders, orderMailTemplate.Subject, orderMailTemplate.Body, true);

            return(OrderId);
        }