public BusinessPartnerType Delete(Guid identifier) { // Load BusinessPartnerType that will be deleted BusinessPartnerType dbEntry = context.BusinessPartnerTypes .Union(context.ChangeTracker.Entries() .Where(x => x.State == EntityState.Added && x.Entity.GetType() == typeof(BusinessPartnerType)) .Select(x => x.Entity as BusinessPartnerType)) .FirstOrDefault(x => x.Identifier == identifier); if (dbEntry != null) { // Set activity dbEntry.Active = false; // Set timestamp dbEntry.UpdatedAt = DateTime.Now; } return(dbEntry); }
public BusinessPartnerTypeResponse Create(BusinessPartnerTypeViewModel re) { BusinessPartnerTypeResponse response = new BusinessPartnerTypeResponse(); try { BusinessPartnerType addedBusinessPartnerType = unitOfWork.GetBusinessPartnerTypeRepository().Create(re.ConvertToBusinessPartnerType()); unitOfWork.Save(); response.BusinessPartnerType = addedBusinessPartnerType.ConvertToBusinessPartnerTypeViewModel(); response.Success = true; } catch (Exception ex) { response.BusinessPartnerType = new BusinessPartnerTypeViewModel(); response.Success = false; response.Message = ex.Message; } return(response); }
public BusinessPartnerType Create(BusinessPartnerType businessPartnerType) { if (context.BusinessPartnerTypes.Where(x => x.Identifier != null && x.Identifier == businessPartnerType.Identifier).Count() == 0) { businessPartnerType.Id = 0; businessPartnerType.Code = GetNewCodeValue(businessPartnerType.CompanyId ?? 0); businessPartnerType.Active = true; businessPartnerType.UpdatedAt = DateTime.Now; businessPartnerType.CreatedAt = DateTime.Now; context.BusinessPartnerTypes.Add(businessPartnerType); return(businessPartnerType); } else { // Load businessPartnerType that will be updated BusinessPartnerType dbEntry = context.BusinessPartnerTypes .FirstOrDefault(x => x.Identifier == businessPartnerType.Identifier && x.Active == true); if (dbEntry != null) { dbEntry.CompanyId = businessPartnerType.CompanyId ?? null; dbEntry.CreatedById = businessPartnerType.CreatedById ?? null; // Set properties dbEntry.Code = businessPartnerType.Code; dbEntry.Name = businessPartnerType.Name; dbEntry.IsBuyer = businessPartnerType.IsBuyer; dbEntry.IsSupplier = businessPartnerType.IsSupplier; dbEntry.ItemStatus = businessPartnerType.ItemStatus; // Set timestamp dbEntry.UpdatedAt = DateTime.Now; } return(dbEntry); } }
public BusinessPartnerTypeResponse Delete(Guid identifier) { BusinessPartnerTypeResponse response = new BusinessPartnerTypeResponse(); try { BusinessPartnerType deletedBusinessPartnerType = unitOfWork.GetBusinessPartnerTypeRepository().Delete(identifier); unitOfWork.Save(); response.BusinessPartnerType = deletedBusinessPartnerType.ConvertToBusinessPartnerTypeViewModel(); response.Success = true; } catch (Exception ex) { response.BusinessPartnerType = new BusinessPartnerTypeViewModel(); response.Success = false; response.Message = ex.Message; } return(response); }
public static BusinessPartnerType ConvertToBusinessPartnerType(this BusinessPartnerTypeViewModel businessPartnerTypeViewModel) { BusinessPartnerType businessPartnerType = new BusinessPartnerType() { Id = businessPartnerTypeViewModel.Id, Identifier = businessPartnerTypeViewModel.Identifier, Code = businessPartnerTypeViewModel.Code, Name = businessPartnerTypeViewModel.Name, IsBuyer = businessPartnerTypeViewModel.IsBuyer, IsSupplier = businessPartnerTypeViewModel.IsSupplier, ItemStatus = businessPartnerTypeViewModel.ItemStatus, Active = businessPartnerTypeViewModel.IsActive, CreatedById = businessPartnerTypeViewModel.CreatedBy?.Id ?? null, CompanyId = businessPartnerTypeViewModel.Company?.Id ?? null, CreatedAt = businessPartnerTypeViewModel.CreatedAt, UpdatedAt = businessPartnerTypeViewModel.UpdatedAt }; return(businessPartnerType); }
public List <BusinessPartnerType> GetBusinessPartnerTypesNewerThen(int companyId, DateTime lastUpdateTime) { List <BusinessPartnerType> BusinessPartnerTypes = new List <BusinessPartnerType>(); string queryString = "SELECT BusinessPartnerTypeId, BusinessPartnerTypeIdentifier, BusinessPartnerTypeCode, BusinessPartnerTypeName, " + "IsBuyer, IsSupplier, ItemStatus, " + "Active, UpdatedAt, CreatedById, CreatedByFirstName, CreatedByLastName, CompanyId, CompanyName " + "FROM vBusinessPartnerTypes " + "WHERE CompanyId = @CompanyId " + "AND CONVERT(DATETIME, CONVERT(VARCHAR(20), UpdatedAt, 120)) > CONVERT(DATETIME, CONVERT(VARCHAR(20), @LastUpdateTime, 120));"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = connection.CreateCommand(); command.CommandText = queryString; command.Parameters.Add(new SqlParameter("@CompanyId", companyId)); command.Parameters.Add(new SqlParameter("@LastUpdateTime", lastUpdateTime)); connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { BusinessPartnerType businessPartnerType; while (reader.Read()) { businessPartnerType = new BusinessPartnerType(); businessPartnerType.Id = Int32.Parse(reader["BusinessPartnerTypeId"].ToString()); businessPartnerType.Identifier = Guid.Parse(reader["BusinessPartnerTypeIdentifier"].ToString()); businessPartnerType.Code = reader["BusinessPartnerTypeCode"].ToString(); businessPartnerType.Name = reader["BusinessPartnerTypeName"].ToString(); businessPartnerType.IsBuyer = bool.Parse(reader["IsBuyer"]?.ToString()); businessPartnerType.IsSupplier = bool.Parse(reader["IsSupplier"]?.ToString()); if (reader["ItemStatus"] != DBNull.Value) { businessPartnerType.ItemStatus = Int32.Parse(reader["ItemStatus"].ToString()); } businessPartnerType.Active = bool.Parse(reader["Active"].ToString()); businessPartnerType.UpdatedAt = DateTime.Parse(reader["UpdatedAt"].ToString()); if (reader["CreatedById"] != DBNull.Value) { businessPartnerType.CreatedBy = new User(); businessPartnerType.CreatedById = Int32.Parse(reader["CreatedById"].ToString()); businessPartnerType.CreatedBy.Id = Int32.Parse(reader["CreatedById"].ToString()); businessPartnerType.CreatedBy.FirstName = reader["CreatedByFirstName"]?.ToString(); businessPartnerType.CreatedBy.LastName = reader["CreatedByLastName"]?.ToString(); } if (reader["CompanyId"] != DBNull.Value) { businessPartnerType.Company = new Company(); businessPartnerType.CompanyId = Int32.Parse(reader["CompanyId"].ToString()); businessPartnerType.Company.Id = Int32.Parse(reader["CompanyId"].ToString()); businessPartnerType.Company.Name = reader["CompanyName"].ToString(); } BusinessPartnerTypes.Add(businessPartnerType); } } } return(BusinessPartnerTypes); //List<BusinessPartnerType> BusinessPartnerTypes = context.BusinessPartnerTypes // .Include(x => x.Company) // .Include(x => x.CreatedBy) // .Where(x => x.Company.Id == companyId && x.UpdatedAt > lastUpdateTime) // .OrderByDescending(x => x.UpdatedAt) // .AsNoTracking() // .ToList(); //return BusinessPartnerTypes; }
public static BusinessPartnerTypeViewModel ConvertToBusinessPartnerTypeViewModelLite(this BusinessPartnerType businessPartnerType) { BusinessPartnerTypeViewModel businessPartnerTypeViewModel = new BusinessPartnerTypeViewModel() { Id = businessPartnerType.Id, Identifier = businessPartnerType.Identifier, Code = businessPartnerType.Code, Name = businessPartnerType.Name, IsBuyer = businessPartnerType.IsBuyer, IsSupplier = businessPartnerType.IsSupplier, ItemStatus = businessPartnerType.ItemStatus, IsActive = businessPartnerType.Active, UpdatedAt = businessPartnerType.UpdatedAt, CreatedAt = businessPartnerType.CreatedAt }; return(businessPartnerTypeViewModel); }
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); }