/// <summary>
        /// This method updates a user, profile and deposit
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="companyId"></param>
        /// <param name="depositId"></param>
        /// <param name="profile"></param>
        /// <param name="user"></param>
        public void UpdateUser(Int32 companyId, Int32 userId, Int32? depositId, Int32? representantId, Profile profile, User user)
        {
            //
            // Save Profile
            //
            var profileManager = new ProfileManager(this);

            profileManager.SaveProfile(profile);

            if (representantId.HasValue)
            {
                RemoveRepresentantFromUser(userId);
                AddRepresentantToUser(companyId, userId, (int)representantId);
            }
            else
                RemoveRepresentantFromUser(userId);

            AttachProfileEntityToUser(companyId, userId, profile.ProfileId);
            UpdateUser(userId, companyId, depositId, user);
        }
        public void UpdateUser(Int32 companyId, User user, Profile originalProfile, Profile profile)
        {

            // - atualizar o user
            // - atualizar o perfil
            // - associar o perfil ao usuário 
            // - associá-lo a uma compania




        }
        /// <summary>
        /// This method inserts a user
        /// </summary>
        /// <param name="companyId">Can't be null</param>
        /// <param name="user">Can't be null. This entity can't be attached in db</param>
        /// <param name="depositId">can be null</param>
        /// <returns>a status based on InsertCompanyStatus</returns>
        public InsertCompanyStatus InsertUser(int companyId, int? depositId, int? representantId, User user, Profile profile)
        {
            var membershipManager = new MembershipManager(this);

            //
            //method to insert a new user
            //

            var provider = Membership.Provider as VivinaMembershipProvider;
            MembershipCreateStatus status;

            membershipManager.Insert(user, out status, provider.RequiresValidEmail);

            //
            //verifies if the status of the inclusion is ok
            //

            if (status != MembershipCreateStatus.Success)
            {
                DataManager.Rollback();
                switch (status)
                {
                    case MembershipCreateStatus.InvalidPassword:
                        return InsertCompanyStatus.InvalidPassword;
                    case MembershipCreateStatus.DuplicateEmail:
                        return InsertCompanyStatus.DuplicatedAdminEmail;
                }
            }

            if (profile.ProfileId == 0)
                new ProfileManager(this).Insert(profile);

            AttachProfileEntityToUser(companyId, user.UserId, profile.ProfileId);

            if (representantId.HasValue)
                AddRepresentantToUser(companyId, user.UserId, Convert.ToInt32(representantId));

            //
            // Associate the User with company
            //
            if (!IsCompanyUser(companyId, user.UserId))
                AssociateUser(companyId, user.UserId, depositId, true);

            return InsertCompanyStatus.Success;
        }
        /// <summary>
        /// M�todo usado para criar a primeira companhia, utilizado na tela de registro
        /// </summary>
        /// <param name="newCompany"></param>
        /// <param name="newUser"></param>
        /// <param name="newProfile"></param>
        /// <returns></returns>
        public InsertCompanyStatus InsertMatrixCompany(Company newCompany, User newUser, Profile profile)
        {
            //
            // Insert the profile
            //
            var pManager = new ProfileManager(this);

            // Profile profile = pManager.GetProfile(newProfile.CPF) ?? newProfile;
            //if (profile.ProfileId == 0)
            pManager.Insert(profile);
            //else
            //{
            //    profile.Name = newProfile.Name;
            //    profile.Email = newProfile.Email;
            //    profile.PostalCode = newProfile.PostalCode;
            //    //profile.Address = newProfile.Address;
            //    profile.AddressComp = newProfile.AddressComp;
            //    profile.AddressNumber = newProfile.AddressNumber;
            //    profile.Phone = newProfile.Phone;
            //    DbContext.SubmitChanges();
            //}

            //
            //Insert Admin user
            //
            Int32 UserId;
            //newUser.ProfileId
            DataClasses.User original_User = GetUserByUserName(newUser.Email);
            if (original_User != null)
            {
                UserId = original_User.UserId;
            }
            else
            {
                MembershipCreateStatus status;
                var membershipManager = new MembershipManager(this);
                newUser.ProfileId = profile.ProfileId;
                membershipManager.Insert(
                    newUser,
                    out status,
                    (Membership.Provider as VivinaMembershipProvider).RequiresValidEmail);

                //
                //verify if the status of the inclusion are ok
                //
                if (status != MembershipCreateStatus.Success)
                {
                    DataManager.Rollback();
                    switch (status)
                    {
                        case MembershipCreateStatus.DuplicateUserName:
                            return InsertCompanyStatus.DuplicatedUserName;
                        case MembershipCreateStatus.InvalidPassword:
                            return InsertCompanyStatus.InvalidPassword;
                        case MembershipCreateStatus.DuplicateEmail:
                            return InsertCompanyStatus.DuplicatedAdminEmail;
                        default:
                            return InsertCompanyStatus.InvalidUser;
                    }
                }
                UserId = newUser.UserId;
            }

            if (newCompany.LegalEntityProfile.IsLiberalProfessional)
                newCompany.LegalEntityProfile.CompanyName = newCompany.LegalEntityProfile.FantasyName = profile.Name;

            var insertCompanyStatus = InsertCompany(newCompany, UserId, 0);

            newCompany.ReferenceCompanyId = newCompany.CompanyId;
            newCompany.MatrixId = newCompany.CompanyId;
            DbContext.SubmitChanges();

            return insertCompanyStatus;
        }
        public InsertCompanyStatus InsertUser(int companyId, User user, Profile profile, int? depositId)
        {
            //
            // If ProfileId equal 0 then the profile no exists in bd
            //
            var pManager = new ProfileManager(this);
            if (profile.ProfileId == 0)
                pManager.Insert(profile);

            //
            //method to insert a new user
            //
            var provider = Membership.Provider as VivinaMembershipProvider;
            MembershipCreateStatus status;
            var membershipManager = new MembershipManager(this);
            user.ProfileId = profile.ProfileId;
            membershipManager.Insert(user, out status, provider.RequiresValidEmail);
            //
            //verify if the status of the inclusion is ok
            //
            if (status != MembershipCreateStatus.Success)
            {
                DataManager.Rollback();
                switch (status)
                {
                    case MembershipCreateStatus.InvalidPassword:
                        return InsertCompanyStatus.InvalidPassword;
                    case MembershipCreateStatus.DuplicateEmail:
                        return InsertCompanyStatus.DuplicatedAdminEmail;
                }
            }

            //
            // Associate the User with company
            //
            AssociateUser(companyId, user.UserId, depositId, true);

            //
            // Insert a Employee
            //
            var humanResourcesManager = new HumanResourcesManager(this);
            var employee = new Employee();
            employee.IsActive = true;
            employee.ProfileId = profile.ProfileId;
            employee.CompanyId = companyId;
            humanResourcesManager.InsertEmployee(employee, new List<EmployeeAdditionalInformation>());

            return InsertCompanyStatus.Success;
        }