Comparing sensitive data, confidential files or internal emails?

Most legal and privacy policies prohibit uploading sensitive data online. Diffchecker Desktop ensures your confidential information never leaves your computer. Work offline and compare documents securely.

Contribution Statement with Start and End Month Page Parameters

Created Diff never expires
4 removals
202 lines
29 additions
226 lines
// <copyright>
// <copyright>
// Copyright by the Spark Development Network
// Copyright by the Spark Development Network
//
//
// Licensed under the Rock Community License (the "License");
// Licensed under the Rock Community License (the "License");
// you may not use this file except in compliance with the License.
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// You may obtain a copy of the License at
//
//
// http://www.rockrms.com/license
// http://www.rockrms.com/license
//
//
// Unless required by applicable law or agreed to in writing, software
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// See the License for the specific language governing permissions and
// limitations under the License.
// limitations under the License.
// </copyright>
// </copyright>
using System;
using System;
using System.ComponentModel;
using System.ComponentModel;
using System.Linq;
using System.Linq;
using System.Net;
using System.Net;
using System.Web.UI;
using System.Web.UI;


using Rock;
using Rock;
using Rock.Attribute;
using Rock.Attribute;
using Rock.Data;
using Rock.Data;
using Rock.Financial;
using Rock.Financial;
using Rock.Model;
using Rock.Model;
using Rock.Web.UI;
using Rock.Web.UI;


