Skip to content
Open
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
106 changes: 78 additions & 28 deletions src/backend/src/services/finance.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,33 +611,77 @@ export default class FinanceServices {

if (!division) throw new NotFoundException('Team Type', teamTypeId);

const results: ReimbursementRequestData = {
totalBudget: 0,
pendingFinance: 0,
pendingLeadership: 0,
submittedToSabo: 0,
reimbursed: 0,
available: 0
};

const projectsById = new Map<string, { budget: number }>();
for (const team of division.teams) {
const data: ReimbursementRequestData = await this.getReimbursementRequestTeamData(
organization,
team.teamId,
startDate,
endDate,
carNumber
);

results.totalBudget += data.totalBudget;
results.pendingFinance += data.pendingFinance;
results.pendingLeadership += data.pendingLeadership;
results.submittedToSabo += data.submittedToSabo;
results.reimbursed += data.reimbursed;
results.available += data.available;
for (const project of team.projects) {
if (!projectsById.has(project.projectId)) {
projectsById.set(project.projectId, { budget: project.budget });
}
}
}
const totalBudget = [...projectsById.values()].reduce((acc, p) => acc + p.budget, 0);
const divisionProjectIds = [...projectsById.keys()];

const reimbursementRequests = await prisma.reimbursement_Request.findMany({
where: {
reimbursementProducts: {
some: {
reimbursementProductReason: {
wbsElement: {
project: {
projectId: { in: divisionProjectIds }
}
}
}
}
},
reimbursementStatuses: {
none: {
type: Reimbursement_Status_Type.DENIED
}
},
...getReimbursementRequestWhereInput(startDate, endDate, carNumber)
},
select: {
reimbursementStatuses: true,
totalCost: true,
reimbursementProducts: {
where: {
dateDeleted: null,
reimbursementProductReason: {
wbsElement: {
project: {
projectId: { in: divisionProjectIds }
}
}
}
},
select: {
cost: true
}
}
}
});

const { pendingFinance, pendingLeadership, submittedToSabo, reimbursed } = computeRRTotals(reimbursementRequests);

const totalBalance =
reimbursementRequests.reduce((acc, curr) => {
const teamProductsCost = curr.reimbursementProducts.reduce((prodAcc, prod) => prodAcc + prod.cost, 0);
return acc + teamProductsCost;
}, 0) / 100;

return results;
const available = totalBudget - totalBalance;

const data: ReimbursementRequestData = {
totalBudget,
pendingFinance,
pendingLeadership,
submittedToSabo,
reimbursed,
available
};
return data;
}

static async getSpendingBarTeamData(
Expand Down Expand Up @@ -817,10 +861,16 @@ export default class FinanceServices {
}
});

const allTotalBudget = teams.reduce((teamAcc, team) => {
const teamBudget = team.projects.reduce((projAcc, project) => projAcc + project.budget, 0);
return teamAcc + teamBudget;
}, 0);
// project budgets no longer double counted if in multiple teams
const projectsById = new Map<string, { budget: number }>();
for (const team of teams) {
for (const project of team.projects) {
if (!projectsById.has(project.projectId)) {
projectsById.set(project.projectId, { budget: project.budget });
}
}
}
const allTotalBudget = [...projectsById.values()].reduce((acc, p) => acc + p.budget, 0);

const cashTotalBudget =
cashReimbursementRequests.reduce((reqAcc, rr) => {
Expand Down