/// <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> /// <exception cref="System.NotImplementedException"></exception> protected void btnSave_Click( object sender, EventArgs e ) { FinancialPledge pledge; var pledgeService = new FinancialPledgeService(); var pledgeId = int.Parse( hfPledgeId.Value ); if ( pledgeId == 0 ) { pledge = new FinancialPledge(); pledgeService.Add( pledge, CurrentPersonId ); } else { pledge = pledgeService.Get( pledgeId ); } pledge.PersonId = ppPerson.PersonId; pledge.AccountId = int.Parse( fpFund.SelectedValue ); pledge.TotalAmount = decimal.Parse( tbAmount.Text ); pledge.StartDate = dpDateRange.LowerValue.Value; pledge.EndDate = dpDateRange.UpperValue.Value; pledge.PledgeFrequencyValueId = int.Parse( ddlFrequencyType.SelectedValue ); if ( !pledge.IsValid ) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction( () => pledgeService.Save( pledge, CurrentPersonId ) ); NavigateToParentPage(); }
/// <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> /// <exception cref="System.NotImplementedException"></exception> protected void btnSave_Click( object sender, EventArgs e ) { FinancialPledge pledge; var rockContext = new RockContext(); var pledgeService = new FinancialPledgeService( rockContext ); var pledgeId = hfPledgeId.Value.AsInteger(); if ( pledgeId == 0 ) { pledge = new FinancialPledge(); pledgeService.Add( pledge ); } else { pledge = pledgeService.Get( pledgeId ); } if (ppPerson.PersonId.HasValue) { pledge.PersonAliasId = ppPerson.PersonAliasId; } pledge.AccountId = apAccount.SelectedValue.AsIntegerOrNull(); pledge.TotalAmount = tbAmount.Text.AsDecimal(); pledge.StartDate = dpDateRange.LowerValue.HasValue ? dpDateRange.LowerValue.Value : DateTime.MinValue; pledge.EndDate = dpDateRange.UpperValue.HasValue ? dpDateRange.UpperValue.Value : DateTime.MaxValue; pledge.PledgeFrequencyValueId = ddlFrequencyType.SelectedValue.AsIntegerOrNull(); if ( !pledge.IsValid ) { // Controls will render the error messages return; } rockContext.SaveChanges(); NavigateToParentPage(); }
/// <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> /// <exception cref="System.NotImplementedException"></exception> protected void btnSave_Click( object sender, EventArgs e ) { var rockContext = new RockContext(); var financialPledgeService = new FinancialPledgeService( rockContext ); var financialAccountService = new FinancialAccountService( rockContext ); var definedValueService = new DefinedValueService( rockContext ); var person = FindPerson( rockContext ); FinancialPledge financialPledge = new FinancialPledge(); financialPledge.PersonAliasId = person.PrimaryAliasId; var financialAccount = financialAccountService.Get( GetAttributeValue( "Account" ).AsGuid() ); if ( financialAccount != null ) { financialPledge.AccountId = financialAccount.Id; } financialPledge.TotalAmount = tbTotalAmount.Text.AsDecimal(); var pledgeFrequencySelection = DefinedValueCache.Read( bddlFrequency.SelectedValue.AsInteger() ); if ( pledgeFrequencySelection != null ) { financialPledge.PledgeFrequencyValueId = pledgeFrequencySelection.Id; } financialPledge.StartDate = drpDateRange.LowerValue ?? DateTime.MinValue; financialPledge.EndDate = drpDateRange.UpperValue ?? DateTime.MaxValue; if ( sender != btnConfirm ) { var duplicatePledges = financialPledgeService.Queryable() .Where( a => a.PersonAlias.PersonId == person.Id ) .Where( a => a.AccountId == financialPledge.AccountId ) .Where( a => a.StartDate == financialPledge.StartDate ) .Where( a => a.EndDate == financialPledge.EndDate ).ToList(); if ( duplicatePledges.Any() ) { pnlAddPledge.Visible = false; pnlConfirm.Visible = true; nbDuplicatePledgeWarning.Text = "The following pledges have already been entered for you:"; nbDuplicatePledgeWarning.Text += "<ul>"; foreach ( var pledge in duplicatePledges.OrderBy( a => a.StartDate ).ThenBy( a => a.Account.Name ) ) { nbDuplicatePledgeWarning.Text += string.Format( "<li>{0} {1} {2}</li>", pledge.Account, pledge.PledgeFrequencyValue, pledge.TotalAmount ); } nbDuplicatePledgeWarning.Text += "</ul>"; return; } } financialPledgeService.Add( financialPledge ); rockContext.SaveChanges(); // populate account so that Liquid can access it financialPledge.Account = financialAccount; // populate PledgeFrequencyValue so that Liquid can access it financialPledge.PledgeFrequencyValue = definedValueService.Get( financialPledge.PledgeFrequencyValueId ?? 0 ); var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( this.RockPage, this.CurrentPerson ); mergeFields.Add( "Person", person ); mergeFields.Add( "FinancialPledge", financialPledge ); mergeFields.Add( "PledgeFrequency", pledgeFrequencySelection ); mergeFields.Add( "Account", financialAccount ); lReceipt.Text = GetAttributeValue( "ReceiptText" ).ResolveMergeFields( mergeFields ); // Resolve any dynamic url references string appRoot = ResolveRockUrl( "~/" ); string themeRoot = ResolveRockUrl( "~~/" ); lReceipt.Text = lReceipt.Text.Replace( "~~/", themeRoot ).Replace( "~/", appRoot ); // show liquid help for debug if ( GetAttributeValue( "EnableDebug" ).AsBoolean() && IsUserAuthorized( Authorization.EDIT ) ) { lReceipt.Text += mergeFields.lavaDebugInfo(); } lReceipt.Visible = true; pnlAddPledge.Visible = false; pnlConfirm.Visible = false; // if a ConfirmationEmailTemplate is configured, send an email var confirmationEmailTemplateGuid = GetAttributeValue( "ConfirmationEmailTemplate" ).AsGuidOrNull(); if ( confirmationEmailTemplateGuid.HasValue ) { var recipients = new List<Rock.Communication.RecipientData>(); // add person and the mergeObjects (same mergeobjects as receipt) recipients.Add( new Rock.Communication.RecipientData( person.Email, mergeFields ) ); Rock.Communication.Email.Send( confirmationEmailTemplateGuid.Value, recipients, ResolveRockUrl( "~/" ), ResolveRockUrl( "~~/" ) ); } }
/// <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> /// <exception cref="System.NotImplementedException"></exception> protected void btnSave_Click( object sender, EventArgs e ) { var rockContext = new RockContext(); var pledgeService = new FinancialPledgeService( rockContext ); var person = FindPerson( rockContext ); var pledges = GetPledges( person ).ToList(); // Does this person already have a pledge for these accounts? // If so, give them the option to create a new one? var personPledgeAccountIds = pledgeService.Queryable() .Where( p => p.PersonId == person.Id ) .Select( p => p.AccountId ) .ToList(); if ( Accounts.Any( a => personPledgeAccountIds.Contains( a.Id ) ) ) { pnlConfirm.Visible = true; Session.Add( "CachedPledges", pledges ); return; } foreach ( var pledge in pledges ) { if ( !pledge.IsValid ) { continue; } pledgeService.Add( pledge ); } rockContext.SaveChanges(); ShowReceipt( pledges.Select( p => p.Id ) ); }
/// <summary> /// Handles the Click event of the btnConfirmYes 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 btnConfirmYes_Click( object sender, EventArgs e ) { var pledges = Session["CachedPledges"] as List<FinancialPledge>; if ( pledges == null ) { return; } var rockContext = new RockContext(); var pledgeService = new FinancialPledgeService( rockContext ); foreach ( var pledge in pledges ) { if ( pledge == null || !pledge.IsValid ) { continue; } pledgeService.Add( pledge ); } rockContext.SaveChanges(); Session.Remove( "CachedPledges" ); ShowReceipt( pledges.Select( p => p.Id ) ); }
/// <summary> /// Handles the Click event of the btnConfirmYes 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 btnConfirmYes_Click( object sender, EventArgs e ) { var pledges = Session["CachedPledges"] as List<FinancialPledge>; if ( pledges == null ) return; RockTransactionScope.WrapTransaction( () => { foreach ( var pledge in pledges ) { if ( pledge == null || !pledge.IsValid ) continue; var pledgeService = new FinancialPledgeService(); pledgeService.Add( pledge, CurrentPersonId ); pledgeService.Save( pledge, CurrentPersonId ); } Session.Remove( "CachedPledges" ); ShowReceipt( pledges.Select( p => p.Id ) ); } ); }
/// <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> /// <exception cref="System.NotImplementedException"></exception> protected void btnSave_Click( object sender, EventArgs e ) { using ( new UnitOfWorkScope() ) { RockTransactionScope.WrapTransaction( () => { var pledgeService = new FinancialPledgeService(); var person = FindPerson(); var pledges = GetPledges( person ).ToList(); // Does this person already have a pledge for these accounts? // If so, give them the option to create a new one? var personPledgeAccountIds = pledgeService.Queryable() .Where( p => p.PersonId == person.Id ) .Select( p => p.AccountId ) .ToList(); if ( Accounts.Any( a => personPledgeAccountIds.Contains( a.Id ) ) ) { pnlConfirm.Visible = true; Session.Add( "CachedPledges", pledges ); return; } foreach ( var pledge in pledges ) { if ( !pledge.IsValid ) continue; pledgeService.Add( pledge, person.Id ); pledgeService.Save( pledge, person.Id ); } ShowReceipt( pledges.Select( p => p.Id ) ); } ); } }
/// <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> /// <exception cref="System.NotImplementedException"></exception> protected void btnSave_Click( object sender, EventArgs e ) { var rockContext = new RockContext(); var financialPledgeService = new FinancialPledgeService( rockContext ); var financialAccountService = new FinancialAccountService( rockContext ); var definedValueService = new DefinedValueService( rockContext ); var person = FindPerson( rockContext ); FinancialPledge financialPledge = new FinancialPledge(); financialPledge.PersonId = person.Id; var financialAccount = financialAccountService.Get( GetAttributeValue( "Account" ).AsGuid() ); if ( financialAccount != null ) { financialPledge.AccountId = financialAccount.Id; } financialPledge.TotalAmount = tbTotalAmount.Text.AsDecimal(); var pledgeFrequencySelection = DefinedValueCache.Read( bddlFrequency.SelectedValue.AsInteger() ); if ( pledgeFrequencySelection != null ) { financialPledge.PledgeFrequencyValueId = pledgeFrequencySelection.Id; } financialPledge.StartDate = drpDateRange.LowerValue ?? DateTime.MinValue; financialPledge.EndDate = drpDateRange.UpperValue ?? DateTime.MaxValue; if ( sender != btnConfirm ) { var duplicatePledges = financialPledgeService.Queryable() .Where( a => a.PersonId == person.Id ) .Where( a => a.AccountId == financialPledge.AccountId ) .Where( a => a.StartDate == financialPledge.StartDate ) .Where( a => a.EndDate == financialPledge.EndDate ).ToList(); if ( duplicatePledges.Any() ) { pnlAddPledge.Visible = false; pnlConfirm.Visible = true; nbDuplicatePledgeWarning.Text = "The following pledges have already been entered for you:"; nbDuplicatePledgeWarning.Text += "<ul>"; foreach ( var pledge in duplicatePledges.OrderBy( a => a.StartDate ).ThenBy( a => a.Account.Name ) ) { nbDuplicatePledgeWarning.Text += string.Format( "<li>{0} {1} {2}</li>", pledge.Account, pledge.PledgeFrequencyValue, pledge.TotalAmount ); } nbDuplicatePledgeWarning.Text += "</ul>"; return; } } financialPledgeService.Add( financialPledge ); rockContext.SaveChanges(); // populate account so that Liquid can access it financialPledge.Account = financialAccount; // populate PledgeFrequencyValue so that Liquid can access it financialPledge.PledgeFrequencyValue = definedValueService.Get( financialPledge.PledgeFrequencyValueId ?? 0 ); var mergeObjects = new Dictionary<string, object>(); mergeObjects.Add( "Person", person ); mergeObjects.Add( "FinancialPledge", financialPledge ); mergeObjects.Add( "PledgeFrequency", pledgeFrequencySelection ); mergeObjects.Add( "Account", financialAccount ); lReceipt.Text = GetAttributeValue( "ReceiptText" ).ResolveMergeFields( mergeObjects ); // show liquid help for debug if ( GetAttributeValue( "EnableDebug" ).AsBooleanOrNull() ?? false ) { StringBuilder debugInfo = new StringBuilder(); debugInfo.Append( "<p /><div class='alert alert-info'><h4>Debug Info</h4>" ); debugInfo.Append( "<pre>" ); debugInfo.Append( "<p /><strong>Liquid Data</strong> <br>" ); debugInfo.Append( mergeObjects.LiquidHelpText() + "</pre>" ); debugInfo.Append( "</div>" ); lReceipt.Text += debugInfo.ToString(); } lReceipt.Visible = true; pnlAddPledge.Visible = false; pnlConfirm.Visible = false; // if a ConfirmationEmailTemplate is configured, send an email var confirmationEmailTemplateGuid = GetAttributeValue( "ConfirmationEmailTemplate" ).AsGuidOrNull(); if ( confirmationEmailTemplateGuid.HasValue ) { var recipients = new Dictionary<string, Dictionary<string, object>>(); // add person and the mergeObjects (same mergeobjects as receipt) recipients.Add( person.Email, mergeObjects ); Rock.Communication.Email.Send( confirmationEmailTemplateGuid.Value, recipients, ResolveRockUrl( "~/" ), ResolveRockUrl( "~~/" ) ); } }