Пример #1
0
        private customer_address_entity SyncEntityAddress(SortedDictionary <string, eav_attribute> addressAttributeList, CustomerHelper helper, XElement customer, customer_entity entity, int address_line_id)
        {
            var addressNode = customer.Element("AddressInformation");

            var phonesNode = customer.Element("TelephoneNumbers").Elements("TelephoneNumber");

            string mainPhone = String.Empty;

            if (phonesNode.Count() > 0)
            {
                mainPhone = phonesNode.FirstOrDefault(x => x.Element("TelephoneType").Value == "TEL").Try(x => x.Element("AreaCode").Value + " " + x.Element("Number").Value, string.Empty);
            }
            else
            {
                mainPhone = "0123456789";
            }

            string addressLines = addressNode.Element("AddressLine1").Value + "\n" + addressNode.Element("AddressLine2").Value;

            string companyName = customer.Element("Name").Value;

            customer_address_entity addressEntity = helper.GetCustomerAddress(entity.entity_id, address_line_id);

            if (addressEntity == null)
            {
                addressEntity = new customer_address_entity()
                {
                    parent_id        = entity.entity_id,
                    entity_type_id   = CUSTOMER_ADDRESS_ENTITY_TYPE_ID,
                    is_active        = true,
                    attribute_set_id = 0,
                    increment_id     = "0",
                    created_at       = DateTime.Now,
                    updated_at       = DateTime.Now,
                };
            }

            helper.SyncAddress(addressEntity);

            helper.SyncAttributeValue(addressAttributeList["address_line_id"].attribute_id, CUSTOMER_ADDRESS_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, addressEntity.entity_id, address_line_id, "int");

            helper.SyncAttributeValue(addressAttributeList["company"].attribute_id, CUSTOMER_ADDRESS_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, addressEntity.entity_id, companyName, "varchar");

            helper.SyncAttributeValue(addressAttributeList["firstname"].attribute_id, CUSTOMER_ADDRESS_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, addressEntity.entity_id, "-", "varchar");
            helper.SyncAttributeValue(addressAttributeList["lastname"].attribute_id, CUSTOMER_ADDRESS_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, addressEntity.entity_id, "-", "varchar");

            helper.SyncAttributeValue(addressAttributeList["street"].attribute_id, CUSTOMER_ADDRESS_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, addressEntity.entity_id, addressLines, "text");
            helper.SyncAttributeValue(addressAttributeList["postcode"].attribute_id, CUSTOMER_ADDRESS_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, addressEntity.entity_id, addressNode.Element("ZipCode").Value, "varchar");
            helper.SyncAttributeValue(addressAttributeList["city"].attribute_id, CUSTOMER_ADDRESS_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, addressEntity.entity_id, addressNode.Element("City").Value, "varchar");
            helper.SyncAttributeValue(addressAttributeList["country_id"].attribute_id, CUSTOMER_ADDRESS_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, addressEntity.entity_id, addressNode.Element("Country").Value, "varchar");

            helper.SyncAttributeValue(addressAttributeList["telephone"].attribute_id, CUSTOMER_ADDRESS_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, addressEntity.entity_id, mainPhone, "varchar");


            return(addressEntity);
        }
