gongxiangp2/lib/performance-salary-pay.ts

27 lines
1.2 KiB
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),
}
}
/** Subtract the displayed, cent-rounded totals; missing is never treated as zero. */
export function calculateSalaryDifference(currentTotal: string | null, previousTotal: string | null): string | null {
if (currentTotal === null || previousTotal === null) return null
return new Prisma.Decimal(currentTotal).minus(previousTotal).toFixed(2, Prisma.Decimal.ROUND_HALF_UP)
}