private static CustomerXmlRecord GetCustomerTreeNode(XElement parent, XElement node, IEnumerable <XElement> customers) { var curNode = new CustomerXmlRecord { Node = node, BackendRelationID = Convert.ToInt32(node.Attribute("BackendRelationID").Value), AddressType = node.Element("AddressInformation").Element("AddressType").Value }; curNode.Children = (from child in customers where child.Attribute("ParentBackendRelationID").Value == node.Attribute("BackendRelationID").Value select GetCustomerTreeNode(node, child, customers) ).ToList(); curNode.Parent = (from p in customers where p.Attribute("BackendRelationID").Value == node.Attribute("ParentBackendRelationID").Value select new CustomerXmlRecord { Node = p, BackendRelationID = Convert.ToInt32(p.Attribute("BackendRelationID").Value), AddressType = p.Element("AddressInformation").Element("AddressType").Value } ).FirstOrDefault(); return(curNode); }
private XElement GetBillToAddress(CustomerXmlRecord record) { var parent = record.Parent; while (parent != null) { if (parent.Parent == null) { break; } parent = parent.Parent; } return(parent.Node); }
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 }