zakat-calculator-api
Version:
A simple NPM package to calculate Zakat based on different asset types.
110 lines (104 loc) • 5.48 kB
JavaScript
exports.calculateZakat = async (zakatData) => {
try {
console.log(zakatData);
//const zakatData = req.body;
const defaultPercentages = {
money: 0.025, // 2.5%
gold: 0.025,
silver: 0.025,
investments: 0.025,
properties: 0.025,
business: 0.025,
agriculture_rainWater: 0.10, // 10% for rain-fed agriculture
agriculture_irrigation: 0.05, // 5% for irrigated agriculture
agriculture_both: 0.075,
cattle: 0.025,
otherAssets: 0.025,
};
const money = zakatData.money || {};
const gold = zakatData.gold || {};
const silver = zakatData.silver || {};
const investments = zakatData.investments || {};
const properties = zakatData.properties || {};
const business = zakatData.business || {};
const agriculture = zakatData.agriculture || {};
const cattle = zakatData.cattle || {};
const payables = zakatData.payables || {};
const otherAssets = zakatData.otherAssets || {};
const percentages = zakatData.percentages || defaultPercentages;
// Calculate total values for each category
const totalMoney = (money.cashInHands || 0) + (money.cashInBankAccounts || 0);
money.total = totalMoney;
// Gold calculation based on carats
let totalGold = 0;
if (gold.unitType === "Value") {
totalGold = (gold.gold24Carats || 0) + (gold.gold22Carats || 0) + (gold.gold18Carats || 0);
} else if (gold.unitType === "Weight") {
totalGold = ((gold.gold24Carats || 0) * (gold.goldPrice || 0)) +
((gold.gold22Carats || 0) * (gold.goldPrice || 0) * 0.9167) +
((gold.gold18Carats || 0) * (gold.goldPrice || 0) * 0.75);
}
gold.totalGoldValue = totalGold;
// Silver calculation based on unit type
let totalSilver = 0;
if (silver.unitType === "Value") {
// totalSilver = silver.silverWeight || 0;
totalSilver = silver.silverPrice || 0;
// totalSilver = (silver.silverWeight || 0) * (silver.silverPrice || 0);
} else if (silver.unitType === "Weight") {
totalSilver = (silver.silverWeight || 0) * (silver.silverPrice || 0);
}
silver.totalSilverValue = totalSilver;
const totalInvestments = (investments.shares || 0) + (investments.otherInvestments || 0);
investments.totalInvest = totalInvestments;
const totalProperties = (properties.rentalIncome || 0);
properties.totalProperties = totalProperties;
const totalBusiness = (business.cash || 0) + (business.goodsStock || 0);
business.totalBusiness = totalBusiness;
const totalAgriculture1 = (agriculture.rainWater || 0) +
(agriculture.irrigation || 0) +
(agriculture.both || 0);
const totalAgriculture = (agriculture.rainWater || 0) * (percentages.agriculture_rainWater || 0.10) +
(agriculture.irrigation || 0) * (percentages.agriculture_irrigation || 0.05) +
(agriculture.both || 0) * (percentages.agriculture_both || 0.075);
agriculture.totalAgriculture = totalAgriculture1;
const totalCattle = (cattle.cows || 0) + (cattle.camels || 0) + (cattle.others || 0);
cattle.totalCattle = totalCattle;
const totalOthers = (otherAssets.pension || 0) + (otherAssets.loansGiven || 0) + (otherAssets.otherHoldings || 0);
otherAssets.totalOthers = totalOthers;
const totalAssets = money.total + gold.totalGoldValue + silver.totalSilverValue + investments.totalInvest +
properties.totalProperties + business.totalBusiness + agriculture.totalAgriculture +
cattle.totalCattle + otherAssets.totalOthers;
const totalPayables = (payables.creditCard || 0) + (payables.homeLoan || 0) +
(payables.carLoan || 0) + (payables.businessPayment || 0) +
(payables.debt_to_family || 0) + (payables.debt_to_other || 0);
payables.totalPayable = totalPayables
const finalTotalAssets = totalAssets - totalPayables;
let payablesPercentage = (totalPayables) * 2.5 / 100
const nisab = zakatData.nisab || 0;
let zakatDue = 0;
if (finalTotalAssets >= nisab) {
zakatDue += totalMoney * percentages.money;
zakatDue += totalGold * percentages.gold;
zakatDue += totalSilver * percentages.silver;
zakatDue += totalInvestments * percentages.investments;
zakatDue += totalProperties * percentages.properties;
zakatDue += totalBusiness * percentages.business;
zakatDue += totalAgriculture;
zakatDue += totalCattle * percentages.cattle;
zakatDue += totalOthers * percentages.otherAssets;
zakatDue -= payablesPercentage;
//zakatDue += totalPayables;
}
zakatData.totalAssets = finalTotalAssets;
zakatData.zakatDue = zakatDue;
return {
status: 200,
message: "Zakat Calculated successfully",
zakatData
};
} catch (error) {
console.log(error);
return { status: 500, message: "Internal Server Error", error };
}
};