fix: recover simulator cashflow and verify QA continuity and report assumptions
This commit is contained in:
@@ -1,5 +1,73 @@
|
||||
import { csvCell } from './csv.js';
|
||||
|
||||
export function defaultCashflowDraftInputs() {
|
||||
return {
|
||||
openingCash: '0',
|
||||
monthlyRevenue: '200000',
|
||||
monthlyOperatingCosts: '150000',
|
||||
monthlyDebtService: '0',
|
||||
monthlyTaxAssessed: '',
|
||||
monthlyTaxSchedule: Array.from({ length: 12 }, () => ''),
|
||||
vatCreditEstimated: '0',
|
||||
vatRefundRequested: '0',
|
||||
vatRefundRecognized: '0',
|
||||
vatRefundExpectedReceipt: '0',
|
||||
vatRefundExpectedMonth: '',
|
||||
};
|
||||
}
|
||||
|
||||
export type CashflowDraftSnapshot = {
|
||||
version: 1;
|
||||
taxMode: 'fixed' | 'schedule';
|
||||
inputs: ReturnType<typeof defaultCashflowDraftInputs>;
|
||||
};
|
||||
|
||||
/** Preserve editable strings, including unanswered and invalid amounts, without inventing zero. */
|
||||
export function restoreCashflowDraftSnapshot(
|
||||
snapshot: string | undefined,
|
||||
): CashflowDraftSnapshot | null {
|
||||
if (!snapshot || snapshot.length > 20000) return null;
|
||||
try {
|
||||
const parsed = JSON.parse(snapshot);
|
||||
if (
|
||||
!parsed ||
|
||||
parsed.version !== 1 ||
|
||||
!['fixed', 'schedule'].includes(parsed.taxMode) ||
|
||||
!parsed.inputs ||
|
||||
typeof parsed.inputs !== 'object' ||
|
||||
Array.isArray(parsed.inputs)
|
||||
)
|
||||
return null;
|
||||
const keys = Object.keys(defaultCashflowDraftInputs());
|
||||
if (Object.keys(parsed.inputs).some((key) => !keys.includes(key)))
|
||||
return null;
|
||||
for (const key of keys) {
|
||||
const value = parsed.inputs[key];
|
||||
if (key === 'monthlyTaxSchedule') {
|
||||
if (
|
||||
!Array.isArray(value) ||
|
||||
value.length !== 12 ||
|
||||
value.some(
|
||||
(item) => typeof item !== 'string' || item.length > 160,
|
||||
)
|
||||
)
|
||||
return null;
|
||||
} else if (typeof value !== 'string' || value.length > 160)
|
||||
return null;
|
||||
}
|
||||
return { version: 1, taxMode: parsed.taxMode, inputs: parsed.inputs };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function serializeCashflowDraftSnapshot(
|
||||
taxMode: CashflowDraftSnapshot['taxMode'],
|
||||
inputs: CashflowDraftSnapshot['inputs'],
|
||||
): string {
|
||||
return JSON.stringify({ version: 1, taxMode, inputs });
|
||||
}
|
||||
|
||||
export type MonthlyCashflowInput = {
|
||||
openingCash: string;
|
||||
monthlyRevenue: string;
|
||||
|
||||
Reference in New Issue
Block a user