The “totalEmployeeSalary” array is accumulating values across multiple documents (each identified by a unique “doc.id”) instead of representing the salaries for a single day.
This is my code on how I store values to ACCOUNTING_DATA:
// Store values to ACCOUNTING_DATA
useEffect(() => {
const addAccountingData = async () => {
// For computations
const totalRevenue = 12281.72;
const totalSales = 12281.72;
const totalExpenses = 12281.72;
const totalDailyRent = 12281.72;
try {
const accountingRef = collection(firestore, "ACCOUNTING_DATA");
const emp = [];
const unsubscribeAccounting = onSnapshot(collection(firestore, "EMPLOYEE_DAILY_SALARY_COMP"), (snapshot) => {
snapshot.docChanges().forEach(async (change) => {
const employee = change.doc.data();
const accountingDataRef = doc(accountingRef, change.doc.id);
if (change.type === "added" || change.type === "modified") {
console.log("Adding or modifying data for employee: ", employee.employeeId);
emp.push(employee);
console.log("Employees: ", emp);
const totalEmpSalaryData = emp.map((employee) => ({
employeeId: employee.employeeId,
dailyRate: employee.dailyRate,
overtimeRate: employee.overtimeRate,
totalRate: employee.dailyRate + employee.overtimeRate,
quotaPay: employee.quotaPay,
overBreak: employee.overBreak,
late: employee.late
}));
console.log("Total Employee Salary: ",totalEmpSalaryData);
// Update or add document in ACCOUNTING_DATA
await setDoc(accountingDataRef, {
accountingDate: employee.computationDate,
branchCode: employee.branchCode,
totalRevenue: totalRevenue,
totalSales: totalSales,
totalExpenses: totalExpenses,
totalDailyRent: totalDailyRent,
totalEmployeeSalary: totalEmpSalaryData,
});
} else if (change.type === "removed") {
console.log("Removing data for employee: ", employee.employeeId);
// Remove document from ACCOUNTING_DATA
await deleteDoc(accountingDataRef);
}
});
});
return () => unsubscribeAccounting();
} catch (err) {
console.error("Error adding accounting: ", err);
}
};
addAccountingData();
}, []);
I am trying to pput values in the totalEmployeeSalary that are filtered by branch and date
Recognized by Google Cloud Collective