getNetIncome = (income, country) => {
const taxes = {};
console.log(taxes);
taxRanges[country].forEach(
({ min = 0, max = 10000000, getRate, label, of }) => {
const rate = _.isFunction(getRate)
? getRate(income > max ? max : income, taxes)
: getRate;
let ofAmount;
if (of) {
ofAmount = _.isFunction(of) ? of(income, taxes) : taxes[of];
} else {
ofAmount = income;
}
const amountToApplyTaxOn = ofAmount > max ? max - min : ofAmount - min;
let value = 0;
if (ofAmount > min) {
value = _.round((rate * amountToApplyTaxOn) / 100, 0);
}
console.log({
label,
min,
max,
getRate,
rate,
of,
amountToApplyTaxOn,
ofAmount,
value
});
if (label in taxes) {
taxes[label] += value;
} else {
taxes[label] = value;
}
}
);
let totalTax = 0;
Object.entries(taxes).forEach(([key, value]) => (totalTax += value));
const netPay = income - totalTax;
return { netPay: netPay, ...taxes };
}