feat: better ux for calc-schedule-budget
This commit is contained in:
@@ -3,30 +3,56 @@ import { format } from 'date-fns';
|
||||
import { checkEnv } from './lib/env.js';
|
||||
import * as actual from './lib/actual.js';
|
||||
import { addFromFrequency } from './lib/dates.js';
|
||||
import chalk from 'chalk';
|
||||
|
||||
checkEnv([
|
||||
'BUDGET_INCOME_NAME',
|
||||
'BUDGET_OUTCOME_NAME',
|
||||
]);
|
||||
|
||||
console.log();
|
||||
if (process.env.DRY_RUN) {
|
||||
console.log(chalk.grey.bold('Running in dry run mode\n'));
|
||||
}
|
||||
|
||||
const [month = format(new Date(), 'yyyy-MM')] = process.argv.slice(2);
|
||||
|
||||
const amountFormatter = new Intl.NumberFormat(undefined, { style: 'currency', currency: 'EUR' });
|
||||
function formatAmount(amount) {
|
||||
return chalk.bold(amountFormatter.format(Math.abs(amount / 100)));
|
||||
}
|
||||
|
||||
function formatSchedule(s) {
|
||||
const operator = s._amountOp === 'isapprox' ? '~ ' : '';
|
||||
let meta = formatAmount(s._amount);
|
||||
if (s._date.frequency) {
|
||||
meta += ` - ${s._date.frequency}`;
|
||||
}
|
||||
return `${chalk.underline(s.name)} (${operator}${meta})`;
|
||||
}
|
||||
|
||||
const schedules = await actual.getSchedules();
|
||||
let totalOutcome = 0;
|
||||
let totalIncome = 0;
|
||||
for (const s of schedules) {
|
||||
// TODO: get end
|
||||
if (s._date.start.slice(0, 7) > month) {
|
||||
console.log(`Skipping ${s.name} cause not in range`);
|
||||
if (s.completed) {
|
||||
console.log(chalk.grey(`- ${formatSchedule(s)}: completed`));
|
||||
continue;
|
||||
}
|
||||
|
||||
let date = s._date.start.slice(0, 7);
|
||||
|
||||
const start = (typeof s._date === 'string' ? s._date : s._date.start).slice(0, 7);
|
||||
// TODO: get end
|
||||
if (start > month) {
|
||||
console.log(chalk.grey(`- ${formatSchedule(s)}: not in range`));
|
||||
continue;
|
||||
}
|
||||
|
||||
let date = start;
|
||||
while (date < month) {
|
||||
date = addFromFrequency(date, s._date.frequency);
|
||||
date = addFromFrequency(date, s._date.frequency, s._date.interval);
|
||||
}
|
||||
if (date !== month) {
|
||||
console.log(`Skipping ${s.name} cause not this month`);
|
||||
console.log(chalk.grey(`- ${formatSchedule(s)}: not the specified month`));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -35,20 +61,26 @@ for (const s of schedules) {
|
||||
} else {
|
||||
totalOutcome += Math.abs(s._amount);
|
||||
}
|
||||
console.log(chalk.green(`+ ${formatSchedule(s)}`));
|
||||
}
|
||||
|
||||
console.log(`\nTotal income: +${totalIncome / 100}`);
|
||||
console.log(`Total outcome: -${totalOutcome / 100}\n`);
|
||||
console.log(chalk.blue(`\nTotal income: +${formatAmount(totalIncome)}\nTotal outcome: -${formatAmount(totalOutcome)}\n`));
|
||||
|
||||
const categories = await actual.getCategories();
|
||||
const incomeCategory = categories.find(c => c.name === process.env.BUDGET_INCOME_NAME);
|
||||
const outcomeCategory = categories.find(c => c.name === process.env.BUDGET_OUTCOME_NAME);
|
||||
if (!process.env.DRY_RUN) {
|
||||
const categories = await actual.getCategories();
|
||||
const incomeCategory = categories.find(c => c.name === process.env.BUDGET_INCOME_NAME);
|
||||
const outcomeCategory = categories.find(c => c.name === process.env.BUDGET_OUTCOME_NAME);
|
||||
|
||||
if (incomeCategory) {
|
||||
actual.setBudgetAmount(month, incomeCategory.id, totalIncome);
|
||||
}
|
||||
if (outcomeCategory) {
|
||||
actual.setBudgetAmount(month, outcomeCategory.id, totalOutcome);
|
||||
if (incomeCategory) {
|
||||
actual.setBudgetAmount(month, incomeCategory.id, totalIncome);
|
||||
console.log(chalk.yellow(`Income set for "${process.env.BUDGET_INCOME_NAME}" at "${month}"`));
|
||||
}
|
||||
if (outcomeCategory) {
|
||||
actual.setBudgetAmount(month, outcomeCategory.id, totalOutcome);
|
||||
console.log(chalk.yellow(`Outcome set for "${process.env.BUDGET_OUTCOME_NAME}" at "${month}"`))
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
|
||||
await actual.shutdown();
|
||||
Reference in New Issue
Block a user