Exemple #1
0
        public static BusinessPartnerInstitution ConvertToBusinessPartnerInstitution(this BusinessPartnerInstitutionViewModel businessPartnerInstitutionViewModel)
        {
            BusinessPartnerInstitution businessPartnerInstitution = new BusinessPartnerInstitution()
            {
                Id         = businessPartnerInstitutionViewModel.Id,
                Identifier = businessPartnerInstitutionViewModel.Identifier,

                BusinessPartnerId = businessPartnerInstitutionViewModel.BusinessPartner?.Id ?? null,
                Code          = businessPartnerInstitutionViewModel.Code,
                Institution   = businessPartnerInstitutionViewModel.Institution,
                Username      = businessPartnerInstitutionViewModel.Username,
                Password      = businessPartnerInstitutionViewModel.Password,
                ContactPerson = businessPartnerInstitutionViewModel.ContactPerson,
                Phone         = businessPartnerInstitutionViewModel.Phone,
                Fax           = businessPartnerInstitutionViewModel.Fax,
                Email         = businessPartnerInstitutionViewModel.Email,
                Note          = businessPartnerInstitutionViewModel.Note,
                ItemStatus    = businessPartnerInstitutionViewModel.ItemStatus,
                Active        = businessPartnerInstitutionViewModel.IsActive,
                CreatedById   = businessPartnerInstitutionViewModel.CreatedBy?.Id ?? null,
                CompanyId     = businessPartnerInstitutionViewModel.Company?.Id ?? null,

                UpdatedAt = businessPartnerInstitutionViewModel.UpdatedAt,
                CreatedAt = businessPartnerInstitutionViewModel.CreatedAt,
            };

            return(businessPartnerInstitution);
        }
        public BusinessPartnerInstitution Delete(Guid identifier)
        {
            BusinessPartnerInstitution dbEntry = context.BusinessPartnerInstitutions
                                                 .Union(context.ChangeTracker.Entries()
                                                        .Where(x => x.State == EntityState.Added && x.Entity.GetType() == typeof(BusinessPartnerInstitution))
                                                        .Select(x => x.Entity as BusinessPartnerInstitution))
                                                 .FirstOrDefault(x => x.Identifier == identifier);

            if (dbEntry != null)
            {
                dbEntry.Active    = false;
                dbEntry.UpdatedAt = DateTime.Now;
            }
            return(dbEntry);
        }
        public BusinessPartnerInstitution Create(BusinessPartnerInstitution businessPartnerInstitution)
        {
            if (context.BusinessPartnerInstitutions.Where(x => x.Identifier != null && x.Identifier == businessPartnerInstitution.Identifier).Count() == 0)
            {
                businessPartnerInstitution.Id = 0;

                businessPartnerInstitution.Active = true;

                businessPartnerInstitution.CreatedAt = DateTime.Now;
                businessPartnerInstitution.UpdatedAt = DateTime.Now;

                context.BusinessPartnerInstitutions.Add(businessPartnerInstitution);
                return(businessPartnerInstitution);
            }
            else
            {
                // Load businessPartnerInstitution that will be updated
                BusinessPartnerInstitution dbEntry = context.BusinessPartnerInstitutions
                                                     .FirstOrDefault(x => x.Identifier == businessPartnerInstitution.Identifier && x.Active == true);

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

                    // Set properties
                    dbEntry.Code          = businessPartnerInstitution.Code;
                    dbEntry.Institution   = businessPartnerInstitution.Institution;
                    dbEntry.Username      = businessPartnerInstitution.Username;
                    dbEntry.Password      = businessPartnerInstitution.Password;
                    dbEntry.ContactPerson = businessPartnerInstitution.ContactPerson;
                    dbEntry.Phone         = businessPartnerInstitution.Phone;
                    dbEntry.Fax           = businessPartnerInstitution.Fax;
                    dbEntry.Email         = businessPartnerInstitution.Email;
                    dbEntry.Note          = businessPartnerInstitution.Note;
                    dbEntry.ItemStatus    = businessPartnerInstitution.ItemStatus;
                    // Set timestamp
                    dbEntry.UpdatedAt = DateTime.Now;
                }

                return(dbEntry);
            }
        }
