25 lines
3.7 KiB
JavaScript
25 lines
3.7 KiB
JavaScript
import {createHash,randomUUID} from 'node:crypto';
|
|
// This exception belongs only to the fixed virtual P&O issuer. It never makes
|
|
// customer, treasury, or arbitrary system accounts eligible for overdrafts.
|
|
export async function postPayrollIssue(tx,order,source,target,{now=Date.now()}={}){
|
|
const amount=BigInt(order.amount_minor),debit=BigInt(source.balance_minor)-amount,credit=BigInt(target.balance_minor)+amount;
|
|
if(source.system_key!=='sys_payroll_po'||source.state!=='active'||target.state!=='active'||source.id===target.id||amount<=0n||debit< -9223372036854775808n||credit>9223372036854775807n)throw Error('Invalid P&O issuance.');
|
|
await tx.query('UPDATE pb_accounts SET balance_minor=? WHERE id=?',[String(debit),source.id]);await tx.query('UPDATE pb_accounts SET balance_minor=? WHERE id=?',[String(credit),target.id]);
|
|
for(const [account,value,balance]of [[source,-amount,debit],[target,amount,credit]])await tx.query('INSERT INTO pb_entries(order_id,account_id,amount_minor,balance_after_minor,created_at) VALUES(?,?,?,?,?)',[order.id,account.id,String(value),String(balance),now]);
|
|
await tx.query("UPDATE pb_orders SET state='posted',posted_at=? WHERE id=?",[now,order.id]);
|
|
await tx.query('INSERT INTO pb_audit(id,actor_id,action,entity_id,details,created_at) VALUES(?,?,?,NULL,?,?)',[randomUUID(),order.actor_id,'payroll_issue_posted',JSON.stringify({paymentId:order.id}),now]);
|
|
}
|
|
const correctionId=period=>{const h=createHash('sha256').update('payroll-treasury-correction:'+period).digest('hex');return h.slice(0,8)+'-'+h.slice(8,12)+'-5'+h.slice(13,16)+'-a'+h.slice(17,20)+'-'+h.slice(20,32);};
|
|
export async function correctPayrollTreasuryFunding(db,{period,actorId,apply=false,now=Date.now()}={}){
|
|
if(!/^\d{4}-\d{2}$/.test(period)||!actorId)throw Error('Select an authorised payroll period.');
|
|
return db.transaction(async tx=>{
|
|
const accounts=await tx.query("SELECT * FROM pb_accounts WHERE system_key IN ('sys_web_treasury','sys_payroll_po') ORDER BY id FOR UPDATE"),treasury=accounts.find(a=>a.system_key==='sys_web_treasury'),issuer=accounts.find(a=>a.system_key==='sys_payroll_po');if(!treasury||!issuer)throw Error('Payroll accounts unavailable.');
|
|
const id=correctionId(period),previous=await tx.first('SELECT state,amount_minor FROM pb_orders WHERE id=? FOR UPDATE',[id]);if(previous){if(previous.state!=='posted')throw Error('Payroll correction requires reconciliation.');return {replayed:true,amountMinor:String(previous.amount_minor)};}
|
|
const orders=await tx.query("SELECT id,amount_minor FROM pb_orders WHERE mode='payroll' AND state='posted' AND source_id=? AND target_id=? AND memo=? FOR UPDATE",[treasury.id,issuer.id,'P&O '+period+' · employer funding']);const amount=orders.reduce((sum,row)=>sum+BigInt(row.amount_minor),0n);
|
|
if(!apply||!amount)return {plan:!apply,period,originalPayments:orders.length,amountMinor:String(amount)};
|
|
const fingerprint=createHash('sha256').update(JSON.stringify([period,orders.map(o=>o.id).sort(),String(amount)])).digest('hex');
|
|
await tx.query("INSERT INTO pb_orders(id,actor_id,source_id,target_id,fingerprint,amount_minor,memo,mode,state,created_at) VALUES(?,?,?,?,?,?,?,'payroll','queued',?)",[id,actorId,issuer.id,treasury.id,fingerprint,String(amount),'P&O '+period+' · restore treasury funding',now]);
|
|
await postPayrollIssue(tx,{id,actor_id:actorId,amount_minor:String(amount)},issuer,treasury,{now});
|
|
await tx.query('INSERT INTO pb_audit(id,actor_id,action,entity_id,details,created_at) VALUES(?,?,?,NULL,?,?)',[randomUUID(),actorId,'payroll_funding_corrected',JSON.stringify({period,amountMinor:String(amount),originalPayments:orders.map(o=>o.id),reason:'Restore the unlimited P&O issuer; salaries and taxes are not paid again.'}),now]);return {applied:true,period,originalPayments:orders.length,amountMinor:String(amount)};
|
|
});
|
|
}
|

Staff