Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Add in fiscal year fetch handler #421

Merged
merged 7 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion FakeXrmEasy.Shared/Extensions/XmlExtensionsForFetchXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static class XmlExtensionsForFetchXml
ConditionOperator.LastXMonths,
ConditionOperator.LastXWeeks,
ConditionOperator.LastXYears,
ConditionOperator.NextXWeeks
ConditionOperator.NextXWeeks,
ConditionOperator.InFiscalYear
};

public static bool IsAttributeTrue(this XElement elem, string attributeName)
Expand Down Expand Up @@ -520,6 +521,9 @@ public static ConditionExpression ToConditionExpression(this XElement elem, XrmF
case "next-week":
op = ConditionOperator.NextWeek;
break;
case "in-fiscal-year":
op = ConditionOperator.InFiscalYear;
break;
#if FAKE_XRM_EASY_9
case "contain-values":
op = ConditionOperator.ContainValues;
Expand Down
1 change: 1 addition & 0 deletions FakeXrmEasy.Shared/FakeXrmEasy.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="$(MSBuildThisFileDirectory)FakeMessageExecutors\UpdateRequestExecutor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FakeMessageExecutors\WhoAmIRequestExecutor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FakeMessageExecutors\WinOpportunityRequestExecutor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FiscalYearSettings.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IXrmFakedContext.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Metadata\DateTimeAttributeBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Metadata\MetadataGenerator.cs" />
Expand Down
25 changes: 25 additions & 0 deletions FakeXrmEasy.Shared/FiscalYearSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;

namespace FakeXrmEasy
{
[EntityLogicalName("organization")]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a mill for this @npavlov ! Curious to know where this EntityLogicalName was generated from... crmsvcutil or XrmToolkit or similar?

public class FiscalYearSettings
{
[AttributeLogicalName("fiscalcalendarstart")]
public DateTime StartDate { get; set; }

[AttributeLogicalName("fiscalperiodtype")]
public Template FiscalPeriodTemplate { get; set; }

public enum Template
{
Annually = 2000,
SemiAnnually = 2001,
Quarterly = 2002,
Monthly = 2003,
FourWeek = 2004
}
}
}
2 changes: 2 additions & 0 deletions FakeXrmEasy.Shared/XrmFakedContext.DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public partial class XrmFakedContext : IXrmContext
{
public TimeZoneInfo SystemTimeZone { get; set; }

public FiscalYearSettings FiscalYearSettings { get; set; }

public Dictionary<string, Dictionary<string, DateTimeAttributeBehavior>> DateBehaviour { get; set; }

private static Dictionary<string, Dictionary<string, DateTimeAttributeBehavior>> DefaultDateBehaviour()
Expand Down
13 changes: 11 additions & 2 deletions FakeXrmEasy.Shared/XrmFakedContext.Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,8 @@ protected static Expression TranslateConditionExpression(QueryExpression qe, Xrm
case ConditionOperator.LastWeek:
case ConditionOperator.ThisWeek:
case ConditionOperator.NextWeek:
operatorExpression = TranslateConditionExpressionBetweenDates(c, getNonBasicValueExpr, containsAttributeExpression);
case ConditionOperator.InFiscalYear:
operatorExpression = TranslateConditionExpressionBetweenDates(c, getNonBasicValueExpr, containsAttributeExpression, context);
break;

case ConditionOperator.Next7Days:
Expand Down Expand Up @@ -1576,7 +1577,7 @@ protected static Expression TranslateConditionExpressionLast(TypedConditionExpre
/// <summary>
/// Takes a condition expression which needs translating into a 'between two dates' expression and works out the relevant dates
/// </summary>
protected static Expression TranslateConditionExpressionBetweenDates(TypedConditionExpression tc, Expression getAttributeValueExpr, Expression containsAttributeExpr)
protected static Expression TranslateConditionExpressionBetweenDates(TypedConditionExpression tc, Expression getAttributeValueExpr, Expression containsAttributeExpr, XrmFakedContext context)
{
var c = tc.CondExpression;

Expand All @@ -1587,6 +1588,7 @@ protected static Expression TranslateConditionExpressionBetweenDates(TypedCondit
var thisYear = today.Year;
var thisMonth = today.Month;


switch (c.Operator)
{
case ConditionOperator.ThisYear: // From first day of this year to last day of this year
Expand Down Expand Up @@ -1628,6 +1630,13 @@ protected static Expression TranslateConditionExpressionBetweenDates(TypedCondit
fromDate = today.ToFirstDayOfDeltaWeek(1);
toDate = today.ToLastDayOfDeltaWeek(1).AddDays(1);
break;
case ConditionOperator.InFiscalYear:
var fiscalYear = (int)c.Values[0];
c.Values.Clear();
var fiscalYearDate = context.FiscalYearSettings?.StartDate ?? new DateTime(fiscalYear, 4, 1);
fromDate = fiscalYearDate;
toDate = fiscalYearDate.AddYears(1).AddDays(-1);
break;
}

c.Values.Add(fromDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ public void FetchXml_Operator_Last_Seven_Days_Translation()
</filter>
</entity>
</fetch>";

var query = XrmFakedContext.TranslateFetchXmlToQueryExpression(ctx, fetchXml);

Assert.True(query.Criteria != null);
Expand Down Expand Up @@ -1299,6 +1299,37 @@ public void FetchXml_Operator_ThisYear_Execution()
Assert.Equal(((DateTime)collection.Entities[2]["anniversary"]).Year, thisYear);
}

[Fact]
public void FetchXml_Operator_InFiscalYear_Execution()
{
var today = DateTime.Today;
var thisYear = today.Year;

var ctx = new XrmFakedContext();
ctx.FiscalYearSettings = new FiscalYearSettings() { StartDate = new DateTime(thisYear, 1, 2), FiscalPeriodTemplate = FiscalYearSettings.Template.Annually };
var fetchXml = $@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='contact'>
<attribute name='anniversary' />
<filter type='and'>
<condition attribute='anniversary' operator='in-fiscal-year' value='{thisYear}' />
</filter>
</entity>
</fetch>";

var ct1 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, 1, 2) }; // Second day of this year - should be returned
var ct2 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, 12, 31) }; // Last day of this year - should be returned
var ct3 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear + 1, 1, 2) }; // Second day of next year - should not be returned
ctx.Initialize(new[] { ct1, ct2, ct3 });
var service = ctx.GetOrganizationService();

var collection = service.RetrieveMultiple(new FetchExpression(fetchXml));

Assert.Equal(2, collection.Entities.Count);

Assert.Equal(((DateTime)collection.Entities[0]["anniversary"]).Year, thisYear);
Assert.Equal(((DateTime)collection.Entities[1]["anniversary"]).Year, thisYear);
}

[Fact]
public void FetchXml_Operator_ThisMonth_Execution()
{
Expand Down Expand Up @@ -1365,7 +1396,7 @@ public void FetchXml_Operator_LastMonth_Execution()
Assert.Equal(2, collection.Entities.Count);

Assert.Equal(((DateTime)collection.Entities[0]["anniversary"]).Month, lastMonth);
Assert.Equal(((DateTime)collection.Entities[1]["anniversary"]).Month, lastMonth);
Assert.Equal(((DateTime)collection.Entities[1]["anniversary"]).Month, lastMonth);
}

[Fact]
Expand All @@ -1383,7 +1414,7 @@ public void FetchXml_Operator_NextMonth_Execution()

var today = DateTime.Today;
var thisYear = today.Year;
var thisMonth = today.Month;
var thisMonth = today.Month;
var nextMonth = new DateTime(thisYear, thisMonth, 1).AddMonths(1).Month;
var ct1 = new Contact() { Id = Guid.NewGuid(), Anniversary = today }; // Today - Should not be returned
var ct2 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, thisMonth, 1) }; // First day of this month - should not be returned
Expand All @@ -1392,7 +1423,7 @@ public void FetchXml_Operator_NextMonth_Execution()
var ct5 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, thisMonth, 1).AddMonths(2).AddDays(-1) }; // Last day of next month - should be returned
var ct6 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, thisMonth, 1).AddDays(-1) }; // Last day of last month - should not be returned
var ct7 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, thisMonth, 1).AddMonths(-1) }; // First day of last month - should not be returned

