public static BusinessPartnerLocation ConvertToBusinessPartnerLocation(this BusinessPartnerLocationViewModel businessPartnerLocationViewModel)
        {
            BusinessPartnerLocation businessPartnerLocation = new BusinessPartnerLocation()
            {
                Id         = businessPartnerLocationViewModel.Id,
                Identifier = businessPartnerLocationViewModel.Identifier,

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

                Address = businessPartnerLocationViewModel.Address,

                CountryId      = businessPartnerLocationViewModel.Country?.Id ?? null,
                CityId         = businessPartnerLocationViewModel.City?.Id ?? null,
                MunicipalityId = businessPartnerLocationViewModel.Municipality?.Id ?? null,
                RegionId       = businessPartnerLocationViewModel.Region?.Id ?? null,
                ItemStatus     = businessPartnerLocationViewModel.ItemStatus,

                Active      = businessPartnerLocationViewModel.IsActive,
                CreatedById = businessPartnerLocationViewModel.CreatedBy?.Id ?? null,
                CompanyId   = businessPartnerLocationViewModel.Company?.Id ?? null,

                UpdatedAt = businessPartnerLocationViewModel.UpdatedAt,
                CreatedAt = businessPartnerLocationViewModel.CreatedAt,
            };

            return(businessPartnerLocation);
        }
Exemple #2
0
        public BusinessPartnerLocation Delete(Guid identifier)
        {
            BusinessPartnerLocation dbEntry = context.BusinessPartnerLocations
                                              .Union(context.ChangeTracker.Entries()
                                                     .Where(x => x.State == EntityState.Added && x.Entity.GetType() == typeof(BusinessPartnerLocation))
                                                     .Select(x => x.Entity as BusinessPartnerLocation))
                                              .FirstOrDefault(x => x.Identifier == identifier);

            if (dbEntry != null)
            {
                dbEntry.Active    = false;
                dbEntry.UpdatedAt = DateTime.Now;
            }
            return(dbEntry);
        }
Exemple #3
0
        public BusinessPartnerLocation Create(BusinessPartnerLocation businessPartnerLocation)
        {
            if (context.BusinessPartnerLocations.Where(x => x.Identifier != null && x.Identifier == businessPartnerLocation.Identifier).Count() == 0)
            {
                businessPartnerLocation.Id = 0;

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

                if (dbEntry != null)
                {
                    dbEntry.BusinessPartnerId = businessPartnerLocation.BusinessPartnerId ?? null;
                    dbEntry.CountryId         = businessPartnerLocation.CountryId ?? null;
                    dbEntry.CityId            = businessPartnerLocation.CityId ?? null;
                    dbEntry.MunicipalityId    = businessPartnerLocation.MunicipalityId ?? null;
                    dbEntry.RegionId          = businessPartnerLocation.RegionId ?? null;
                    dbEntry.CompanyId         = businessPartnerLocation.CompanyId ?? null;
                    dbEntry.CreatedById       = businessPartnerLocation.CreatedById ?? null;

                    // Set properties
                    dbEntry.Address    = businessPartnerLocation.Address;
                    dbEntry.ItemStatus = businessPartnerLocation.ItemStatus;
                    // Set timestamp
                    dbEntry.UpdatedAt = DateTime.Now;
                }

                return(dbEntry);
            }
        }
        public static BusinessPartnerLocationViewModel ConvertToBusinessPartnerLocationViewModelLite(this BusinessPartnerLocation businessPartnerLocation)
        {
            BusinessPartnerLocationViewModel businessPartnerLocationViewModel = new BusinessPartnerLocationViewModel()
            {
                Id         = businessPartnerLocation.Id,
                Identifier = businessPartnerLocation.Identifier,

                Address    = businessPartnerLocation.Address,
                ItemStatus = businessPartnerLocation.ItemStatus,
                IsActive   = businessPartnerLocation.Active,

                UpdatedAt = businessPartnerLocation.UpdatedAt,
                CreatedAt = businessPartnerLocation.CreatedAt,
            };

            return(businessPartnerLocationViewModel);
        }
        public static BusinessPartnerLocationViewModel ConvertToBusinessPartnerLocationViewModel(this BusinessPartnerLocation businessPartnerLocation)
        {
            BusinessPartnerLocationViewModel businessPartnerLocationViewModel = new BusinessPartnerLocationViewModel()
            {
                Id         = businessPartnerLocation.Id,
                Identifier = businessPartnerLocation.Identifier,

                BusinessPartner = businessPartnerLocation.BusinessPartner?.ConvertToBusinessPartnerViewModelLite(),

                Address = businessPartnerLocation.Address,

                Country      = businessPartnerLocation.Country?.ConvertToCountryViewModelLite(),
                City         = businessPartnerLocation.City?.ConvertToCityViewModelLite(),
                Municipality = businessPartnerLocation.Municipality?.ConvertToMunicipalityViewModelLite(),
                Region       = businessPartnerLocation.Region?.ConvertToRegionViewModelLite(),
                ItemStatus   = businessPartnerLocation.ItemStatus,
                IsActive     = businessPartnerLocation.Active,

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

                UpdatedAt = businessPartnerLocation.UpdatedAt,
                CreatedAt = businessPartnerLocation.CreatedAt,
            };

            return(businessPartnerLocationViewModel);
        }
