diff --git a/packages/server/src/interfaces/BalanceSheet.ts b/packages/server/src/interfaces/BalanceSheet.ts index 45e514287..ddc36a53a 100644 --- a/packages/server/src/interfaces/BalanceSheet.ts +++ b/packages/server/src/interfaces/BalanceSheet.ts @@ -64,7 +64,11 @@ export interface IBalanceSheetQuery extends IFinancialSheetBranchesQuery { } // Balance sheet meta. -export interface IBalanceSheetMeta extends IFinancialSheetCommonMeta {} +export interface IBalanceSheetMeta extends IFinancialSheetCommonMeta { + formattedFromDate: string; + formattedToDate: string; + formattedDateRange: string; +} export interface IBalanceSheetFormatNumberSettings extends IFormatNumberSettings { diff --git a/packages/server/src/interfaces/CashFlow.ts b/packages/server/src/interfaces/CashFlow.ts index 1a4a1a6a1..40cd50896 100644 --- a/packages/server/src/interfaces/CashFlow.ts +++ b/packages/server/src/interfaces/CashFlow.ts @@ -1,4 +1,7 @@ -import { INumberFormatQuery } from './FinancialStatements'; +import { + IFinancialSheetCommonMeta, + INumberFormatQuery, +} from './FinancialStatements'; import { IAccount } from './Account'; import { ILedger } from './Ledger'; import { IFinancialTable, ITableRow } from './Table'; @@ -79,8 +82,8 @@ export interface ICashFlowStatementAggregateSection export interface ICashFlowCashBeginningNode extends ICashFlowStatementCommonSection { - sectionType: ICashFlowStatementSectionType.CASH_AT_BEGINNING; - } + sectionType: ICashFlowStatementSectionType.CASH_AT_BEGINNING; +} export type ICashFlowStatementSection = | ICashFlowStatementAccountSection @@ -89,10 +92,10 @@ export type ICashFlowStatementSection = | ICashFlowStatementCommonSection; export interface ICashFlowStatementColumn {} -export interface ICashFlowStatementMeta { - isCostComputeRunning: boolean; - organizationName: string; - baseCurrency: string; +export interface ICashFlowStatementMeta extends IFinancialSheetCommonMeta { + formattedToDate: string; + formattedFromDate: string; + formattedDateRange: string; } export interface ICashFlowStatementDOO { diff --git a/packages/server/src/interfaces/ProfitLossSheet.ts b/packages/server/src/interfaces/ProfitLossSheet.ts index 944a0a950..64b787667 100644 --- a/packages/server/src/interfaces/ProfitLossSheet.ts +++ b/packages/server/src/interfaces/ProfitLossSheet.ts @@ -1,5 +1,6 @@ import { IFinancialSheetBranchesQuery, + IFinancialSheetCommonMeta, INumberFormatQuery, } from './FinancialStatements'; import { IFinancialTable } from './Table'; @@ -134,10 +135,10 @@ export type IProfitLossSheetNode = | IProfitLossSheetEquationNode | IProfitLossSheetAccountNode; -export interface IProfitLossSheetMeta { - isCostComputeRunning: boolean; - organizationName: string; - baseCurrency: string; +export interface IProfitLossSheetMeta extends IFinancialSheetCommonMeta{ + formattedDateRange: string; + formattedFromDate: string; + formattedToDate: string; } // ------------------------------------------------ diff --git a/packages/server/src/interfaces/TrialBalanceSheet.ts b/packages/server/src/interfaces/TrialBalanceSheet.ts index f423503ae..b6cd79736 100644 --- a/packages/server/src/interfaces/TrialBalanceSheet.ts +++ b/packages/server/src/interfaces/TrialBalanceSheet.ts @@ -1,4 +1,7 @@ -import { INumberFormatQuery } from './FinancialStatements'; +import { + IFinancialSheetCommonMeta, + INumberFormatQuery, +} from './FinancialStatements'; import { IFinancialTable } from './Table'; export interface ITrialBalanceSheetQuery { @@ -24,10 +27,10 @@ export interface ITrialBalanceTotal { formattedBalance: string; } -export interface ITrialBalanceSheetMeta { - isCostComputeRunning: boolean; - organizationName: string; - baseCurrency: string; +export interface ITrialBalanceSheetMeta extends IFinancialSheetCommonMeta { + formattedFromDate: string; + formattedToDate: string; + formattedDateRange: string; } export interface ITrialBalanceAccount extends ITrialBalanceTotal { diff --git a/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetInjectable.ts b/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetInjectable.ts index 52edfe4ef..5e171110b 100644 --- a/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetInjectable.ts +++ b/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetInjectable.ts @@ -82,6 +82,7 @@ export default class BalanceSheetStatementService const models = this.tenancy.models(tenantId); const balanceSheetRepo = new BalanceSheetRepository(models, filter); + // Loads all resources. await balanceSheetRepo.asyncInitialize(); // Balance sheet report instance. @@ -95,7 +96,7 @@ export default class BalanceSheetStatementService const data = balanceSheetInstanace.reportData(); // Balance sheet meta. - const meta = await this.balanceSheetMeta.meta(tenantId); + const meta = await this.balanceSheetMeta.meta(tenantId, filter); return { query: filter, diff --git a/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetMeta.ts b/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetMeta.ts index 370a8b605..91ecb068c 100644 --- a/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetMeta.ts +++ b/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetMeta.ts @@ -1,6 +1,7 @@ import { Inject, Service } from 'typedi'; import { FinancialSheetMeta } from '../FinancialSheetMeta'; -import { IBalanceSheetMeta } from '@/interfaces'; +import { IBalanceSheetMeta, IBalanceSheetQuery } from '@/interfaces'; +import moment from 'moment'; @Service() export class BalanceSheetMetaInjectable { @@ -12,12 +13,21 @@ export class BalanceSheetMetaInjectable { * @param {number} tenantId - * @returns {IBalanceSheetMeta} */ - public async meta(tenantId: number): Promise { + public async meta( + tenantId: number, + query: IBalanceSheetQuery + ): Promise { const commonMeta = await this.financialSheetMeta.meta(tenantId); + const formattedToDate = moment(query.toDate).format('YYYY/MM/DD'); + const formattedFromDate = moment(query.fromDate).format('YYYY/MM/DD'); + const formattedDateRange = `From ${formattedFromDate} | To ${formattedToDate}`; return { ...commonMeta, sheetName: 'Balance Sheet', + formattedFromDate, + formattedToDate, + formattedDateRange }; } } diff --git a/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetPdfInjectable.ts b/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetPdfInjectable.ts index 691aacad2..2f776c224 100644 --- a/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetPdfInjectable.ts +++ b/packages/server/src/services/FinancialStatements/BalanceSheet/BalanceSheetPdfInjectable.ts @@ -27,7 +27,7 @@ export class BalanceSheetPdfInjectable { tenantId, table.table, table.meta.sheetName, - table.meta.baseCurrency + table.meta.formattedDateRange ); } } diff --git a/packages/server/src/services/FinancialStatements/CashFlow/CashFlowService.ts b/packages/server/src/services/FinancialStatements/CashFlow/CashFlowService.ts index 09fdf91ca..201a31be4 100644 --- a/packages/server/src/services/FinancialStatements/CashFlow/CashFlowService.ts +++ b/packages/server/src/services/FinancialStatements/CashFlow/CashFlowService.ts @@ -8,7 +8,7 @@ import { ICashFlowStatementQuery, ICashFlowStatementDOO, IAccountTransaction, - ICashFlowStatementMeta + ICashFlowStatementMeta, } from '@/interfaces'; import CashFlowStatement from './CashFlow'; import Ledger from '@/services/Accounting/Ledger'; @@ -16,6 +16,7 @@ import CashFlowRepository from './CashFlowRepository'; import InventoryService from '@/services/Inventory/Inventory'; import { parseBoolean } from 'utils'; import { Tenant } from '@/system/models'; +import { CashflowSheetMeta } from './CashflowSheetMeta'; @Service() export default class CashFlowStatementService @@ -31,6 +32,9 @@ export default class CashFlowStatementService @Inject() inventoryService: InventoryService; + @Inject() + private cashflowSheetMeta: CashflowSheetMeta; + /** * Defaults balance sheet filter query. * @return {IBalanceSheetQuery} @@ -138,38 +142,13 @@ export default class CashFlowStatementService tenant.metadata.baseCurrency, i18n ); + // Retrieve the cashflow sheet meta. + const meta = await this.cashflowSheetMeta.meta(tenantId, filter); return { data: cashFlowInstance.reportData(), query: filter, - meta: this.reportMetadata(tenantId), + meta, }; } - - /** - * Retrieve the balance sheet meta. - * @param {number} tenantId - - * @returns {ICashFlowStatementMeta} - */ - private reportMetadata(tenantId: number): ICashFlowStatementMeta { - const settings = this.tenancy.settings(tenantId); - - const isCostComputeRunning = this.inventoryService - .isItemsCostComputeRunning(tenantId); - - const organizationName = settings.get({ - group: 'organization', - key: 'name', - }); - const baseCurrency = settings.get({ - group: 'organization', - key: 'base_currency', - }); - - return { - isCostComputeRunning: parseBoolean(isCostComputeRunning, false), - organizationName, - baseCurrency - }; - } } diff --git a/packages/server/src/services/FinancialStatements/CashFlow/CashflowSheetMeta.ts b/packages/server/src/services/FinancialStatements/CashFlow/CashflowSheetMeta.ts new file mode 100644 index 000000000..e1ed4a0d5 --- /dev/null +++ b/packages/server/src/services/FinancialStatements/CashFlow/CashflowSheetMeta.ts @@ -0,0 +1,33 @@ +import { Inject, Service } from 'typedi'; +import moment from 'moment'; +import { ICashFlowStatementMeta, ICashFlowStatementQuery } from '@/interfaces'; +import { FinancialSheetMeta } from '../FinancialSheetMeta'; + +@Service() +export class CashflowSheetMeta { + @Inject() + private financialSheetMeta: FinancialSheetMeta; + + /** + * CAshflow sheet meta. + * @param {number} tenantId + * @param {ICashFlowStatementQuery} query + * @returns {Promise} + */ + public async meta( + tenantId: number, + query: ICashFlowStatementQuery + ): Promise { + const meta = await this.financialSheetMeta.meta(tenantId); + const formattedToDate = moment(query.toDate).format('YYYY/MM/DD'); + const formattedFromDate = moment(query.fromDate).format('YYYY/MM/DD'); + const formattedDateRange = `From ${formattedFromDate} | To ${formattedToDate}`; + + return { + ...meta, + formattedToDate, + formattedFromDate, + formattedDateRange, + }; + } +} diff --git a/packages/server/src/services/FinancialStatements/CashFlow/CashflowTablePdfInjectable.ts b/packages/server/src/services/FinancialStatements/CashFlow/CashflowTablePdfInjectable.ts index eaab60db4..8455028f9 100644 --- a/packages/server/src/services/FinancialStatements/CashFlow/CashflowTablePdfInjectable.ts +++ b/packages/server/src/services/FinancialStatements/CashFlow/CashflowTablePdfInjectable.ts @@ -27,7 +27,7 @@ export class CashflowTablePdfInjectable { tenantId, table.table, sheetName, - table.meta.baseCurrency + table.meta.formattedDateRange ); } } diff --git a/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossSheetMeta.ts b/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossSheetMeta.ts new file mode 100644 index 000000000..c278420f5 --- /dev/null +++ b/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossSheetMeta.ts @@ -0,0 +1,35 @@ +import moment from 'moment'; +import { Inject, Service } from 'typedi'; +import { FinancialSheetMeta } from '../FinancialSheetMeta'; +import { ICashFlowStatementMeta, ICashFlowStatementQuery } from '@/interfaces'; + +@Service() +export class ProfitLossSheetMeta { + @Inject() + private financialSheetMeta: FinancialSheetMeta; + + /** + * Retrieve the P/L sheet meta. + * @param {number} tenantId - + * @returns {IBalanceSheetMeta} + */ + public async meta( + tenantId: number, + query: ICashFlowStatementQuery + ): Promise { + const commonMeta = await this.financialSheetMeta.meta(tenantId); + const formattedToDate = moment(query.toDate).format('YYYY/MM/DD'); + const formattedFromDate = moment(query.fromDate).format('YYYY/MM/DD'); + const formattedDateRange = `From ${formattedFromDate} | To ${formattedToDate}`; + + const sheetName = 'Cashflow Statement'; + + return { + ...commonMeta, + sheetName, + formattedFromDate, + formattedToDate, + formattedDateRange, + }; + } +} diff --git a/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossSheetService.ts b/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossSheetService.ts index 20cf9a170..f3b24e7a6 100644 --- a/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossSheetService.ts +++ b/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossSheetService.ts @@ -6,11 +6,10 @@ import { } from '@/interfaces'; import ProfitLossSheet from './ProfitLossSheet'; import TenancyService from '@/services/Tenancy/TenancyService'; -import InventoryService from '@/services/Inventory/Inventory'; -import { parseBoolean } from 'utils'; import { Tenant } from '@/system/models'; import { mergeQueryWithDefaults } from './utils'; import { ProfitLossSheetRepository } from './ProfitLossSheetRepository'; +import { ProfitLossSheetMeta } from './ProfitLossSheetMeta'; // Profit/Loss sheet service. @Service() @@ -19,7 +18,7 @@ export default class ProfitLossSheetService { private tenancy: TenancyService; @Inject() - private inventoryService: InventoryService; + private profitLossSheetMeta: ProfitLossSheetMeta; /** * Retrieve profit/loss sheet statement. @@ -47,6 +46,7 @@ export default class ProfitLossSheetService { const profitLossRepo = new ProfitLossSheetRepository(models, filter); + // Loads the profit/loss sheet data. await profitLossRepo.asyncInitialize(); // Profit/Loss report instance. @@ -57,38 +57,15 @@ export default class ProfitLossSheetService { i18n ); // Profit/loss report data and collumns. - const profitLossData = profitLossInstance.reportData(); + const data = profitLossInstance.reportData(); + + // Retrieve the profit/loss sheet meta. + const meta = await this.profitLossSheetMeta.meta(tenantId, filter); return { - data: profitLossData, query: filter, - meta: this.reportMetadata(tenantId), + data, + meta, }; }; - - /** - * Retrieve the trial balance sheet meta. - * @param {number} tenantId - Tenant id. - * @returns {ITrialBalanceSheetMeta} - */ - private reportMetadata(tenantId: number): IProfitLossSheetMeta { - const settings = this.tenancy.settings(tenantId); - - const isCostComputeRunning = - this.inventoryService.isItemsCostComputeRunning(tenantId); - const organizationName = settings.get({ - group: 'organization', - key: 'name', - }); - const baseCurrency = settings.get({ - group: 'organization', - key: 'base_currency', - }); - - return { - isCostComputeRunning: parseBoolean(isCostComputeRunning, false), - organizationName, - baseCurrency, - }; - } } diff --git a/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossTablePdfInjectable.ts b/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossTablePdfInjectable.ts index 24b720b1a..e6348fdf6 100644 --- a/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossTablePdfInjectable.ts +++ b/packages/server/src/services/FinancialStatements/ProfitLossSheet/ProfitLossTablePdfInjectable.ts @@ -22,13 +22,12 @@ export class ProfitLossTablePdfInjectable { query: IProfitLossSheetQuery ): Promise { const table = await this.profitLossTable.table(tenantId, query); - const sheetName = 'Profit & Loss Sheet'; return this.tableSheetPdf.convertToPdf( tenantId, table.table, - sheetName, - table.meta.baseCurrency + table.meta.sheetName, + table.meta.formattedDateRange ); } } diff --git a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetInjectable.ts b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetInjectable.ts index bc880ec99..df498241b 100644 --- a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetInjectable.ts +++ b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetInjectable.ts @@ -1,28 +1,20 @@ import { Service, Inject } from 'typedi'; import moment from 'moment'; import TenancyService from '@/services/Tenancy/TenancyService'; -import { - ITrialBalanceSheetMeta, - ITrialBalanceSheetQuery, - ITrialBalanceStatement, -} from '@/interfaces'; +import { ITrialBalanceSheetQuery, ITrialBalanceStatement } from '@/interfaces'; import TrialBalanceSheet from './TrialBalanceSheet'; import FinancialSheet from '../FinancialSheet'; -import InventoryService from '@/services/Inventory/Inventory'; -import { parseBoolean } from 'utils'; import { Tenant } from '@/system/models'; import { TrialBalanceSheetRepository } from './TrialBalanceSheetRepository'; +import { TrialBalanceSheetMeta } from './TrialBalanceSheetMeta'; @Service() export default class TrialBalanceSheetService extends FinancialSheet { @Inject() - tenancy: TenancyService; + private tenancy: TenancyService; @Inject() - inventoryService: InventoryService; - - @Inject('logger') - logger: any; + private trialBalanceSheetMetaService: TrialBalanceSheetMeta; /** * Defaults trial balance sheet filter query. @@ -47,32 +39,6 @@ export default class TrialBalanceSheetService extends FinancialSheet { }; } - /** - * Retrieve the trial balance sheet meta. - * @param {number} tenantId - Tenant id. - * @returns {ITrialBalanceSheetMeta} - */ - private reportMetadata(tenantId: number): ITrialBalanceSheetMeta { - const settings = this.tenancy.settings(tenantId); - - const isCostComputeRunning = - this.inventoryService.isItemsCostComputeRunning(tenantId); - const organizationName = settings.get({ - group: 'organization', - key: 'name', - }); - const baseCurrency = settings.get({ - group: 'organization', - key: 'base_currency', - }); - - return { - isCostComputeRunning: parseBoolean(isCostComputeRunning, false), - organizationName, - baseCurrency, - }; - } - /** * Retrieve trial balance sheet statement. * @param {number} tenantId @@ -99,6 +65,7 @@ export default class TrialBalanceSheetService extends FinancialSheet { repos, filter ); + // Loads the resources. await trialBalanceSheetRepos.asyncInitialize(); // Trial balance report instance. @@ -111,10 +78,13 @@ export default class TrialBalanceSheetService extends FinancialSheet { // Trial balance sheet data. const trialBalanceSheetData = trialBalanceInstance.reportData(); + // Trial balance sheet meta. + const meta = await this.trialBalanceSheetMetaService.meta(tenantId, filter); + return { data: trialBalanceSheetData, query: filter, - meta: this.reportMetadata(tenantId), + meta, }; } } diff --git a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetMeta.ts b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetMeta.ts new file mode 100644 index 000000000..7561e2b66 --- /dev/null +++ b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetMeta.ts @@ -0,0 +1,37 @@ +import { Inject, Service } from 'typedi'; +import moment from 'moment'; +import { ITrialBalanceSheetMeta, ITrialBalanceSheetQuery } from '@/interfaces'; +import { FinancialSheetMeta } from '../FinancialSheetMeta'; + +@Service() +export class TrialBalanceSheetMeta { + @Inject() + private financialSheetMeta: FinancialSheetMeta; + + /** + * Retrieves the trial balance sheet meta. + * @param {number} tenantId + * @param {ITrialBalanceSheetQuery} query + * @returns {Promise} + */ + public async meta( + tenantId: number, + query: ITrialBalanceSheetQuery + ): Promise { + const commonMeta = await this.financialSheetMeta.meta(tenantId); + + const formattedFromDate = moment(query.fromDate).format('YYYY/MM/DD'); + const formattedToDate = moment(query.toDate).format('YYYY/MM/DD'); + const formattedDateRange = `From ${formattedFromDate} to ${formattedToDate}`; + + const sheetName = 'Trial Balance Sheet'; + + return { + ...commonMeta, + sheetName, + formattedFromDate, + formattedToDate, + formattedDateRange, + }; + } +} diff --git a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetPdfInjectsable.ts b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetPdfInjectsable.ts index 1907dd26d..89e1e73f3 100644 --- a/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetPdfInjectsable.ts +++ b/packages/server/src/services/FinancialStatements/TrialBalanceSheet/TrialBalanceSheetPdfInjectsable.ts @@ -12,9 +12,9 @@ export class TrialBalanceSheetPdfInjectable { private tableSheetPdf: TableSheetPdf; /** - * Converts the given balance sheet table to pdf. + * Converts the given trial balance sheet table to pdf. * @param {number} tenantId - Tenant ID. - * @param {IBalanceSheetQuery} query - Balance sheet query. + * @param {ITrialBalanceSheetQuery} query - Trial balance sheet query. * @returns {Promise} */ public async pdf( @@ -22,13 +22,12 @@ export class TrialBalanceSheetPdfInjectable { query: ITrialBalanceSheetQuery ): Promise { const table = await this.trialBalanceSheetTable.table(tenantId, query); - const sheetName = 'Trial Balance Sheet'; return this.tableSheetPdf.convertToPdf( tenantId, table.table, - sheetName, - table.meta.baseCurrency + table.meta.sheetName, + table.meta.formattedDateRange ); } }