Skip to content

Commit 732540d

Browse files
authored
Merge pull request #3952 from Northeastern-Electric-Racing/#3951-finance-budget-double-counting
#3951 duplicate project budget fix
2 parents ef3281e + 2813eb3 commit 732540d

1 file changed

Lines changed: 80 additions & 28 deletions

File tree

src/backend/src/services/finance.services.ts

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -611,33 +611,79 @@ export default class FinanceServices {
611611

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

614-
const results: ReimbursementRequestData = {
615-
totalBudget: 0,
616-
pendingFinance: 0,
617-
pendingLeadership: 0,
618-
submittedToSabo: 0,
619-
reimbursed: 0,
620-
available: 0
621-
};
622-
614+
const projectsById = new Map<string, { budget: number }>();
623615
for (const team of division.teams) {
624-
const data: ReimbursementRequestData = await this.getReimbursementRequestTeamData(
625-
organization,
626-
team.teamId,
627-
startDate,
628-
endDate,
629-
carNumber
630-
);
631-
632-
results.totalBudget += data.totalBudget;
633-
results.pendingFinance += data.pendingFinance;
634-
results.pendingLeadership += data.pendingLeadership;
635-
results.submittedToSabo += data.submittedToSabo;
636-
results.reimbursed += data.reimbursed;
637-
results.available += data.available;
616+
for (const project of team.projects) {
617+
if (!projectsById.has(project.projectId)) {
618+
projectsById.set(project.projectId, { budget: project.budget });
619+
}
620+
}
638621
}
622+
const totalBudget = [...projectsById.values()].reduce((acc, p) => acc + p.budget, 0);
623+
const divisionProjectIds = [...projectsById.keys()];
624+
625+
const reimbursementRequests = await prisma.reimbursement_Request.findMany({
626+
where: {
627+
dateDeleted: null,
628+
reimbursementProducts: {
629+
some: {
630+
dateDeleted: null,
631+
reimbursementProductReason: {
632+
wbsElement: {
633+
project: {
634+
projectId: { in: divisionProjectIds }
635+
}
636+
}
637+
}
638+
}
639+
},
640+
reimbursementStatuses: {
641+
none: {
642+
type: Reimbursement_Status_Type.DENIED
643+
}
644+
},
645+
...getReimbursementRequestWhereInput(startDate, endDate, carNumber)
646+
},
647+
select: {
648+
reimbursementStatuses: true,
649+
totalCost: true,
650+
reimbursementProducts: {
651+
where: {
652+
dateDeleted: null,
653+
reimbursementProductReason: {
654+
wbsElement: {
655+
project: {
656+
projectId: { in: divisionProjectIds }
657+
}
658+
}
659+
}
660+
},
661+
select: {
662+
cost: true
663+
}
664+
}
665+
}
666+
});
667+
668+
const { pendingFinance, pendingLeadership, submittedToSabo, reimbursed } = computeRRTotals(reimbursementRequests);
639669

640-
return results;
670+
const totalBalance =
671+
reimbursementRequests.reduce((acc, curr) => {
672+
const teamProductsCost = curr.reimbursementProducts.reduce((prodAcc, prod) => prodAcc + prod.cost, 0);
673+
return acc + teamProductsCost;
674+
}, 0) / 100;
675+
676+
const available = totalBudget - totalBalance;
677+
678+
const data: ReimbursementRequestData = {
679+
totalBudget,
680+
pendingFinance,
681+
pendingLeadership,
682+
submittedToSabo,
683+
reimbursed,
684+
available
685+
};
686+
return data;
641687
}
642688

643689
static async getSpendingBarTeamData(
@@ -817,10 +863,16 @@ export default class FinanceServices {
817863
}
818864
});
819865

820-
const allTotalBudget = teams.reduce((teamAcc, team) => {
821-
const teamBudget = team.projects.reduce((projAcc, project) => projAcc + project.budget, 0);
822-
return teamAcc + teamBudget;
823-
}, 0);
866+
// project budgets no longer double counted if in multiple teams
867+
const projectsById = new Map<string, { budget: number }>();
868+
for (const team of teams) {
869+
for (const project of team.projects) {
870+
if (!projectsById.has(project.projectId)) {
871+
projectsById.set(project.projectId, { budget: project.budget });
872+
}
873+
}
874+
}
875+
const allTotalBudget = [...projectsById.values()].reduce((acc, p) => acc + p.budget, 0);
824876

825877
const cashTotalBudget =
826878
cashReimbursementRequests.reduce((reqAcc, rr) => {

0 commit comments

Comments
 (0)