gongxiangp2/lib/performance-salary-pay.ts

21 lines
809 B
TypeScript

import { Prisma } from '@prisma/client'
type BaseWages = {
baseSalary: Prisma.Decimal
gradeSalary: Prisma.Decimal
performanceSalary: Prisma.Decimal
}
/** The rounded coefficient is both displayed and used for the monetary calculation. */
export function calculateSalaryPay(totalScore: number | null, wages: BaseWages) {
if (totalScore === null || !Number.isFinite(totalScore)) {
return { performanceCoefficient: null, currentTotal: null }
}
const coefficient = new Prisma.Decimal(totalScore).div(100).toDecimalPlaces(2, Prisma.Decimal.ROUND_HALF_UP)
const total = wages.baseSalary.plus(wages.gradeSalary).plus(wages.performanceSalary.times(coefficient))
return {
performanceCoefficient: coefficient.toNumber(),
currentTotal: total.toFixed(2, Prisma.Decimal.ROUND_HALF_UP),
}
}