ctx.Initialize(new[] { ct1, ct2, ct3, ct4, ct5, ct6, ct7 });
var service = ctx.GetFakedOrganizationService();

Expand Down Expand Up @@ -1579,9 +1610,9 @@ public void FetchXml_Operator_Next_X_Weeks_Execution()
</fetch>";

var date = DateTime.Now;
var ct1 = new Contact() { Id = Guid.NewGuid(), Anniversary = date.AddDays(7*2) }; //Should be returned
var ct2 = new Contact() { Id = Guid.NewGuid(), Anniversary = date.AddDays(7*4) }; //Shouldnt
ctx.Initialize(new[] { ct1, ct2});
var ct1 = new Contact() { Id = Guid.NewGuid(), Anniversary = date.AddDays(7 * 2) }; //Should be returned
var ct2 = new Contact() { Id = Guid.NewGuid(), Anniversary = date.AddDays(7 * 4) }; //Shouldnt
ctx.Initialize(new[] { ct1, ct2 });
var service = ctx.GetOrganizationService();

var collection = service.RetrieveMultiple(new FetchExpression(fetchXml));
Expand Down Expand Up @@ -1788,7 +1819,7 @@ public void FetchXml_Operator_Next_Week_Execution()
Assert.Equal(retrievedUser, ct1.Id);
}


#if FAKE_XRM_EASY_9
[Fact]
public void FetchXml_Operator_ContainValues_Translation()
Expand Down