Exemple #1
0
        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <returns></returns>
        private int? SaveChanges()
        {
            Registration registration = null;

            if ( !string.IsNullOrWhiteSpace( TransactionCode ) )
            {
                ShowError( string.Empty, "You have already completed this " + RegistrationTerm.ToLower() );
            }
            else
            {
                try
                {
                    if ( RegistrationState != null && RegistrationState.Registrants.Any() && RegistrationTemplate != null )
                    {
                        var rockContext = new RockContext();

                        rockContext.WrapTransaction( () =>
                        {
                            bool hasPayment = ( RegistrationState.PaymentAmount ?? 0.0m ) > 0.0m;

                            if ( RegistrationState.RegistrationId.HasValue )
                            {
                                registration = new RegistrationService( rockContext ).Get( RegistrationState.RegistrationId.Value );
                            }
                            else
                            {
                                registration = SaveRegistration( rockContext, hasPayment );
                            }

                            if ( registration != null )
                            {
                                if ( hasPayment )
                                {
                                    string errorMessage = string.Empty;
                                    if ( !ProcessPayment( rockContext, registration, out errorMessage ) )
                                    {
                                        registration = null;
                                        throw new Exception( errorMessage );
                                    }
                                }

                                if ( registration != null )
                                {
                                    string appRoot = ResolveRockUrl( "~/" );
                                    string themeRoot = ResolveRockUrl( "~~/" );

                                    var confirmation = new Rock.Transactions.SendRegistrationConfirmationTransaction();
                                    confirmation.RegistrationId = registration.Id;
                                    confirmation.AppRoot = appRoot;
                                    confirmation.ThemeRoot = themeRoot;
                                    Rock.Transactions.RockQueue.TransactionQueue.Enqueue( confirmation );

                                    var notification = new Rock.Transactions.SendRegistrationNotificationTransaction();
                                    notification.RegistrationId = registration.Id;
                                    notification.AppRoot = appRoot;
                                    notification.ThemeRoot = themeRoot;
                                    Rock.Transactions.RockQueue.TransactionQueue.Enqueue( notification );
                                }
                            }
                        } );

                        // Re-create State
                        if ( registration != null )
                        {
                            var registrationService = new RegistrationService( rockContext );
                            var newRegistration = registrationService
                                .Queryable( "Registrants.PersonAlias.Person,Registrants.GroupMember,RegistrationInstance.Account,RegistrationInstance.RegistrationTemplate.Fees,RegistrationInstance.RegistrationTemplate.Discounts,RegistrationInstance.RegistrationTemplate.Forms.Fields.Attribute,RegistrationInstance.RegistrationTemplate.FinancialGateway" )
                                .AsNoTracking()
                                .Where( r => r.Id == registration.Id )
                                .FirstOrDefault();
                            if ( newRegistration != null )
                            {
                                RegistrationInstanceState = newRegistration.RegistrationInstance;
                                RegistrationState = new RegistrationInfo( newRegistration, rockContext );
                                RegistrationState.PreviousPaymentTotal = registrationService.GetTotalPayments( registration.Id );
                            }
                        }

                    }
                }
                catch ( Exception ex )
                {
                    ExceptionLogService.LogException( ex, Context, this.RockPage.PageId, this.RockPage.Site.Id, CurrentPersonAlias );
                    ShowError( "An Error Occurred Processing Your " + RegistrationTerm, ex.Message );
                    return (int?)null;
                }
            }

            return registration != null ? registration.Id : (int?)null;
        }
        private void ProcessPostSave( bool isNewRegistration, Registration registration, List<int> previousRegistrantIds, RockContext rockContext )
        {
            try
            {
                SavePersonNotes( rockContext, previousRegistrantIds, registration );
                AddRegistrantsToGroup( rockContext, registration );

                string appRoot = ResolveRockUrl( "~/" );
                string themeRoot = ResolveRockUrl( "~~/" );

                if ( isNewRegistration )
                {
                    var confirmation = new Rock.Transactions.SendRegistrationConfirmationTransaction();
                    confirmation.RegistrationId = registration.Id;
                    confirmation.AppRoot = appRoot;
                    confirmation.ThemeRoot = themeRoot;
                    Rock.Transactions.RockQueue.TransactionQueue.Enqueue( confirmation );

                    var notification = new Rock.Transactions.SendRegistrationNotificationTransaction();
                    notification.RegistrationId = registration.Id;
                    notification.AppRoot = appRoot;
                    notification.ThemeRoot = themeRoot;
                    Rock.Transactions.RockQueue.TransactionQueue.Enqueue( notification );
                }

                var registrationService = new RegistrationService( new RockContext() );
                var newRegistration = registrationService.Get( registration.Id );
                if ( newRegistration != null )
                {
                    if ( isNewRegistration )
                    {
                        if ( RegistrationTemplate.RequiredSignatureDocumentTemplateId.HasValue )
                        {
                            string email = newRegistration.ConfirmationEmail;
                            if ( string.IsNullOrWhiteSpace( email ) && newRegistration.PersonAlias != null && newRegistration.PersonAlias.Person != null )
                            {
                                email = newRegistration.PersonAlias.Person.Email;
                            }

                            Guid? adultRole = Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT.AsGuid();
                            var groupMemberService = new GroupMemberService( rockContext );

                            foreach ( var registrant in newRegistration.Registrants.Where( r => r.PersonAlias != null && r.PersonAlias.Person != null ) )
                            {
                                var assignedTo = registrant.PersonAlias.Person;

                                var registrantIsAdult = adultRole.HasValue && groupMemberService
                                    .Queryable().AsNoTracking()
                                    .Any( m =>
                                        m.PersonId == registrant.PersonAlias.PersonId &&
                                        m.GroupRole.Guid.Equals( adultRole.Value ) );
                                if ( !registrantIsAdult && newRegistration.PersonAlias != null && newRegistration.PersonAlias.Person != null )
                                {
                                    assignedTo = newRegistration.PersonAlias.Person;
                                }
                                else
                                {
                                    if ( !string.IsNullOrWhiteSpace( registrant.PersonAlias.Person.Email ) )
                                    {
                                        email = registrant.PersonAlias.Person.Email;
                                    }
                                }

                                if ( DigitalSignatureComponent != null )
                                {
                                    var sendDocumentTxn = new Rock.Transactions.SendDigitalSignatureRequestTransaction();
                                    sendDocumentTxn.SignatureDocumentTemplateId = RegistrationTemplate.RequiredSignatureDocumentTemplateId.Value;
                                    sendDocumentTxn.AppliesToPersonAliasId = registrant.PersonAlias.Id;
                                    sendDocumentTxn.AssignedToPersonAliasId = assignedTo.PrimaryAliasId ?? 0;
                                    sendDocumentTxn.DocumentName = string.Format( "{0}_{1}", RegistrationInstanceState.Name.RemoveSpecialCharacters(), registrant.PersonAlias.Person.FullName.RemoveSpecialCharacters() );
                                    sendDocumentTxn.Email = email;
                                    Rock.Transactions.RockQueue.TransactionQueue.Enqueue( sendDocumentTxn );
                                }
                            }
                        }

                        newRegistration.LaunchWorkflow( RegistrationTemplate.RegistrationWorkflowTypeId, newRegistration.ToString() );
                        newRegistration.LaunchWorkflow( RegistrationInstanceState.RegistrationWorkflowTypeId, newRegistration.ToString() );
                    }

                    RegistrationInstanceState = newRegistration.RegistrationInstance;
                    RegistrationState = new RegistrationInfo( newRegistration, rockContext );
                    RegistrationState.PreviousPaymentTotal = registrationService.GetTotalPayments( registration.Id );
                }
            }

            catch ( Exception postSaveEx )
            {
                ShowWarning( "The following occurred after processing your " + RegistrationTerm, postSaveEx.Message );
                ExceptionLogService.LogException( postSaveEx, Context, RockPage.PageId, RockPage.Layout.SiteId, CurrentPersonAlias );
            }
        }
        private void ProcessPostSave( RegistrationService registrationService, Registration registration, List<int> previousRegistrantIds, RockContext rockContext )
        {
            try
            {
                SavePersonNotes( rockContext, previousRegistrantIds, registration );
                AddRegistrantsToGroup( rockContext, registration );

                string appRoot = ResolveRockUrl( "~/" );
                string themeRoot = ResolveRockUrl( "~~/" );

                var confirmation = new Rock.Transactions.SendRegistrationConfirmationTransaction();
                confirmation.RegistrationId = registration.Id;
                confirmation.AppRoot = appRoot;
                confirmation.ThemeRoot = themeRoot;
                Rock.Transactions.RockQueue.TransactionQueue.Enqueue( confirmation );

                var notification = new Rock.Transactions.SendRegistrationNotificationTransaction();
                notification.RegistrationId = registration.Id;
                notification.AppRoot = appRoot;
                notification.ThemeRoot = themeRoot;
                Rock.Transactions.RockQueue.TransactionQueue.Enqueue( notification );

                var newRegistration = registrationService
                    .Queryable( "Registrants.PersonAlias.Person,Registrants.GroupMember,RegistrationInstance.Account,RegistrationInstance.RegistrationTemplate.Fees,RegistrationInstance.RegistrationTemplate.Discounts,RegistrationInstance.RegistrationTemplate.Forms.Fields.Attribute,RegistrationInstance.RegistrationTemplate.FinancialGateway" )
                    .Where( r => r.Id == registration.Id )
                    .FirstOrDefault();
                if ( newRegistration != null )
                {
                    RegistrationInstanceState = newRegistration.RegistrationInstance;
                    RegistrationState = new RegistrationInfo( newRegistration, rockContext );
                    RegistrationState.PreviousPaymentTotal = registrationService.GetTotalPayments( registration.Id );
                }
            }

            catch ( Exception postSaveEx )
            {
                ShowWarning( "The following occurred after processing your " + RegistrationTerm, postSaveEx.Message );
                ExceptionLogService.LogException( postSaveEx, Context, RockPage.PageId, RockPage.Layout.SiteId, CurrentPersonAlias );
            }
        }
        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <returns></returns>
        private int? SaveChanges()
        {
            if ( !string.IsNullOrWhiteSpace( TransactionCode ) )
            {
                ShowError( string.Empty, "You have already completed this " + RegistrationTerm.ToLower() );
                return null;
            }

            Registration registration = null;

            if ( RegistrationState != null && RegistrationState.Registrants.Any() && RegistrationTemplate != null )
            {
                var rockContext = new RockContext();

                var registrationService = new RegistrationService( rockContext );

                bool isNewRegistration = true;
                var previousRegistrants = new List<int>();
                if ( RegistrationState.RegistrationId.HasValue )
                {
                    var previousRegistration = registrationService.Get( RegistrationState.RegistrationId.Value );
                    if ( previousRegistration != null )
                    {
                        isNewRegistration = false;
                        previousRegistrants = previousRegistration.Registrants
                            .Where( r => r.PersonAlias != null )
                            .Select( r => r.PersonAlias.PersonId )
                            .ToList();
                    }
                }

                try
                {
                    bool hasPayment = ( RegistrationState.PaymentAmount ?? 0.0m ) > 0.0m;

                    // Save the registration
                    registration = SaveRegistration( rockContext, hasPayment );
                    if ( registration != null )
                    {
                        // If there is a payment being made, process the payment
                        if ( hasPayment )
                        {
                            string errorMessage = string.Empty;
                            if ( !ProcessPayment( rockContext, registration, out errorMessage ) )
                            {
                                throw new Exception( errorMessage );
                            }
                        }

                        try
                        {
                            // If there is a valid registration, and nothing went wrong processing the payment, add registrants to group and send the notifications
                            if ( registration != null )
                            {
                                SavePersonNotes( rockContext, previousRegistrants, registration );
                                AddRegistrantsToGroup( rockContext, registration );

                                string appRoot = ResolveRockUrl( "~/" );
                                string themeRoot = ResolveRockUrl( "~~/" );

                                var confirmation = new Rock.Transactions.SendRegistrationConfirmationTransaction();
                                confirmation.RegistrationId = registration.Id;
                                confirmation.AppRoot = appRoot;
                                confirmation.ThemeRoot = themeRoot;
                                Rock.Transactions.RockQueue.TransactionQueue.Enqueue( confirmation );

                                var notification = new Rock.Transactions.SendRegistrationNotificationTransaction();
                                notification.RegistrationId = registration.Id;
                                notification.AppRoot = appRoot;
                                notification.ThemeRoot = themeRoot;
                                Rock.Transactions.RockQueue.TransactionQueue.Enqueue( notification );

                                var newRegistration = registrationService
                                    .Queryable( "Registrants.PersonAlias.Person,Registrants.GroupMember,RegistrationInstance.Account,RegistrationInstance.RegistrationTemplate.Fees,RegistrationInstance.RegistrationTemplate.Discounts,RegistrationInstance.RegistrationTemplate.Forms.Fields.Attribute,RegistrationInstance.RegistrationTemplate.FinancialGateway" )
                                    .Where( r => r.Id == registration.Id )
                                    .FirstOrDefault();
                                if ( newRegistration != null )
                                {
                                    RegistrationInstanceState = newRegistration.RegistrationInstance;
                                    RegistrationState = new RegistrationInfo( newRegistration, rockContext );
                                    RegistrationState.PreviousPaymentTotal = registrationService.GetTotalPayments( registration.Id );
                                }
                            }
                        }
                        catch ( Exception postSaveEx )
                        {
                            ShowWarning( "The following occurred after processing your " + RegistrationTerm, postSaveEx.Message );
                            ExceptionLogService.LogException( postSaveEx, Context, RockPage.PageId, RockPage.Layout.SiteId, CurrentPersonAlias );
                        }
                    }

                }
                catch ( Exception ex )
                {
                    ExceptionLogService.LogException( ex, Context, this.RockPage.PageId, this.RockPage.Site.Id, CurrentPersonAlias );

                    string message = ex.Message;
                    while ( ex.InnerException != null )
                    {
                        ex = ex.InnerException;
                        message = ex.Message;
                    }

                    ShowError( "An Error Occurred Processing Your " + RegistrationTerm, ex.Message );

                    // Try to delete the registration if it was just created
                    try
                    {
                        if ( isNewRegistration && registration != null && registration.Id > 0 )
                        {
                            RegistrationState.RegistrationId = null;
                            using ( var newRockContext = new RockContext() )
                            {
                                HistoryService.DeleteChanges( newRockContext, typeof( Registration ), registration.Id );

                                var newRegistrationService = new RegistrationService( newRockContext );
                                var newRegistration = newRegistrationService.Get( registration.Id );
                                if ( newRegistration != null )
                                {
                                    newRegistrationService.Delete( newRegistration );
                                    newRockContext.SaveChanges();
                                }
                            }
                        }
                    }
                    catch { }

                    return (int?)null;
                }
            }

            return registration != null ? registration.Id : (int?)null;
        }