Added a number asset to limit to a specific number of decimal cases if needed
This commit is contained in:
@@ -11,7 +11,7 @@ import {errorMessages} from "@/constants/errors";
|
||||
import moment from "moment";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
import {Payment} from "@/interfaces/paypal";
|
||||
|
||||
import { toFixedNumber } from "@/utils/number";
|
||||
const db = getFirestore(app);
|
||||
const auth = getAuth(app);
|
||||
|
||||
@@ -32,7 +32,7 @@ const managePaymentRecords = async (user: User, userId: string | undefined): Pro
|
||||
corporate: userId,
|
||||
agent: user.corporateInformation.referralAgent,
|
||||
agentCommission: user.corporateInformation.payment!.commission,
|
||||
agentValue: (user.corporateInformation.payment!.commission / 100) * user.corporateInformation.payment!.value,
|
||||
agentValue: toFixedNumber((user.corporateInformation.payment!.commission / 100) * user.corporateInformation.payment!.value, 2),
|
||||
currency: user.corporateInformation.payment!.currency,
|
||||
value: user.corporateInformation.payment!.value,
|
||||
isPaid: false,
|
||||
|
||||
@@ -25,6 +25,7 @@ import Input from "@/components/Low/Input";
|
||||
import ReactDatePicker from "react-datepicker";
|
||||
import moment from "moment";
|
||||
import PaymentAssetManager from "@/components/PaymentAssetManager";
|
||||
import { toFixedNumber } from "@/utils/number";
|
||||
|
||||
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
|
||||
const user = req.session.user;
|
||||
@@ -89,7 +90,7 @@ const PaymentCreator = ({onClose, reload}: {onClose: () => void; reload: () => v
|
||||
corporate: corporate?.id,
|
||||
agent: referralAgent?.id,
|
||||
agentCommission: commission,
|
||||
agentValue: (commission / 100) * price,
|
||||
agentValue: toFixedNumber((commission / 100) * price, 2),
|
||||
currency,
|
||||
value: price,
|
||||
isPaid: false,
|
||||
@@ -431,7 +432,7 @@ export default function PaymentRecord() {
|
||||
header: "Amount",
|
||||
cell: (info) => (
|
||||
<span>
|
||||
{info.getValue().toFixed(2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label}
|
||||
{toFixedNumber(info.getValue(), 2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label}
|
||||
</span>
|
||||
),
|
||||
}),
|
||||
@@ -453,7 +454,7 @@ export default function PaymentRecord() {
|
||||
header: "Commission Value",
|
||||
cell: (info) => (
|
||||
<span>
|
||||
{info.getValue().toFixed(2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label}
|
||||
{toFixedNumber(info.getValue(), 2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label}
|
||||
</span>
|
||||
),
|
||||
}),
|
||||
|
||||
13
src/utils/number.ts
Normal file
13
src/utils/number.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export function isDecimal(num: number) {
|
||||
return num % 1 !== 0;
|
||||
}
|
||||
|
||||
export function toFixedNumber(num: number, decimals: number = 2) {
|
||||
// Rounds to 2 decimal places
|
||||
if(isDecimal(num)) {
|
||||
const multiplier = Math.pow(10, decimals);
|
||||
return Math.round(num * multiplier) / multiplier;
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
Reference in New Issue
Block a user