fix: update balance changes usage, not included
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user