Пример #2
0
        private long SyncAddress(magentoContext container, string country,
                                 eav_attribute countryAttribute, string city,
                                 eav_attribute cityAttribute, string telephones,
                                 eav_attribute telephoneAttribute,
                                 long backEndRelationID,
                                 string company,
                                 eav_attribute companyAttribute, string postcode,
                                 eav_attribute postcodeAttribute, eav_attribute firstNameAddressAttribute,
                                 eav_attribute lastNameAddressAttribute, eav_attribute telephoneAddressAttribute,
                                 string addressLines, eav_attribute addressLinesAttribute,
                                 long addressEntityID = 0)
        {
            customer_address_entity customerAddressEntity = null;

            if (addressEntityID == 0)
            {
                customerAddressEntity = container.customer_address_entity.FirstOrDefault(c => c.parent_id == backEndRelationID && c.customer_address_entity_text.Any(al => al.value == addressLines));
                if (customerAddressEntity == null)
                {
                    customerAddressEntity = new customer_address_entity()
                    {
                        entity_type_id   = 2,
                        attribute_set_id = 0,
                        increment_id     = "0",
                        parent_id        = backEndRelationID,
                        created_at       = DateTime.Now,
                        updated_at       = DateTime.Now,
                        is_active        = true
                    };
                    container.AddTocustomer_address_entity(customerAddressEntity);
                    container.SaveChanges();
                }
                addressEntityID = customerAddressEntity.entity_id;
            }

            SyncCustomerAddressEntityVarchar(container, telephoneAttribute, (c => c.entity_id == addressEntityID), addressEntityID, telephones);
            SyncCustomerAddressEntityVarchar(container, countryAttribute, (c => c.entity_id == addressEntityID), addressEntityID, country);
            SyncCustomerAddressEntityVarchar(container, cityAttribute, (c => c.entity_id == addressEntityID), addressEntityID, city);
            SyncCustomerAddressEntityVarchar(container, companyAttribute, (c => c.entity_id == addressEntityID), addressEntityID, company);
            SyncCustomerAddressEntityVarchar(container, postcodeAttribute, (c => c.entity_id == addressEntityID), addressEntityID, postcode);
            SyncCustomerAddressEntityVarchar(container, firstNameAddressAttribute, (c => c.entity_id == addressEntityID), addressEntityID);
            SyncCustomerAddressEntityVarchar(container, lastNameAddressAttribute, (c => c.entity_id == addressEntityID), addressEntityID);
            SyncCustomerAddressEntityVarchar(container, telephoneAddressAttribute, (c => c.entity_id == addressEntityID), addressEntityID);
            SyncCustomerAddressEntityText(container, addressLinesAttribute, (c => c.entity_id == addressEntityID), addressLines, addressEntityID);

            return(addressEntityID);
        }
