I have successfully calculated the sum of costs between each other, but now I am facing a challenge in summing up the total budget for the entire year. I have attempted to achieve this, but unfortunately, I am encountering an issue as it is showing me 'undefined.'
The 'Costs' interface consists of a variable created with a type date, a variable 'costs' with a type number, and the ID of the actual costs being sent to an array called 'actualcostIds.' How can I calculate the total sum from January 1st to December 31st? Below you will find my code for calculating the sum for each cost and the total summed costs.
export interface TotalCosten {
planned: number;
actual: number;
}
export function emptyCosts(): Costs {
return {
id: '',
created: new Date(),
type: 0,
costs: 0,
reference: '',
comment: ''
};
}
export function getTotalYearCosts(valueItem: ValueItem, allCosts: Map<string, Costs>): TotalCosten {
const totalYearCosts = { planned: 0, actual: 0 };
const Q1 = new Date(2018, 11, 31);
const Q2 = new Date(2018, 0, 1);
totalYearCosts.actual = valueItem.actualCostIds
.map(costId => allCosts.get(costId, emptyCosts()).costs)
.filter() //Here to make a filter for costs that are in the date range
.reduce((reduction, costs) => reduction + costs, 0);
return totalYearCosts;
}
export interface ValueItem {
plannedCostIds: string[];
actualCostIds: string[];
}