public static void SavePerson(ref int persID, ref bool blnSaveSuccess, PersonDetails persDetails) { if (!SecurityManager.IsCurrentUserInRole("Data Mtce: Account Edit")) throw new System.Security.SecurityException("You are not authorized to update contact details."); IDalSession session = NHSessionFactory.CreateSession(); try { IContactPerson person = null; Address postalAddress = null; Address residentialAddress = null; IContactsNAW newNaw = null; IContactsNAW currentNaw = null; IContactsIntroducer currentIntroducer = null; IContactsIntroducer newIntroducer = null; bool boolNawInsert = true; IManagementCompany company = LoginMapper.GetCurrentManagmentCompany(session); bool loggedInAsStichting = company.IsStichting; if (persID != 0) { person = GetContact(session, persID); currentNaw = person.CurrentNAW; currentIntroducer = person.CurrentIntroducer; } else { person = new ContactPerson(); currentNaw = new ContactsNAW(); } // check if bsn does not already exists long bsnEntries = session.Session.GetNamedQuery( "B4F.TotalGiro.CRM.Contact.CheckBSNIsUnique") .SetParameter("personId", persID) .SetParameter("companyId", (person.AssetManager != null ? person.AssetManager.Key : company.Key)) .SetParameter("bsn", persDetails.BurgerServiceNummer) .UniqueResult<long>(); if (bsnEntries > 0) throw new ApplicationException("It is not possible to enter this person since the bsn already exists"); person.IsActive = Convert.ToBoolean(persDetails.IsActive); person.HasMinimumData = persDetails.HasMinimumData; if (person.Identification == null) person.Identification = new Identification(); if (person.Identification.IdentificationType == null) person.Identification.IdentificationType = new IdentificationType(); if (person.ContactDetails == null) person.ContactDetails = new ContactDetails(); if (person.ContactDetails.Fax == null) person.ContactDetails.Fax = new TelephoneNumber(); if (person.ContactDetails.Telephone == null) person.ContactDetails.Telephone = new TelephoneNumber(); if (person.ContactDetails.Mobile == null) person.ContactDetails.Mobile = new TelephoneNumber(); if (person.ContactDetails.TelephoneAH == null) person.ContactDetails.TelephoneAH = new TelephoneNumber(); if (persDetails.Introducer != null && persDetails.Introducer.Length > 0 && Convert.ToInt32(persDetails.Introducer) != int.MinValue) { newIntroducer = new ContactsIntroducer(); newIntroducer.Remisier = RemisierMapper.GetRemisier(session, Convert.ToInt32(persDetails.Introducer)); newIntroducer.RemisierEmployee = RemisierEmployeeMapper.GetRemisierEmployee(session, Convert.ToInt32(persDetails.IntroducerEmployee)); } if (persDetails.InternetEnabled.Length > 0) { if (persDetails.InternetEnabled.Equals(InternetEnabled.No.ToString())) person.InternetEnabled = InternetEnabled.No; else if (persDetails.InternetEnabled.Equals(InternetEnabled.Yes.ToString())) person.InternetEnabled = InternetEnabled.Yes; else person.InternetEnabled = InternetEnabled.Unknown; } else person.InternetEnabled = InternetEnabled.Unknown; if (int.Parse(persDetails.IDType) != int.MinValue) { person.Identification.Number = persDetails.IDNumber; if (persDetails.IDExpirationDate.Length > 0) { person.Identification.ValidityPeriod = Convert.ToDateTime(persDetails.IDExpirationDate); } else { person.Identification.ValidityPeriod = DateTime.MinValue; } person.Identification.IdentificationType.Key = Convert.ToInt32(persDetails.IDType); } else person.Identification = null; person.ContactDetails.SendNewsItem = persDetails.SendNewsItem; person.ContactDetails.Email = persDetails.Email; person.ContactDetails.Mobile.Number = persDetails.Mobile; person.ContactDetails.Telephone.Number = persDetails.Telephone; person.ContactDetails.TelephoneAH.Number = persDetails.TelephoneAH; person.ContactDetails.Fax.Number = persDetails.Fax; if (persDetails.Street.Length > 0 || persDetails.HouseNumber.Length > 0 || persDetails.HouseNumberSuffix.Length > 0 || persDetails.Postalcode.Length > 0 || persDetails.City.Length > 0) { residentialAddress = new Address(); residentialAddress.Street = persDetails.Street; residentialAddress.HouseNumber = persDetails.HouseNumber; residentialAddress.HouseNumberSuffix = persDetails.HouseNumberSuffix; residentialAddress.PostalCode = persDetails.Postalcode; residentialAddress.City = persDetails.City; if (int.Parse(persDetails.Country) != int.MinValue) residentialAddress.Country = CountryMapper.GetCountry(session, Convert.ToInt32(persDetails.Country)); else residentialAddress.Country = null; } if (persDetails.PostalStreet.Length > 0 || persDetails.PostalHouseNumber.Length > 0 || persDetails.PostalHouseNumberSuffix.Length > 0 || persDetails.PostalPostalcode.Length > 0 || persDetails.PostalCity.Length > 0) { postalAddress = new Address(); postalAddress.Street = persDetails.PostalStreet; postalAddress.HouseNumber = persDetails.PostalHouseNumber; postalAddress.HouseNumberSuffix = persDetails.PostalHouseNumberSuffix; postalAddress.PostalCode = persDetails.PostalPostalcode; postalAddress.City = persDetails.PostalCity; if (int.Parse(persDetails.PostalCountry) != int.MinValue) postalAddress.Country = CountryMapper.GetCountry(session, Convert.ToInt32(persDetails.PostalCountry)); else postalAddress.Country = null; } if (residentialAddress != null && postalAddress == null) postalAddress = residentialAddress; else if (residentialAddress == null && postalAddress != null) residentialAddress = postalAddress; newNaw = new ContactsNAW(persDetails.LastName, postalAddress, residentialAddress); if (currentNaw.Name == null || (currentNaw != null && !currentNaw.Name.Equals(newNaw.Name))) boolNawInsert = true; // Then compare addresses else if (currentNaw.Name.Equals(newNaw.Name) && residentialAddress != null) { if (currentNaw.ResidentialAddress != null) { // If addresses the same: no need for an insert if (newNaw.Equals(currentNaw)) boolNawInsert = false; } else boolNawInsert = true; } else { newNaw = new ContactsNAW(); newNaw.Name = persDetails.LastName; boolNawInsert = true; } if (boolNawInsert) { person.ContactsNAWs.Add(newNaw); person.CurrentNAW = newNaw; } if (currentIntroducer != null || newIntroducer != null) { if (!(currentIntroducer != null && newIntroducer != null && newIntroducer.Equals(currentIntroducer))) { person.ContactsIntroducers.Add(newIntroducer); person.CurrentIntroducer = newIntroducer; } } person.Title = persDetails.Title; person.FirstName = persDetails.Initials; person.MiddleName = persDetails.MiddleName; if (persDetails.Gender.Length > 0) person.Gender = (Convert.ToInt32(persDetails.Gender).Equals((int)Gender.Male) ? Gender.Male : Gender.Female); if (persDetails.ResidentialState.Length > 0) person.ResidentialState = (Convert.ToInt32(persDetails.ResidentialState).Equals((int)ResidentStatus.Resident) ? ResidentStatus.Resident : ResidentStatus.NonResident); if (int.Parse(persDetails.Nationality) != int.MinValue) { if (person.Nationality == null || person.Nationality.Key != int.Parse(persDetails.Nationality)) person.Nationality = NationalityMapper.GetNationality(session, Convert.ToInt32(persDetails.Nationality)); } person.BurgerServiceNummer = persDetails.BurgerServiceNummer; if (persDetails.BirthDate.Length > 0) person.DateOfBirth = Convert.ToDateTime(persDetails.BirthDate); else person.DateOfBirth = DateTime.MinValue; if (person.CurrentNAW.Name.Length > 0 && person.FirstName.Length > 0 && Util.IsNotNullDate(person.DateOfBirth) && persDetails.Gender.Length > 0 && person.Nationality != null && (person.Identification != null && person.Identification.Number.Length > 0 && Util.IsNotNullDate(person.Identification.ValidityPeriod)) && person.BurgerServiceNummer.Length > 0 && person.InternetEnabled != InternetEnabled.Unknown && (person.ContactDetails != null && person.ContactDetails.Email.Length > 0 && person.ContactDetails.Telephone != null && person.ContactDetails.Telephone.Number.Length > 0) && residentialAddress != null && residentialAddress.Street.Length > 0 && residentialAddress.HouseNumber.Length > 0 && residentialAddress.PostalCode.Length > 0 && residentialAddress.City.Length > 0) { person.StatusNAR = EnumStatusNAR.Complete; } else person.StatusNAR = EnumStatusNAR.Incomplete; if (persID == 0 && !loggedInAsStichting) person.AssetManager = (IAssetManager)company; if (persID == 0) blnSaveSuccess = ContactMapper.Insert(session, person); else { blnSaveSuccess = ContactMapper.Update(session, person); } persID = person.Key; } finally { session.Close(); } }
public static void SaveCompany(ref int compID, ref bool blnSave, CompanyDetails compDetails) { if (!SecurityManager.IsCurrentUserInRole("Data Mtce: Account Edit")) throw new System.Security.SecurityException("You are not authorized to update contact details."); IDalSession session = NHSessionFactory.CreateSession(); ; try { IContactCompany comp = null; Address postalAddress = null; Address residentialAddress = null; IContactsNAW newNaw = null; IContactsNAW currentNaw = null; IContactsIntroducer currentIntroducer = null; IContactsIntroducer newIntroducer = null; bool boolNawInsert = true; if (compID != 0) { comp = GetContact(session, compID); currentNaw = comp.CurrentNAW; currentIntroducer = comp.CurrentIntroducer; } else { comp = new ContactCompany(); currentNaw = new ContactsNAW(); } comp.IsActive = Convert.ToBoolean(compDetails.IsActive); if (compDetails.DateOfFoundation.Length > 0) comp.DateOfFounding = Convert.ToDateTime(compDetails.DateOfFoundation); else comp.DateOfFounding = DateTime.MinValue; if (comp.ContactDetails == null) comp.ContactDetails = new ContactDetails(); if (comp.ContactDetails.Fax == null) comp.ContactDetails.Fax = new TelephoneNumber(); if (comp.ContactDetails.Telephone == null) comp.ContactDetails.Telephone = new TelephoneNumber(); if (comp.ContactDetails.Mobile == null) comp.ContactDetails.Mobile = new TelephoneNumber(); if (comp.ContactDetails.TelephoneAH == null) comp.ContactDetails.TelephoneAH = new TelephoneNumber(); if (compDetails.InternetEnabled.Length > 0) { if (compDetails.InternetEnabled.Equals(InternetEnabled.No.ToString())) comp.InternetEnabled = InternetEnabled.No; else if (compDetails.InternetEnabled.Equals(InternetEnabled.Yes.ToString())) comp.InternetEnabled = InternetEnabled.Yes; else comp.InternetEnabled = InternetEnabled.Unknown; } else comp.InternetEnabled = InternetEnabled.Unknown; if (compDetails.Introducer != null && compDetails.Introducer.Length > 0 && Convert.ToInt32(compDetails.Introducer) != int.MinValue) { newIntroducer = new ContactsIntroducer(); newIntroducer.Remisier = RemisierMapper.GetRemisier(session, Convert.ToInt32(compDetails.Introducer)); newIntroducer.RemisierEmployee = RemisierEmployeeMapper.GetRemisierEmployee(session, Convert.ToInt32(compDetails.IntroducerEmployee)); } comp.ContactDetails.Email = compDetails.Email; comp.ContactDetails.Fax.Number = compDetails.Fax; comp.ContactDetails.Mobile.Number = compDetails.Mobile; comp.ContactDetails.Telephone.Number = compDetails.Telephone; comp.ContactDetails.TelephoneAH.Number = compDetails.TelephoneAH; if (compDetails.Street.Length > 0 || compDetails.HouseNumber.Length > 0 || compDetails.HouseNumberSuffix.Length > 0 || compDetails.Postalcode.Length > 0 || compDetails.City.Length > 0) { residentialAddress = new Address(); residentialAddress.Street = compDetails.Street; residentialAddress.HouseNumber = compDetails.HouseNumber; residentialAddress.HouseNumberSuffix = compDetails.HouseNumberSuffix; residentialAddress.PostalCode = compDetails.Postalcode; residentialAddress.City = compDetails.City; if (int.Parse(compDetails.Country) != int.MinValue) residentialAddress.Country = CountryMapper.GetCountry(session, Convert.ToInt32(compDetails.Country)); else residentialAddress.Country = null; } if (compDetails.PostalStreet.Length > 0 || compDetails.PostalHouseNumber.Length > 0 || compDetails.PostalHouseNumberSuffix.Length > 0 || compDetails.PostalPostalcode.Length > 0 || compDetails.PostalCity.Length > 0) { postalAddress = new Address(); postalAddress.Street = compDetails.PostalStreet; postalAddress.HouseNumber = compDetails.PostalHouseNumber; postalAddress.HouseNumberSuffix = compDetails.PostalHouseNumberSuffix; postalAddress.PostalCode = compDetails.PostalPostalcode; postalAddress.City = compDetails.PostalCity; if (int.Parse(compDetails.PostalCountry) != int.MinValue) postalAddress.Country = CountryMapper.GetCountry(session, Convert.ToInt32(compDetails.PostalCountry)); else postalAddress.Country = null; } if (residentialAddress != null && postalAddress == null) postalAddress = residentialAddress; else if (residentialAddress == null && postalAddress != null) residentialAddress = postalAddress; newNaw = new ContactsNAW(compDetails.CompanyName, postalAddress, residentialAddress); if (currentNaw.Name == null || (currentNaw != null && !currentNaw.Name.Equals(newNaw.Name))) boolNawInsert = true; // Then compare addresses else if (currentNaw.Name.Equals(newNaw.Name) && residentialAddress != null) { if (currentNaw.ResidentialAddress != null) { // If addresses the same: no need for an insert if (newNaw.Equals(currentNaw)) boolNawInsert = false; } else boolNawInsert = true; } else { newNaw = new ContactsNAW(); newNaw.Name = compDetails.CompanyName; boolNawInsert = true; } if (boolNawInsert) { comp.ContactsNAWs.Add(newNaw); comp.CurrentNAW = newNaw; } if (currentIntroducer != null || newIntroducer != null) { if (!(currentIntroducer != null && newIntroducer != null && newIntroducer.Equals(currentIntroducer))) { comp.ContactsIntroducers.Add(newIntroducer); comp.CurrentIntroducer = newIntroducer; } } if (compID == 0 && !LoginMapper.IsLoggedInAsStichting(session)) comp.AssetManager = (IAssetManager)LoginMapper.GetCurrentManagmentCompany(session); comp.KvKNumber = compDetails.KvKNumber; if (comp.CurrentNAW.Name.Length > 0 && comp.KvKNumber.Length > 0 && Util.IsNotNullDate(comp.DateOfFounding) && comp.InternetEnabled != InternetEnabled.Unknown && (comp.ContactDetails != null && comp.ContactDetails.Email.Length > 0 && comp.ContactDetails.Telephone != null && comp.ContactDetails.Telephone.Number.Length > 0) && residentialAddress != null && residentialAddress.Street.Length > 0 && residentialAddress.HouseNumber.Length > 0 && residentialAddress.PostalCode.Length > 0 && residentialAddress.City.Length > 0) { comp.StatusNAR = EnumStatusNAR.Complete; } else comp.StatusNAR = EnumStatusNAR.Incomplete; if (comp.CreationDate.Year < 2000) comp.CreationDate = DateTime.Now; if (compID == 0) blnSave = ContactMapper.Insert(session, comp); else blnSave = ContactMapper.Update(session, comp); compID = comp.Key; } finally { session.Close(); } }