Exemple #6
0
        public List <BusinessPartnerLocation> GetBusinessPartnerLocationsNewerThen(int companyId, DateTime lastUpdateTime)
        {
            List <BusinessPartnerLocation> BusinessPartnerLocations = new List <BusinessPartnerLocation>();

            string queryString =
                "SELECT BusinessPartnerLocationId, BusinessPartnerLocationIdentifier, " +
                "BusinessPartnerId, BusinessPartnerIdentifier, BusinessPartnerCode, BusinessPartnerName, " +
                "Address, " +
                "CountryId, CountryIdentifier, CountryCode, CountryName, " +
                "CityId, CityIdentifier, CityZipCode, CityName, " +
                "MunicipalityId, MunicipalityIdentifier, MunicipalityCode, MunicipalityName, " +
                "RegionId, RegionIdentifier, RegionCode, RegionName, " +
                "ItemStatus, Active, UpdatedAt, CreatedById, CreatedByFirstName, CreatedByLastName, CompanyId, CompanyName " +
                "FROM vBusinessPartnerLocations " +
                "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())
                {
                    BusinessPartnerLocation businessPartnerLocation;
                    while (reader.Read())
                    {
                        businessPartnerLocation            = new BusinessPartnerLocation();
                        businessPartnerLocation.Id         = Int32.Parse(reader["BusinessPartnerLocationId"].ToString());
                        businessPartnerLocation.Identifier = Guid.Parse(reader["BusinessPartnerLocationIdentifier"].ToString());

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

                        if (reader["Address"] != DBNull.Value)
                        {
                            businessPartnerLocation.Address = reader["Address"].ToString();
                        }

                        if (reader["CountryId"] != DBNull.Value)
                        {
                            businessPartnerLocation.Country            = new Country();
                            businessPartnerLocation.CountryId          = Int32.Parse(reader["CountryId"].ToString());
                            businessPartnerLocation.Country.Id         = Int32.Parse(reader["CountryId"].ToString());
                            businessPartnerLocation.Country.Identifier = Guid.Parse(reader["CountryIdentifier"].ToString());
                            businessPartnerLocation.Country.Mark       = reader["CountryCode"].ToString();
                            businessPartnerLocation.Country.Name       = reader["CountryName"].ToString();
                        }

                        if (reader["CityId"] != DBNull.Value)
                        {
                            businessPartnerLocation.City            = new City();
                            businessPartnerLocation.CityId          = Int32.Parse(reader["CityId"].ToString());
                            businessPartnerLocation.City.Id         = Int32.Parse(reader["CityId"].ToString());
                            businessPartnerLocation.City.Identifier = Guid.Parse(reader["CityIdentifier"].ToString());
                            businessPartnerLocation.City.ZipCode    = reader["CityZipCode"].ToString();
                            businessPartnerLocation.City.Name       = reader["CityName"].ToString();
                        }

                        if (reader["MunicipalityId"] != DBNull.Value)
                        {
                            businessPartnerLocation.Municipality                  = new Municipality();
                            businessPartnerLocation.MunicipalityId                = Int32.Parse(reader["MunicipalityId"].ToString());
                            businessPartnerLocation.Municipality.Id               = Int32.Parse(reader["MunicipalityId"].ToString());
                            businessPartnerLocation.Municipality.Identifier       = Guid.Parse(reader["MunicipalityIdentifier"].ToString());
                            businessPartnerLocation.Municipality.MunicipalityCode = reader["MunicipalityCode"].ToString();
                            businessPartnerLocation.Municipality.Name             = reader["MunicipalityName"].ToString();
                        }

                        if (reader["RegionId"] != DBNull.Value)
                        {
                            businessPartnerLocation.Region            = new Region();
                            businessPartnerLocation.RegionId          = Int32.Parse(reader["RegionId"].ToString());
                            businessPartnerLocation.Region.Id         = Int32.Parse(reader["RegionId"].ToString());
                            businessPartnerLocation.Region.Identifier = Guid.Parse(reader["RegionIdentifier"].ToString());
                            businessPartnerLocation.Region.RegionCode = reader["RegionCode"].ToString();
                            businessPartnerLocation.Region.Name       = reader["RegionName"].ToString();
                        }
                        if (reader["ItemStatus"] != DBNull.Value)
                        {
                            businessPartnerLocation.ItemStatus = Int32.Parse(reader["ItemStatus"].ToString());
                        }
                        businessPartnerLocation.Active    = bool.Parse(reader["Active"].ToString());
                        businessPartnerLocation.UpdatedAt = DateTime.Parse(reader["UpdatedAt"].ToString());

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

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

                        BusinessPartnerLocations.Add(businessPartnerLocation);
                    }
                }
            }
            return(BusinessPartnerLocations);

            //List<BusinessPartnerLocation> businessPartnerLocationes = context.BusinessPartnerLocations
            //    .Include(x => x.BusinessPartner)
            //    .Include(x => x.Country)
            //    .Include(x => x.City)
            //    .Include(x => x.Municipality)
            //    .Include(x => x.Region)
            //    .Include(x => x.Company)
            //    .Include(x => x.CreatedBy)
            //    .Where(x => x.Company.Id == companyId && x.UpdatedAt > lastUpdateTime)
            //    .AsNoTracking()
            //    .ToList();

            //return businessPartnerLocationes;
        }
