Entrega dashboard e correccoes transversais preservando Ofertas instalado
tests / ci (push) Canceled after 0s
tests / ci (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
export type OperatingAssumptions = {
|
||||
model: '' | 'hotel' | 'agriculture' | 'other';
|
||||
capacity: string;
|
||||
price: string;
|
||||
occupancy: string;
|
||||
frequency: string;
|
||||
source: string;
|
||||
costs: { label: string; annual: string }[];
|
||||
};
|
||||
export const emptyOperatingAssumptions = (): OperatingAssumptions => ({
|
||||
model: '',
|
||||
capacity: '',
|
||||
price: '',
|
||||
occupancy: '',
|
||||
frequency: '',
|
||||
source: '',
|
||||
costs: [],
|
||||
});
|
||||
export function restoreOperatingAssumptions(
|
||||
snapshot: string,
|
||||
): OperatingAssumptions {
|
||||
try {
|
||||
const parsed = JSON.parse(snapshot);
|
||||
const data = parsed?.data;
|
||||
if (
|
||||
parsed.version !== 1 ||
|
||||
!data ||
|
||||
!['', 'hotel', 'agriculture', 'other'].includes(data.model)
|
||||
)
|
||||
return emptyOperatingAssumptions();
|
||||
if (
|
||||
!['capacity', 'price', 'occupancy', 'frequency', 'source'].every(
|
||||
(key) =>
|
||||
typeof data[key] === 'string' && data[key].length <= 1000,
|
||||
)
|
||||
)
|
||||
return emptyOperatingAssumptions();
|
||||
if (
|
||||
!Array.isArray(data.costs) ||
|
||||
data.costs.length > 30 ||
|
||||
!data.costs.every(
|
||||
(row: { label: unknown; annual: unknown }) =>
|
||||
typeof row?.label === 'string' &&
|
||||
typeof row?.annual === 'string' &&
|
||||
row.label.length <= 120 &&
|
||||
row.annual.length <= 40,
|
||||
)
|
||||
)
|
||||
return emptyOperatingAssumptions();
|
||||
return data;
|
||||
} catch {
|
||||
return emptyOperatingAssumptions();
|
||||
}
|
||||
}
|
||||
function hundredths(value: string): bigint {
|
||||
const match = /^(\d{1,15})(?:\.(\d{1,2}))?$/.exec(value.trim());
|
||||
if (!match)
|
||||
throw new Error(
|
||||
'Indique cada pressuposto em falta, com até duas casas decimais.',
|
||||
);
|
||||
return BigInt(match[1]) * 100n + BigInt((match[2] ?? '').padEnd(2, '0'));
|
||||
}
|
||||
const money = (cents: bigint) =>
|
||||
`${cents / 100n}.${String(cents % 100n).padStart(2, '0')}`;
|
||||
export function estimateOperatingRevenue(data: OperatingAssumptions): string {
|
||||
if (!data.model)
|
||||
throw new Error('Escolha o modelo de actividade que pretende estimar.');
|
||||
const capacity = hundredths(data.capacity),
|
||||
price = hundredths(data.price),
|
||||
frequency = hundredths(data.frequency);
|
||||
let numerator = capacity * price * frequency;
|
||||
let divisor = 10000n;
|
||||
if (data.model === 'hotel') {
|
||||
const occupancy = hundredths(data.occupancy);
|
||||
if (occupancy > 10000n)
|
||||
throw new Error('A ocupação não pode exceder 100%.');
|
||||
numerator *= occupancy;
|
||||
divisor *= 10000n;
|
||||
}
|
||||
return money((numerator + divisor / 2n) / divisor);
|
||||
}
|
||||
export function estimateOperatingCosts(data: OperatingAssumptions): string {
|
||||
if (!data.costs.length)
|
||||
throw new Error(
|
||||
'Acrescente as categorias de custos que conhece. Valores em falta não são zero.',
|
||||
);
|
||||
return money(
|
||||
data.costs.reduce((total, row) => {
|
||||
if (!row.label.trim())
|
||||
throw new Error('Identifique a categoria de cada custo.');
|
||||
return total + hundredths(row.annual);
|
||||
}, 0n),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user