/// <summary> /// Sets the registration state /// </summary> private void SetRegistrationState() { string registrationSlug = PageParameter( REGISTRATION_SLUG_PARAM_NAME ); int? registrationInstanceId = PageParameter( REGISTRATION_INSTANCE_ID_PARAM_NAME ).AsIntegerOrNull(); int? registrationId = PageParameter( REGISTRATION_ID_PARAM_NAME ).AsIntegerOrNull(); int? groupId = PageParameter( REGISTRATION_GROUP_ID_PARAM_NAME ).AsIntegerOrNull(); int? campusId = PageParameter( REGISTRATION_CAMPUS_ID_PARAM_NAME ).AsIntegerOrNull(); // Not inside a "using" due to serialization needing context to still be active var rockContext = new RockContext(); // An existing registration id was specified if ( registrationId.HasValue ) { var registrationService = new RegistrationService( rockContext ); var registration = 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 == registrationId.Value ) .FirstOrDefault(); if ( registration != null ) { RegistrationInstanceState = registration.RegistrationInstance; RegistrationState = new RegistrationInfo( registration, rockContext ); RegistrationState.PreviousPaymentTotal = registrationService.GetTotalPayments( registration.Id ); } } // A registration slug was specified if ( RegistrationState == null && !string.IsNullOrWhiteSpace( registrationSlug ) ) { var dateTime = RockDateTime.Now; var linkage = new EventItemOccurrenceGroupMapService( rockContext ) .Queryable( "RegistrationInstance.Account,RegistrationInstance.RegistrationTemplate.Fees,RegistrationInstance.RegistrationTemplate.Discounts,RegistrationInstance.RegistrationTemplate.Forms.Fields.Attribute,RegistrationInstance.RegistrationTemplate.FinancialGateway" ) .Where( l => l.UrlSlug == registrationSlug && l.RegistrationInstance != null && l.RegistrationInstance.IsActive && l.RegistrationInstance.RegistrationTemplate != null && l.RegistrationInstance.RegistrationTemplate.IsActive && (!l.RegistrationInstance.StartDateTime.HasValue || l.RegistrationInstance.StartDateTime <= dateTime ) && (!l.RegistrationInstance.EndDateTime.HasValue || l.RegistrationInstance.EndDateTime > dateTime ) ) .FirstOrDefault(); if ( linkage != null ) { RegistrationInstanceState = linkage.RegistrationInstance; GroupId = linkage.GroupId; RegistrationState = new RegistrationInfo( CurrentPerson ); } } // A group id and campus id were specified if ( RegistrationState == null && groupId.HasValue && campusId.HasValue ) { var dateTime = RockDateTime.Now; var linkage = new EventItemOccurrenceGroupMapService( rockContext ) .Queryable( "RegistrationInstance.Account,RegistrationInstance.RegistrationTemplate.Fees,RegistrationInstance.RegistrationTemplate.Discounts,RegistrationInstance.RegistrationTemplate.Forms.Fields.Attribute,RegistrationInstance.RegistrationTemplate.FinancialGateway" ) .Where( l => l.GroupId == groupId && l.EventItemOccurrence != null && l.EventItemOccurrence.CampusId == campusId && l.RegistrationInstance != null && l.RegistrationInstance.IsActive && l.RegistrationInstance.RegistrationTemplate != null && l.RegistrationInstance.RegistrationTemplate.IsActive && ( !l.RegistrationInstance.StartDateTime.HasValue || l.RegistrationInstance.StartDateTime <= dateTime ) && ( !l.RegistrationInstance.EndDateTime.HasValue || l.RegistrationInstance.EndDateTime > dateTime ) ) .FirstOrDefault(); if ( linkage != null ) { RegistrationInstanceState = linkage.RegistrationInstance; GroupId = linkage.GroupId; RegistrationState = new RegistrationInfo( CurrentPerson ); } } // A registratio instance id was specified if ( RegistrationState == null && registrationInstanceId.HasValue ) { var dateTime = RockDateTime.Now; RegistrationInstanceState = new RegistrationInstanceService( rockContext ) .Queryable( "Account,RegistrationTemplate.Fees,RegistrationTemplate.Discounts,RegistrationTemplate.Forms.Fields.Attribute,RegistrationTemplate.FinancialGateway" ) .Where( r => r.Id == registrationInstanceId.Value && r.IsActive && r.RegistrationTemplate != null && r.RegistrationTemplate.IsActive && ( !r.StartDateTime.HasValue || r.StartDateTime <= dateTime ) && ( !r.EndDateTime.HasValue || r.EndDateTime > dateTime ) ) .FirstOrDefault(); if ( RegistrationInstanceState != null ) { RegistrationState = new RegistrationInfo( CurrentPerson ); } } if ( RegistrationState != null && !RegistrationState.Registrants.Any() ) { SetRegistrantState( 1 ); } }
/// <summary> /// Job that will run quick SQL queries on a schedule. /// /// Called by the <see cref="IScheduler" /> when a /// <see cref="ITrigger" /> fires that is associated with /// the <see cref="IJob" />. /// </summary> public virtual void Execute( IJobExecutionContext context ) { JobDataMap dataMap = context.JobDetail.JobDataMap; // get registrations where // + template is active // + instance is active // + template has a number of days between reminders // + template as fields needed to send a reminder email // + the registration has a cost // + the registration has been closed within the last xx days (to prevent eternal nagging) using ( RockContext rockContext = new RockContext()) { int sendCount = 0; int registrationInstanceCount = 0; var appRoot = Rock.Web.Cache.GlobalAttributesCache.Read().GetValue( "ExternalApplicationRoot" ); RegistrationService registrationService = new RegistrationService( rockContext ); var currentDate = RockDateTime.Today; var cutoffDays = dataMap.GetIntFromString( "CutoffDate" ); var registrations = registrationService.Queryable( "RegistrationInstance" ) .Where( r => r.RegistrationInstance.RegistrationTemplate.IsActive && r.RegistrationInstance.IsActive == true && (r.RegistrationInstance.RegistrationTemplate.PaymentReminderTimeSpan != null && r.RegistrationInstance.RegistrationTemplate.PaymentReminderTimeSpan != 0) && r.RegistrationInstance.RegistrationTemplate.PaymentReminderEmailTemplate != null && r.RegistrationInstance.RegistrationTemplate.PaymentReminderEmailTemplate.Length > 0 && r.RegistrationInstance.RegistrationTemplate.PaymentReminderFromEmail != null && r.RegistrationInstance.RegistrationTemplate.PaymentReminderFromEmail.Length > 0 && r.RegistrationInstance.RegistrationTemplate.PaymentReminderSubject != null && r.RegistrationInstance.RegistrationTemplate.PaymentReminderSubject.Length > 0 && (r.RegistrationInstance.RegistrationTemplate.Cost != 0 || (r.RegistrationInstance.Cost != null && r.RegistrationInstance.Cost != 0)) && (r.RegistrationInstance.EndDateTime == null || currentDate <= System.Data.Entity.SqlServer.SqlFunctions.DateAdd("day", cutoffDays, r.RegistrationInstance.EndDateTime) ) ) .ToList(); registrationInstanceCount = registrations.Select( r => r.RegistrationInstance.Id ).Distinct().Count(); foreach(var registration in registrations ) { if ( registration.DiscountedCost > registration.TotalPaid ) { var reminderDate = RockDateTime.Now.AddDays( registration.RegistrationInstance.RegistrationTemplate.PaymentReminderTimeSpan.Value * -1 ); if ( registration.LastPaymentReminderDateTime < reminderDate ) { var recipients = new List<string>(); Dictionary<string, object> mergeObjects = new Dictionary<string, object>(); mergeObjects.Add( "Registration", registration ); mergeObjects.Add( "RegistrationInstance", registration.RegistrationInstance ); recipients.Add( registration.ConfirmationEmail ); string message = registration.RegistrationInstance.RegistrationTemplate.PaymentReminderEmailTemplate.ResolveMergeFields( mergeObjects ); string fromEmail = registration.RegistrationInstance.RegistrationTemplate.PaymentReminderFromEmail.ResolveMergeFields( mergeObjects ); string fromName = registration.RegistrationInstance.RegistrationTemplate.PaymentReminderFromName.ResolveMergeFields( mergeObjects ); string subject = registration.RegistrationInstance.RegistrationTemplate.PaymentReminderSubject.ResolveMergeFields( mergeObjects ); Email.Send( fromEmail, fromName, subject, recipients, message, appRoot ); registration.LastPaymentReminderDateTime = RockDateTime.Now; rockContext.SaveChanges(); sendCount++; } } } context.Result = string.Format("Sent {0} from {1}", "reminder".ToQuantity( sendCount ), "registration instances".ToQuantity(registrationInstanceCount) ); } }
/// <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( 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> /// Sets the registration state /// </summary> private bool SetRegistrationState() { string registrationSlug = PageParameter( SLUG_PARAM_NAME ); int? registrationInstanceId = PageParameter( REGISTRATION_INSTANCE_ID_PARAM_NAME ).AsIntegerOrNull(); int? registrationId = PageParameter( REGISTRATION_ID_PARAM_NAME ).AsIntegerOrNull(); int? groupId = PageParameter( GROUP_ID_PARAM_NAME ).AsIntegerOrNull(); int? campusId = PageParameter( CAMPUS_ID_PARAM_NAME ).AsIntegerOrNull(); int? eventOccurrenceId = PageParameter( EVENT_OCCURRENCE_ID_PARAM_NAME ).AsIntegerOrNull(); // Not inside a "using" due to serialization needing context to still be active var rockContext = new RockContext(); // An existing registration id was specified if ( registrationId.HasValue ) { var registrationService = new RegistrationService( rockContext ); var registration = 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 == registrationId.Value ) .FirstOrDefault(); if ( registration == null ) { ShowError( "Error", "Registration not found" ); return false; } if ( CurrentPersonId == null ) { ShowWarning( "Please log in", "You must be logged in to access this registration." ); return false; } // Only allow the person that was logged in when this registration was created. // If the logged in person, registered on someone elses behalf (for example, husband logged in, but entered wife's name as the Registrar), // also allow that person to access the regisratiuon if ( ( registration.PersonAlias != null && registration.PersonAlias.PersonId == CurrentPersonId.Value ) || ( registration.CreatedByPersonAlias != null && registration.CreatedByPersonAlias.PersonId == CurrentPersonId.Value ) ) { RegistrationInstanceState = registration.RegistrationInstance; RegistrationState = new RegistrationInfo( registration, rockContext ); RegistrationState.PreviousPaymentTotal = registrationService.GetTotalPayments( registration.Id ); } else { ShowWarning( "Sorry", "You are not allowed to view or edit the selected registration since you are not the one who created the registration." ); return false; } // set the max number of steps in the progress bar numHowMany.Value = registration.Registrants.Count(); this.ProgressBarSteps = numHowMany.Value * FormCount + 2; // set group id if ( groupId.HasValue ) { GroupId = groupId; } else if ( !string.IsNullOrWhiteSpace( registrationSlug ) ) { var dateTime = RockDateTime.Now; var linkage = new EventItemOccurrenceGroupMapService( rockContext ) .Queryable().AsNoTracking() .Where( l => l.UrlSlug == registrationSlug && l.RegistrationInstance != null && l.RegistrationInstance.IsActive && l.RegistrationInstance.RegistrationTemplate != null && l.RegistrationInstance.RegistrationTemplate.IsActive && ( !l.RegistrationInstance.StartDateTime.HasValue || l.RegistrationInstance.StartDateTime <= dateTime ) && ( !l.RegistrationInstance.EndDateTime.HasValue || l.RegistrationInstance.EndDateTime > dateTime ) ) .FirstOrDefault(); if ( linkage != null ) { GroupId = linkage.GroupId; } } } // A registration slug was specified if ( RegistrationState == null && !string.IsNullOrWhiteSpace( registrationSlug ) ) { var dateTime = RockDateTime.Now; var linkage = new EventItemOccurrenceGroupMapService( rockContext ) .Queryable( "RegistrationInstance.Account,RegistrationInstance.RegistrationTemplate.Fees,RegistrationInstance.RegistrationTemplate.Discounts,RegistrationInstance.RegistrationTemplate.Forms.Fields.Attribute,RegistrationInstance.RegistrationTemplate.FinancialGateway" ) .Where( l => l.UrlSlug == registrationSlug && l.RegistrationInstance != null && l.RegistrationInstance.IsActive && l.RegistrationInstance.RegistrationTemplate != null && l.RegistrationInstance.RegistrationTemplate.IsActive && ( !l.RegistrationInstance.StartDateTime.HasValue || l.RegistrationInstance.StartDateTime <= dateTime ) && ( !l.RegistrationInstance.EndDateTime.HasValue || l.RegistrationInstance.EndDateTime > dateTime ) ) .FirstOrDefault(); if ( linkage != null ) { RegistrationInstanceState = linkage.RegistrationInstance; GroupId = linkage.GroupId; RegistrationState = new RegistrationInfo( CurrentPerson ); } } // A group id and campus id were specified if ( RegistrationState == null && groupId.HasValue && campusId.HasValue ) { var dateTime = RockDateTime.Now; var linkage = new EventItemOccurrenceGroupMapService( rockContext ) .Queryable( "RegistrationInstance.Account,RegistrationInstance.RegistrationTemplate.Fees,RegistrationInstance.RegistrationTemplate.Discounts,RegistrationInstance.RegistrationTemplate.Forms.Fields.Attribute,RegistrationInstance.RegistrationTemplate.FinancialGateway" ) .Where( l => l.GroupId == groupId && l.EventItemOccurrence != null && l.EventItemOccurrence.CampusId == campusId && l.RegistrationInstance != null && l.RegistrationInstance.IsActive && l.RegistrationInstance.RegistrationTemplate != null && l.RegistrationInstance.RegistrationTemplate.IsActive && ( !l.RegistrationInstance.StartDateTime.HasValue || l.RegistrationInstance.StartDateTime <= dateTime ) && ( !l.RegistrationInstance.EndDateTime.HasValue || l.RegistrationInstance.EndDateTime > dateTime ) ) .FirstOrDefault(); CampusId = campusId; if ( linkage != null ) { RegistrationInstanceState = linkage.RegistrationInstance; GroupId = linkage.GroupId; RegistrationState = new RegistrationInfo( CurrentPerson ); } } // A registration instance id was specified if ( RegistrationState == null && registrationInstanceId.HasValue ) { var dateTime = RockDateTime.Now; RegistrationInstanceState = new RegistrationInstanceService( rockContext ) .Queryable( "Account,RegistrationTemplate.Fees,RegistrationTemplate.Discounts,RegistrationTemplate.Forms.Fields.Attribute,RegistrationTemplate.FinancialGateway" ) .Where( r => r.Id == registrationInstanceId.Value && r.IsActive && r.RegistrationTemplate != null && r.RegistrationTemplate.IsActive && ( !r.StartDateTime.HasValue || r.StartDateTime <= dateTime ) && ( !r.EndDateTime.HasValue || r.EndDateTime > dateTime ) ) .FirstOrDefault(); if ( RegistrationInstanceState != null ) { RegistrationState = new RegistrationInfo( CurrentPerson ); } } // If registration instance id and event occurrence were specified, but a group (linkage) hasn't been loaded, find the first group for the event occurrence if ( RegistrationInstanceState != null && eventOccurrenceId.HasValue && !groupId.HasValue ) { var eventItemOccurrence = new EventItemOccurrenceService( rockContext ) .Queryable() .Where( o => o.Id == eventOccurrenceId.Value ) .FirstOrDefault(); if ( eventItemOccurrence != null ) { CampusId = eventItemOccurrence.CampusId; var linkage = eventItemOccurrence.Linkages .Where( l => l.RegistrationInstanceId == RegistrationInstanceState.Id ) .FirstOrDefault(); if ( linkage != null ) { GroupId = linkage.GroupId; } } } if ( RegistrationState != null && RegistrationState.FamilyGuid == Guid.Empty && RegistrationTemplate != null && RegistrationTemplate.RegistrantsSameFamily != RegistrantsSameFamily.Ask ) { RegistrationState.FamilyGuid = Guid.NewGuid(); } if ( RegistrationState != null && !RegistrationState.Registrants.Any() ) { SetRegistrantState( 1 ); } if ( RegistrationTemplate != null && RegistrationTemplate.FinancialGateway != null ) { var threeStepGateway = RegistrationTemplate.FinancialGateway.GetGatewayComponent() as ThreeStepGatewayComponent; Using3StepGateway = threeStepGateway != null; if ( Using3StepGateway ) { Step2IFrameUrl = ResolveRockUrl( threeStepGateway.Step2FormUrl ); } } return true; }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click( object sender, EventArgs e ) { if ( RegistrationId.HasValue ) { Registration registration = null; RockContext rockContext = new RockContext(); var registrationService = new RegistrationService( rockContext ); bool newRegistration = false; if ( RegistrationId.Value != 0 ) { registration = registrationService.Queryable().Where( g => g.Id == RegistrationId.Value ).FirstOrDefault(); } if ( registration == null ) { registration = new Registration { RegistrationInstanceId = RegistrationInstanceId ?? 0 }; registrationService.Add( registration ); newRegistration = true; } if ( registration != null && RegistrationInstanceId > 0 ) { registration.PersonAliasId = ppPerson.PersonAliasId; registration.FirstName = tbFirstName.Text; registration.LastName = tbLastName.Text; registration.ConfirmationEmail = ebConfirmationEmail.Text; registration.DiscountCode = ddlDiscountCode.SelectedValue; registration.DiscountPercentage = nbDiscountPercentage.Text.AsDecimal() * 0.01m; registration.DiscountAmount = cbDiscountAmount.Text.AsDecimal(); if ( !Page.IsValid ) { return; } if ( !registration.IsValid ) { // Controls will render the error messages return; } // use WrapTransaction since SaveAttributeValues does it's own RockContext.SaveChanges() rockContext.WrapTransaction( () => { rockContext.SaveChanges(); } ); if ( newRegistration ) { var pageRef = CurrentPageReference; pageRef.Parameters.AddOrReplace("RegistrationId", registration.Id.ToString()); NavigateToPage( pageRef ); } else { // Reload registration var reloadedRegistration = GetRegistration( registration.Id ); lWizardRegistrationName.Text = reloadedRegistration.ToString(); ShowReadonlyDetails( reloadedRegistration ); } } } }
/// <summary> /// Cleans up temporary registrations. /// </summary> private int CleanUpTemporaryRegistrations() { var registrationRockContext = new Rock.Data.RockContext(); int totalRowsDeleted = 0; // clean out any temporary registrations RegistrationService registrationService = new RegistrationService( registrationRockContext ); foreach ( var registration in registrationService.Queryable().Where( bf => bf.IsTemporary == true ).ToList() ) { if ( registration.ModifiedDateTime < RockDateTime.Now.AddHours( -1 ) ) { string errorMessage; if ( registrationService.CanDelete( registration, out errorMessage ) ) { registrationService.Delete( registration ); registrationRockContext.SaveChanges(); totalRowsDeleted++; } } } return totalRowsDeleted; }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click( object sender, EventArgs e ) { if ( RegistrationId.HasValue ) { Registration registration = null; RockContext rockContext = new RockContext(); var registrationService = new RegistrationService( rockContext ); bool newRegistration = false; var changes = new List<string>(); if ( RegistrationId.Value != 0 ) { registration = registrationService.Queryable().Where( g => g.Id == RegistrationId.Value ).FirstOrDefault(); } if ( registration == null ) { registration = new Registration { RegistrationInstanceId = RegistrationInstanceId ?? 0 }; registrationService.Add( registration ); newRegistration = true; changes.Add( "Created Registration" ); } if ( registration != null && RegistrationInstanceId > 0 ) { if ( !registration.PersonAliasId.Equals( ppPerson.PersonAliasId ) ) { string prevPerson = ( registration.PersonAlias != null && registration.PersonAlias.Person != null ) ? registration.PersonAlias.Person.FullName : string.Empty; string newPerson = ppPerson.PersonName; History.EvaluateChange( changes, "Registrar", prevPerson, newPerson ); } registration.PersonAliasId = ppPerson.PersonAliasId; History.EvaluateChange( changes, "First Name", registration.FirstName, tbFirstName.Text ); registration.FirstName = tbFirstName.Text; History.EvaluateChange( changes, "Last Name", registration.LastName, tbLastName.Text ); registration.LastName = tbLastName.Text; History.EvaluateChange( changes, "Confirmation Email", registration.ConfirmationEmail, ebConfirmationEmail.Text ); registration.ConfirmationEmail = ebConfirmationEmail.Text; bool groupChanged = !registration.GroupId.Equals( ddlGroup.SelectedValueAsInt() ); if ( groupChanged ) { History.EvaluateChange( changes, "Group", registration.GroupId, ddlGroup.SelectedValueAsInt() ); registration.GroupId = ddlGroup.SelectedValueAsInt(); } History.EvaluateChange( changes, "Discount Code", registration.DiscountCode, ddlDiscountCode.SelectedValue ); registration.DiscountCode = ddlDiscountCode.SelectedValue; History.EvaluateChange( changes, "Discount Percentage", registration.DiscountPercentage, nbDiscountPercentage.Text.AsDecimal() * 0.01m ); registration.DiscountPercentage = nbDiscountPercentage.Text.AsDecimal() * 0.01m; History.EvaluateChange( changes, "Discount Amount", registration.DiscountAmount, cbDiscountAmount.Text.AsDecimal() ); registration.DiscountAmount = cbDiscountAmount.Text.AsDecimal(); if ( !Page.IsValid ) { return; } if ( !registration.IsValid ) { // Controls will render the error messages return; } // use WrapTransaction since SaveAttributeValues does it's own RockContext.SaveChanges() rockContext.WrapTransaction( () => { rockContext.SaveChanges(); HistoryService.SaveChanges( rockContext, typeof( Registration ), Rock.SystemGuid.Category.HISTORY_EVENT_REGISTRATION.AsGuid(), registration.Id, changes ); } ); if ( newRegistration ) { var pageRef = CurrentPageReference; pageRef.Parameters.AddOrReplace("RegistrationId", registration.Id.ToString()); NavigateToPage( pageRef ); } else { // Reload registration Registration = GetRegistration( Registration.Id ); if ( groupChanged && Registration.GroupId.HasValue ) { foreach( var registrant in Registration.Registrants.Where( r => !r.GroupMemberId.HasValue ) ) { AddRegistrantToGroup( registrant.Id ); } // ...Add, reload again Registration = GetRegistration( Registration.Id ); } lWizardRegistrationName.Text = Registration.ToString(); ShowReadonlyDetails( Registration ); } } } }
/// <summary> /// Loads the content. /// </summary> protected void LoadContent() { RockContext rockContext = new RockContext(); var registrationService = new RegistrationService( rockContext ); var qryRegistrations = registrationService.Queryable(); // only show Active registrations qryRegistrations = qryRegistrations .Where( a => a.RegistrationInstance.IsActive == true && !a.IsTemporary ); // limit to the current person int currentPersonId = this.CurrentPersonId ?? 0; qryRegistrations = qryRegistrations.Where( a => a.PersonAlias.PersonId == currentPersonId ); // bring into a list so we can filter on non-database columns var registrationList = qryRegistrations.ToList(); List<Registration> hasDates = registrationList.Where(a => a.RegistrationInstance.Linkages.Any(x => x.EventItemOccurrenceId.HasValue && x.EventItemOccurrence.NextStartDateTime.HasValue)).ToList(); List<Registration> noDates = registrationList.Where( a => !hasDates.Any( d => d.Id == a.Id ) ).OrderBy( x => x.RegistrationInstance.Name ).ToList(); hasDates = hasDates.OrderBy(a => a.RegistrationInstance.Linkages.OrderBy(b => b.EventItemOccurrence.NextStartDateTime).FirstOrDefault().EventItemOccurrence.NextStartDateTime).ToList(); // filter by date range var requestDateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues( GetAttributeValue( "DateRange" ) ?? "-1||" ); if ( requestDateRange.Start.HasValue ) { hasDates = hasDates.Where( a => a.RegistrationInstance.Linkages.OrderBy( b => b.EventItemOccurrence.NextStartDateTime ).FirstOrDefault().EventItemOccurrence.NextStartDateTime >= requestDateRange.Start ).ToList(); } if ( requestDateRange.End.HasValue ) { hasDates = hasDates.Where( a => a.RegistrationInstance.Linkages.OrderBy( b => b.EventItemOccurrence.NextStartDateTime ).FirstOrDefault().EventItemOccurrence.NextStartDateTime < requestDateRange.End ).ToList(); } registrationList = hasDates; registrationList.AddRange(noDates); if ( this.GetAttributeValue( "LimitToOwed" ).AsBooleanOrNull() ?? true ) { registrationList = registrationList.Where( a => a.BalanceDue != 0 ).ToList(); } int? maxResults = GetAttributeValue( "MaxResults" ).AsIntegerOrNull(); if ( maxResults.HasValue && maxResults > 0 ) { registrationList = registrationList.Take( maxResults.Value ).ToList(); } var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( this.RockPage, this.CurrentPerson ); mergeFields.Add( "Registrations", registrationList ); string template = GetAttributeValue( "LavaTemplate" ); lContent.Text = template.ResolveMergeFields( mergeFields ); // show debug info if ( GetAttributeValue( "EnableDebug" ).AsBoolean() && IsUserAuthorized( Authorization.EDIT ) ) { lDebug.Visible = true; lDebug.Text = mergeFields.lavaDebugInfo( rockContext ); } }
/// <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; }