fix: update balance changes usage, not included

This commit is contained in:
John Yeo
2026-02-17 15:46:09 +00:00
parent 547c545ee2
commit fa16f17d29
58 changed files with 3803 additions and 6722 deletions

View File

@@ -1,22 +1,32 @@
import { nullish } from "@utils/utils";
import { Decimal } from "decimal.js";
import type { FullCusEntWithFullCusProduct } from "../../../models/cusProductModels/cusEntModels/cusEntWithProduct";
import { isEntityScopedCusEnt } from "../classifyCusEntUtils";
export const cusEntToInvoiceOverage = ({
cusEnt,
entityId,
}: {
cusEnt: FullCusEntWithFullCusProduct;
entityId?: string;
}) => {
// 1. If entity scoped
if (isEntityScopedCusEnt(cusEnt)) {
let totalOverage = new Decimal(0);
for (const [_, entity] of Object.entries(cusEnt.entities || {})) {
if (nullish(entityId)) {
for (const [_, entity] of Object.entries(cusEnt.entities || {})) {
const overage = Decimal.max(0, new Decimal(-entity.balance));
totalOverage = totalOverage.add(overage);
}
return totalOverage.toNumber(); // this is NOT to be used for any amount calculations OR billing calculations. ONLY display purposes (invoice descriptions)
} else {
const entity = cusEnt.entities?.[entityId];
if (nullish(entity)) return 0;
const overage = Decimal.max(0, new Decimal(-entity.balance));
totalOverage = totalOverage.add(overage);
return overage.toNumber();
}
return totalOverage.toNumber(); // this is NOT to be used for any amount calculations OR billing calculations. ONLY display purposes (invoice descriptions)
}
// 2. If not entity scoped