namespace RockWeb.Blocks.Finance
namespace RockWeb.Blocks.Finance
{
{
/// <summary>
/// <summary>
/// </summary>
/// </summary>
[DisplayName( "Contribution Statement Generator" )]
[DisplayName( "Contribution Statement Generator" )]
[Category( "Finance" )]
[Category( "Finance" )]
[Description( "Block for generating a Contribution Statement" )]
[Description( "Block for generating a Contribution Statement" )]




[BooleanField(
[BooleanField(
"Allow Person QueryString",
"Allow Person QueryString",
Key = AttributeKey.AllowPersonQueryString,
Key = AttributeKey.AllowPersonQueryString,
Description = "Determines if any person other than the currently logged in person is allowed to be passed through the query string. For security reasons this is not allowed by default.",
Description = "Determines if any person other than the currently logged in person is allowed to be passed through the query string. For security reasons this is not allowed by default.",
DefaultBooleanValue = false,
DefaultBooleanValue = false,
Order = 0 )]
Order = 0 )]


[FinancialStatementTemplateField(
[FinancialStatementTemplateField(
"Statement Template",
"Statement Template",
Key = AttributeKey.FinancialStatementTemplate,
Key = AttributeKey.FinancialStatementTemplate,
DefaultValue = Rock.SystemGuid.FinancialStatementTemplate.ROCK_DEFAULT,
DefaultValue = Rock.SystemGuid.FinancialStatementTemplate.ROCK_DEFAULT,
Order = 1 )]
Order = 1 )]


[Rock.SystemGuid.BlockTypeGuid( "E0A699C3-61AA-4522-9067-1FE56FA80972" )]
[Rock.SystemGuid.BlockTypeGuid( "E0A699C3-61AA-4522-9067-1FE56FA80972" )]
public partial class ContributionStatementGenerator : RockBlock
public partial class ContributionStatementGenerator : RockBlock
{
{
#region Attribute Keys
#region Attribute Keys


private static class AttributeKey
private static class AttributeKey
{
{
public const string FinancialStatementTemplate = "FinancialStatementTemplate";
public const string FinancialStatementTemplate = "FinancialStatementTemplate";
public const string AllowPersonQueryString = "AllowPersonQueryString";
public const string AllowPersonQueryString = "AllowPersonQueryString";
}
}


#endregion Attribute Keys
#endregion Attribute Keys


#region PageParameterKeys
#region PageParameterKeys


private static class PageParameterKey
private static class PageParameterKey
{
{
public const string StatementYear = "StatementYear";
public const string StatementYear = "StatementYear";
public const string StatementStartMonth = "StatementStartMonth";
public const string StatementEndMonth = "StatementEndMonth";
public const string StatementEndMonth = "StatementEndMonth";
public const string PersonActionIdentifier = "rckid";
public const string PersonActionIdentifier = "rckid";
public const string PersonGuid = "PersonGuid";
public const string PersonGuid = "PersonGuid";
}
}


#endregion PageParameterKeys
#endregion PageParameterKeys


#region Events
#region Events


/// <summary>
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
/// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
/// </summary>
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
/// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnInit( EventArgs e )
protected override void OnInit( EventArgs e )
{
{
base.OnInit( e );
base.OnInit( e );


// this event gets fired after block settings are updated. it's nice to repaint the screen if these settings would alter it
// this event gets fired after block settings are updated. it's nice to repaint the screen if these settings would alter it
this.BlockUpdated += Block_BlockUpdated;
this.BlockUpdated += Block_BlockUpdated;
}
}


/// <summary>
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load" /> event.
/// Raises the <see cref="E:System.Web.UI.Control.Load" /> event.
/// </summary>
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param>
/// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnLoad( EventArgs e )
protected override void OnLoad( EventArgs e )
{
{
base.OnLoad( e );
base.OnLoad( e );


if ( !Page.IsPostBack )
if ( !Page.IsPostBack )
{
{
DisplayResults();
DisplayResults();
}
}
}
}


/// <summary>
/// <summary>
/// Handles the BlockUpdated event of the control.
/// Handles the BlockUpdated event of the control.
/// </summary>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected void Block_BlockUpdated( object sender, EventArgs e )
protected void Block_BlockUpdated( object sender, EventArgs e )
{
{
DisplayResults();
DisplayResults();
}
}


#endregion
#endregion


#region Methods
#region Methods


private void DisplayResults()
private void DisplayResults()
{
{
Person targetPerson = CurrentPerson;
Person targetPerson = CurrentPerson;


RockContext rockContext = new RockContext();
RockContext rockContext = new RockContext();
var statementYear = PageParameter( PageParameterKey.StatementYear ).AsIntegerOrNull() ?? RockDateTime.Now.Year;
var statementYear = PageParameter( PageParameterKey.StatementYear ).AsIntegerOrNull() ?? RockDateTime.Now.Year;
var statementStartMonth = PageParameter( PageParameterKey.StatementStartMonth ).AsIntegerOrNull() ?? 0;
var statementEndMonth = PageParameter( PageParameterKey.StatementEndMonth ).AsIntegerOrNull() ?? 0;
var statementEndMonth = PageParameter( PageParameterKey.StatementEndMonth ).AsIntegerOrNull() ?? 0;
var personActionId = PageParameter( PageParameterKey.PersonActionIdentifier );
var personActionId = PageParameter( PageParameterKey.PersonActionIdentifier );
var personGuid = PageParameter( PageParameterKey.PersonGuid ).AsGuidOrNull();
var personGuid = PageParameter( PageParameterKey.PersonGuid ).AsGuidOrNull();
var allowPersonQueryString = GetAttributeValue( AttributeKey.AllowPersonQueryString ).AsBoolean();
var allowPersonQueryString = GetAttributeValue( AttributeKey.AllowPersonQueryString ).AsBoolean();


if ( personActionId.IsNotNullOrWhiteSpace() )
if ( personActionId.IsNotNullOrWhiteSpace() )
{
{
var person = new PersonService( rockContext ).GetByPersonActionIdentifier( personActionId, "contribution-statement" );
var person = new PersonService( rockContext ).GetByPersonActionIdentifier( personActionId, "contribution-statement" );
var isCurrentPersonsBusiness = targetPerson != null && targetPerson.GetBusinesses().Any( b => b.Guid == person.Guid );
var isCurrentPersonsBusiness = targetPerson != null && targetPerson.GetBusinesses().Any( b => b.Guid == person.Guid );
if ( person != null && ( allowPersonQueryString || isCurrentPersonsBusiness ) )
if ( person != null && ( allowPersonQueryString || isCurrentPersonsBusiness ) )
{
{
targetPerson = person;
targetPerson = person;
}
}
}
}
else if ( personGuid.HasValue )
else if ( personGuid.HasValue )
{
{
// if "AllowPersonQueryString is False", only use the PersonGuid if it is a Guid of one of the current person's businesses
// if "AllowPersonQueryString is False", only use the PersonGuid if it is a Guid of one of the current person's businesses
var isCurrentPersonsBusiness = targetPerson != null && targetPerson.GetBusinesses().Any( b => b.Guid == personGuid.Value );
var isCurrentPersonsBusiness = targetPerson != null && targetPerson.GetBusinesses().Any( b => b.Guid == personGuid.Value );


if ( allowPersonQueryString || isCurrentPersonsBusiness )
if ( allowPersonQueryString || isCurrentPersonsBusiness )
{
{
var person = new PersonService( rockContext ).Get( personGuid.Value );
var person = new PersonService( rockContext ).Get( personGuid.Value );
if ( person != null )
if ( person != null )
{
{
targetPerson = person;
targetPerson = person;
}
}
}
}
}
}


if ( targetPerson == null )
if ( targetPerson == null )
{
{
Response.StatusCode = ( int ) HttpStatusCode.BadRequest;
Response.StatusCode = ( int ) HttpStatusCode.BadRequest;
Response.Write( "Invalid Person" );
Response.Write( "Invalid Person" );
Response.End();
Response.End();
}
}


FinancialStatementGeneratorOptions financialStatementGeneratorOptions = new FinancialStatementGeneratorOptions();
FinancialStatementGeneratorOptions financialStatementGeneratorOptions = new FinancialStatementGeneratorOptions();
var startDate = new DateTime( statementYear, 1, 1 );
DateTime startDate; // Declare startDate here
// If the statementStartMonth page parameter exists and its value is between 1 and 12 use it, otherwise startDate retains the normal behavior.
if ( statementStartMonth > 0 && statementStartMonth < 12 )
{
startDate = new DateTime( statementYear, statementStartMonth, 1 );
}
else
{
startDate = new DateTime( statementYear, 1, 1 ); // Assuming default startDate behavior
}
DateTime endDate; // Declare endDate here
// If the statementEndMonth page parameter exists and its value is between 1 and 12 use it, otherwise endDate retains the normal behavior.
// If the statementEndMonth page parameter exists and its value is between 1 and 12 use it, otherwise endDate retains the normal behavior.
var endDate = ( statementEndMonth > 0 && statementEndMonth < 12 ) ? ( new DateTime( statementYear, statementEndMonth, 1 ) ).AddMonths( 1 ) : startDate.AddYears( 1 );
if ( statementEndMonth > 0 && statementEndMonth < 12 )
{
endDate = new DateTime( statementYear, statementEndMonth, 1 ).AddMonths(1);
}
else
{
endDate = startDate.AddYears(1); // Assuming default endDate behavior
}
// Ensure that endDate is greater than or equal to startDate
if ( statementEndMonth < statementStartMonth )
{
endDate = startDate.AddYears(1).AddMonths(-1); // Removing a month from endDate if it's less than the startDate to create a full year
}
financialStatementGeneratorOptions.StartDate = startDate;
financialStatementGeneratorOptions.StartDate = startDate;
financialStatementGeneratorOptions.EndDate = endDate;
financialStatementGeneratorOptions.EndDate = endDate;
financialStatementGeneratorOptions.RenderMedium = "Html";
financialStatementGeneratorOptions.RenderMedium = "Html";


var financialStatementTemplateGuid = this.GetAttributeValue( AttributeKey.FinancialStatementTemplate ).AsGuidOrNull() ?? Rock.SystemGuid.FinancialStatementTemplate.ROCK_DEFAULT.AsGuid();
var financialStatementTemplateGuid = this.GetAttributeValue( AttributeKey.FinancialStatementTemplate ).AsGuidOrNull() ?? Rock.SystemGuid.FinancialStatementTemplate.ROCK_DEFAULT.AsGuid();


financialStatementGeneratorOptions.FinancialStatementTemplateId = new FinancialStatementTemplateService( rockContext ).GetId( financialStatementTemplateGuid );
financialStatementGeneratorOptions.FinancialStatementTemplateId = new FinancialStatementTemplateService( rockContext ).GetId( financialStatementTemplateGuid );


FinancialStatementGeneratorRecipient financialStatementGeneratorRecipient = new FinancialStatementGeneratorRecipient();
FinancialStatementGeneratorRecipient financialStatementGeneratorRecipient = new FinancialStatementGeneratorRecipient();


// It's required that we set the LocationId in order for the GetStatementGeneratorRecipientResult() to
// It's required that we set the LocationId in order for the GetStatementGeneratorRecipientResult() to
// fetch all the required data for the Lava.
// fetch all the required data for the Lava.
financialStatementGeneratorRecipient.LocationId = targetPerson.GetMailingLocation()?.Id;
financialStatementGeneratorRecipient.LocationId = targetPerson.GetMailingLocation()?.Id;
if ( targetPerson.GivingGroupId.HasValue )
if ( targetPerson.GivingGroupId.HasValue )
{
{
financialStatementGeneratorRecipient.GroupId = targetPerson.GivingGroupId.Value;
financialStatementGeneratorRecipient.GroupId = targetPerson.GivingGroupId.Value;
}
}
else
else
{
{
financialStatementGeneratorRecipient.GroupId = targetPerson.PrimaryFamilyId ?? 0;
financialStatementGeneratorRecipient.GroupId = targetPerson.PrimaryFamilyId ?? 0;
financialStatementGeneratorRecipient.PersonId = targetPerson.Id;
financialStatementGeneratorRecipient.PersonId = targetPerson.Id;
}
}


FinancialStatementGeneratorRecipientRequest financialStatementGeneratorRecipientRequest = new FinancialStatementGeneratorRecipientRequest( financialStatementGeneratorOptions )
FinancialStatementGeneratorRecipientRequest financialStatementGeneratorRecipientRequest = new FinancialStatementGeneratorRecipientRequest( financialStatementGeneratorOptions )
{
{
FinancialStatementGeneratorRecipient = financialStatementGeneratorRecipient
FinancialStatementGeneratorRecipient = financialStatementGeneratorRecipient
};
};


var result = FinancialStatementGeneratorHelper.GetStatementGeneratorRecipientResult( financialStatementGeneratorRecipientRequest, this.CurrentPerson );
var result = FinancialStatementGeneratorHelper.GetStatementGeneratorRecipientResult( financialStatementGeneratorRecipientRequest, this.CurrentPerson );


Response.Write( result.Html );
Response.Write( result.Html );
Response.End();
Response.End();
}
}


#endregion Methods
#endregion Methods
}
}
}
}