Skip to content
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
14 changes: 14 additions & 0 deletions moxygen/main/default/classes/gmt/GMT.cls
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ public inherited sharing class GMT {
public static Datetime startOfThisCalendarQuarter() {
return Datetime.newInstanceGmt(Gmt.today().year(), Math.mod(Gmt.today().month() - (Gmt.today().month() - 1), 3), 1);
}

/**
* @description Return the start of the current fiscal year
* @return `Date`
*/
public static Date startOfThisFiscalYear() {
Organization org = OrganizationSingleton.getInstance();
Integer orgFiscalMonth = org.FiscalYearStartMonth;
Date possibleStartOfThisFiscalYear = Date.newInstance(Gmt.today().year(), orgFiscalMonth, 1);
if(possibleStartOfThisFiscalYear > Gmt.today()) {
return possibleStartOfThisFiscalYear.addYears(-1);
}
return possibleStartOfThisFiscalYear;
}
}
17 changes: 17 additions & 0 deletions moxygen/main/default/classes/gmt/GmtTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,21 @@ private class GmtTest {
}
Assert.isTrue(foundDate, 'Start of this calendar quarter is not a valid quarter date');
}

@IsTest
static void ensureStartOfThisFiscalYearReturnsCorrectValue() {
Test.startTest();
Date startOfThisFiscalYear = Gmt.startOfThisFiscalYear();
Test.stopTest();

Organization org = OrganizationSingleton.getInstance();
Integer orgFiscalMonth = org.FiscalYearStartMonth;
Date possibleStartOfThisFiscalYear = Date.newInstance(Gmt.today().year(), orgFiscalMonth, 1);
if(possibleStartOfThisFiscalYear > Gmt.today()) {
Date expectedStartOfThisFiscalYear = possibleStartOfThisFiscalYear.addYears(-1);
Assert.areEqual(expectedStartOfThisFiscalYear, startOfThisFiscalYear, 'Start of this fiscal year is not correct');
} else {
Assert.areEqual(possibleStartOfThisFiscalYear, startOfThisFiscalYear, 'Start of this fiscal year is not correct');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3959,4 +3959,26 @@ private class MockDatabaseTest {

Assert.areEqual(12, opportunities.size(), 'Incorrect number of opportunities');
}

@isTest
static void ensureThisFiscalYearWorksInWhereClause() {

List<Opportunity> oppList = new List<Opportunity>();
Date startOfYear = Date.newInstance(Gmt.today().year(), 1, 1);
for(Integer i = 0; i < 36; i++) {
oppList.add(new Opportunity(
Name = 'Opp' + i,
CloseDate = startOfYear.addMonths(i)
));
}


MockDatabase.doInsert(oppList, true);

Test.startTest();
List<Opportunity> opportunities = MockDatabase.query('SELECT Id FROM Opportunity WHERE CloseDate = THIS_FISCAL_YEAR');
Test.stopTest();

Assert.areEqual(12, opportunities.size(), 'Incorrect number of opportunities');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public with sharing class DateLiteralComparableFactory {
Token.LAST_QUARTER_LITERAL => LastQuarterComparable.class,
Token.NEXT_QUARTER_LITERAL => NextQuarterComparable.class,
Token.NEXT_N_QUARTERS_LITERAL => NextNQuartersComparable.class,
Token.THIS_YEAR_LITERAL => ThisYearComparable.class
Token.THIS_YEAR_LITERAL => ThisYearComparable.class,
Token.THIS_FISCAL_YEAR_LITERAL => ThisFiscalYearComparable.class
};

@TestVisible
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @description Comparable for the THIS_FISCAL_YEAR token
* @author Zackary Frazier
* @since 1/7/2025
*/
public with sharing class ThisFiscalYearComparable extends DateLiteralComparable {

/**
* @description Returns if the field value is equal to the current fiscal year
* @param fieldValue `Datetime`
* @return `Boolean`
*/
public override Boolean isEqual(Datetime fieldValue) {
Date orgFiscalYear = Gmt.startOfThisFiscalYear();
return orgFiscalYear <= fieldValue && fieldValue < orgFiscalYear.addYears(1);
}

/**
* @description Returns if the field value is greater than the current fiscal year
* @param fieldValue `Datetime`
* @return `Boolean`
*/
public override Boolean isGreaterThan(Datetime fieldValue) {
Date orgFiscalYear = Gmt.startOfThisFiscalYear();
return fieldValue >= orgFiscalYear.addYears(1);
}

/**
* @description Returns if the field value is less than the current fiscal year
* @param fieldValue `Datetime`
* @return `Boolean`
*/
public override Boolean isLessThan(Datetime fieldValue) {
Date orgFiscalYear = Gmt.startOfThisFiscalYear();
return fieldValue < orgFiscalYear;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
@isTest
private class ThisFiscalYearComparableTest {

@isTest
static void ensureIsEqualWorks() {
Date startOfThisFiscalYear = Gmt.startOfThisFiscalYear();
Date endOfThisFiscalYear = startOfThisFiscalYear.addYears(1).addDays(-1);
Date startOfNextFiscalYear = startOfThisFiscalYear.addYears(1);

ThisFiscalYearComparable thisFiscalYearComparable = new ThisFiscalYearComparable();
Assert.isTrue(thisFiscalYearComparable.isEqual(startOfThisFiscalYear), 'Start of this fiscal year should be equal');
Assert.isTrue(thisFiscalYearComparable.isEqual(endOfThisFiscalYear), 'End of this fiscal year should be equal');
Assert.isFalse(thisFiscalYearComparable.isEqual(startOfNextFiscalYear), 'Start of next fiscal year should not be equal');
}

@IsTest
static void ensureIsLessThanWorks() {
Date startOfThisFiscalYear = Gmt.startOfThisFiscalYear();
Date startOfNextFiscalYear = startOfThisFiscalYear.addYears(1);
Date dateBeforeThisFiscalYear = startOfThisFiscalYear.addDays(-1);

ThisFiscalYearComparable thisFiscalYearComparable = new ThisFiscalYearComparable();
Assert.isTrue(thisFiscalYearComparable.isLessThan(dateBeforeThisFiscalYear), 'End of this fiscal year should be greater');
Assert.isFalse(thisFiscalYearComparable.isLessThan(startOfThisFiscalYear), 'Start of this fiscal year should not be less');
Assert.isFalse(thisFiscalYearComparable.isLessThan(startOfNextFiscalYear), 'Start of next fiscal year should not be less');
}

@IsTest
static void ensureIsGreaterThanWorks() {
Date startOfThisFiscalYear = Gmt.startOfThisFiscalYear();
Date endOfThisFiscalYear = startOfThisFiscalYear.addYears(1).addDays(-1);
Date startOfNextFiscalYear = startOfThisFiscalYear.addYears(1);

ThisFiscalYearComparable thisFiscalYearComparable = new ThisFiscalYearComparable();
Assert.isFalse(thisFiscalYearComparable.isGreaterThan(startOfThisFiscalYear), 'Start of this fiscal year should be less');
Assert.isFalse(thisFiscalYearComparable.isGreaterThan(endOfThisFiscalYear), 'End of this fiscal year should not be greater');
Assert.isTrue(thisFiscalYearComparable.isGreaterThan(startOfNextFiscalYear), 'Start of next fiscal year should not be greater');
}

@IsTest
static void ensureIsLessThanOrEqualWorks() {
Date startOfThisFiscalYear = Gmt.startOfThisFiscalYear();
Date endOfThisFiscalYear = startOfThisFiscalYear.addYears(1).addDays(-1);
Date startOfNextFiscalYear = startOfThisFiscalYear.addYears(1);

ThisFiscalYearComparable thisFiscalYearComparable = new ThisFiscalYearComparable();
Assert.isTrue(thisFiscalYearComparable.isLessThanOrEqual(endOfThisFiscalYear), 'End of this fiscal year should be greater');
Assert.isTrue(thisFiscalYearComparable.isLessThanOrEqual(startOfThisFiscalYear), 'Start of this fiscal year should be less');
Assert.isFalse(thisFiscalYearComparable.isLessThanOrEqual(startOfNextFiscalYear), 'Start of next fiscal year should not be less');
}

@IsTest
static void ensureIsGreaterThanOrEqualWorks() {
Date startOfThisFiscalYear = Gmt.startOfThisFiscalYear();
Date endOfThisFiscalYear = startOfThisFiscalYear.addYears(1).addDays(-1);
Date startOfNextFiscalYear = startOfThisFiscalYear.addYears(1);
Date dayBeforeStartOfThisFiscalYear = startOfThisFiscalYear.addDays(-1);

ThisFiscalYearComparable thisFiscalYearComparable = new ThisFiscalYearComparable();
Assert.isTrue(thisFiscalYearComparable.isGreaterThanOrEqual(startOfThisFiscalYear), 'Start of this fiscal year should be less');
Assert.isTrue(thisFiscalYearComparable.isGreaterThanOrEqual(endOfThisFiscalYear), 'End of this fiscal year should be greater');
Assert.isTrue(thisFiscalYearComparable.isGreaterThanOrEqual(startOfNextFiscalYear), 'Start of next fiscal year should not be greater');
Assert.isFalse(thisFiscalYearComparable.isGreaterThanOrEqual(dayBeforeStartOfThisFiscalYear), 'Day before start of this fiscal year should not be greater');
}

@IsTest
static void ensureIsNotEqualWorks() {
Date startOfThisFiscalYear = Gmt.startOfThisFiscalYear();
Date endOfThisFiscalYear = startOfThisFiscalYear.addYears(1).addDays(-1);
Date startOfNextFiscalYear = startOfThisFiscalYear.addYears(1);

ThisFiscalYearComparable thisFiscalYearComparable = new ThisFiscalYearComparable();
Assert.isFalse(thisFiscalYearComparable.isNotEqual(startOfThisFiscalYear), 'Start of this fiscal year should not be not equal');
Assert.isFalse(thisFiscalYearComparable.isNotEqual(endOfThisFiscalYear), 'End of this fiscal year should not be not equal');
Assert.isTrue(thisFiscalYearComparable.isNotEqual(startOfNextFiscalYear), 'Start of next fiscal year should be not equal');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,17 @@ private class ParserTest {
}
Test.stopTest();
}

@isTest
static void ensureParserCanParseThisFiscalYear() {
Parser p = new Parser();
String soqlQuery = 'SELECT Id FROM Opportunity WHERE CreatedDate = THIS_FISCAL_YEAR';
Test.startTest();
try {
p.parse(soqlQuery);
} catch(Exception e) {
Assert.fail('Expected no exception but got ' + e.getMessage() + ' for ' + soqlQuery);
}
Test.stopTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public with sharing class PrimitiveParserFactory {
Token.LAST_QUARTER_LITERAL => LastQuarterParser.class,
Token.NEXT_QUARTER_LITERAL => NextQuarterParser.class,
Token.NEXT_N_QUARTERS_LITERAL => NextNQuartersParser.class,
Token.THIS_FISCAL_YEAR_LITERAL => ThisFiscalYearParser.class,
Token.THIS_YEAR_LITERAL => ThisYearParser.class,
Token.XTRUE => BooleanParser.class,
Token.XFALSE => BooleanParser.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @description Parser for the FISCAL_YEAR token
* @author Zackary Frazier
* @since 1/6/2025
*/
public with sharing class ThisFiscalYearParser extends DateLiteralParser {
/**
* @description Parses the query and returns an `Intermediary` object
* @param query `String`
* @return `Intermediary`
*/
public override Intermediary parse(String query) {
return parse(query, Token.THIS_FISCAL_YEAR_LITERAL);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@isTest
private class ThisFiscalYearParserTest {

@isTest
static void ensureThisFiscalYearIsParseable() {
DateLiteralParser parser = new ThisFiscalYearParser();
Intermediary intermediary = parser.parse('THIS_FISCAL_YEAR');
Assert.areEqual('', intermediary.subquery, 'Subquery should be empty');
Assert.areEqual(Token.THIS_FISCAL_YEAR_LITERAL, intermediary.head.id, 'id should be THIS_FISCAL_YEAR');
Assert.areEqual(NodeType.DATE_LITERAL, intermediary.head.nodeType, 'value should be THIS_FISCAL_YEAR');
Assert.isNull(intermediary.head.left, 'left should be null');
Assert.isNull(intermediary.head.right, 'right should be null');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,5 @@ public inherited sharing class Token {
public final static String NEXT_QUARTER_LITERAL = 'next_quarter';
public final static String NEXT_N_QUARTERS_LITERAL = 'next_n_quarters';
public final static String THIS_YEAR_LITERAL = 'this_year';
public final static String THIS_FISCAL_YEAR_LITERAL = 'this_fiscal_year';
}
31 changes: 31 additions & 0 deletions moxygen/main/default/classes/singletons/OrganizationSingleton.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @description Singleton for the Organization object
* @author Zackary Frazier
* @since 1/7/2025
*/
public with sharing class OrganizationSingleton {

static Organization org;

/**
* @description Gets the Organization object
* @return `Organization`
* @exception ValidationException
*/
public static Organization getInstance() {
if(org != null) {
return org;
}

if (Schema.sObjectType.Organization.isAccessible()) {
org = [
SELECT FiscalYearStartMonth
FROM Organization
LIMIT 1
];
} else {
throw new ValidationException('You do not have permission to access the Organization object.');
}
return org;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@isTest
private class OrganizationSingletonTest {

@isTest
static void ensureGetInstanceReturnsOurOrganization() {
Test.startTest();
Organization org = OrganizationSingleton.getInstance();
Test.stopTest();

Assert.isNotNull(org, 'Organization should not be null');
}

@IsTest
static void ensureOurOrganizationIsPulledFromCacheWhenRetrievedTwice() {
Test.startTest();
Organization org1 = OrganizationSingleton.getInstance();
Organization org2 = OrganizationSingleton.getInstance();

Integer numberOfSoqlQueries = Limits.getQueries();
Test.stopTest();

Assert.areEqual(org1, org2, 'Organization should be the same instance');

Assert.areEqual(1, numberOfSoqlQueries, 'Should only have queried for Organization once');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading