refactor: remove deprecated components and streamline member coverage calculations, enhance budget management with improved payroll handling, and update UI elements for better clarity
This commit is contained in:
parent
983aeca2dc
commit
09d8794d72
42 changed files with 2166 additions and 2974 deletions
209
stores/budget.ts
209
stores/budget.ts
|
|
@ -319,15 +319,22 @@ export const useBudgetStore = defineStore(
|
|||
if (!isInitialized.value) return;
|
||||
|
||||
const coopStore = useCoopBuilderStore();
|
||||
const payrollIndex = budgetWorksheet.value.expenses.findIndex(item => item.id === "expense-payroll");
|
||||
const basePayrollIndex = budgetWorksheet.value.expenses.findIndex(item => item.id === "expense-payroll-base");
|
||||
const oncostIndex = budgetWorksheet.value.expenses.findIndex(item => item.id === "expense-payroll-oncosts");
|
||||
|
||||
if (payrollIndex === -1) return; // No existing payroll entry
|
||||
// If no split payroll entries exist, look for legacy single entry
|
||||
const legacyIndex = budgetWorksheet.value.expenses.findIndex(item => item.id === "expense-payroll");
|
||||
|
||||
if (basePayrollIndex === -1 && legacyIndex === -1) return; // No existing payroll entries
|
||||
|
||||
const totalHours = coopStore.members.reduce((sum, m) => sum + (m.hoursPerMonth || 0), 0);
|
||||
const hourlyWage = coopStore.equalHourlyWage || 0;
|
||||
const oncostPct = coopStore.payrollOncostPct || 0;
|
||||
const basePayrollBudget = totalHours * hourlyWage;
|
||||
|
||||
// Declare today once for the entire function
|
||||
const today = new Date();
|
||||
|
||||
if (basePayrollBudget > 0 && coopStore.members.length > 0) {
|
||||
// Use policy-driven allocation
|
||||
const payPolicy = {
|
||||
|
|
@ -340,44 +347,92 @@ export const useBudgetStore = defineStore(
|
|||
displayName: m.name,
|
||||
monthlyPayPlanned: m.monthlyPayPlanned || 0,
|
||||
minMonthlyNeeds: m.minMonthlyNeeds || 0,
|
||||
targetMonthlyPay: m.targetMonthlyPay || 0,
|
||||
role: m.role || '',
|
||||
hoursPerMonth: m.hoursPerMonth || 0
|
||||
}));
|
||||
|
||||
const allocatedMembers = allocatePayroll(membersForAllocation, payPolicy, basePayrollBudget);
|
||||
|
||||
// Sum with operating mode consideration
|
||||
// Sum the allocated payroll amounts
|
||||
const totalAllocatedPayroll = allocatedMembers.reduce((sum, m) => {
|
||||
const planned = m.monthlyPayPlanned || 0;
|
||||
if (coopStore.operatingMode === 'min' && m.minMonthlyNeeds) {
|
||||
return sum + Math.min(planned, m.minMonthlyNeeds);
|
||||
}
|
||||
return sum + planned;
|
||||
return sum + (m.monthlyPayPlanned || 0);
|
||||
}, 0);
|
||||
|
||||
const monthlyPayroll = totalAllocatedPayroll * (1 + oncostPct / 100);
|
||||
// Update monthly values for base payroll
|
||||
|
||||
// Update monthly values
|
||||
const today = new Date();
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = new Date(today.getFullYear(), today.getMonth() + i, 1);
|
||||
const monthKey = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}`;
|
||||
budgetWorksheet.value.expenses[payrollIndex].monthlyValues[monthKey] = monthlyPayroll;
|
||||
if (basePayrollIndex !== -1) {
|
||||
// Update base payroll entry
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = new Date(today.getFullYear(), today.getMonth() + i, 1);
|
||||
const monthKey = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}`;
|
||||
budgetWorksheet.value.expenses[basePayrollIndex].monthlyValues[monthKey] = totalAllocatedPayroll;
|
||||
}
|
||||
|
||||
// Update annual values for base payroll
|
||||
budgetWorksheet.value.expenses[basePayrollIndex].values = {
|
||||
year1: { best: totalAllocatedPayroll * 12, worst: totalAllocatedPayroll * 8, mostLikely: totalAllocatedPayroll * 12 },
|
||||
year2: { best: totalAllocatedPayroll * 14, worst: totalAllocatedPayroll * 10, mostLikely: totalAllocatedPayroll * 13 },
|
||||
year3: { best: totalAllocatedPayroll * 16, worst: totalAllocatedPayroll * 12, mostLikely: totalAllocatedPayroll * 15 }
|
||||
};
|
||||
}
|
||||
|
||||
// Update annual values
|
||||
budgetWorksheet.value.expenses[payrollIndex].values = {
|
||||
year1: { best: monthlyPayroll * 12, worst: monthlyPayroll * 8, mostLikely: monthlyPayroll * 12 },
|
||||
year2: { best: monthlyPayroll * 14, worst: monthlyPayroll * 10, mostLikely: monthlyPayroll * 13 },
|
||||
year3: { best: monthlyPayroll * 16, worst: monthlyPayroll * 12, mostLikely: monthlyPayroll * 15 }
|
||||
};
|
||||
if (oncostIndex !== -1) {
|
||||
// Update oncost entry
|
||||
const oncostAmount = totalAllocatedPayroll * (oncostPct / 100);
|
||||
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = new Date(today.getFullYear(), today.getMonth() + i, 1);
|
||||
const monthKey = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}`;
|
||||
budgetWorksheet.value.expenses[oncostIndex].monthlyValues[monthKey] = oncostAmount;
|
||||
}
|
||||
|
||||
// Update name with current percentage
|
||||
budgetWorksheet.value.expenses[oncostIndex].name = `Payroll Taxes & Benefits (${oncostPct}%)`;
|
||||
|
||||
// Update annual values for oncosts
|
||||
budgetWorksheet.value.expenses[oncostIndex].values = {
|
||||
year1: { best: oncostAmount * 12, worst: oncostAmount * 8, mostLikely: oncostAmount * 12 },
|
||||
year2: { best: oncostAmount * 14, worst: oncostAmount * 10, mostLikely: oncostAmount * 13 },
|
||||
year3: { best: oncostAmount * 16, worst: oncostAmount * 12, mostLikely: oncostAmount * 15 }
|
||||
};
|
||||
}
|
||||
|
||||
// Handle legacy single payroll entry (update to combined amount for backwards compatibility)
|
||||
if (legacyIndex !== -1 && basePayrollIndex === -1) {
|
||||
const monthlyPayroll = totalAllocatedPayroll * (1 + oncostPct / 100);
|
||||
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = new Date(today.getFullYear(), today.getMonth() + i, 1);
|
||||
const monthKey = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}`;
|
||||
budgetWorksheet.value.expenses[legacyIndex].monthlyValues[monthKey] = monthlyPayroll;
|
||||
}
|
||||
|
||||
budgetWorksheet.value.expenses[legacyIndex].values = {
|
||||
year1: { best: monthlyPayroll * 12, worst: monthlyPayroll * 8, mostLikely: monthlyPayroll * 12 },
|
||||
year2: { best: monthlyPayroll * 14, worst: monthlyPayroll * 10, mostLikely: monthlyPayroll * 13 },
|
||||
year3: { best: monthlyPayroll * 16, worst: monthlyPayroll * 12, mostLikely: monthlyPayroll * 15 }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Force reinitialize - always reload from wizard data
|
||||
async function forceInitializeFromWizardData() {
|
||||
console.log("=== FORCE BUDGET INITIALIZATION ===");
|
||||
isInitialized.value = false;
|
||||
budgetWorksheet.value.revenue = [];
|
||||
budgetWorksheet.value.expenses = [];
|
||||
await initializeFromWizardData();
|
||||
}
|
||||
|
||||
// Initialize worksheet from wizard data
|
||||
async function initializeFromWizardData() {
|
||||
if (isInitialized.value && budgetWorksheet.value.revenue.length > 0) {
|
||||
console.log("=== BUDGET INITIALIZATION DEBUG ===");
|
||||
console.log("Is already initialized:", isInitialized.value);
|
||||
console.log("Current revenue items:", budgetWorksheet.value.revenue.length);
|
||||
console.log("Current expense items:", budgetWorksheet.value.expenses.length);
|
||||
|
||||
// Check if we have actual budget data (not just initialized flag)
|
||||
if (isInitialized.value && (budgetWorksheet.value.revenue.length > 0 || budgetWorksheet.value.expenses.length > 0)) {
|
||||
console.log("Already initialized with data, skipping...");
|
||||
return;
|
||||
}
|
||||
|
|
@ -388,15 +443,19 @@ export const useBudgetStore = defineStore(
|
|||
// Use the new coopBuilder store instead of the old stores
|
||||
const coopStore = useCoopBuilderStore();
|
||||
|
||||
console.log("Streams:", coopStore.streams.length, "streams");
|
||||
console.log("Members:", coopStore.members.length, "members");
|
||||
console.log("Equal wage:", coopStore.equalHourlyWage || "No wage set");
|
||||
console.log("Overhead costs:", coopStore.overheadCosts.length, "costs");
|
||||
console.log("CoopStore Data:");
|
||||
console.log("- Streams:", coopStore.streams.length, coopStore.streams);
|
||||
console.log("- Members:", coopStore.members.length, coopStore.members);
|
||||
console.log("- Equal wage:", coopStore.equalHourlyWage || "No wage set");
|
||||
console.log("- Overhead costs:", coopStore.overheadCosts.length, coopStore.overheadCosts);
|
||||
|
||||
// Clear existing data
|
||||
budgetWorksheet.value.revenue = [];
|
||||
budgetWorksheet.value.expenses = [];
|
||||
|
||||
// Declare today once for the entire function
|
||||
const today = new Date();
|
||||
|
||||
// Add revenue streams from wizard (but don't auto-load fixtures)
|
||||
// Note: We don't auto-load fixtures anymore, but wizard data should still work
|
||||
|
||||
|
|
@ -423,7 +482,6 @@ export const useBudgetStore = defineStore(
|
|||
|
||||
// Create monthly values - split the annual target evenly across 12 months
|
||||
const monthlyValues: Record<string, number> = {};
|
||||
const today = new Date();
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = new Date(today.getFullYear(), today.getMonth() + i, 1);
|
||||
const monthKey = `${date.getFullYear()}-${String(
|
||||
|
|
@ -470,8 +528,16 @@ export const useBudgetStore = defineStore(
|
|||
const hourlyWage = coopStore.equalHourlyWage || 0;
|
||||
const oncostPct = coopStore.payrollOncostPct || 0;
|
||||
|
||||
console.log("=== PAYROLL CALCULATION DEBUG ===");
|
||||
console.log("Total hours:", totalHours);
|
||||
console.log("Hourly wage:", hourlyWage);
|
||||
console.log("Oncost %:", oncostPct);
|
||||
console.log("Operating mode:", coopStore.operatingMode);
|
||||
console.log("Policy relationship:", coopStore.policy.relationship);
|
||||
|
||||
// Calculate total payroll budget using policy allocation
|
||||
const basePayrollBudget = totalHours * hourlyWage;
|
||||
console.log("Base payroll budget:", basePayrollBudget);
|
||||
|
||||
if (basePayrollBudget > 0 && coopStore.members.length > 0) {
|
||||
// Use policy-driven allocation to get actual member pay amounts
|
||||
|
|
@ -487,30 +553,28 @@ export const useBudgetStore = defineStore(
|
|||
// Ensure all required fields exist
|
||||
monthlyPayPlanned: m.monthlyPayPlanned || 0,
|
||||
minMonthlyNeeds: m.minMonthlyNeeds || 0,
|
||||
targetMonthlyPay: m.targetMonthlyPay || 0,
|
||||
role: m.role || '',
|
||||
hoursPerMonth: m.hoursPerMonth || 0
|
||||
}));
|
||||
|
||||
// Allocate payroll based on policy
|
||||
const allocatedMembers = allocatePayroll(membersForAllocation, payPolicy, basePayrollBudget);
|
||||
console.log("Allocated members:", allocatedMembers.map(m => ({ name: m.name, planned: m.monthlyPayPlanned, needs: m.minMonthlyNeeds })));
|
||||
|
||||
// Sum the allocated amounts for total payroll, respecting operating mode
|
||||
// Sum the allocated amounts for total payroll
|
||||
const totalAllocatedPayroll = allocatedMembers.reduce((sum, m) => {
|
||||
const planned = m.monthlyPayPlanned || 0;
|
||||
// In "minimum" mode, cap at min needs to show a lean runway scenario
|
||||
if (coopStore.operatingMode === 'min' && m.minMonthlyNeeds) {
|
||||
return sum + Math.min(planned, m.minMonthlyNeeds);
|
||||
}
|
||||
console.log(`Member ${m.name}: planned ${planned}`);
|
||||
return sum + planned;
|
||||
}, 0);
|
||||
|
||||
console.log("Total allocated payroll:", totalAllocatedPayroll);
|
||||
|
||||
// Apply oncosts to the policy-allocated total
|
||||
const monthlyPayroll = totalAllocatedPayroll * (1 + oncostPct / 100);
|
||||
console.log("Monthly payroll with oncosts:", monthlyPayroll);
|
||||
|
||||
// Create monthly values for payroll
|
||||
const monthlyValues: Record<string, number> = {};
|
||||
const today = new Date();
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = new Date(today.getFullYear(), today.getMonth() + i, 1);
|
||||
const monthKey = `${date.getFullYear()}-${String(
|
||||
|
|
@ -519,31 +583,77 @@ export const useBudgetStore = defineStore(
|
|||
monthlyValues[monthKey] = monthlyPayroll;
|
||||
}
|
||||
|
||||
console.log("Creating split payroll budget items with monthly values:", Object.keys(monthlyValues).length, "months");
|
||||
|
||||
// Create base payroll monthly values (without oncosts)
|
||||
const baseMonthlyValues: Record<string, number> = {};
|
||||
const oncostMonthlyValues: Record<string, number> = {};
|
||||
// Reuse the today variable from above
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = new Date(today.getFullYear(), today.getMonth() + i, 1);
|
||||
const monthKey = `${date.getFullYear()}-${String(
|
||||
date.getMonth() + 1
|
||||
).padStart(2, "0")}`;
|
||||
baseMonthlyValues[monthKey] = totalAllocatedPayroll;
|
||||
oncostMonthlyValues[monthKey] = totalAllocatedPayroll * (oncostPct / 100);
|
||||
}
|
||||
|
||||
// Add base payroll item
|
||||
budgetWorksheet.value.expenses.push({
|
||||
id: "expense-payroll",
|
||||
id: "expense-payroll-base",
|
||||
name: "Payroll",
|
||||
mainCategory: "Salaries & Benefits",
|
||||
subcategory: "Base wages and benefits",
|
||||
source: "wizard",
|
||||
monthlyValues,
|
||||
monthlyValues: baseMonthlyValues,
|
||||
values: {
|
||||
year1: {
|
||||
best: monthlyPayroll * 12,
|
||||
worst: monthlyPayroll * 8,
|
||||
mostLikely: monthlyPayroll * 12,
|
||||
best: totalAllocatedPayroll * 12,
|
||||
worst: totalAllocatedPayroll * 8,
|
||||
mostLikely: totalAllocatedPayroll * 12,
|
||||
},
|
||||
year2: {
|
||||
best: monthlyPayroll * 14,
|
||||
worst: monthlyPayroll * 10,
|
||||
mostLikely: monthlyPayroll * 13,
|
||||
best: totalAllocatedPayroll * 14,
|
||||
worst: totalAllocatedPayroll * 10,
|
||||
mostLikely: totalAllocatedPayroll * 13,
|
||||
},
|
||||
year3: {
|
||||
best: monthlyPayroll * 16,
|
||||
worst: monthlyPayroll * 12,
|
||||
mostLikely: monthlyPayroll * 15,
|
||||
best: totalAllocatedPayroll * 16,
|
||||
worst: totalAllocatedPayroll * 12,
|
||||
mostLikely: totalAllocatedPayroll * 15,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Add payroll oncosts item (if oncost percentage > 0)
|
||||
if (oncostPct > 0) {
|
||||
const oncostAmount = totalAllocatedPayroll * (oncostPct / 100);
|
||||
budgetWorksheet.value.expenses.push({
|
||||
id: "expense-payroll-oncosts",
|
||||
name: `Payroll Taxes & Benefits (${oncostPct}%)`,
|
||||
mainCategory: "Salaries & Benefits",
|
||||
subcategory: "Payroll taxes and benefits",
|
||||
source: "wizard",
|
||||
monthlyValues: oncostMonthlyValues,
|
||||
values: {
|
||||
year1: {
|
||||
best: oncostAmount * 12,
|
||||
worst: oncostAmount * 8,
|
||||
mostLikely: oncostAmount * 12,
|
||||
},
|
||||
year2: {
|
||||
best: oncostAmount * 14,
|
||||
worst: oncostAmount * 10,
|
||||
mostLikely: oncostAmount * 13,
|
||||
},
|
||||
year3: {
|
||||
best: oncostAmount * 16,
|
||||
worst: oncostAmount * 12,
|
||||
mostLikely: oncostAmount * 15,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Add overhead costs from wizard
|
||||
|
|
@ -563,7 +673,6 @@ export const useBudgetStore = defineStore(
|
|||
|
||||
// Create monthly values for overhead costs
|
||||
const monthlyValues: Record<string, number> = {};
|
||||
const today = new Date();
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = new Date(today.getFullYear(), today.getMonth() + i, 1);
|
||||
const monthKey = `${date.getFullYear()}-${String(
|
||||
|
|
@ -696,7 +805,6 @@ export const useBudgetStore = defineStore(
|
|||
if (!item.monthlyValues) {
|
||||
console.log("Migrating item to monthly values:", item.name);
|
||||
item.monthlyValues = {};
|
||||
const today = new Date();
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = new Date(today.getFullYear(), today.getMonth() + i, 1);
|
||||
const monthKey = `${date.getFullYear()}-${String(
|
||||
|
|
@ -856,6 +964,7 @@ export const useBudgetStore = defineStore(
|
|||
groupedExpenses,
|
||||
isInitialized,
|
||||
initializeFromWizardData,
|
||||
forceInitializeFromWizardData,
|
||||
refreshPayrollInBudget,
|
||||
updateBudgetValue,
|
||||
updateMonthlyValue,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue