Hello, can you analyze this code " function calculateEmployeeSalary() { // Access the…
- User
Hello, can you analyze this code " function calculateEmployeeSalary() { // Access the active spreadsheet and sheets const ss = SpreadsheetApp.getActiveSpreadsheet(); const workEfficiencySheet = ss.getSheetByName("WorkEfficiency"); const salaryRateRefSheet = ss.getSheetByName("SalaryRateRef");
// Create a new sheet for computed salary if it doesn't already exist let computedSalarySheet = ss.getSheetByName("ComputedSalary"); if (!computedSalarySheet) { computedSalarySheet = ss.insertSheet("ComputedSalary"); } else { computedSalarySheet.clear(); // Clear old data if sheet exists }
// Set up headers for the ComputedSalary sheet const headers = ["OTC Name", "Agent Tier", "Sales", "Total Hours Worked", "Hourly Rate", "Total Hourly Pay", "Commission Rate", "Commission Amount", "Total Pay"]; computedSalarySheet.appendRow(headers);
// Get data from WorkEfficiency and SalaryRateRef sheets const workData = workEfficiencySheet.getDataRange().getValues(); const salaryRateData = salaryRateRefSheet.getDataRange().getValues();
// Process each row in the WorkEfficiency sheet (excluding header row) for (let i = 1; i < workData.length; i++) { const [otcName, agentTier, , sales, , , , , , , , , , totalHoursWorked] = workData[i];
let hourlyRate = 0; let commissionRate = 0; let tierMaxRate = 0; let tierMaxCommission = 0; // Find the matching tier and sales bracket in SalaryRateRef for (let j = 1; j < salaryRateData.length; j++) { const [tier, weeklySales, rate, commission] = [salaryRateData[j][0], salaryRateData[j][1], salaryRateData[j][2], salaryRateData[j][3]]; // If the tier matches, update the highest rate and commission for cases where sales exceed the last threshold if (tier === agentTier) { tierMaxRate = rate; tierMaxCommission = commission; // Check if sales match the current range if (sales <= weeklySales) { hourlyRate = rate; commissionRate = commission; break; } } } // Special handling for Tier A with sales between 8,000 and 10,000 if (agentTier === "A" && sales >= 8000 && sales < 10000) { hourlyRate = 4; commissionRate = 0.035; } // If sales exceed all defined brackets for this tier, use the maximum rate and commission if (hourlyRate === 0 && commissionRate === 0) { hourlyRate = tierMaxRate; commissionRate = tierMaxCommission; } // Calculate Total Hourly Pay, Commission Amount, and Total Pay const totalHourlyPay = hourlyRate * totalHoursWorked; const commissionAmount = sales * commissionRate; const totalPay = totalHourlyPay + commissionAmount; // Append row to ComputedSalary sheet with all requested fields computedSalarySheet.appendRow([otcName, agentTier, sales, totalHoursWorked, hourlyRate, totalHourlyPay, commissionRate * 100 + "%", commissionAmount, totalPay]);} } "
- Assistant
- User
but I want to change the qualifiers to this Tier MIN MAX Hourly Rate Commission A 1 2,000 2 0 A 2,001 3,000 2 2.50% A 3,001 4,000 2 3.00% A 4,001 5,000 3 3.00% A 5,001 8,000 4 3.50% A 8,001 100,000 5 4.00% B 1 1,500 2 0 B 1,501 3,000 2 2.50% B 3,001 4,000 2 3.00% B 4,001 100,000 3 3.00% C 1 1,000 2 0 C 1,001 2,000 2 2.00% C 2,001 100,000 2 2.50%
- Assistant
- User
I actually have another sheet for Salary rate reference, maybe we can use that as a reference cause we might change it in the future. So I have a sheet named "SalaryRateRef"
so for TIER A that has sales ranging from 1 to 2000 will get 2 hourly and 2.50% commission 3001 to 4000 would be 3 and 3% commission.
- Assistant