@railzai/railz-visualizations
Version:
Railz.ai Visualizations
213 lines • 8.19 kB
JavaScript
/*!
* Accounting Data as a Service™ is the solution that makes sense of your business customers' financial data.
* Built with Stencil
* Copyright (c) FIS.
*/
import { isNil, invert } from 'lodash-es';
import { parseISO, format } from 'date-fns';
import * as locales from 'date-fns/locale';
import numbro from 'numbro';
import Translations from '../config/translations/en.json';
import { RVReportTypes } from '../types/enum/report-type';
import { errorLog } from '../services/logger';
/**
* Format date that will be shown on charts and show short form
*/
export const formatDate = (summary, reportFrequency, dateFormat) => {
return summary === null || summary === void 0 ? void 0 : summary.map((data) => {
const date = parseISO(data.period.date);
if (reportFrequency === 'quarter')
return `${(dateFormat === null || dateFormat === void 0 ? void 0 : dateFormat.prefix) || 'Q'}${data.period.quarter} ${format(date, (dateFormat === null || dateFormat === void 0 ? void 0 : dateFormat.format) || 'yyyy')}`;
if (reportFrequency === 'year')
return format(new Date(data.period.year, 1, 1), (dateFormat === null || dateFormat === void 0 ? void 0 : dateFormat.format) || 'yyyy', {
// eslint-disable-next-line import/namespace
locale: locales[(dateFormat === null || dateFormat === void 0 ? void 0 : dateFormat.locale) || 'us'],
});
return format(date, (dateFormat === null || dateFormat === void 0 ? void 0 : dateFormat.format) || 'MMM yy', {
// eslint-disable-next-line import/namespace
locale: locales[(dateFormat === null || dateFormat === void 0 ? void 0 : dateFormat.locale) || 'us'],
});
});
};
export const formatNegativeValues = (name, value) => {
if (['currentLiabilities', 'nonCurrentLiabilities'].includes(name)) {
return value > 0 ? value * -1 : value;
}
return value;
};
/**
* Filter data to ensure there is no undefined field result
*/
export const formatSeries = (summary, name, field) => {
var _a;
return ({
name,
data: (_a = summary === null || summary === void 0 ? void 0 : summary.map((data) => data[field]).filter((data) => data !== undefined)) === null || _a === void 0 ? void 0 : _a.map((item) => formatNegativeValues(field, item)),
});
};
/**
* Format number displayed on UI to 2 decimals and thousand separator
*/
export const formatNumber = (number, decimals = 2, minimum = 0) => {
if (!isNil(number)) {
const formatter = new Intl.NumberFormat('en-US', {
minimumFractionDigits: minimum,
maximumFractionDigits: decimals,
});
return formatter.format(Number(number));
}
return '';
};
const formatCurrencyNumber = (value, decimals = 2) => {
if (isNil(value))
return '';
return numbro(Number(value)).format(`0,000.${'0'.repeat(decimals)}`);
};
export const formatCurrencyValue = (value, decimals = 2, fallbackValue = '-') => {
const formattedValue = formatCurrencyNumber(value, decimals);
return ['', 'NaN'].includes(formattedValue) ? fallbackValue : '$' + formattedValue;
};
/**
* Determine if report type is table
*/
export const isBankAccounts = (reportType) => {
return reportType && [RVReportTypes.BANK_ACCOUNT].includes(reportType);
};
/**
* Determine if report type is financialRatios
*/
export const isFinancialRatios = (reportType) => {
return reportType && [RVReportTypes.FINANCIAL_RATIO].includes(reportType);
};
/**
* Determine if report type is gauge
*/
export const isCreditScore = (reportType) => {
return reportType && [RVReportTypes.CREDIT_SCORE].includes(reportType);
};
/**
* Determine if report type is bank reconciliation
*/
export const isBankReconciliation = (reportType) => {
return reportType && [RVReportTypes.BANK_RECONCILIATION].includes(reportType);
};
/**
* Determine if report type is business valuations
*/
export const isBusinessValuations = (reportType) => {
return reportType && [RVReportTypes.BUSINESS_VALUATIONS].includes(reportType);
};
/**
* Determine if report type is Tax Benchmarking
*/
export const isTaxBenchmarking = (reportType) => {
return reportType && [RVReportTypes.TAX_BENCHMARKING].includes(reportType);
};
/**
* Determine if report type is pie
*/
export const isIncomeStatements = (reportType) => {
return reportType && [RVReportTypes.EXPENSES, RVReportTypes.REVENUE].includes(reportType);
};
/**
* Determine if report type is financial statements
*/
export const isStatements = (reportType) => {
return (reportType &&
[
RVReportTypes.BALANCE_SHEET,
RVReportTypes.INCOME_STATEMENTS,
RVReportTypes.CASHFLOW_STATEMENTS,
RVReportTypes.FINANCIAL_FORECASTS,
].includes(reportType));
};
/**
* Determine if report type is transactional
*/
export const isTransactions = (reportType) => {
return reportType && [RVReportTypes.INVOICES, RVReportTypes.BILLS].includes(reportType);
};
/**
* Get title of report types to be displayed on the box
*/
export const getTitleByReportType = (reportType) => {
const mappingReportTypeTranslation = invert(RVReportTypes);
return Translations[`RV_${mappingReportTypeTranslation[reportType]}`] || '';
};
/**
* Get information about reports with report frequency
*/
export const isRequiredReportFrequency = (reportType) => {
return (reportType &&
[
RVReportTypes.REVENUE,
RVReportTypes.EXPENSES,
RVReportTypes.CASHFLOW_STATEMENTS,
RVReportTypes.BALANCE_SHEET,
RVReportTypes.INCOME_STATEMENTS,
RVReportTypes.FINANCIAL_RATIO,
].includes(reportType));
};
/**
* Get information about reports with accounting method
*/
export const isRequiredAccountingMethod = (reportType) => {
return reportType && [RVReportTypes.BALANCE_SHEET].includes(reportType);
};
/**
* Round number and format to display
*/
export const roundNumber = (number, mantissa = 2) => {
var _a;
if (!isNil(number)) {
return (_a = numbro(Number(number))
.format({
average: true,
mantissa,
optionalMantissa: true,
lowPrecision: false,
})) === null || _a === void 0 ? void 0 : _a.toUpperCase();
}
return '';
};
/**
* return an inline styling string based on a css styling object,
* taking in consideration only stirng or number styling values.
*/
export const fromCssObjectToInline = (cssObject) => {
if (!cssObject)
return '';
return Object.entries(cssObject)
.filter(([key, value]) => key && (typeof value === 'string' || typeof value === 'number'))
.map(([key, value]) => `${key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase())}: ${value}`)
.join(';');
};
export const handleError = (responseData) => {
var _a, _b, _c, _d, _e;
let errorStatusCode;
if ((responseData === null || responseData === void 0 ? void 0 : responseData.status) === 202) {
errorLog(Translations.RV_ERROR_202_TITLE);
errorStatusCode = 202;
}
else if (((_a = responseData === null || responseData === void 0 ? void 0 : responseData.error) === null || _a === void 0 ? void 0 : _a.message[0]) === 'Combination of service and report type is not supported' ||
((_b = responseData === null || responseData === void 0 ? void 0 : responseData.error) === null || _b === void 0 ? void 0 : _b.statusCode) === 404) {
errorLog(Translations.DASHBOARD_FINANCIAL_SUMMARY_CHART_ERROR_ASP_NOT_SUPPORTED);
errorStatusCode = 404;
}
else if (((_c = responseData === null || responseData === void 0 ? void 0 : responseData.error) === null || _c === void 0 ? void 0 : _c.message[0]) === 'Service provider not supported' ||
((_d = responseData === null || responseData === void 0 ? void 0 : responseData.error) === null || _d === void 0 ? void 0 : _d.statusCode) === 400) {
errorLog(Translations.DASHBOARD_FINANCIAL_SUMMARY_CHART_ERROR_ASP_NOT_SUPPORTED);
errorStatusCode = 404;
}
else if (((_e = responseData === null || responseData === void 0 ? void 0 : responseData.error) === null || _e === void 0 ? void 0 : _e.statusCode) === 500) {
errorLog(Translations.RV_ERROR_500_TITLE);
errorStatusCode = 500;
}
else {
// generic error response
errorLog(Translations.RV_ERROR_204_TITLE);
errorStatusCode = 204;
}
return errorStatusCode;
};
//# sourceMappingURL=utils.js.map