Initialize the API and add managed app banners.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
export type CheckRassInput = {
|
||||
amount: number;
|
||||
dueDate: Date | string;
|
||||
};
|
||||
|
||||
export type CheckRassResult = {
|
||||
checkCount: number;
|
||||
totalAmount: number;
|
||||
nearestDueDate: string | null;
|
||||
farthestDueDate: string | null;
|
||||
rassDays: number;
|
||||
rassDate: string | null;
|
||||
};
|
||||
|
||||
import {
|
||||
addDays,
|
||||
calendarDayOf,
|
||||
diffInDays,
|
||||
tehranCalendarDay,
|
||||
} from './tehran-time';
|
||||
|
||||
export function calculateCheckRass(
|
||||
checks: CheckRassInput[],
|
||||
baseDate: Date = new Date(),
|
||||
): CheckRassResult {
|
||||
if (checks.length === 0) {
|
||||
return {
|
||||
checkCount: 0,
|
||||
totalAmount: 0,
|
||||
nearestDueDate: null,
|
||||
farthestDueDate: null,
|
||||
rassDays: 0,
|
||||
rassDate: null,
|
||||
};
|
||||
}
|
||||
|
||||
const base = tehranCalendarDay(baseDate);
|
||||
let totalAmount = 0;
|
||||
let weightedDays = 0;
|
||||
const dueDates: Date[] = [];
|
||||
|
||||
for (const check of checks) {
|
||||
const due = calendarDayOf(check.dueDate);
|
||||
dueDates.push(due);
|
||||
const days = Math.max(0, diffInDays(base, due));
|
||||
totalAmount += check.amount;
|
||||
weightedDays += check.amount * days;
|
||||
}
|
||||
|
||||
const rassDays = totalAmount > 0 ? Math.round(weightedDays / totalAmount) : 0;
|
||||
const rassDate = addDays(base, rassDays);
|
||||
dueDates.sort((a, b) => a.getTime() - b.getTime());
|
||||
|
||||
return {
|
||||
checkCount: checks.length,
|
||||
totalAmount,
|
||||
nearestDueDate: dueDates[0]?.toISOString() ?? null,
|
||||
farthestDueDate: dueDates[dueDates.length - 1]?.toISOString() ?? null,
|
||||
rassDays,
|
||||
rassDate: rassDate.toISOString(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user