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) { if (s.completed) { console.log(chalk.grey(`- ${formatSchedule(s)}: completed`)); continue; } 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, s._date.interval); } if (date !== month) { console.log(chalk.grey(`- ${formatSchedule(s)}: not the specified month`)); continue; } if (s._amount > 0) { totalIncome += s._amount; } else { totalOutcome += Math.abs(s._amount); } console.log(chalk.green(`+ ${formatSchedule(s)}`)); } console.log(chalk.blue(`\nTotal income: +${formatAmount(totalIncome)}\nTotal outcome: -${formatAmount(totalOutcome)}\n`)); 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); 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();