Exemple #4
0
        public static BusinessPartnerInstitutionViewModel ConvertToBusinessPartnerInstitutionViewModelLite(this BusinessPartnerInstitution businessPartnerInstitution)
        {
            BusinessPartnerInstitutionViewModel businessPartnerInstitutionViewModel = new BusinessPartnerInstitutionViewModel()
            {
                Id            = businessPartnerInstitution.Id,
                Identifier    = businessPartnerInstitution.Identifier,
                Code          = businessPartnerInstitution.Code,
                Institution   = businessPartnerInstitution.Institution,
                Username      = businessPartnerInstitution.Username,
                Password      = businessPartnerInstitution.Password,
                ContactPerson = businessPartnerInstitution.ContactPerson,
                Phone         = businessPartnerInstitution.Phone,
                Fax           = businessPartnerInstitution.Fax,
                Email         = businessPartnerInstitution.Email,
                Note          = businessPartnerInstitution.Note,
                ItemStatus    = businessPartnerInstitution.ItemStatus,
                IsActive      = businessPartnerInstitution.Active,

                UpdatedAt = businessPartnerInstitution.UpdatedAt,
                CreatedAt = businessPartnerInstitution.CreatedAt,
            };

            return(businessPartnerInstitutionViewModel);
        }
        public List <BusinessPartnerInstitution> GetBusinessPartnerInstitutions(int companyId)
        {
            List <BusinessPartnerInstitution> BusinessPartnerInstitutions = new List <BusinessPartnerInstitution>();

            string queryString =
                "SELECT BusinessPartnerInstitutionId, BusinessPartnerInstitutionIdentifier, " +
                "BusinessPartnerId, BusinessPartnerIdentifier, BusinessPartnerCode, BusinessPartnerName, " +
                "Code, Institution, Username, Password, ContactPerson, Phone, Fax, Email, Note, ItemStatus, " +
                "Active, UpdatedAt, CreatedById, CreatedByFirstName, CreatedByLastName, CompanyId, CompanyName " +
                "FROM vBusinessPartnerInstitutions " +
                "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())
                {
                    BusinessPartnerInstitution businessPartnerInstitution;
                    while (reader.Read())
                    {
                        businessPartnerInstitution            = new BusinessPartnerInstitution();
                        businessPartnerInstitution.Id         = Int32.Parse(reader["BusinessPartnerInstitutionId"].ToString());
                        businessPartnerInstitution.Identifier = Guid.Parse(reader["BusinessPartnerInstitutionIdentifier"].ToString());

                        if (reader["BusinessPartnerId"] != DBNull.Value)
                        {
                            businessPartnerInstitution.BusinessPartner            = new BusinessPartner();
                            businessPartnerInstitution.BusinessPartnerId          = Int32.Parse(reader["BusinessPartnerId"].ToString());
                            businessPartnerInstitution.BusinessPartner.Id         = Int32.Parse(reader["BusinessPartnerId"].ToString());
                            businessPartnerInstitution.BusinessPartner.Identifier = Guid.Parse(reader["BusinessPartnerIdentifier"].ToString());
                            businessPartnerInstitution.BusinessPartner.Code       = reader["BusinessPartnerCode"].ToString();
                            businessPartnerInstitution.BusinessPartner.Name       = reader["BusinessPartnerName"].ToString();
                        }
                        if (reader["Code"] != DBNull.Value)
                        {
                            businessPartnerInstitution.Code = reader["Code"].ToString();
                        }
                        if (reader["Institution"] != DBNull.Value)
                        {
                            businessPartnerInstitution.Institution = reader["Institution"].ToString();
                        }
                        if (reader["Username"] != DBNull.Value)
                        {
                            businessPartnerInstitution.Username = reader["Username"].ToString();
                        }
                        if (reader["Password"] != DBNull.Value)
                        {
                            businessPartnerInstitution.Password = reader["Password"].ToString();
                        }
                        if (reader["ContactPerson"] != DBNull.Value)
                        {
                            businessPartnerInstitution.ContactPerson = reader["ContactPerson"].ToString();
                        }
                        if (reader["Phone"] != DBNull.Value)
                        {
                            businessPartnerInstitution.Phone = reader["Phone"].ToString();
                        }
                        if (reader["Fax"] != DBNull.Value)
                        {
                            businessPartnerInstitution.Fax = reader["Fax"].ToString();
                        }
                        if (reader["Email"] != DBNull.Value)
                        {
                            businessPartnerInstitution.Email = reader["Email"].ToString();
                        }
                        if (reader["Note"] != DBNull.Value)
                        {
                            businessPartnerInstitution.Note = reader["Note"].ToString();
                        }
                        if (reader["ItemStatus"] != DBNull.Value)
                        {
                            businessPartnerInstitution.ItemStatus = Int32.Parse(reader["ItemStatus"].ToString());
                        }
                        businessPartnerInstitution.Active    = bool.Parse(reader["Active"].ToString());
                        businessPartnerInstitution.UpdatedAt = DateTime.Parse(reader["UpdatedAt"].ToString());

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

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

                        BusinessPartnerInstitutions.Add(businessPartnerInstitution);
                    }
                }
            }
            return(BusinessPartnerInstitutions);

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

            //return businessPartnerInstitutions;
        }
Exemple #6
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);
        }