add billing cycle date range filter

This commit is contained in:
MaysWind
2024-12-16 23:44:20 +08:00
parent 8fdbb39ee4
commit 647cd3c33f
12 changed files with 226 additions and 23 deletions
+65
View File
@@ -136,6 +136,10 @@ export function getCurrentYear() {
return moment().year();
}
export function getCurrentDay() {
return moment().date();
}
export function parseDateFromUnixTime(unixTime, utcOffset, currentUtcOffset) {
if (isNumber(utcOffset)) {
if (!isNumber(currentUtcOffset)) {
@@ -270,6 +274,14 @@ export function getThisMonthLastUnixTime() {
return moment.unix(getThisMonthFirstUnixTime()).add(1, 'months').subtract(1, 'seconds').unix();
}
export function getThisMonthSpecifiedDayFirstUnixTime(date) {
return moment().set({ date: date, hour: 0, minute: 0, second: 0, millisecond: 0 }).unix();
}
export function getThisMonthSpecifiedDayLastUnixTime(date) {
return moment.unix(getThisMonthSpecifiedDayFirstUnixTime(date)).add(1, 'days').subtract(1, 'seconds').unix();
}
export function getThisYearFirstUnixTime() {
const today = moment.unix(getTodayFirstUnixTime());
return today.subtract(today.dayOfYear() - 1, 'days').unix();
@@ -476,6 +488,16 @@ export function getShiftedDateRangeAndDateType(minTime, maxTime, scale, firstDay
};
}
export function getShiftedDateRangeAndDateTypeForBillingCycle(dateType, scale, firstDayOfWeek, statementDate) {
if (dateType === dateTimeConstants.allDateRanges.PreviousBillingCycle.type && scale === 1) {
return getDateRangeByBillingCycleDateType(dateTimeConstants.allDateRanges.CurrentBillingCycle.type, firstDayOfWeek, statementDate);
} else if (dateType === dateTimeConstants.allDateRanges.CurrentBillingCycle.type && scale === -1) {
return getDateRangeByBillingCycleDateType(dateTimeConstants.allDateRanges.PreviousBillingCycle.type, firstDayOfWeek, statementDate);
}
return null;
}
export function getDateTypeByDateRange(minTime, maxTime, firstDayOfWeek, scene) {
let newDateType = dateTimeConstants.allDateRanges.Custom.type;
@@ -567,6 +589,49 @@ export function getDateRangeByDateType(dateType, firstDayOfWeek) {
};
}
export function getDateRangeByBillingCycleDateType(dateType, firstDayOfWeek, statementDate) {
let maxTime = 0;
let minTime = 0;
if (dateType === dateTimeConstants.allDateRanges.PreviousBillingCycle.type || dateType === dateTimeConstants.allDateRanges.CurrentBillingCycle.type) { // Previous Billing Cycle | Current Billing Cycle
if (statementDate) {
if (getCurrentDay() <= statementDate) {
maxTime = getThisMonthSpecifiedDayLastUnixTime(statementDate);
minTime = getUnixTimeBeforeUnixTime(getUnixTimeAfterUnixTime(getThisMonthSpecifiedDayFirstUnixTime(statementDate), 1, 'days'), 1, 'months');
} else {
maxTime = getUnixTimeAfterUnixTime(getThisMonthSpecifiedDayLastUnixTime(statementDate), 1, 'months');
minTime = getUnixTimeAfterUnixTime(getThisMonthSpecifiedDayFirstUnixTime(statementDate), 1, 'days');
}
if (dateType === dateTimeConstants.allDateRanges.PreviousBillingCycle.type) {
maxTime = getUnixTimeBeforeUnixTime(maxTime, 1, 'months');
minTime = getUnixTimeBeforeUnixTime(minTime, 1, 'months');
}
} else {
let fallbackDateRange = null;
if (dateType === dateTimeConstants.allDateRanges.CurrentBillingCycle.type) { // same as This Month
fallbackDateRange = getDateRangeByDateType(dateTimeConstants.allDateRanges.ThisMonth.type, firstDayOfWeek);
} else if (dateType === dateTimeConstants.allDateRanges.PreviousBillingCycle.type) { // same as Last Month
fallbackDateRange = getDateRangeByDateType(dateTimeConstants.allDateRanges.LastMonth.type, firstDayOfWeek);
}
if (fallbackDateRange) {
maxTime = fallbackDateRange.maxTime;
minTime = fallbackDateRange.minTime;
}
}
} else {
return null;
}
return {
dateType: dateType,
maxTime: maxTime,
minTime: minTime
};
}
export function getRecentMonthDateRanges(monthCount) {
const recentDateRanges = [];
const thisMonthFirstUnixTime = getThisMonthFirstUnixTime();
+14 -2
View File
@@ -644,7 +644,7 @@ function getAllWeekDays(firstDayOfWeek, translateFn) {
return allWeekDays;
}
function getAllDateRanges(scene, includeCustom, translateFn) {
function getAllDateRanges(scene, includeCustom, includeBillingCycle, translateFn) {
const allDateRanges = [];
for (let dateRangeField in datetimeConstants.allDateRanges) {
@@ -658,6 +658,18 @@ function getAllDateRanges(scene, includeCustom, translateFn) {
continue;
}
if (dateRangeType.isBillingCycle) {
if (includeBillingCycle) {
allDateRanges.push({
type: dateRangeType.type,
displayName: translateFn(dateRangeType.name),
isBillingCycle: dateRangeType.isBillingCycle
});
}
continue;
}
if (includeCustom || dateRangeType.type !== datetimeConstants.allDateRanges.Custom.type) {
allDateRanges.push({
type: dateRangeType.type,
@@ -1746,7 +1758,7 @@ export function i18nFunctions(i18nGlobal) {
getTimezoneDifferenceDisplayText: (utcOffset) => getTimezoneDifferenceDisplayText(utcOffset, i18nGlobal.t),
getAllCurrencies: () => getAllCurrencies(i18nGlobal.t),
getAllWeekDays: (firstDayOfWeek) => getAllWeekDays(firstDayOfWeek, i18nGlobal.t),
getAllDateRanges: (scene, includeCustom) => getAllDateRanges(scene, includeCustom, i18nGlobal.t),
getAllDateRanges: (scene, includeCustom, includeBillingCycle) => getAllDateRanges(scene, includeCustom, includeBillingCycle, i18nGlobal.t),
getAllRecentMonthDateRanges: (userStore, includeAll, includeCustom) => getAllRecentMonthDateRanges(userStore, includeAll, includeCustom, i18nGlobal.t),
getDateRangeDisplayName: (userStore, dateType, startTime, endTime) => getDateRangeDisplayName(userStore, dateType, startTime, endTime, i18nGlobal.t),
getAllTimezoneTypesUsedForStatistics: (currentTimezone) => getAllTimezoneTypesUsedForStatistics(currentTimezone, i18nGlobal.t),