-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.js
More file actions
80 lines (63 loc) · 2.35 KB
/
Copy pathcsv.js
File metadata and controls
80 lines (63 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs');
const path = require('path');
const csv = require('csv-parser');
const CPF_RATES = {
'<55': { employee: 0.2, employer: 0.17 },
'55-60': { employee: 0.13, employer: 0.13 },
'60-65': { employee: 0.075, employer: 0.09 },
'>65': { employee: 0.05, employer: 0.075 },
};
const getAgeGroup = (age) => {
if (age < 55) return '<55';
if (age >= 55 && age < 60) return '55-60';
if (age >= 60 && age < 65) return '60-65';
return '>65';
};
const calculateCPF = (salary, age, isCitizen = true) => {
if (!isCitizen) return { employeeCPF: 0, employerCPF: 0, totalCPF: 0 };
const ageGroup = getAgeGroup(age);
const rates = CPF_RATES[ageGroup];
const employeeCPF = salary * rates.employee;
const employerCPF = salary * rates.employer;
const totalCPF = employeeCPF + employerCPF;
return { employeeCPF, employerCPF, totalCPF };
};
const calculatePayroll = (grossSalary, age, isCitizen, additionalPayments = 0) => {
const totalSalary = grossSalary + additionalPayments;
const { employeeCPF, employerCPF, totalCPF } = calculateCPF(totalSalary, age, isCitizen);
const netSalary = totalSalary - employeeCPF;
return {
grossSalary,
additionalPayments,
totalSalary,
employeeCPF,
employerCPF,
totalCPF,
netSalary,
};
};
const processCSV = (filePath) => {
const results = [];
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (row) => {
const name = row['Name'];
const grossSalary = parseFloat(row['Gross Salary']);
const age = parseInt(row['Age'], 10);
const isCitizen = row['Citizen'].toLowerCase() === 'yes';
const additionalPayments = parseFloat(row['Additional Payments']) || 0;
const payroll = calculatePayroll(grossSalary, age, isCitizen, additionalPayments);
console.log(`Payroll for ${name}:`, payroll);
results.push({ name, ...payroll });
})
.on('end', () => {
console.log('CSV file successfully processed.');
});
};
const filePath = process.argv[2];
if (!filePath) {
console.error('Please provide the path to the CSV file as a command-line argument.');
process.exit(1);
}
processCSV(path.resolve(filePath));
module.exports = { calculateCPF, calculatePayroll, processCSV };