コード例 #1
0
ファイル: ImportManager.cs プロジェクト: ndhelix/s-t-r.ru
        /// <summary>
        /// Import customer list from XLS file
        /// </summary>
        /// <param name="filePath">Excel file path</param>
        public static void ImportCustomersFromXls(string filePath)
        {
            using (ExcelHelper excelHelper = new ExcelHelper(filePath))
            {
                excelHelper.Hdr  = "YES";
                excelHelper.Imex = "1";

                DataTable dt = excelHelper.ReadTable("Customers");
                foreach (DataRow dr in dt.Rows)
                {
                    int      customerId            = Convert.ToInt32(dr["CustomerId"]);
                    Guid     customerGuid          = new Guid(dr["CustomerGuid"].ToString());
                    string   email                 = dr["Email"].ToString();
                    string   username              = dr["Username"].ToString();
                    string   passwordHash          = dr["PasswordHash"].ToString();
                    string   saltKey               = dr["SaltKey"].ToString();
                    int      affiliateId           = Convert.ToInt32(dr["AffiliateId"]);
                    int      billingAddressId      = 0;
                    int      shippingAddressId     = 0;
                    int      lastPaymentMethodId   = 0;
                    string   lastAppliedCouponCode = string.Empty;
                    int      languageId            = Convert.ToInt32(dr["LanguageId"]);
                    int      currencyId            = Convert.ToInt32(dr["CurrencyId"]);
                    int      taxDisplayTypeId      = Convert.ToInt32(dr["TaxDisplayTypeId"]);
                    bool     isTaxExempt           = Convert.ToBoolean(dr["IsTaxExempt"]);
                    bool     isAdmin               = Convert.ToBoolean(dr["IsAdmin"]);
                    bool     isGuest               = Convert.ToBoolean(dr["IsGuest"]);
                    bool     isForumModerator      = Convert.ToBoolean(dr["IsForumModerator"]);
                    int      totalForumPosts       = Convert.ToInt32(dr["TotalForumPosts"]);
                    string   signature             = dr["Signature"].ToString();
                    string   adminComment          = dr["AdminComment"].ToString();
                    bool     active                = Convert.ToBoolean(dr["Active"]);
                    bool     deleted               = Convert.ToBoolean(dr["Deleted"]);
                    DateTime registrationDate      = DateTime.FromOADate(Convert.ToDouble(dr["RegistrationDate"]));
                    string   timeZoneId            = dr["TimeZoneId"].ToString();
                    int      avatarId              = Convert.ToInt32(dr["AvatarId"]);

                    //custom properties
                    string gender            = dr["Gender"].ToString();
                    string firstName         = dr["FirstName"].ToString();
                    string lastName          = dr["LastName"].ToString();
                    string company           = dr["Company"].ToString();
                    string streetAddress     = dr["StreetAddress"].ToString();
                    string streetAddress2    = dr["StreetAddress2"].ToString();
                    string zipPostalCode     = dr["ZipPostalCode"].ToString();
                    string city              = dr["City"].ToString();
                    string phoneNumber       = dr["PhoneNumber"].ToString();
                    string faxNumber         = dr["FaxNumber"].ToString();
                    int    countryId         = Convert.ToInt32(dr["CountryId"]);
                    int    stateProvinceId   = Convert.ToInt32(dr["StateProvinceId"]);
                    bool   receiveNewsletter = Convert.ToBoolean(dr["ReceiveNewsletter"]);


                    var customer = CustomerManager.GetCustomerByEmail(email);
                    if (customer == null)
                    {
                        //no customers found
                        customer = CustomerManager.AddCustomerForced(customerGuid, email, username,
                                                                     passwordHash, saltKey, affiliateId, billingAddressId, shippingAddressId, lastPaymentMethodId,
                                                                     lastAppliedCouponCode, string.Empty, string.Empty,
                                                                     languageId, currencyId, (TaxDisplayTypeEnum)taxDisplayTypeId, isTaxExempt,
                                                                     isAdmin, isGuest, isForumModerator, totalForumPosts, signature,
                                                                     adminComment, active, deleted, registrationDate, timeZoneId, avatarId);
                    }
                    else
                    {
                        if (!customer.IsGuest)
                        {
                            //customer is not a guest
                            customer = CustomerManager.UpdateCustomer(customer.CustomerId, customer.CustomerGuid,
                                                                      email, username, passwordHash, saltKey, affiliateId, billingAddressId,
                                                                      shippingAddressId, lastPaymentMethodId, lastAppliedCouponCode,
                                                                      string.Empty, string.Empty, languageId, currencyId,
                                                                      (TaxDisplayTypeEnum)taxDisplayTypeId, isTaxExempt, isAdmin, isGuest,
                                                                      isForumModerator, totalForumPosts, signature, adminComment,
                                                                      active, deleted, registrationDate, timeZoneId, avatarId);
                        }
                        else
                        {
                            //customer is a guest
                            customer = CustomerManager.GetCustomerByGuid(customerGuid);
                            if (customer == null)
                            {
                                customer = CustomerManager.AddCustomerForced(customerGuid, email, username,
                                                                             passwordHash, saltKey, affiliateId, billingAddressId, shippingAddressId, lastPaymentMethodId,
                                                                             lastAppliedCouponCode, string.Empty,
                                                                             string.Empty, languageId, currencyId,
                                                                             (TaxDisplayTypeEnum)taxDisplayTypeId, isTaxExempt,
                                                                             isAdmin, isGuest, isForumModerator, totalForumPosts, signature,
                                                                             adminComment, active, deleted, registrationDate, timeZoneId, avatarId);
                            }
                            else
                            {
                                customer = CustomerManager.UpdateCustomer(customer.CustomerId, customer.CustomerGuid,
                                                                          email, username, passwordHash, saltKey, affiliateId, billingAddressId,
                                                                          shippingAddressId, lastPaymentMethodId, lastAppliedCouponCode,
                                                                          string.Empty, string.Empty, languageId, currencyId,
                                                                          (TaxDisplayTypeEnum)taxDisplayTypeId, isTaxExempt, isAdmin, isGuest,
                                                                          isForumModerator, totalForumPosts, signature, adminComment,
                                                                          active, deleted, registrationDate, timeZoneId, avatarId);
                            }
                        }
                    }

                    if (CustomerManager.FormFieldGenderEnabled)
                    {
                        customer.Gender = gender;
                    }
                    customer.FirstName = firstName;
                    customer.LastName  = lastName;
                    if (CustomerManager.FormFieldCompanyEnabled)
                    {
                        customer.Company = company;
                    }
                    if (CustomerManager.FormFieldStreetAddressEnabled)
                    {
                        customer.StreetAddress = streetAddress;
                    }
                    if (CustomerManager.FormFieldStreetAddress2Enabled)
                    {
                        customer.StreetAddress2 = streetAddress2;
                    }
                    if (CustomerManager.FormFieldPostCodeEnabled)
                    {
                        customer.ZipPostalCode = zipPostalCode;
                    }
                    if (CustomerManager.FormFieldCityEnabled)
                    {
                        customer.City = city;
                    }
                    if (CustomerManager.FormFieldPhoneEnabled)
                    {
                        customer.PhoneNumber = phoneNumber;
                    }
                    if (CustomerManager.FormFieldFaxEnabled)
                    {
                        customer.FaxNumber = faxNumber;
                    }
                    if (CustomerManager.FormFieldCountryEnabled)
                    {
                        customer.CountryId = countryId;
                    }
                    if (CustomerManager.FormFieldStateEnabled)
                    {
                        customer.StateProvinceId = stateProvinceId;
                    }
                    customer.ReceiveNewsletter = receiveNewsletter;
                }
            }
        }