/// <summary>
        ///  Create web model from entity
        /// </summary>
        public static ApiModel.BusinessPartnerMarketingChannel CreateFrom(this BusinessPartnerMarketingChannel source)
        {
            if (source == null)
            {
                return(null);
            }

            return(new ApiModel.BusinessPartnerMarketingChannel
            {
                BusinessPartnerMarketingChannelId = source.BusinessPartnerMarketingChannelId,
                MarketingChannelId = source.MarketingChannelId,
                MarketingChannelName = source.MarketingChannel != null ? (source.MarketingChannel.MarketingChannelCode + "-" + source.MarketingChannel.MarketingChannelName) : string.Empty,
                BusinessPartnerId = source.BusinessPartnerId
            });
        }
        /// <summary>
        /// Update business partner
        /// </summary>
        /// <param name="businessPartner"></param>
        /// <returns></returns>
        public bool UpdateBusinessPartner(BusinessPartner businessPartner)
        {
            BusinessPartner businessPartnerDbVersion = FindBusinessPartner((int)businessPartner.BusinessPartnerId);

            if (businessPartnerDbVersion != null)
            {
                //set master(business partner) properties
                #region Business Partner
                businessPartnerDbVersion.BusinessPartnerName       = businessPartner.BusinessPartnerName;
                businessPartnerDbVersion.BusinessPartnerDesciption = businessPartner.BusinessPartnerDesciption;
                businessPartnerDbVersion.IsSystemGuarantor         = businessPartner.IsSystemGuarantor;
                businessPartnerDbVersion.NonSystemGuarantor        = businessPartner.NonSystemGuarantor;
                businessPartnerDbVersion.IsIndividual = businessPartner.IsIndividual;
                businessPartnerDbVersion.BusinessPartnerEmailAddress = businessPartner.BusinessPartnerEmailAddress;
                businessPartnerDbVersion.CompanyId             = businessPartner.CompanyId;
                businessPartnerDbVersion.SystemGuarantorId     = businessPartner.SystemGuarantorId;
                businessPartnerDbVersion.BusinessLegalStatusId = businessPartner.BusinessLegalStatusId;
                businessPartnerDbVersion.DealingEmployeeId     = businessPartner.DealingEmployeeId;
                businessPartnerDbVersion.PaymentTermId         = businessPartner.PaymentTermId;
                businessPartnerDbVersion.BPRatingTypeId        = businessPartner.BPRatingTypeId;

                businessPartnerDbVersion.RecLastUpdatedDt = DateTime.Now;
                businessPartnerDbVersion.RecCreatedBy     = businessPartnerRepository.LoggedInUserIdentity;
                businessPartnerDbVersion.RowVersion       = businessPartnerDbVersion.RowVersion + 1;
                #endregion

                //set child (buiness partner individual properties)
                #region Business Partner Individual
                businessPartnerDbVersion.BusinessPartnerIndividual.BusinessPartnerId        = businessPartner.BusinessPartnerIndividual.BusinessPartnerId;
                businessPartnerDbVersion.BusinessPartnerIndividual.FirstName                = businessPartner.BusinessPartnerIndividual.FirstName;
                businessPartnerDbVersion.BusinessPartnerIndividual.MiddleName               = businessPartner.BusinessPartnerIndividual.MiddleName;
                businessPartnerDbVersion.BusinessPartnerIndividual.LastName                 = businessPartner.BusinessPartnerIndividual.LastName;
                businessPartnerDbVersion.BusinessPartnerIndividual.Initials                 = businessPartner.BusinessPartnerIndividual.Initials;
                businessPartnerDbVersion.BusinessPartnerIndividual.GenderStatus             = businessPartner.BusinessPartnerIndividual.GenderStatus;
                businessPartnerDbVersion.BusinessPartnerIndividual.MaritalStatusCode        = businessPartner.BusinessPartnerIndividual.MaritalStatusCode;
                businessPartnerDbVersion.BusinessPartnerIndividual.OccupationTypeId         = businessPartner.BusinessPartnerIndividual.OccupationTypeId;
                businessPartnerDbVersion.BusinessPartnerIndividual.LastName                 = businessPartner.BusinessPartnerIndividual.LastName;
                businessPartnerDbVersion.BusinessPartnerIndividual.DateOfBirth              = businessPartner.BusinessPartnerIndividual.DateOfBirth;
                businessPartnerDbVersion.BusinessPartnerIndividual.NicNumber                = businessPartner.BusinessPartnerIndividual.NicNumber;
                businessPartnerDbVersion.BusinessPartnerIndividual.NicExpiryDate            = businessPartner.BusinessPartnerIndividual.NicExpiryDate;
                businessPartnerDbVersion.BusinessPartnerIndividual.IqamaNo                  = businessPartner.BusinessPartnerIndividual.IqamaNo;
                businessPartnerDbVersion.BusinessPartnerIndividual.IqamaExpiryDate          = businessPartner.BusinessPartnerIndividual.IqamaExpiryDate;
                businessPartnerDbVersion.BusinessPartnerIndividual.PassportNumber           = businessPartner.BusinessPartnerIndividual.PassportNumber;
                businessPartnerDbVersion.BusinessPartnerIndividual.PassportExpiryDate       = businessPartner.BusinessPartnerIndividual.PassportExpiryDate;
                businessPartnerDbVersion.BusinessPartnerIndividual.PassportCountryId        = businessPartner.BusinessPartnerIndividual.PassportCountryId;
                businessPartnerDbVersion.BusinessPartnerIndividual.LiscenseNumber           = businessPartner.BusinessPartnerIndividual.LiscenseNumber;
                businessPartnerDbVersion.BusinessPartnerIndividual.LiscenseExpiryDate       = businessPartner.BusinessPartnerIndividual.LiscenseExpiryDate;
                businessPartnerDbVersion.BusinessPartnerIndividual.TaxRegisterationCode     = businessPartner.BusinessPartnerIndividual.TaxRegisterationCode;
                businessPartnerDbVersion.BusinessPartnerIndividual.TaxNumber                = businessPartner.BusinessPartnerIndividual.TaxNumber;
                businessPartnerDbVersion.BusinessPartnerIndividual.IsCompanyExternal        = businessPartner.BusinessPartnerIndividual.IsCompanyExternal;
                businessPartnerDbVersion.BusinessPartnerIndividual.BusinessPartnerCompanyId = businessPartner.BusinessPartnerIndividual.BusinessPartnerCompanyId;
                businessPartnerDbVersion.BusinessPartnerIndividual.CompanyName              = businessPartner.BusinessPartnerIndividual.CompanyName;
                businessPartnerDbVersion.BusinessPartnerIndividual.CompanyPhone             = businessPartner.BusinessPartnerIndividual.CompanyPhone;
                businessPartnerDbVersion.BusinessPartnerIndividual.CompanyAddress           = businessPartner.BusinessPartnerIndividual.CompanyAddress;

                businessPartnerDbVersion.BusinessPartnerIndividual.RowVersion       = businessPartnerDbVersion.BusinessPartnerIndividual.RowVersion + 1;
                businessPartnerDbVersion.BusinessPartnerIndividual.RecLastUpdatedDt = DateTime.Now;
                businessPartnerDbVersion.BusinessPartnerIndividual.RecLastUpdatedBy = businessPartnerRepository.LoggedInUserIdentity;
                #endregion

                //set child (buiness partner company properties)
                #region Business Partner Company
                businessPartnerDbVersion.BusinessPartnerCompany.BusinessPartnerId          = businessPartner.BusinessPartnerId;
                businessPartnerDbVersion.BusinessPartnerCompany.BusinessPartnerCompanyCode =
                    businessPartner.BusinessPartnerCompany.BusinessPartnerCompanyCode;
                businessPartnerDbVersion.BusinessPartnerCompany.BusinessPartnerCompanyName =
                    businessPartner.BusinessPartnerCompany.BusinessPartnerCompanyName;
                businessPartnerDbVersion.BusinessPartnerCompany.BusinessSegmentId =
                    businessPartner.BusinessPartnerCompany.BusinessSegmentId;
                businessPartnerDbVersion.BusinessPartnerCompany.AccountNumber =
                    businessPartner.BusinessPartnerCompany.AccountNumber;
                businessPartnerDbVersion.BusinessPartnerCompany.EstablishedSince =
                    businessPartner.BusinessPartnerCompany.EstablishedSince;
                businessPartnerDbVersion.BusinessPartnerCompany.SwiftCode =
                    businessPartner.BusinessPartnerCompany.SwiftCode;

                businessPartnerDbVersion.BusinessPartnerCompany.RowVersion =
                    businessPartnerDbVersion.BusinessPartnerCompany.RowVersion + 1;
                businessPartnerDbVersion.BusinessPartnerCompany.RecLastUpdatedDt = DateTime.Now;
                businessPartnerDbVersion.BusinessPartnerCompany.RecLastUpdatedBy = businessPartnerRepository.LoggedInUserIdentity;
                #endregion

                //set child (business partner intypes)
                #region Business Partner InTypes
                //add new items
                foreach (BusinessPartnerInType itemInType in businessPartner.BusinessPartnerInTypes)
                {
                    if (businessPartnerDbVersion.BusinessPartnerInTypes.All(x => x.BusinessPartnerInTypeId != itemInType.BusinessPartnerInTypeId) || itemInType.BusinessPartnerInTypeId == 0)
                    {
                        // set user domain key
                        itemInType.UserDomainKey = businessPartnerRepository.UserDomainKey;
                        businessPartnerDbVersion.BusinessPartnerInTypes.Add(itemInType);
                    }
                }
                //find missing items
                List <BusinessPartnerInType> missingItems = new List <BusinessPartnerInType>();
                foreach (BusinessPartnerInType dbversionItemInType in businessPartnerDbVersion.BusinessPartnerInTypes)
                {
                    if (businessPartner.BusinessPartnerInTypes.All(x => x.BusinessPartnerInTypeId != dbversionItemInType.BusinessPartnerInTypeId))
                    {
                        missingItems.Add(dbversionItemInType);
                    }
                }
                //remove missing items
                foreach (BusinessPartnerInType missingBusinessPartnerInType in missingItems)
                {
                    BusinessPartnerInType dbVersionMissingItem = businessPartnerDbVersion.BusinessPartnerInTypes.First(x => x.BusinessPartnerInTypeId == missingBusinessPartnerInType.BusinessPartnerInTypeId);
                    if (dbVersionMissingItem.BusinessPartnerInTypeId > 0)
                    {
                        businessPartnerDbVersion.BusinessPartnerInTypes.Remove(dbVersionMissingItem);
                        businessPartnerInTypeRepository.Delete(dbVersionMissingItem);
                    }
                }

                #endregion

                //set child (business partner phones)
                #region Business Partner Phones
                //add new phone items
                foreach (Phone phone in businessPartner.BusinessPartnerPhoneNumbers)
                {
                    if (businessPartnerDbVersion.BusinessPartnerPhoneNumbers.All(x => x.PhoneId != phone.PhoneId) || phone.PhoneId == 0)
                    {
                        // set properties
                        phone.IsActive         = true;
                        phone.IsDeleted        = false;
                        phone.IsPrivate        = false;
                        phone.IsReadOnly       = false;
                        phone.RecCreatedDt     = DateTime.Now;
                        phone.RecLastUpdatedDt = DateTime.Now;
                        phone.RecCreatedBy     = businessPartnerRepository.LoggedInUserIdentity;
                        phone.RecLastUpdatedBy = businessPartnerRepository.LoggedInUserIdentity;
                        phone.RowVersion       = 0;
                        phone.UserDomainKey    = businessPartnerRepository.UserDomainKey;
                        businessPartnerDbVersion.BusinessPartnerPhoneNumbers.Add(phone);
                    }
                }
                //find missing phone items
                List <Phone> missingPhoneItems = new List <Phone>();
                foreach (Phone dbversionPhoneItem in businessPartnerDbVersion.BusinessPartnerPhoneNumbers)
                {
                    if (businessPartner.BusinessPartnerPhoneNumbers.All(x => x.PhoneId != dbversionPhoneItem.PhoneId))
                    {
                        missingPhoneItems.Add(dbversionPhoneItem);
                    }
                }
                //remove missing phone items
                foreach (Phone missingBusinessPartnerPhone in missingPhoneItems)
                {
                    Phone dbVersionMissingPhoneItem = businessPartnerDbVersion.BusinessPartnerPhoneNumbers.First(x => x.PhoneId == missingBusinessPartnerPhone.PhoneId);
                    if (dbVersionMissingPhoneItem.PhoneId > 0)
                    {
                        businessPartnerDbVersion.BusinessPartnerPhoneNumbers.Remove(dbVersionMissingPhoneItem);
                        businessPartnerPhoneRepository.Delete(dbVersionMissingPhoneItem);
                    }
                }
                #endregion

                //set child (business partner address list)
                #region Business Partner Address List
                //add new address items
                foreach (Address address in businessPartner.BusinessPartnerAddressList)
                {
                    if (businessPartnerDbVersion.BusinessPartnerAddressList.All(x => x.AddressId != address.AddressId) || address.AddressId == 0)
                    {
                        // set properties
                        address.IsActive         = true;
                        address.IsDeleted        = false;
                        address.IsPrivate        = false;
                        address.IsReadOnly       = false;
                        address.RecCreatedDt     = DateTime.Now;
                        address.RecLastUpdatedDt = DateTime.Now;
                        address.RecCreatedBy     = businessPartnerRepository.LoggedInUserIdentity;
                        address.RecLastUpdatedBy = businessPartnerRepository.LoggedInUserIdentity;
                        address.RowVersion       = 0;
                        address.UserDomainKey    = businessPartnerRepository.UserDomainKey;
                        businessPartnerDbVersion.BusinessPartnerAddressList.Add(address);
                    }
                }
                //find missing address items
                List <Address> missingAddressItems = new List <Address>();
                foreach (Address dbversionAddressItem in businessPartnerDbVersion.BusinessPartnerAddressList)
                {
                    if (businessPartner.BusinessPartnerAddressList.All(x => x.AddressId != dbversionAddressItem.AddressId))
                    {
                        missingAddressItems.Add(dbversionAddressItem);
                    }
                }
                //remove missing address items
                foreach (Address missingBusinessPartnerAddress in missingAddressItems)
                {
                    Address dbVersionMissingAddressItem = businessPartnerDbVersion.BusinessPartnerAddressList.First(x => x.AddressId == missingBusinessPartnerAddress.AddressId);
                    if (dbVersionMissingAddressItem.AddressId > 0)
                    {
                        businessPartnerDbVersion.BusinessPartnerAddressList.Remove(dbVersionMissingAddressItem);
                        businessPartnerAddressRepository.Delete(dbVersionMissingAddressItem);
                    }
                }
                #endregion

                //set child (business partner marketing channel list)
                #region Business Partner Marketing Channels
                //add new marketing channel items
                foreach (BusinessPartnerMarketingChannel channel in businessPartner.BusinessPartnerMarketingChannels)
                {
                    if (businessPartnerDbVersion.BusinessPartnerMarketingChannels
                        .All(x => x.BusinessPartnerMarketingChannelId != channel.BusinessPartnerMarketingChannelId) || channel.BusinessPartnerMarketingChannelId == 0)
                    {
                        // set properties
                        channel.RecCreatedDt     = DateTime.Now;
                        channel.RecLastUpdatedDt = DateTime.Now;
                        channel.RecCreatedBy     = businessPartnerRepository.LoggedInUserIdentity;
                        channel.RecLastUpdatedBy = businessPartnerRepository.LoggedInUserIdentity;
                        channel.RowVersion       = 0;
                        channel.UserDomainKey    = businessPartnerRepository.UserDomainKey;
                        businessPartnerDbVersion.BusinessPartnerMarketingChannels.Add(channel);
                    }
                }
                //find missing marketing channel items
                List <BusinessPartnerMarketingChannel> missingChannelItems = new List <BusinessPartnerMarketingChannel>();
                foreach (BusinessPartnerMarketingChannel dbversionChannelItem in businessPartnerDbVersion.BusinessPartnerMarketingChannels)
                {
                    if (businessPartner.BusinessPartnerMarketingChannels.
                        All(x => x.BusinessPartnerMarketingChannelId != dbversionChannelItem.BusinessPartnerMarketingChannelId))
                    {
                        missingChannelItems.Add(dbversionChannelItem);
                    }
                }
                //remove missing Business Marketing Channel items
                foreach (BusinessPartnerMarketingChannel missingBusinessPartnerChannel in missingChannelItems)
                {
                    BusinessPartnerMarketingChannel dbversionMissingChannelItem = businessPartnerDbVersion.BusinessPartnerMarketingChannels.First(x => x.BusinessPartnerMarketingChannelId == missingBusinessPartnerChannel.BusinessPartnerMarketingChannelId);
                    if (dbversionMissingChannelItem.BusinessPartnerMarketingChannelId > 0)
                    {
                        businessPartnerDbVersion.BusinessPartnerMarketingChannels.Remove(dbversionMissingChannelItem);
                        businessPartnerMarketingChannelRepository.Delete(dbversionMissingChannelItem);
                    }
                }
                #endregion

                //set child (business partner relationship items list)
                #region Business Partner Relationship Items
                //add new business partner relationship items
                foreach (BusinessPartnerRelationship item in businessPartner.BusinessPartnerRelationshipItemList)
                {
                    if (businessPartnerDbVersion.BusinessPartnerRelationshipItemList
                        .All(x => x.BusinessPartnerRelationshipId != item.BusinessPartnerRelationshipId) || item.BusinessPartnerRelationshipId == 0)
                    {
                        // set properties
                        item.RecCreatedDt     = DateTime.Now;
                        item.RecLastUpdatedDt = DateTime.Now;
                        item.RecCreatedBy     = businessPartnerRepository.LoggedInUserIdentity;
                        item.RecLastUpdatedBy = businessPartnerRepository.LoggedInUserIdentity;
                        item.RowVersion       = 0;
                        item.UserDomainKey    = businessPartnerRepository.UserDomainKey;
                        businessPartnerDbVersion.BusinessPartnerRelationshipItemList.Add(item);
                    }
                }
                //find missing relationship items
                List <BusinessPartnerRelationship> missingRelationshipItems = new List <BusinessPartnerRelationship>();
                foreach (BusinessPartnerRelationship dbversionRelationshipItem in businessPartnerDbVersion.BusinessPartnerRelationshipItemList)
                {
                    if (businessPartner.BusinessPartnerRelationshipItemList.
                        All(x => x.BusinessPartnerRelationshipId != dbversionRelationshipItem.BusinessPartnerRelationshipId))
                    {
                        missingRelationshipItems.Add(dbversionRelationshipItem);
                    }
                }
                //remove missing relationship items
                foreach (BusinessPartnerRelationship missingBusinessPartnerRelationshipItem in missingRelationshipItems)
                {
                    BusinessPartnerRelationship dbversionMissingRelationshipItem = businessPartnerDbVersion.BusinessPartnerRelationshipItemList.First(x => x.BusinessPartnerRelationshipId == missingBusinessPartnerRelationshipItem.BusinessPartnerRelationshipId);
                    if (dbversionMissingRelationshipItem.BusinessPartnerRelationshipId > 0)
                    {
                        businessPartnerDbVersion.BusinessPartnerRelationshipItemList.Remove(
                            dbversionMissingRelationshipItem);
                        businessPartnerRelationshipRepository.Delete(dbversionMissingRelationshipItem);
                    }
                }
                #endregion

                // save changes
                businessPartnerRepository.SaveChanges();
                return(true);
            }

            return(false);
        }