web/website/node/postbank-payroll-model.mjs
2026-11-27 23:43:05 +00:00

29 lines
3.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Fictional DM payroll, inspired by published Belgian 2026 parameters. It is
// deliberately not a real Belgian payslip calculator (no household deductions).
export const DEFAULT_PAYROLL_POLICY=Object.freeze({enabled:false,payDay:28,netMinor:{officer:280000,branch_lead:336000,department_head:420000,executive:504000,owner_ceo:560000}});
export const DEFAULT_PAYROLL_TAX=Object.freeze({employeeSocialBps:1307,employerSocialBps:2500,annualAllowanceMinor:1155000,bands:[{upToMinor:1672000,bps:2500},{upToMinor:2951000,bps:4000},{upToMinor:5107000,bps:4500},{upToMinor:null,bps:5000}],basisYear:2026,sources:['https://fin.belgium.be/en/private-individuals/tax-return/income/tax-rates','https://www.socialsecurity.be/site_nl/employer/infos/employers_nsso/which-contributions.htm']});
export const PAYROLL_ROLE_WEIGHTS=Object.freeze({officer:1,branch_lead:2,department_head:3,executive:4,owner_ceo:5});
const integer=(n,min,max)=>Number.isSafeInteger(n)&&n>=min&&n<=max;
export function validatePayrollPolicy(p){
if(!p||Object.keys(p).some(k=>!['enabled','payDay','netMinor'].includes(k))||typeof p.enabled!=='boolean'||!integer(p.payDay,1,28)||!p.netMinor||Object.keys(p.netMinor).length!==5)throw Error('Choose a payroll day of 1–28 and a net monthly wage for each function rank.');
let previous=0;for(const role of Object.keys(PAYROLL_ROLE_WEIGHTS)){const amount=p.netMinor[role];if(!integer(amount,100,100000000)||amount<previous)throw Error('Net rank wages must increase with responsibility and use whole DM cents.');previous=amount;}
return structuredClone(p);
}
export function validatePayrollTax(t){
if(!t||Object.keys(t).some(k=>!['employeeSocialBps','employerSocialBps','annualAllowanceMinor','bands','basisYear','sources'].includes(k))||!integer(t.employeeSocialBps,0,4000)||!integer(t.employerSocialBps,0,10000)||!integer(t.annualAllowanceMinor,0,100000000)||!Array.isArray(t.bands)||t.bands.length!==4||!integer(t.basisYear,2020,2100))throw Error('Choose valid payroll tax parameters.');
let last=0,rate=0;for(let i=0;i<t.bands.length;i++){const b=t.bands[i];if(!b||Object.keys(b).some(k=>!['upToMinor','bps'].includes(k))||!integer(b.bps,rate,7500)||i===3&&b.upToMinor!==null||i!==3&&!integer(b.upToMinor,last+1,10000000000))throw Error('Choose four ascending annual tax bands with an open final band.');last=b.upToMinor;rate=b.bps;}
return {...structuredClone(t),sources:[...DEFAULT_PAYROLL_TAX.sources]};
}
const round=(n,d)=>(n+d/2n)/d;
function annualTax(income,tax){let remaining=income,from=0n,total=0n;for(const band of tax.bands){const width=band.upToMinor===null?remaining:BigInt(band.upToMinor)-from,part=remaining<width?remaining:width;if(part>0n)total+=round(part*BigInt(band.bps),10000n);remaining-=part;if(remaining<=0n)break;from=BigInt(band.upToMinor);}return total;}
export function payslipForGross(gross,tax=DEFAULT_PAYROLL_TAX){
gross=BigInt(gross);if(gross<0n||gross>1000000000n)throw Error('Gross salary is outside the supported range.');
const employeeSocial=round(gross*BigInt(tax.employeeSocialBps),10000n),taxable=gross-employeeSocial,credit=annualTax(BigInt(tax.annualAllowanceMinor),tax),annual=annualTax(taxable*12n,tax),incomeTax=round(annual>credit?annual-credit:0n,12n),employerSocial=round(gross*BigInt(tax.employerSocialBps),10000n),net=gross-employeeSocial-incomeTax;
return {gross,employeeSocial,incomeTax,employerSocial,net,employerCost:gross+employerSocial,government:employeeSocial+incomeTax+employerSocial};
}
export function payslipForNet(target,tax=DEFAULT_PAYROLL_TAX){
target=BigInt(target);if(target<100n||target>100000000n)throw Error('Choose a supported net salary.');let low=target,high=1000000000n;
if(payslipForGross(high,tax).net<target)throw Error('The tax model cannot produce this net salary.');
while(low<high){const mid=(low+high)/2n;if(payslipForGross(mid,tax).net<target)low=mid+1n;else high=mid;}
const result=payslipForGross(low,tax);if(result.net!==target)throw Error('Exact net salary could not be represented.');return result;
}