public BusinessPartnerBank Delete(Guid identifier) { BusinessPartnerBank dbEntry = context.BusinessPartnerBanks .Union(context.ChangeTracker.Entries() .Where(x => x.State == EntityState.Added && x.Entity.GetType() == typeof(BusinessPartnerBank)) .Select(x => x.Entity as BusinessPartnerBank)) .FirstOrDefault(x => x.Identifier == identifier); if (dbEntry != null) { dbEntry.Active = false; dbEntry.UpdatedAt = DateTime.Now; } return(dbEntry); }
public static BusinessPartnerBank ConvertToBusinessPartnerBank(this BusinessPartnerBankViewModel businessPartnerBankViewModel) { BusinessPartnerBank businessPartnerBank = new BusinessPartnerBank() { Id = businessPartnerBankViewModel.Id, Identifier = businessPartnerBankViewModel.Identifier, BusinessPartnerId = businessPartnerBankViewModel.BusinessPartner?.Id ?? null, BankId = businessPartnerBankViewModel.Bank?.Id ?? 0, CountryId = businessPartnerBankViewModel.Country?.Id ?? 0, AccountNumber = businessPartnerBankViewModel.AccountNumber, ItemStatus = businessPartnerBankViewModel.ItemStatus, Active = businessPartnerBankViewModel.IsActive, CreatedById = businessPartnerBankViewModel.CreatedBy?.Id ?? null, CompanyId = businessPartnerBankViewModel.Company?.Id ?? null, UpdatedAt = businessPartnerBankViewModel.UpdatedAt, CreatedAt = businessPartnerBankViewModel.CreatedAt, }; return(businessPartnerBank); }
public BusinessPartnerBank Create(BusinessPartnerBank businessPartnerBank) { if (context.BusinessPartnerBanks.Where(x => x.Identifier != null && x.Identifier == businessPartnerBank.Identifier).Count() == 0) { businessPartnerBank.Id = 0; businessPartnerBank.Active = true; businessPartnerBank.UpdatedAt = DateTime.Now; businessPartnerBank.CreatedAt = DateTime.Now; context.BusinessPartnerBanks.Add(businessPartnerBank); return(businessPartnerBank); } else { // Load businessPartnerBank that will be updated BusinessPartnerBank dbEntry = context.BusinessPartnerBanks .FirstOrDefault(x => x.Identifier == businessPartnerBank.Identifier && x.Active == true); if (dbEntry != null) { dbEntry.BusinessPartnerId = businessPartnerBank.BusinessPartnerId ?? null; dbEntry.BankId = businessPartnerBank.BankId ?? null; dbEntry.CountryId = businessPartnerBank.CountryId ?? null; dbEntry.CompanyId = businessPartnerBank.CompanyId ?? null; dbEntry.CreatedById = businessPartnerBank.CreatedById ?? null; // Set properties dbEntry.AccountNumber = businessPartnerBank.AccountNumber; dbEntry.ItemStatus = businessPartnerBank.ItemStatus; // Set timestamp dbEntry.UpdatedAt = DateTime.Now; } return(dbEntry); } }
public static BusinessPartnerBankViewModel ConvertToBusinessPartnerBankViewModelLite(this BusinessPartnerBank businessPartnerBank) { BusinessPartnerBankViewModel businessPartnerBankViewModel = new BusinessPartnerBankViewModel() { Id = businessPartnerBank.Id, Identifier = businessPartnerBank.Identifier, AccountNumber = businessPartnerBank.AccountNumber, ItemStatus = businessPartnerBank.ItemStatus, IsActive = businessPartnerBank.Active, UpdatedAt = businessPartnerBank.UpdatedAt, CreatedAt = businessPartnerBank.CreatedAt, }; return(businessPartnerBankViewModel); }
public static BusinessPartnerBankViewModel ConvertToBusinessPartnerBankViewModel(this BusinessPartnerBank businessPartnerBank) { BusinessPartnerBankViewModel businessPartnerBankViewModel = new BusinessPartnerBankViewModel() { Id = businessPartnerBank.Id, Identifier = businessPartnerBank.Identifier, BusinessPartner = businessPartnerBank.BusinessPartner?.ConvertToBusinessPartnerViewModelLite(), Bank = businessPartnerBank.Bank?.ConvertToBankViewModelLite(), Country = businessPartnerBank.Country?.ConvertToCountryViewModelLite(), AccountNumber = businessPartnerBank.AccountNumber, ItemStatus = businessPartnerBank.ItemStatus, IsActive = businessPartnerBank.Active, CreatedBy = businessPartnerBank.CreatedBy?.ConvertToUserViewModelLite(), Company = businessPartnerBank.Company?.ConvertToCompanyViewModelLite(), UpdatedAt = businessPartnerBank.UpdatedAt, CreatedAt = businessPartnerBank.CreatedAt, }; return(businessPartnerBankViewModel); }
public BusinessPartnerResponse Create(BusinessPartnerViewModel re) { BusinessPartnerResponse response = new BusinessPartnerResponse(); try { // Backup notes List <BusinessPartnerNoteViewModel> businessPartnerNotes = re.BusinessPartnerNotes?.ToList(); re.BusinessPartnerNotes = null; // Backup documents List <BusinessPartnerDocumentViewModel> businessPartnerDocuments = re.BusinessPartnerDocuments?.ToList(); re.BusinessPartnerDocuments = null; //Phone List <BusinessPartnerPhoneViewModel> businessPartnerPhones = re.Phones?.ToList(); re.Phones = null; //Location List <BusinessPartnerLocationViewModel> businessPartnerLocations = re.Locations?.ToList(); re.Locations = null; //Institution List <BusinessPartnerInstitutionViewModel> businessPartnerInstitutions = re.Institutions?.ToList(); re.Institutions = null; //Bank List <BusinessPartnerBankViewModel> businessPartnerBanks = re.Banks?.ToList(); re.Banks = null; //Type List <BusinessPartnerTypeViewModel> businessPartnerTypes = re.BusinessPartnerTypes?.ToList(); re.BusinessPartnerTypes = null; BusinessPartner createdBusinessPartner = unitOfWork.GetBusinessPartnerRepository().Create(re.ConvertToBusinessPartner()); // Update notes if (businessPartnerNotes != null && businessPartnerNotes.Count > 0) { // Items for create or update foreach (var businessPartnerNote in businessPartnerNotes .Where(x => x.ItemStatus == ItemStatus.Added || x.ItemStatus == ItemStatus.Edited)?.ToList() ?? new List <BusinessPartnerNoteViewModel>()) { businessPartnerNote.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; businessPartnerNote.ItemStatus = ItemStatus.Submited; BusinessPartnerNote createdBusinessPartnerNote = unitOfWork.GetBusinessPartnerNoteRepository() .Create(businessPartnerNote.ConvertToBusinessPartnerNote()); } foreach (var item in businessPartnerNotes .Where(x => x.ItemStatus == ItemStatus.Deleted)?.ToList() ?? new List <BusinessPartnerNoteViewModel>()) { item.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; unitOfWork.GetBusinessPartnerNoteRepository().Create(item.ConvertToBusinessPartnerNote()); unitOfWork.GetBusinessPartnerNoteRepository().Delete(item.Identifier); } } // Update documents if (businessPartnerDocuments != null && businessPartnerDocuments.Count > 0) { // Items for create or update foreach (var businessPartnerDocument in businessPartnerDocuments .Where(x => x.ItemStatus == ItemStatus.Added || x.ItemStatus == ItemStatus.Edited)?.ToList() ?? new List <BusinessPartnerDocumentViewModel>()) { businessPartnerDocument.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; businessPartnerDocument.ItemStatus = ItemStatus.Submited; BusinessPartnerDocument createdBusinessPartnerDocument = unitOfWork.GetBusinessPartnerDocumentRepository() .Create(businessPartnerDocument.ConvertToBusinessPartnerDocument()); } foreach (var item in businessPartnerDocuments .Where(x => x.ItemStatus == ItemStatus.Deleted)?.ToList() ?? new List <BusinessPartnerDocumentViewModel>()) { item.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; unitOfWork.GetBusinessPartnerDocumentRepository().Create(item.ConvertToBusinessPartnerDocument()); unitOfWork.GetBusinessPartnerDocumentRepository().Delete(item.Identifier); } } // Update Phone if (businessPartnerPhones != null && businessPartnerPhones.Count > 0) { // Items for create or update foreach (var businessPartnerPhone in businessPartnerPhones .Where(x => x.ItemStatus == ItemStatus.Added || x.ItemStatus == ItemStatus.Edited)?.ToList() ?? new List <BusinessPartnerPhoneViewModel>()) { businessPartnerPhone.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; businessPartnerPhone.ItemStatus = ItemStatus.Submited; BusinessPartnerPhone createdBusinessPartnerPhone = unitOfWork.GetBusinessPartnerPhoneRepository() .Create(businessPartnerPhone.ConvertToBusinessPartnerPhone()); } foreach (var item in businessPartnerPhones .Where(x => x.ItemStatus == ItemStatus.Deleted)?.ToList() ?? new List <BusinessPartnerPhoneViewModel>()) { item.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; unitOfWork.GetBusinessPartnerPhoneRepository().Create(item.ConvertToBusinessPartnerPhone()); unitOfWork.GetBusinessPartnerPhoneRepository().Delete(item.Identifier); } } // Update Location if (businessPartnerLocations != null && businessPartnerLocations.Count > 0) { // Items for create or update foreach (var businessPartnerLocation in businessPartnerLocations .Where(x => x.ItemStatus == ItemStatus.Added || x.ItemStatus == ItemStatus.Edited)?.ToList() ?? new List <BusinessPartnerLocationViewModel>()) { businessPartnerLocation.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; businessPartnerLocation.ItemStatus = ItemStatus.Submited; BusinessPartnerLocation createdBusinessPartnerLocation = unitOfWork.GetBusinessPartnerLocationRepository() .Create(businessPartnerLocation.ConvertToBusinessPartnerLocation()); } foreach (var item in businessPartnerLocations .Where(x => x.ItemStatus == ItemStatus.Deleted)?.ToList() ?? new List <BusinessPartnerLocationViewModel>()) { item.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; unitOfWork.GetBusinessPartnerLocationRepository().Create(item.ConvertToBusinessPartnerLocation()); unitOfWork.GetBusinessPartnerLocationRepository().Delete(item.Identifier); } } // Update Institution if (businessPartnerInstitutions != null && businessPartnerInstitutions.Count > 0) { // Items for create or update foreach (var businessPartnerInstitution in businessPartnerInstitutions .Where(x => x.ItemStatus == ItemStatus.Added || x.ItemStatus == ItemStatus.Edited)?.ToList() ?? new List <BusinessPartnerInstitutionViewModel>()) { businessPartnerInstitution.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; businessPartnerInstitution.ItemStatus = ItemStatus.Submited; BusinessPartnerInstitution createdBusinessPartnerInstitution = unitOfWork.GetBusinessPartnerInstitutionRepository() .Create(businessPartnerInstitution.ConvertToBusinessPartnerInstitution()); } foreach (var item in businessPartnerInstitutions .Where(x => x.ItemStatus == ItemStatus.Deleted)?.ToList() ?? new List <BusinessPartnerInstitutionViewModel>()) { item.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; unitOfWork.GetBusinessPartnerInstitutionRepository().Create(item.ConvertToBusinessPartnerInstitution()); unitOfWork.GetBusinessPartnerInstitutionRepository().Delete(item.Identifier); } } // Update Bank if (businessPartnerBanks != null && businessPartnerBanks.Count > 0) { // Items for create or update foreach (var businessPartnerBank in businessPartnerBanks .Where(x => x.ItemStatus == ItemStatus.Added || x.ItemStatus == ItemStatus.Edited)?.ToList() ?? new List <BusinessPartnerBankViewModel>()) { businessPartnerBank.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; businessPartnerBank.ItemStatus = ItemStatus.Submited; BusinessPartnerBank createdBusinessPartnerBank = unitOfWork.GetBusinessPartnerBankRepository() .Create(businessPartnerBank.ConvertToBusinessPartnerBank()); } foreach (var item in businessPartnerBanks .Where(x => x.ItemStatus == ItemStatus.Deleted)?.ToList() ?? new List <BusinessPartnerBankViewModel>()) { item.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; unitOfWork.GetBusinessPartnerBankRepository().Create(item.ConvertToBusinessPartnerBank()); unitOfWork.GetBusinessPartnerBankRepository().Delete(item.Identifier); } } // Update Type //unitOfWork.GetBusinessPartnerBusinessPartnerTypeRepository().Delete(createdBusinessPartner.Id); //foreach (var item in businessPartnerTypes) //{ // unitOfWork.GetBusinessPartnerBusinessPartnerTypeRepository().Create(createdBusinessPartner.Id, item.Id); //} if (businessPartnerTypes != null && businessPartnerTypes.Count > 0) { // Items for create or update foreach (var businessPartnerType in businessPartnerTypes .Where(x => x.ItemStatus == ItemStatus.Added || x.ItemStatus == ItemStatus.Edited)?.ToList() ?? new List <BusinessPartnerTypeViewModel>()) { businessPartnerType.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; businessPartnerType.ItemStatus = ItemStatus.Submited; BusinessPartnerType createdBusinessPartnerType = unitOfWork.GetBusinessPartnerTypeRepository() .Create(businessPartnerType.ConvertToBusinessPartnerType()); } foreach (var item in businessPartnerTypes .Where(x => x.ItemStatus == ItemStatus.Deleted)?.ToList() ?? new List <BusinessPartnerTypeViewModel>()) { item.BusinessPartner = new BusinessPartnerViewModel() { Id = createdBusinessPartner.Id }; unitOfWork.GetBusinessPartnerTypeRepository().Create(item.ConvertToBusinessPartnerType()); unitOfWork.GetBusinessPartnerTypeRepository().Delete(item.Identifier); } } unitOfWork.Save(); response.BusinessPartner = createdBusinessPartner.ConvertToBusinessPartnerViewModel(); response.Success = true; } catch (Exception ex) { response.BusinessPartner = new BusinessPartnerViewModel(); response.Success = false; response.Message = ex.Message; } return(response); }
public List <BusinessPartnerBank> GetBusinessPartnerBanks(int companyId) { List <BusinessPartnerBank> businessPartnerBanks = new List <BusinessPartnerBank>(); string queryString = "SELECT BusinessPartnerBankId, BusinessPartnerBankIdentifier, " + "BusinessPartnerId, BusinessPartnerIdentifier, BusinessPartnerCode, BusinessPartnerName, " + "BankId, BankIdentifier, BankCode, BankName, " + "CountryId, CountryIdentifier, CountryCode, CountryName, " + "AccountNumber, ItemStatus, " + "Active, UpdatedAt, CreatedById, CreatedByFirstName, CreatedByLastName, CompanyId, CompanyName " + "FROM vBusinessPartnerBanks " + "WHERE CompanyId = @CompanyId AND Active = 1;"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = connection.CreateCommand(); command.CommandText = queryString; command.Parameters.Add(new SqlParameter("@CompanyId", companyId)); connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { BusinessPartnerBank businessPartnerBank; while (reader.Read()) { businessPartnerBank = new BusinessPartnerBank(); businessPartnerBank.Id = Int32.Parse(reader["BusinessPartnerBankId"].ToString()); businessPartnerBank.Identifier = Guid.Parse(reader["BusinessPartnerBankIdentifier"].ToString()); if (reader["BusinessPartnerId"] != DBNull.Value) { businessPartnerBank.BusinessPartner = new BusinessPartner(); businessPartnerBank.BusinessPartnerId = Int32.Parse(reader["BusinessPartnerId"].ToString()); businessPartnerBank.BusinessPartner.Id = Int32.Parse(reader["BusinessPartnerId"].ToString()); businessPartnerBank.BusinessPartner.Identifier = Guid.Parse(reader["BusinessPartnerIdentifier"].ToString()); businessPartnerBank.BusinessPartner.Code = reader["BusinessPartnerCode"].ToString(); businessPartnerBank.BusinessPartner.Name = reader["BusinessPartnerName"].ToString(); } if (reader["BankId"] != DBNull.Value) { businessPartnerBank.Bank = new Bank(); businessPartnerBank.BankId = Int32.Parse(reader["BankId"].ToString()); businessPartnerBank.Bank.Id = Int32.Parse(reader["BankId"].ToString()); businessPartnerBank.Bank.Identifier = Guid.Parse(reader["BankIdentifier"].ToString()); businessPartnerBank.Bank.Swift = reader["BankCode"].ToString(); businessPartnerBank.Bank.Name = reader["BankName"].ToString(); } if (reader["CountryId"] != DBNull.Value) { businessPartnerBank.Country = new Country(); businessPartnerBank.CountryId = Int32.Parse(reader["CountryId"].ToString()); businessPartnerBank.Country.Id = Int32.Parse(reader["CountryId"].ToString()); businessPartnerBank.Country.Identifier = Guid.Parse(reader["CountryIdentifier"].ToString()); businessPartnerBank.Country.Mark = reader["CountryCode"].ToString(); businessPartnerBank.Country.Name = reader["CountryName"].ToString(); } if (reader["AccountNumber"] != DBNull.Value) { businessPartnerBank.AccountNumber = reader["AccountNumber"].ToString(); } if (reader["ItemStatus"] != DBNull.Value) { businessPartnerBank.ItemStatus = Int32.Parse(reader["ItemStatus"].ToString()); } businessPartnerBank.Active = bool.Parse(reader["Active"].ToString()); businessPartnerBank.UpdatedAt = DateTime.Parse(reader["UpdatedAt"].ToString()); if (reader["CreatedById"] != DBNull.Value) { businessPartnerBank.CreatedBy = new User(); businessPartnerBank.CreatedById = Int32.Parse(reader["CreatedById"].ToString()); businessPartnerBank.CreatedBy.Id = Int32.Parse(reader["CreatedById"].ToString()); businessPartnerBank.CreatedBy.FirstName = reader["CreatedByFirstName"]?.ToString(); businessPartnerBank.CreatedBy.LastName = reader["CreatedByLastName"]?.ToString(); } if (reader["CompanyId"] != DBNull.Value) { businessPartnerBank.Company = new Company(); businessPartnerBank.CompanyId = Int32.Parse(reader["CompanyId"].ToString()); businessPartnerBank.Company.Id = Int32.Parse(reader["CompanyId"].ToString()); businessPartnerBank.Company.Name = reader["CompanyName"].ToString(); } businessPartnerBanks.Add(businessPartnerBank); } } } //List<BusinessPartnerBank> businessPartnerBanks = context.BusinessPartnerBanks // .Include(x => x.BusinessPartner) // .Include(x => x.Bank) // .Include(x => x.Country) // .Include(x => x.Company) // .Include(x => x.CreatedBy) // .Where(x => x.Active == true && x.CompanyId == companyId) // .AsNoTracking() // .ToList(); return(businessPartnerBanks); }