- Use Dinero in place of straight math in production board
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -32,29 +32,30 @@ const ProductionStatistics = ({ data, cardSettings, reducerData }) => {
|
|||||||
return items.reduce((acc, item) => acc + (item[key]?.aggregate?.sum?.[subKey] || 0), 0);
|
return items.reduce((acc, item) => acc + (item[key]?.aggregate?.sum?.[subKey] || 0), 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
const calculateTotalAmount = (items, key) => {
|
const sumDineroAmounts = (items, key, getAmountFn) => {
|
||||||
return items.reduce(
|
return items.reduce(
|
||||||
(acc, item) => {
|
(acc, item) => {
|
||||||
const amountInCents = item[key]?.totals?.subtotal?.amount || 0;
|
const amount = getAmountFn(item, key);
|
||||||
const dineroAmount = Dinero({ amount: amountInCents });
|
const dineroAmount = Dinero(amount ?? 0);
|
||||||
return acc.add(dineroAmount);
|
return acc.add(dineroAmount);
|
||||||
},
|
},
|
||||||
Dinero({ amount: 0 })
|
Dinero({ amount: 0 })
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const calculateTotalAmount = (items, key) => {
|
||||||
|
return items.reduce((acc, item) => acc.add(Dinero(item[key]?.totals?.subtotal ?? Dinero())), Dinero({ amount: 0 }));
|
||||||
|
};
|
||||||
|
|
||||||
const calculateReducerTotalAmount = (lanes, key) => {
|
const calculateReducerTotalAmount = (lanes, key) => {
|
||||||
return lanes.reduce(
|
return lanes.reduce(
|
||||||
(acc, lane) => {
|
(acc, lane) => {
|
||||||
const laneTotal = lane.cards.reduce(
|
return acc.add(
|
||||||
(laneAcc, card) => {
|
lane.cards.reduce(
|
||||||
const amountInCents = card.metadata[key]?.totals?.subtotal?.amount || 0;
|
(laneAcc, card) => laneAcc.add(Dinero(card.metadata[key]?.totals?.subtotal ?? Dinero())),
|
||||||
const dineroAmount = Dinero({ amount: amountInCents });
|
Dinero({ amount: 0 })
|
||||||
return laneAcc.add(dineroAmount);
|
)
|
||||||
},
|
|
||||||
Dinero({ amount: 0 })
|
|
||||||
);
|
);
|
||||||
return acc.add(laneTotal);
|
|
||||||
},
|
},
|
||||||
Dinero({ amount: 0 })
|
Dinero({ amount: 0 })
|
||||||
);
|
);
|
||||||
@@ -104,13 +105,13 @@ const ProductionStatistics = ({ data, cardSettings, reducerData }) => {
|
|||||||
const totalAmountInProduction = useMemo(() => {
|
const totalAmountInProduction = useMemo(() => {
|
||||||
if (!cardSettings.totalAmountInProduction) return null;
|
if (!cardSettings.totalAmountInProduction) return null;
|
||||||
const total = calculateTotalAmount(data, "job_totals");
|
const total = calculateTotalAmount(data, "job_totals");
|
||||||
return total.toFormat("$0,0.00"); // Formatting the Dinero object to a string
|
return total.toFormat("$0,0.00");
|
||||||
}, [data, cardSettings.totalAmountInProduction]);
|
}, [data, cardSettings.totalAmountInProduction]);
|
||||||
|
|
||||||
const totalAmountOnBoard = useMemo(() => {
|
const totalAmountOnBoard = useMemo(() => {
|
||||||
if (!reducerData || !cardSettings.totalAmountOnBoard) return null;
|
if (!reducerData || !cardSettings.totalAmountOnBoard) return null;
|
||||||
const total = calculateReducerTotalAmount(reducerData.lanes, "job_totals");
|
const total = calculateReducerTotalAmount(reducerData.lanes, "job_totals");
|
||||||
return total.toFormat("$0,0.00"); // Formatting the Dinero object to a string
|
return total.toFormat("$0,0.00");
|
||||||
}, [reducerData, cardSettings.totalAmountOnBoard]);
|
}, [reducerData, cardSettings.totalAmountOnBoard]);
|
||||||
|
|
||||||
const totalHrsOnBoard = useMemo(() => {
|
const totalHrsOnBoard = useMemo(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user