app/stores/session.ts

159 lines
3.7 KiB
TypeScript

import { defineStore } from "pinia";
export const useSessionStore = defineStore("session", () => {
// Value Accounting session checklist state
const checklist = ref({
monthClosed: false,
contributionsLogged: false,
surplusCalculated: false,
needsReviewed: false,
});
// Draft distribution allocations
const draftAllocations = ref({
deferredRepay: 0,
savings: 0,
hardship: 0,
training: 0,
patronage: 0,
retained: 0,
});
// Session rationale text
const rationale = ref("");
// Current session period - use current month/year
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const currentMonth = String(currentDate.getMonth() + 1).padStart(2, '0');
const currentSession = ref(`${currentYear}-${currentMonth}`);
// Saved distribution records
const savedRecords = ref([]);
// Available amounts for distribution
const availableAmounts = ref({
surplus: 0,
deferredOwed: 0,
savingsNeeded: 0,
});
// Computed total allocated
const totalAllocated = computed(() =>
Object.values(draftAllocations.value).reduce(
(sum, amount) => sum + amount,
0
)
);
// Computed checklist completion
const checklistComplete = computed(() =>
Object.values(checklist.value).every(Boolean)
);
// Actions
function updateChecklistItem(key, value) {
if (key in checklist.value) {
checklist.value[key] = value;
}
}
function updateAllocation(key, amount) {
if (key in draftAllocations.value) {
draftAllocations.value[key] = Number(amount) || 0;
}
}
function resetAllocations() {
Object.keys(draftAllocations.value).forEach((key) => {
draftAllocations.value[key] = 0;
});
}
function updateRationale(text) {
rationale.value = text;
}
function saveSession() {
const record = {
id: Date.now().toString(),
period: currentSession.value,
date: new Date().toISOString(),
allocations: { ...draftAllocations.value },
rationale: rationale.value,
checklist: { ...checklist.value },
};
savedRecords.value.push(record);
// Reset for next session
resetAllocations();
rationale.value = "";
Object.keys(checklist.value).forEach((key) => {
checklist.value[key] = false;
});
return record;
}
function loadSession(period) {
const record = savedRecords.value.find((r) => r.period === period);
if (record) {
currentSession.value = period;
Object.assign(draftAllocations.value, record.allocations);
rationale.value = record.rationale;
Object.assign(checklist.value, record.checklist);
}
}
function setCurrentSession(period) {
currentSession.value = period;
}
function updateAvailableAmounts(amounts) {
Object.assign(availableAmounts.value, amounts);
}
function resetSession() {
// Reset checklist
Object.keys(checklist.value).forEach((key) => {
checklist.value[key] = false;
});
// Reset allocations
Object.keys(draftAllocations.value).forEach((key) => {
draftAllocations.value[key] = 0;
});
// Reset other values
rationale.value = "";
savedRecords.value = [];
// Reset available amounts
availableAmounts.value = {
surplus: 0,
deferredOwed: 0,
savingsNeeded: 0,
};
}
return {
checklist,
draftAllocations,
rationale,
currentSession: readonly(currentSession),
savedRecords: readonly(savedRecords),
availableAmounts: readonly(availableAmounts),
totalAllocated,
checklistComplete,
updateChecklistItem,
updateAllocation,
resetAllocations,
updateRationale,
saveSession,
loadSession,
setCurrentSession,
updateAvailableAmounts,
resetSession,
};
});