예제 #1
0
        public BusinessPartnerNote Create(BusinessPartnerNote BusinessPartnerNote)
        {
            if (context.BusinessPartnerNotes.Where(x => x.Identifier != null && x.Identifier == BusinessPartnerNote.Identifier).Count() == 0)
            {
                BusinessPartnerNote.Id = 0;

                BusinessPartnerNote.Active    = true;
                BusinessPartnerNote.CreatedAt = DateTime.Now;
                BusinessPartnerNote.UpdatedAt = DateTime.Now;
                context.BusinessPartnerNotes.Add(BusinessPartnerNote);
                return(BusinessPartnerNote);
            }
            else
            {
                // Load item that will be updated
                BusinessPartnerNote dbEntry = context.BusinessPartnerNotes
                                              .FirstOrDefault(x => x.Identifier == BusinessPartnerNote.Identifier && x.Active == true);

                if (dbEntry != null)
                {
                    dbEntry.BusinessPartnerId = BusinessPartnerNote.BusinessPartnerId ?? null;
                    dbEntry.CompanyId         = BusinessPartnerNote.CompanyId ?? null;
                    dbEntry.CreatedById       = BusinessPartnerNote.CreatedById ?? null;

                    // Set properties
                    dbEntry.Note       = BusinessPartnerNote.Note;
                    dbEntry.NoteDate   = BusinessPartnerNote.NoteDate;
                    dbEntry.ItemStatus = BusinessPartnerNote.ItemStatus;
                    // Set timestamp
                    dbEntry.UpdatedAt = DateTime.Now;
                }

                return(dbEntry);
            }
        }
        public static List <BusinessPartnerNoteViewModel> ConvertToBusinessPartnerNoteViewModelList(this IEnumerable <BusinessPartnerNote> BusinessPartnerNotes)
        {
            List <BusinessPartnerNoteViewModel> BusinessPartnerNoteViewModels = new List <BusinessPartnerNoteViewModel>();

            foreach (BusinessPartnerNote BusinessPartnerNote in BusinessPartnerNotes)
            {
                BusinessPartnerNoteViewModels.Add(BusinessPartnerNote.ConvertToBusinessPartnerNoteViewModel());
            }
            return(BusinessPartnerNoteViewModels);
        }
예제 #3
0
        public BusinessPartnerNote Delete(Guid identifier)
        {
            BusinessPartnerNote dbEntry = context.BusinessPartnerNotes
                                          .Union(context.ChangeTracker.Entries()
                                                 .Where(x => x.State == EntityState.Added && x.Entity.GetType() == typeof(BusinessPartnerNote))
                                                 .Select(x => x.Entity as BusinessPartnerNote))
                                          .FirstOrDefault(x => x.Identifier == identifier);

            if (dbEntry != null)
            {
                dbEntry.Active    = false;
                dbEntry.UpdatedAt = DateTime.Now;
            }
            return(dbEntry);
        }
        public static BusinessPartnerNote ConvertToBusinessPartnerNote(this BusinessPartnerNoteViewModel BusinessPartnerNoteViewModel)
        {
            BusinessPartnerNote BusinessPartnerNote = new BusinessPartnerNote()
            {
                Id         = BusinessPartnerNoteViewModel.Id,
                Identifier = BusinessPartnerNoteViewModel.Identifier,

                BusinessPartnerId = BusinessPartnerNoteViewModel.BusinessPartner?.Id ?? null,

                Note        = BusinessPartnerNoteViewModel.Note,
                NoteDate    = BusinessPartnerNoteViewModel.NoteDate,
                ItemStatus  = BusinessPartnerNoteViewModel.ItemStatus,
                Active      = BusinessPartnerNoteViewModel.IsActive,
                CreatedById = BusinessPartnerNoteViewModel.CreatedBy?.Id ?? null,
                CompanyId   = BusinessPartnerNoteViewModel.Company?.Id ?? null,

                CreatedAt = BusinessPartnerNoteViewModel.CreatedAt,
                UpdatedAt = BusinessPartnerNoteViewModel.UpdatedAt
            };

            return(BusinessPartnerNote);
        }
        public static BusinessPartnerNoteViewModel ConvertToBusinessPartnerNoteViewModelLite(this BusinessPartnerNote BusinessPartnerNote)
        {
            BusinessPartnerNoteViewModel BusinessPartnerNoteViewModel = new BusinessPartnerNoteViewModel()
            {
                Id         = BusinessPartnerNote.Id,
                Identifier = BusinessPartnerNote.Identifier,

                Note       = BusinessPartnerNote.Note,
                NoteDate   = BusinessPartnerNote.NoteDate,
                ItemStatus = BusinessPartnerNote.ItemStatus,
                IsActive   = BusinessPartnerNote.Active,

                UpdatedAt = BusinessPartnerNote.UpdatedAt,
                CreatedAt = BusinessPartnerNote.CreatedAt
            };

            return(BusinessPartnerNoteViewModel);
        }
        public static BusinessPartnerNoteViewModel ConvertToBusinessPartnerNoteViewModel(this BusinessPartnerNote BusinessPartnerNote)
        {
            BusinessPartnerNoteViewModel BusinessPartnerNoteViewModel = new BusinessPartnerNoteViewModel()
            {
                Id         = BusinessPartnerNote.Id,
                Identifier = BusinessPartnerNote.Identifier,

                BusinessPartner = BusinessPartnerNote.BusinessPartner?.ConvertToBusinessPartnerViewModelLite(),

                Note       = BusinessPartnerNote.Note,
                NoteDate   = BusinessPartnerNote.NoteDate,
                ItemStatus = BusinessPartnerNote.ItemStatus,
                IsActive   = BusinessPartnerNote.Active,

                CreatedBy = BusinessPartnerNote.CreatedBy?.ConvertToUserViewModelLite(),
                Company   = BusinessPartnerNote.Company?.ConvertToCompanyViewModelLite(),

                UpdatedAt = BusinessPartnerNote.UpdatedAt,
                CreatedAt = BusinessPartnerNote.CreatedAt
            };

            return(BusinessPartnerNoteViewModel);
        }
예제 #7
0
        public List <BusinessPartnerNote> GetBusinessPartnerNotes(int companyId)
        {
            List <BusinessPartnerNote> BusinessPartnerNotes = new List <BusinessPartnerNote>();

            string queryString =
                "SELECT BusinessPartnerNoteId, BusinessPartnerNoteIdentifier, " +
                "BusinessPartnerId, BusinessPartnerIdentifier, BusinessPartnerCode, BusinessPartnerName, " +
                "Note, NoteDate, ItemStatus, " +
                "Active, UpdatedAt, CreatedById, CreatedByFirstName, CreatedByLastName, CompanyId, CompanyName " +
                "FROM vBusinessPartnerNotes " +
                "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())
                {
                    BusinessPartnerNote businessPartnerNote;
                    while (reader.Read())
                    {
                        businessPartnerNote            = new BusinessPartnerNote();
                        businessPartnerNote.Id         = Int32.Parse(reader["BusinessPartnerNoteId"].ToString());
                        businessPartnerNote.Identifier = Guid.Parse(reader["BusinessPartnerNoteIdentifier"].ToString());

                        if (reader["BusinessPartnerId"] != DBNull.Value)
                        {
                            businessPartnerNote.BusinessPartner            = new BusinessPartner();
                            businessPartnerNote.BusinessPartnerId          = Int32.Parse(reader["BusinessPartnerId"].ToString());
                            businessPartnerNote.BusinessPartner.Id         = Int32.Parse(reader["BusinessPartnerId"].ToString());
                            businessPartnerNote.BusinessPartner.Identifier = Guid.Parse(reader["BusinessPartnerIdentifier"].ToString());
                            businessPartnerNote.BusinessPartner.Code       = reader["BusinessPartnerCode"].ToString();
                            businessPartnerNote.BusinessPartner.Name       = reader["BusinessPartnerName"].ToString();
                        }

                        if (reader["Note"] != DBNull.Value)
                        {
                            businessPartnerNote.Note = reader["Note"].ToString();
                        }
                        if (reader["NoteDate"] != DBNull.Value)
                        {
                            businessPartnerNote.NoteDate = DateTime.Parse(reader["NoteDate"].ToString());
                        }
                        if (reader["ItemStatus"] != DBNull.Value)
                        {
                            businessPartnerNote.ItemStatus = Int32.Parse(reader["ItemStatus"].ToString());
                        }
                        businessPartnerNote.Active    = bool.Parse(reader["Active"].ToString());
                        businessPartnerNote.UpdatedAt = DateTime.Parse(reader["UpdatedAt"].ToString());

                        if (reader["CreatedById"] != DBNull.Value)
                        {
                            businessPartnerNote.CreatedBy           = new User();
                            businessPartnerNote.CreatedById         = Int32.Parse(reader["CreatedById"].ToString());
                            businessPartnerNote.CreatedBy.Id        = Int32.Parse(reader["CreatedById"].ToString());
                            businessPartnerNote.CreatedBy.FirstName = reader["CreatedByFirstName"]?.ToString();
                            businessPartnerNote.CreatedBy.LastName  = reader["CreatedByLastName"]?.ToString();
                        }

                        if (reader["CompanyId"] != DBNull.Value)
                        {
                            businessPartnerNote.Company      = new Company();
                            businessPartnerNote.CompanyId    = Int32.Parse(reader["CompanyId"].ToString());
                            businessPartnerNote.Company.Id   = Int32.Parse(reader["CompanyId"].ToString());
                            businessPartnerNote.Company.Name = reader["CompanyName"].ToString();
                        }

                        BusinessPartnerNotes.Add(businessPartnerNote);
                    }
                }
            }
            return(BusinessPartnerNotes);


            //List<BusinessPartnerNote> BusinessPartnerNotes = context.BusinessPartnerNotes
            //    .Include(x => x.BusinessPartner)
            //    .Include(x => x.Company)
            //    .Include(x => x.CreatedBy)
            //    .Where(x => x.Active == true && x.CompanyId == companyId)
            //    .AsNoTracking()
            //    .ToList();

            //return BusinessPartnerNotes;
        }
예제 #8
0
        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);
        }