public BPAddress AddAddress(string cardCode, int addressType, BPAddress addr) { var a = DatabaseSAP.Addresses .FromSql("usp_PortalAddSAPAddress @p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12", 0.ToString(), cardCode, addressType.ToString(), addr.AddressName, addr.Street, addr.Block, addr.City, addr.StateCode, addr.ZipCode, addr.CountryCode, addr.PhoneNumber, 0.ToString(), 1.ToString()).FirstOrDefault(); return(a); }
/// <summary> /// Creates a BP. /// </summary> /// <param name="o">The Amazon order object.</param> /// <returns>The BP's CardCode or null on error.</returns> public string CreateBusinessPartner(AmazonOrder o) { try { // If state is longer than two characters, find abbreviation and update Amazon order. // If unable to resolve state, throw exception. string stateCode = null; if (o.StateOrRegion?.Length > 2) // potentially full name of state { var s = _sapDataService.GetStateCode(o.StateOrRegion); stateCode = s.StateCode; if (!s.StateCode.IsNullOrWhitespace()) // abbreviation found: Set Amazon-order stateCode to abbreviation { o.StateOrRegion = stateCode; } else // unable to find stateCode. Allow BP creation to continue (allowing for correction), even though invoice creation will fail. { o.StateOrRegion = null; _amazonOrderLogRepository.LogEvent( o.AmazonOrderId, "Shipping state could not be resolved. Please update manually, as invoice creation will fail.", nameof(LogEventLevel.Error), "Bast", "DIAPI_Services", StackExtensions.GetCurrentMethod(), "HAL 9000"); } } // Get Matching BPs from SAP for user's email. var bp = _sapDataService.GetCardCodeForEmail(o.BuyerEmail?.Trim()); if (bp.Count > 0) // Matching BP found. Grab first CardCode in list (most recent Sales Order date) and return. { if (bp.Count > 1) // At least one dupe BP. Deactivate. { int i = _sapDataService.DeactivateRedundantBPs(o.BuyerEmail?.Trim(), bp[0].CardCode); } // Add both shipping and billing addresses using the Amazon order's Shipping address var addr = new BPAddress { AddressName = o.AddressName, Street = o.AddressLine1, Block = o.AddressLine2, City = o.City, StateCode = o.StateOrRegion, ZipCode = o.PostalCode, CountryCode = o.CountryCode, PhoneNumber = o.Phone }; _sapDataService.AddAddress(bp[0].CardCode, 1, addr); // shipping _sapDataService.AddAddress(bp[0].CardCode, 2, addr); // billing _amazonOrderLogRepository.LogEvent( o.AmazonOrderId, "BP " + bp[0].CardCode + " already exists. Billing and shipping addresses updated.", nameof(LogEventLevel.Information), "Bast", "DIAPI_Services", StackExtensions.GetCurrentMethod(), "HAL 9000"); _unitOfWorkMarketplace.Commit(); return(bp[0].CardCode); } // create new Business Partner DIAPI_Objects.BusinessPartner = (BusinessPartners)DIAPI_Objects.Company.GetBusinessObject(BoObjectTypes.oBusinessPartners); DIAPI_Objects.BusinessPartner.CardName = o.BuyerName; DIAPI_Objects.BusinessPartner.CardType = BoCardTypes.cCustomer; DIAPI_Objects.BusinessPartner.Phone1 = o.Phone; DIAPI_Objects.BusinessPartner.EmailAddress = !o.BuyerEmail.IsNullOrWhitespace() ? o.BuyerEmail : "*****@*****.**"; DIAPI_Objects.BusinessPartner.ContactPerson = o.BuyerName; DIAPI_Objects.BusinessPartner.Notes = "Auto-created by Marketplace App"; DIAPI_Objects.BusinessPartner.PayTermsGrpCode = 5; // Credit Card DIAPI_Objects.BusinessPartner.Valid = BoYesNoEnum.tYES; DIAPI_Objects.BusinessPartner.Addresses.Add(); // Shipping DIAPI_Objects.BusinessPartner.Addresses.SetCurrentLine(0); DIAPI_Objects.BusinessPartner.Addresses.AddressType = BoAddressType.bo_ShipTo; DIAPI_Objects.BusinessPartner.Addresses.AddressName = o.AddressName; DIAPI_Objects.BusinessPartner.Addresses.Street = o.AddressLine1; DIAPI_Objects.BusinessPartner.Addresses.Block = o.AddressLine2; DIAPI_Objects.BusinessPartner.Addresses.City = o.City; DIAPI_Objects.BusinessPartner.Addresses.State = o.StateOrRegion; DIAPI_Objects.BusinessPartner.Addresses.ZipCode = o.PostalCode; DIAPI_Objects.BusinessPartner.Addresses.Country = o.CountryCode; DIAPI_Objects.BusinessPartner.Addresses.UserFields.Fields.Item("U_MarketingOptOut").Value = 0; // Billing DIAPI_Objects.BusinessPartner.Addresses.SetCurrentLine(1); DIAPI_Objects.BusinessPartner.Addresses.AddressType = BoAddressType.bo_BillTo; DIAPI_Objects.BusinessPartner.Addresses.AddressName = o.AddressName; DIAPI_Objects.BusinessPartner.Addresses.Street = o.AddressLine1; DIAPI_Objects.BusinessPartner.Addresses.Block = o.AddressLine2; DIAPI_Objects.BusinessPartner.Addresses.City = o.City; DIAPI_Objects.BusinessPartner.Addresses.State = o.StateOrRegion; DIAPI_Objects.BusinessPartner.Addresses.ZipCode = o.PostalCode; DIAPI_Objects.BusinessPartner.Addresses.Country = o.CountryCode; DIAPI_Objects.BusinessPartner.Addresses.UserFields.Fields.Item("U_MarketingOptOut").Value = 0; DIAPI_Objects.BusinessPartner.CardCode = GetNextID("BusinessPartner");; // contact info DIAPI_Objects.BusinessPartner.ContactEmployees.Add(); DIAPI_Objects.BusinessPartner.ContactEmployees.SetCurrentLine(0); DIAPI_Objects.BusinessPartner.ContactEmployees.Name = o.BuyerName; DIAPI_Objects.BusinessPartner.ContactEmployees.Address = o.AddressLine1 + ", " + o.City + ", " + o.StateOrRegion + " " + o.PostalCode + " " + o.CountryCode; DIAPI_Objects.BusinessPartner.ContactEmployees.E_Mail = o.BuyerEmail; DIAPI_Objects.BusinessPartner.ContactEmployees.Phone1 = o.Phone; RetCode = DIAPI_Objects.BusinessPartner.Add(); // Save CardCode to log and update OrderHeader and UserAccount string CardCode = DIAPI_Objects.BusinessPartner.CardCode; if (RetCode != 0) { DIAPI_Objects.Company.GetLastError(out errCode, out errMsg); _amazonOrderLogRepository.LogEvent( o.AmazonOrderId, errCode + " - " + errMsg + " - BP was not created.", nameof(LogEventLevel.Critical), "Bast", "DIAPI_Services", StackExtensions.GetCurrentMethod(), "HAL 9000"); } else { _amazonOrderLogRepository.LogEvent( o.AmazonOrderId, "BP " + CardCode + " was created.", nameof(LogEventLevel.Information), "Bast", "DIAPI_Services", StackExtensions.GetCurrentMethod(), "HAL 9000"); } _unitOfWorkMarketplace.Commit(); return(CardCode); } catch (Exception ex) { _amazonOrderLogRepository.LogEvent( o.AmazonOrderId, ex.GetType().Name + " - " + ex.Message, nameof(LogEventLevel.Critical), "Bast", "DIAPI_Services", StackExtensions.GetCurrentMethod(), "HAL 9000"); _unitOfWorkMarketplace.Commit(); return(null); } }