/// <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; financialPledge.GroupId = ddlGroup.SelectedValueAsInt(); var financialAccount = financialAccountService.Get(GetAttributeValue("Account").AsGuid()); if (financialAccount != null) { financialPledge.AccountId = financialAccount.Id; } financialPledge.TotalAmount = tbTotalAmount.Text.AsDecimal(); var pledgeFrequencySelection = DefinedValueCache.Read(ddlFrequency.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> /// Binds the grid. /// </summary> private void BindGrid() { var rockContext = new RockContext(); var pledgeService = new FinancialPledgeService(rockContext); var sortProperty = gPledges.SortProperty; var pledges = pledgeService.Queryable(); Person person = null; if (TargetPerson != null) { person = TargetPerson; } else { int?personId = gfPledges.GetUserPreference("Person").AsIntegerOrNull(); if (personId.HasValue && ppFilterPerson.Visible) { person = new PersonService(rockContext).Get(personId.Value); } } if (person != null) { // if a person is specified, get pledges for that person ( and also anybody in their GivingUnit ) pledges = pledges.Where(p => p.PersonAlias.Person.GivingId == person.GivingId); } // Filter by configured limit accounts if specified. var accountGuids = GetAttributeValue("Accounts").SplitDelimitedValues().AsGuidList(); if (accountGuids.Any()) { pledges = pledges.Where(p => accountGuids.Contains(p.Account.Guid)); } // get the accounts and make sure they still exist by checking the database var accountIds = gfPledges.GetUserPreference("Accounts").Split(',').AsIntegerList(); accountIds = new FinancialAccountService(rockContext).GetByIds(accountIds).Select(a => a.Id).ToList(); if (accountIds.Any() && apFilterAccount.Visible) { pledges = pledges.Where(p => p.AccountId.HasValue && accountIds.Contains(p.AccountId.Value)); } // Date Range DateRange filterDateRange = DateRangePicker.CalculateDateRangeFromDelimitedValues(gfPledges.GetUserPreference("Date Range")); var filterStartDate = filterDateRange.Start ?? DateTime.MinValue; var filterEndDate = filterDateRange.End ?? DateTime.MaxValue; /**** * Include any pledges whose Start/EndDates overlap with the Filtered Date Range * * * Pledge1 Range 1/1/2011 - 12/31/2011 * * Pledge2 Range 1/1/0000 - 1/1/9999 * * Pledge3 Range 6/1/2011 - 6/1/2012 * * Filter1 Range 1/1/2010 - 1/1/2013 * * * All Pledges should show * Filter1 Range 1/1/2012 - 1/1/2013 * * * Pledge2 and Pledge3 should show * Filter2 Range 5/1/2012 - 5/2/2012 * * * Pledge2 and Pledge3 should show * Filter3 Range 5/1/2012 - 1/1/9999 * * * Pledge2 and Pledge3 should show * Filter4 Range 5/1/2010 - 5/1/2010 * * * Pledge2 should show ***/ // exclude pledges that start after the filter's end date or end before the filter's start date if (drpDates.Visible && (filterDateRange.Start.HasValue || filterDateRange.End.HasValue)) { pledges = pledges.Where(p => !(p.StartDate > filterEndDate) && !(p.EndDate < filterStartDate)); } // Filter query by any configured attribute filters if (AvailableAttributes != null && AvailableAttributes.Any()) { foreach (var attribute in AvailableAttributes) { var filterControl = phAttributeFilters.FindControl("filter_" + attribute.Id.ToString()); pledges = attribute.FieldType.Field.ApplyAttributeQueryFilter(pledges, filterControl, attribute, pledgeService, Rock.Reporting.FilterMode.SimpleFilter); } } // Last Modified DateRange filterModifiedDateRange = DateRangePicker.CalculateDateRangeFromDelimitedValues(gfPledges.GetUserPreference("Last Modified")); var filterModifiedStartDate = filterModifiedDateRange.Start ?? DateTime.MinValue; var filterModifiedEndDate = filterModifiedDateRange.End ?? DateTime.MaxValue; if (drpLastModifiedDates.Visible && (filterModifiedDateRange.Start.HasValue || filterModifiedDateRange.End.HasValue)) { pledges = pledges.Where(p => !(p.ModifiedDateTime >= filterModifiedEndDate) && !(p.ModifiedDateTime <= filterModifiedStartDate)); } gPledges.DataSource = sortProperty != null?pledges.Sort(sortProperty).ToList() : pledges.OrderBy(p => p.AccountId).ToList(); gPledges.DataBind(); var showAccountSummary = this.GetAttributeValue("ShowAccountSummary").AsBoolean(); if (showAccountSummary || TargetPerson == null) { pnlSummary.Visible = true; var summaryList = pledges .GroupBy(a => a.AccountId) .Select(a => new AccountSummaryRow { AccountId = a.Key.Value, TotalAmount = a.Sum(x => x.TotalAmount), Name = a.Where(p => p.Account != null).Select(p => p.Account.Name).FirstOrDefault(), Order = a.Where(p => p.Account != null).Select(p => p.Account.Order).FirstOrDefault() }).ToList(); var grandTotalAmount = (summaryList.Count > 0) ? summaryList.Sum(a => a.TotalAmount) : 0; lGrandTotal.Text = grandTotalAmount.FormatAsCurrency(); rptAccountSummary.DataSource = summaryList.Select(a => new { a.Name, TotalAmount = a.TotalAmount.FormatAsCurrency() }).ToList(); rptAccountSummary.DataBind(); } else { pnlSummary.Visible = false; } }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { var rockContext = new RockContext(); var pledgeService = new FinancialPledgeService(rockContext); var sortProperty = gPledges.SortProperty; var pledges = pledgeService.Queryable(); Person person = null; if (TargetPerson != null) { person = TargetPerson; } else { int?personId = gfPledges.GetUserPreference("Person").AsIntegerOrNull(); if (personId.HasValue && ppFilterPerson.Visible) { person = new PersonService(rockContext).Get(personId.Value); } } if (person != null) { // if a person is specified, get pledges for that person ( and also anybody in their GivingUnit ) pledges = pledges.Where(p => p.PersonAlias.Person.GivingId == person.GivingId); } // get the accounts and make sure they still exist by checking the database var accountIds = gfPledges.GetUserPreference("Accounts").Split(',').AsIntegerList(); accountIds = new FinancialAccountService(rockContext).GetByIds(accountIds).Select(a => a.Id).ToList(); if (accountIds.Any() && apFilterAccount.Visible) { pledges = pledges.Where(p => p.AccountId.HasValue && accountIds.Contains(p.AccountId.Value)); } // Date Range var drp = new DateRangePicker(); drp.DelimitedValues = gfPledges.GetUserPreference("Date Range"); var filterStartDate = drp.LowerValue ?? DateTime.MinValue; var filterEndDate = drp.UpperValue ?? DateTime.MaxValue; if (filterEndDate != DateTime.MaxValue) { filterEndDate = filterEndDate.AddDays(1); } /**** * Include any pledges whose Start/EndDates overlap with the Filtered Date Range * * * Pledge1 Range 1/1/2011 - 12/31/2011 * * Pledge2 Range 1/1/0000 - 1/1/9999 * * Pledge3 Range 6/1/2011 - 6/1/2012 * * Filter1 Range 1/1/2010 - 1/1/2013 * * * All Pledges should show * Filter1 Range 1/1/2012 - 1/1/2013 * * * Pledge2 and Pledge3 should show * Filter2 Range 5/1/2012 - 5/2/2012 * * * Pledge2 and Pledge3 should show * Filter3 Range 5/1/2012 - 1/1/9999 * * * Pledge2 and Pledge3 should show * Filter4 Range 5/1/2010 - 5/1/2010 * * * Pledge2 should show ***/ // exclude pledges that start after the filter's end date or end before the filter's start date if (drpDates.Visible) { pledges = pledges.Where(p => !(p.StartDate > filterEndDate) && !(p.EndDate < filterStartDate)); } // Last Modified drp.DelimitedValues = gfPledges.GetUserPreference("Last Modified"); filterStartDate = drp.LowerValue ?? DateTime.MinValue; filterEndDate = drp.UpperValue ?? DateTime.MaxValue; if (filterEndDate != DateTime.MaxValue) { filterEndDate = filterEndDate.AddDays(1); } if (drpLastModifiedDates.Visible) { pledges = pledges.Where(p => !(p.ModifiedDateTime >= filterEndDate) && !(p.ModifiedDateTime <= filterStartDate)); } gPledges.DataSource = sortProperty != null?pledges.Sort(sortProperty).ToList() : pledges.OrderBy(p => p.AccountId).ToList(); gPledges.DataBind(); }
/// <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; financialPledge.GroupId = ddlGroup.SelectedValueAsInt(); var financialAccount = financialAccountService.Get(GetAttributeValue("Account").AsGuid()); if (financialAccount != null) { if (GetAttributeValue("UseCampusContext").AsBoolean()) { var campusEntity = RockPage.GetCurrentContext(EntityTypeCache.Get(typeof(Campus))); int?CurrentCampusContextId = null; if (campusEntity != null) { CurrentCampusContextId = campusEntity.Id; } if (CurrentCampusContextId != null && CurrentCampusContextId > -1) { var CampusAccount = financialAccount.ChildAccounts.OrderBy(c => c.Order).FirstOrDefault(c => c.CampusId == CurrentCampusContextId); if (CampusAccount != null) { financialAccount = CampusAccount; } } } financialPledge.AccountId = financialAccount.Id; } financialPledge.TotalAmount = tbTotalAmount.Text.AsDecimal(); var pledgeFrequencySelection = DefinedValueCache.Get(ddlFrequency.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; } } if (financialPledge.IsValid) { 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); 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 emailMessage = new RockEmailMessage(confirmationEmailTemplateGuid.Value); emailMessage.AddRecipient(new RockEmailMessageRecipient(person, mergeFields)); emailMessage.AppRoot = ResolveRockUrl("~/"); emailMessage.ThemeRoot = ResolveRockUrl("~~/"); emailMessage.Send(); } } else { ShowInvalidResults(financialPledge.ValidationResults); } }