How to use the entities/Report.ReportTimespanT.Yearly function in entities

To help you get started, we’ve selected a few entities examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ayastreb / money-tracker / src / entities / Report / ExpenseIncomeData.ts View on Github external
report.timespan === ReportTimespanT.Yearly
      ? range(0, 12).map(month => format(new Date().setMonth(month), 'MMM'))
      : range(1, getDaysInMonth(report.date.start) + 1).map(day =>
          `${day}`.padStart(2, '0')
        );
  const data = [
    new Array(labels.length).fill(0), // income series
    new Array(labels.length).fill(0) // expense series
  ];

  for (const tx of transactions) {
    if (tx.kind !== Expense && tx.kind !== Income) continue;

    const period = format(
      toUtcTimestamp(tx.date),
      report.timespan === ReportTimespanT.Yearly ? 'M' : 'D'
    );
    data[tx.kind === Income ? 0 : 1][parseInt(period) - 1] += Currency.convert(
      Math.abs(tx.amount),
      exchangeRate[tx.currency],
      base,
      tx.currency
    );
  }

  return {
    labels,
    series: data.map(set =>
      set.map(amount => Math.floor(Currency.centsToNumber(amount, base)))
    )
  };
}
github ayastreb / money-tracker / src / entities / Report / ExpenseIncomeData.ts View on Github external
export default function ExpenseIncomeData(
  report: ReportStateT,
  transactions: TransactionStateT[],
  exchangeRate: ExchangeRateT,
  base: string
): ReportDataT {
  const labels =
    report.timespan === ReportTimespanT.Yearly
      ? range(0, 12).map(month => format(new Date().setMonth(month), 'MMM'))
      : range(1, getDaysInMonth(report.date.start) + 1).map(day =>
          `${day}`.padStart(2, '0')
        );
  const data = [
    new Array(labels.length).fill(0), // income series
    new Array(labels.length).fill(0) // expense series
  ];

  for (const tx of transactions) {
    if (tx.kind !== Expense && tx.kind !== Income) continue;

    const period = format(
      toUtcTimestamp(tx.date),
      report.timespan === ReportTimespanT.Yearly ? 'M' : 'D'
    );
github ayastreb / money-tracker / src / entities / Report / NetWorthData.ts View on Github external
export default function NetWorthData(
  report: ReportStateT,
  transactions: TransactionStateT[],
  exchangeRate: ExchangeRateT,
  base: string,
  netWorthEnd: number
): ReportDataT {
  const labels =
    report.timespan === ReportTimespanT.Yearly
      ? range(0, 12).map(month => format(new Date().setMonth(month), 'MMM'))
      : range(1, getDaysInMonth(report.date.start) + 1).map(day =>
          `${day}`.padStart(2, '0')
        );

  const data = [];
  let lastPeriod: number | undefined;
  for (const tx of transactions) {
    if (tx.kind !== Expense && tx.kind !== Income) continue;

    const period =
      parseInt(
        format(
          toUtcTimestamp(tx.date),
          report.timespan === ReportTimespanT.Yearly ? 'M' : 'D'
        )
github ayastreb / money-tracker / src / entities / Report / NetWorthData.ts View on Github external
report.timespan === ReportTimespanT.Yearly
      ? range(0, 12).map(month => format(new Date().setMonth(month), 'MMM'))
      : range(1, getDaysInMonth(report.date.start) + 1).map(day =>
          `${day}`.padStart(2, '0')
        );

  const data = [];
  let lastPeriod: number | undefined;
  for (const tx of transactions) {
    if (tx.kind !== Expense && tx.kind !== Income) continue;

    const period =
      parseInt(
        format(
          toUtcTimestamp(tx.date),
          report.timespan === ReportTimespanT.Yearly ? 'M' : 'D'
        )
      ) - 1;
    if (period !== lastPeriod) {
      if (lastPeriod === undefined) {
        lastPeriod = period + 1;
        data[lastPeriod] = netWorthEnd;
      }
      data[period] = data[lastPeriod];
    }

    lastPeriod = period;

    data[period] -= Currency.convert(
      tx.amount,
      exchangeRate[tx.currency],
      base,