Exemple #7
0
        public List <BusinessPartnerLocation> GetBusinessPartnerLocations(int companyId)
        {
            List <BusinessPartnerLocation> BusinessPartnerLocations = new List <BusinessPartnerLocation>();

            string queryString =
                "SELECT BusinessPartnerLocationId, BusinessPartnerLocationIdentifier, " +
                "BusinessPartnerId, BusinessPartnerIdentifier, BusinessPartnerCode, BusinessPartnerName, " +
                "Address, " +
                "CountryId, CountryIdentifier, CountryCode, CountryName, " +
                "CityId, CityIdentifier, CityZipCode, CityName, " +
                "MunicipalityId, MunicipalityIdentifier, MunicipalityCode, MunicipalityName, " +
                "RegionId, RegionIdentifier, RegionCode, RegionName, " +
                "ItemStatus, Active, UpdatedAt, CreatedById, CreatedByFirstName, CreatedByLastName, CompanyId, CompanyName " +
                "FROM vBusinessPartnerLocations " +
                "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())
                {
                    BusinessPartnerLocation businessPartnerLocation;
                    while (reader.Read())
                    {
                        businessPartnerLocation            = new BusinessPartnerLocation();
                        businessPartnerLocation.Id         = Int32.Parse(reader["BusinessPartnerLocationId"].ToString());
                        businessPartnerLocation.Identifier = Guid.Parse(reader["BusinessPartnerLocationIdentifier"].ToString());

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

                        if (reader["Address"] != DBNull.Value)
                        {
                            businessPartnerLocation.Address = reader["Address"].ToString();
                        }

                        if (reader["CountryId"] != DBNull.Value)
                        {
                            businessPartnerLocation.Country            = new Country();
                            businessPartnerLocation.CountryId          = Int32.Parse(reader["CountryId"].ToString());
                            businessPartnerLocation.Country.Id         = Int32.Parse(reader["CountryId"].ToString());
                            businessPartnerLocation.Country.Identifier = Guid.Parse(reader["CountryIdentifier"].ToString());
                            businessPartnerLocation.Country.Mark       = reader["CountryCode"].ToString();
                            businessPartnerLocation.Country.Name       = reader["CountryName"].ToString();
                        }

                        if (reader["CityId"] != DBNull.Value)
                        {
                            businessPartnerLocation.City            = new City();
                            businessPartnerLocation.CityId          = Int32.Parse(reader["CityId"].ToString());
                            businessPartnerLocation.City.Id         = Int32.Parse(reader["CityId"].ToString());
                            businessPartnerLocation.City.Identifier = Guid.Parse(reader["CityIdentifier"].ToString());
                            businessPartnerLocation.City.ZipCode    = reader["CityZipCode"].ToString();
                            businessPartnerLocation.City.Name       = reader["CityName"].ToString();
                        }

                        if (reader["MunicipalityId"] != DBNull.Value)
                        {
                            businessPartnerLocation.Municipality                  = new Municipality();
                            businessPartnerLocation.MunicipalityId                = Int32.Parse(reader["MunicipalityId"].ToString());
                            businessPartnerLocation.Municipality.Id               = Int32.Parse(reader["MunicipalityId"].ToString());
                            businessPartnerLocation.Municipality.Identifier       = Guid.Parse(reader["MunicipalityIdentifier"].ToString());
                            businessPartnerLocation.Municipality.MunicipalityCode = reader["MunicipalityCode"].ToString();
                            businessPartnerLocation.Municipality.Name             = reader["MunicipalityName"].ToString();
                        }

                        if (reader["RegionId"] != DBNull.Value)
                        {
                            businessPartnerLocation.Region            = new Region();
                            businessPartnerLocation.RegionId          = Int32.Parse(reader["RegionId"].ToString());
                            businessPartnerLocation.Region.Id         = Int32.Parse(reader["RegionId"].ToString());
                            businessPartnerLocation.Region.Identifier = Guid.Parse(reader["RegionIdentifier"].ToString());
                            businessPartnerLocation.Region.RegionCode = reader["RegionCode"].ToString();
                            businessPartnerLocation.Region.Name       = reader["RegionName"].ToString();
                        }
                        if (reader["ItemStatus"] != DBNull.Value)
                        {
                            businessPartnerLocation.ItemStatus = Int32.Parse(reader["ItemStatus"].ToString());
                        }
                        businessPartnerLocation.Active    = bool.Parse(reader["Active"].ToString());
                        businessPartnerLocation.UpdatedAt = DateTime.Parse(reader["UpdatedAt"].ToString());

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

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

                        BusinessPartnerLocations.Add(businessPartnerLocation);
                    }
                }
            }
            return(BusinessPartnerLocations);
        }
Exemple #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);
        }