Пример #3
0
        public void SyncAddress(customer_address_entity addressEntity)
        {
            if (syncAddressCommand == null)
            {
                syncAddressCommand             = Connection.CreateCommand();
                syncAddressCommand.CommandType = CommandType.Text;
                syncAddressCommand.CommandText = String.Format(@"INSERT INTO {0} (entity_id, entity_type_id, attribute_set_id, increment_id, parent_id, created_at, updated_at, is_active)
                                                         VALUES (?entity_id, ?entity_type_id, ?attribute_set_id, ?increment_id, ?parent_id, ?created_at, ?updated_at, ?is_active)
                                                         ON DUPLICATE KEY UPDATE is_active = VALUES(is_active)
                                                        ", _tablesNames["customer_address_entity"]);

                syncAddressCommand.Prepare();
                syncAddressCommand.Parameters.AddWithValue("?entity_id", null);
                syncAddressCommand.Parameters.AddWithValue("?entity_type_id", 0);
                syncAddressCommand.Parameters.AddWithValue("?attribute_set_id", 0);
                syncAddressCommand.Parameters.AddWithValue("?increment_id", String.Empty);
                syncAddressCommand.Parameters.AddWithValue("?parent_id", 0);
                syncAddressCommand.Parameters.AddWithValue("?created_at", DateTime.Now);
                syncAddressCommand.Parameters.AddWithValue("?updated_at", DateTime.Now);
                syncAddressCommand.Parameters.AddWithValue("?is_active", true);
            }



            syncAddressCommand.Parameters["?entity_id"].Value        = addressEntity.entity_id;
            syncAddressCommand.Parameters["?entity_type_id"].Value   = addressEntity.entity_type_id;
            syncAddressCommand.Parameters["?attribute_set_id"].Value = addressEntity.attribute_set_id;
            syncAddressCommand.Parameters["?increment_id"].Value     = addressEntity.increment_id;
            syncAddressCommand.Parameters["?parent_id"].Value        = addressEntity.parent_id;
            syncAddressCommand.Parameters["?created_at"].Value       = addressEntity.created_at;
            syncAddressCommand.Parameters["?updated_at"].Value       = addressEntity.updated_at;
            syncAddressCommand.Parameters["?is_active"].Value        = addressEntity.is_active;

            syncAddressCommand.ExecuteNonQuery();

            if (addressEntity.entity_id == 0)
            {
                addressEntity.entity_id = (int)syncAddressCommand.LastInsertedId;
            }
        }
Пример #4
0
        private void ProcessCustomerNode(List <string> emailAddressCodes, SortedDictionary <string, eav_attribute> attributeList, SortedDictionary <string, eav_attribute> addressAttributeList,
                                         Dictionary <int, customer_entity> existingCustomers, HashSet <string> existingEmailAddresses, CustomerHelper helper, CustomerXmlRecord record)
        {
            var customer = record.Node;

            int backendRelationId       = Convert.ToInt32(customer.Attribute("BackendRelationID").Value);
            int parentBackendRelationId = 0;

            if (record.Parent != null)
            {
                parentBackendRelationId = Convert.ToInt32(record.Parent.Node.Attribute("BackendRelationID").Value);
            }

            customer_entity entity = null;

            bool prefixEmailRecords = false;


            var electronicAddressesNode = customer.Element("Addresses").Elements("Address").Where(x => x.Element("ElectronicAddressType") != null &&
                                                                                                  emailAddressCodes.Contains(x.Element("ElectronicAddressType").Value)
                                                                                                  );

            //filter bas addresses
            electronicAddressesNode = electronicAddressesNode.Where(x =>
                                                                    !x.Element("ElectronicAddress").Value.Contains("basdistributie.") &&
                                                                    !x.Element("ElectronicAddress").Value.Contains("basdistribution.") &&
                                                                    !x.Element("ElectronicAddress").Value.Contains("basgroup."));



            if (electronicAddressesNode.Count() == 0)
            {
                if (parentBackendRelationId > 0)
                {
                    var parent = GetBillToAddress(record);
                    electronicAddressesNode = parent.Element("Addresses").Elements("Address").Where(x => x.Element("ElectronicAddressType") != null &&
                                                                                                    emailAddressCodes.Contains(x.Element("ElectronicAddressType").Value));

                    //filter bas addresses
                    electronicAddressesNode = electronicAddressesNode.Where(x =>
                                                                            !x.Element("ElectronicAddress").Value.Contains("basdistributie.") &&
                                                                            !x.Element("ElectronicAddress").Value.Contains("basdistribution.") &&
                                                                            !x.Element("ElectronicAddress").Value.Contains("basgroup."));

                    prefixEmailRecords = true;
                }

                if (electronicAddressesNode.Count() == 0)
                {
                    Logger.WarnFormat("Ignoring customer {0}, because no email address exists", backendRelationId);
                    return;
                }
            }


            string mainEmailAddress = electronicAddressesNode
                                      .OrderBy(e => emailAddressCodes.IndexOf(e.Element("ElectronicAddressType").Value)).FirstOrDefault().Try(x => x.Element("ElectronicAddress").Value, null);


            if (String.IsNullOrEmpty(mainEmailAddress))
            {
                if (parentBackendRelationId == 0)
                {
                    Logger.WarnFormat("Ignoring customer {0}, because primary email address is empty", backendRelationId);
                    return;
                }
                else
                {
                    // get parents address
                }
            }

            string financialEmailAddress = electronicAddressesNode.FirstOrDefault(x => x.Element("ElectronicAddressType").Value == "F").Try(x => x.Element("ElectronicAddress").Value, null);

            if (prefixEmailRecords)
            {
                mainEmailAddress      = String.Format("{0}_{1}", backendRelationId, mainEmailAddress);
                financialEmailAddress = String.Format("{0}_{1}", backendRelationId, financialEmailAddress);
            }


            if (!existingCustomers.TryGetValue(backendRelationId, out entity))
            {
                if (existingEmailAddresses.Contains(mainEmailAddress))
                {
                    Logger.WarnFormat("Ignoring customer {0}, because email address {1} is already exists", backendRelationId, mainEmailAddress);
                    return;
                }


                //create new
                entity = new customer_entity()
                {
                    entity_id        = backendRelationId,
                    entity_type_id   = CUSTOMER_ENTITY_TYPE_ID,
                    attribute_set_id = 0,
                    website_id       = 1,
                    group_id         = 1,
                    store_id         = 1,
                    created_at       = DateTime.Now,
                    updated_at       = DateTime.Now,
                    is_active        = true,
                    increment_id     = "",
                    email            = mainEmailAddress
                };

                helper.AddCustomer(entity);
            }
            else
            {
                if (mainEmailAddress != entity.email)
                {
                    Logger.WarnFormat("Cannot change email (for now), relation : {0}", backendRelationId);
                    return;
                }
            }


            #region Password

            string password = HttpUtility.HtmlDecode(customer.Element("Password").Value.Trim());
            var    hasher   = System.Security.Cryptography.MD5.Create();

            var bytes = hasher.ComputeHash(System.Text.Encoding.Default.GetBytes(password));

            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data
            // and format each one as a hexadecimal string.
            for (int i = 0; i < bytes.Length; i++)
            {
                sBuilder.Append(bytes[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            helper.SyncAttributeValue(attributeList["password_hash"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, sBuilder.ToString());
            #endregion

            #region Entity Attributes


            helper.SyncAttributeValue(attributeList["company_name"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, customer.Element("Name").Value);

            helper.SyncAttributeValue(attributeList["firstname"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, "-");
            helper.SyncAttributeValue(attributeList["lastname"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, "-");

            helper.SyncAttributeValue(attributeList["account_number"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, backendRelationId, type: "int");

            helper.SyncAttributeValue(attributeList["created_in"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, "JD Edwards");


            string taxNumber = customer.Element("TaxNumber").Try(x => x.Value.Trim(), string.Empty);

            helper.SyncAttributeValue(attributeList["taxvat"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, taxNumber, type: "varchar");

            var accountManagerNode = customer.Element("AccountManager");

            string accountManagerName = String.Empty;

            if (accountManagerNode != null)
            {
                accountManagerName = accountManagerNode.Element("Name").Value;
            }


            helper.SyncAttributeValue(attributeList["account_manager_name"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, accountManagerName);

            #region Financial Attributes
            string  paymentInstrument = customer.Element("PaymentInstrument").Try(x => x.Value, string.Empty);
            string  paymentDays       = customer.Element("PaymentDays").Try(x => x.Value, string.Empty);
            string  routeCode         = customer.Element("RouteCode").Try(x => x.Value, string.Empty);
            decimal invoiceAmount     = customer.Element("InvoiceAmount").Try(x => Convert.ToDecimal(x.Value), 0);
            decimal invoiceAmountOpen = customer.Element("OpenInvoiceAmount").Try(x => Convert.ToDecimal(x.Value), 0);
            decimal creditLimit       = customer.Element("CreditLimit").Try(x => Convert.ToDecimal(x.Value), 0);

            helper.SyncAttributeValue(attributeList["payment_instrument"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, paymentInstrument, type: "varchar");
            helper.SyncAttributeValue(attributeList["payment_days"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, paymentDays, type: "varchar");

            helper.SyncAttributeValue(attributeList["credit_limit"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, creditLimit, type: "decimal");
            helper.SyncAttributeValue(attributeList["invoice_amount"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, invoiceAmount, type: "decimal");
            helper.SyncAttributeValue(attributeList["invoice_amount_open"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, invoiceAmountOpen, type: "decimal");

            helper.SyncAttributeValue(attributeList["route_code"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, routeCode, type: "varchar");

            #endregion

            #region Logistic Attributes

            int    defaultCarrierId   = customer.Element("DefaultCarrier").Try(x => Convert.ToInt32(x.Value), 0);
            string defaultCarrierName = customer.Element("DefaultCarrierName").Try(x => x.Value, string.Empty);

            helper.SyncAttributeValue(attributeList["default_carrier"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, defaultCarrierId, type: "int");
            helper.SyncAttributeValue(attributeList["default_carrier_name"].attribute_id, CUSTOMER_ENTITY_TYPE_ID, StoreList[CurrentStoreCode].store_id, backendRelationId, defaultCarrierName, type: "varchar");

            #endregion


            #endregion

            #region Addresses

            customer_address_entity addressEntity = SyncEntityAddress(addressAttributeList, helper, customer, entity, entity.entity_id);

            if (record.Children.Count() == 0) // no children, so default shipping remains here
            {
                helper.SyncAttributeValue(attributeList["default_shipping"].attribute_id, entity.entity_type_id, StoreList[CurrentStoreCode].store_id, entity.entity_id, addressEntity.entity_id, "int");
            }

            if (record.Parent == null)
            {
                //top level
                helper.SyncAttributeValue(attributeList["default_billing"].attribute_id, entity.entity_type_id, StoreList[CurrentStoreCode].store_id, entity.entity_id, addressEntity.entity_id, "int");
            }
            else
            {
                var billToAddress = GetBillToAddress(record);
                customer_address_entity billTo = SyncEntityAddress(addressAttributeList, helper, billToAddress,
                                                                   entity, address_line_id: Convert.ToInt32(billToAddress.Attribute("BackendRelationID").Value));

                helper.SyncAttributeValue(attributeList["default_billing"].attribute_id, entity.entity_type_id, StoreList[CurrentStoreCode].store_id, entity.entity_id, billTo.entity_id, "int");
            }

            if (record.Children.Count() > 0)
            {
                var shipToAddresses = GetChildAddresses(record.Children);

                foreach (var address in shipToAddresses)
                {
                    customer_address_entity childAddress = SyncEntityAddress(addressAttributeList, helper, address, entity, address_line_id: Convert.ToInt32(address.Attribute("BackendRelationID").Value));
                }
            }

            #endregion
        }