Merge branch 'develop' into draft-import-resources

This commit is contained in:
Ahmed Bouhuolia
2024-03-10 14:54:32 +02:00
740 changed files with 22505 additions and 4723 deletions

View File

@@ -1,6 +1,5 @@
import { IAccountTransaction } from '@/interfaces';
import { Transformer } from '@/lib/Transformer/Transformer';
import { transaction } from 'objection';
export default class AccountTransactionTransformer extends Transformer {
/**

View File

@@ -13,7 +13,7 @@ export class AccountTransformer extends Transformer {
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return ['formattedAmount', 'flattenName'];
return ['formattedAmount', 'flattenName', 'bankBalanceFormatted'];
};
/**
@@ -34,13 +34,24 @@ export class AccountTransformer extends Transformer {
/**
* Retrieve formatted account amount.
* @param {IAccount} invoice
* @param {IAccount} invoice
* @returns {string}
*/
protected formattedAmount = (account: IAccount): string => {
return formatNumber(account.amount, { currencyCode: account.currencyCode });
};
/**
* Retrieves the formatted bank balance.
* @param {IAccount} account
* @returns {string}
*/
protected bankBalanceFormatted = (account: IAccount): string => {
return formatNumber(account.bankBalance, {
currencyCode: account.currencyCode,
});
};
/**
* Transformes the accounts collection to flat or nested array.
* @param {IAccount[]}

View File

@@ -3,8 +3,10 @@ import {
IAccount,
IAccountCreateDTO,
IAccountEditDTO,
IAccountResponse,
IAccountsFilter,
IAccountsTransactionsFilter,
IFilterMeta,
IGetAccountTransactionPOJO,
} from '@/interfaces';
import { CreateAccount } from './CreateAccount';
@@ -14,6 +16,7 @@ import { ActivateAccount } from './ActivateAccount';
import { GetAccounts } from './GetAccounts';
import { GetAccount } from './GetAccount';
import { GetAccountTransactions } from './GetAccountTransactions';
@Service()
export class AccountsApplication {
@Inject()
@@ -113,19 +116,22 @@ export class AccountsApplication {
/**
* Retrieves the accounts list.
* @param {number} tenantId
* @param {IAccountsFilter} filterDTO
* @returns
* @param {number} tenantId
* @param {IAccountsFilter} filterDTO
* @returns {Promise<{ accounts: IAccountResponse[]; filterMeta: IFilterMeta }>}
*/
public getAccounts = (tenantId: number, filterDTO: IAccountsFilter) => {
public getAccounts = (
tenantId: number,
filterDTO: IAccountsFilter
): Promise<{ accounts: IAccountResponse[]; filterMeta: IFilterMeta }> => {
return this.getAccountsService.getAccountsList(tenantId, filterDTO);
};
/**
* Retrieves the given account transactions.
* @param {number} tenantId
* @param {IAccountsTransactionsFilter} filter
* @returns {Promise<IGetAccountTransactionPOJO[]>}
* @param {number} tenantId
* @param {IAccountsTransactionsFilter} filter
* @returns {Promise<IGetAccountTransactionPOJO[]>}
*/
public getAccountsTransactions = (
tenantId: number,

View File

@@ -5,7 +5,6 @@ import { IAccountEventActivatedPayload } from '@/interfaces';
import events from '@/subscribers/events';
import UnitOfWork from '@/services/UnitOfWork';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import { CommandAccountValidators } from './CommandAccountValidators';
@Service()
export class ActivateAccount {
@@ -18,9 +17,6 @@ export class ActivateAccount {
@Inject()
private uow: UnitOfWork;
@Inject()
private validator: CommandAccountValidators;
/**
* Activates/Inactivates the given account.
* @param {number} tenantId

View File

@@ -0,0 +1,59 @@
import { Inject, Service } from 'typedi';
import { PlaidLinkTokenService } from './PlaidLinkToken';
import { PlaidItemService } from './PlaidItem';
import { PlaidItemDTO } from '@/interfaces';
import { PlaidWebooks } from './PlaidWebhooks';
@Service()
export class PlaidApplication {
@Inject()
private getLinkTokenService: PlaidLinkTokenService;
@Inject()
private plaidItemService: PlaidItemService;
@Inject()
private plaidWebhooks: PlaidWebooks;
/**
* Retrieves the Plaid link token.
* @param {number} tenantId
* @param {number} itemId
* @returns
*/
public getLinkToken(tenantId: number) {
return this.getLinkTokenService.getLinkToken(tenantId);
}
/**
* Exchanges the Plaid access token.
* @param {number} tenantId
* @param {PlaidItemDTO} itemDTO
* @returns
*/
public exchangeToken(tenantId: number, itemDTO: PlaidItemDTO): Promise<void> {
return this.plaidItemService.item(tenantId, itemDTO);
}
/**
* Listens to Plaid webhooks
* @param {number} tenantId
* @param {string} webhookType
* @param {string} plaidItemId
* @param {string} webhookCode
* @returns
*/
public webhooks(
tenantId: number,
plaidItemId: string,
webhookType: string,
webhookCode: string
) {
return this.plaidWebhooks.webhooks(
tenantId,
plaidItemId,
webhookType,
webhookCode
);
}
}

View File

@@ -0,0 +1,43 @@
import Container, { Service } from 'typedi';
import { PlaidUpdateTransactions } from './PlaidUpdateTransactions';
import { IPlaidItemCreatedEventPayload } from '@/interfaces';
@Service()
export class PlaidFetchTransactionsJob {
/**
* Constructor method.
*/
constructor(agenda) {
agenda.define(
'plaid-update-account-transactions',
{ priority: 'high', concurrency: 2 },
this.handler
);
}
/**
* Triggers the function.
*/
private handler = async (job, done: Function) => {
const { tenantId, plaidItemId } = job.attrs
.data as IPlaidItemCreatedEventPayload;
const plaidFetchTransactionsService = Container.get(
PlaidUpdateTransactions
);
const io = Container.get('socket');
try {
await plaidFetchTransactionsService.updateTransactions(
tenantId,
plaidItemId
);
// Notify the frontend to reflect the new transactions changes.
io.emit('NEW_TRANSACTIONS_DATA', { plaidItemId });
done();
} catch (error) {
console.log(error);
done(error);
}
};
}

View File

@@ -0,0 +1,58 @@
import { Inject, Service } from 'typedi';
import { PlaidClientWrapper } from '@/lib/Plaid';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import {
IPlaidItemCreatedEventPayload,
PlaidItemDTO,
} from '@/interfaces/Plaid';
import SystemPlaidItem from '@/system/models/SystemPlaidItem';
@Service()
export class PlaidItemService {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
/**
* Exchanges the public token to get access token and item id and then creates
* a new Plaid item.
* @param {number} tenantId
* @param {PlaidItemDTO} itemDTO
* @returns {Promise<void>}
*/
public async item(tenantId: number, itemDTO: PlaidItemDTO): Promise<void> {
const { PlaidItem } = this.tenancy.models(tenantId);
const { publicToken, institutionId } = itemDTO;
const plaidInstance = new PlaidClientWrapper();
// Exchange the public token for a private access token and store with the item.
const response = await plaidInstance.itemPublicTokenExchange({
public_token: publicToken,
});
const plaidAccessToken = response.data.access_token;
const plaidItemId = response.data.item_id;
// Store the Plaid item metadata on tenant scope.
const plaidItem = await PlaidItem.query().insertAndFetch({
tenantId,
plaidAccessToken,
plaidItemId,
plaidInstitutionId: institutionId,
});
// Stores the Plaid item id on system scope.
await SystemPlaidItem.query().insert({ tenantId, plaidItemId });
// Triggers `onPlaidItemCreated` event.
await this.eventPublisher.emitAsync(events.plaid.onItemCreated, {
tenantId,
plaidAccessToken,
plaidItemId,
plaidInstitutionId: institutionId,
} as IPlaidItemCreatedEventPayload);
}
}

View File

@@ -0,0 +1,34 @@
import { PlaidClientWrapper } from '@/lib/Plaid';
import { Service } from 'typedi';
import config from '@/config';
@Service()
export class PlaidLinkTokenService {
/**
* Retrieves the plaid link token.
* @param {number} tenantId
* @returns
*/
async getLinkToken(tenantId: number) {
const accessToken = null;
// Must include transactions in order to receive transactions webhooks
const products = ['transactions'];
const linkTokenParams = {
user: {
// This should correspond to a unique id for the current user.
client_user_id: 'uniqueId' + tenantId,
},
client_name: 'Pattern',
products,
country_codes: ['US'],
language: 'en',
webhook: config.plaid.linkWebhook,
access_token: accessToken,
};
const plaidInstance = new PlaidClientWrapper();
const createResponse = await plaidInstance.linkTokenCreate(linkTokenParams);
return createResponse.data;
}
}

View File

@@ -0,0 +1,198 @@
import * as R from 'ramda';
import { Inject, Service } from 'typedi';
import bluebird from 'bluebird';
import { entries, groupBy } from 'lodash';
import { CreateAccount } from '@/services/Accounts/CreateAccount';
import { PlaidAccount, PlaidTransaction } from '@/interfaces';
import {
transformPlaidAccountToCreateAccount,
transformPlaidTrxsToCashflowCreate,
} from './utils';
import { DeleteCashflowTransaction } from '@/services/Cashflow/DeleteCashflowTransactionService';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
const CONCURRENCY_ASYNC = 10;
@Service()
export class PlaidSyncDb {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private createAccountService: CreateAccount;
@Inject()
private cashflowApp: CashflowApplication;
@Inject()
private deleteCashflowTransactionService: DeleteCashflowTransaction;
/**
* Syncs the plaid accounts to the system accounts.
* @param {number} tenantId Tenant ID.
* @param {PlaidAccount[]} plaidAccounts
* @returns {Promise<void>}
*/
public async syncBankAccounts(
tenantId: number,
plaidAccounts: PlaidAccount[],
institution: any
): Promise<void> {
const transformToPlaidAccounts =
transformPlaidAccountToCreateAccount(institution);
const accountCreateDTOs = R.map(transformToPlaidAccounts)(plaidAccounts);
await bluebird.map(
accountCreateDTOs,
(createAccountDTO: any) =>
this.createAccountService.createAccount(tenantId, createAccountDTO),
{ concurrency: CONCURRENCY_ASYNC }
);
}
/**
* Synsc the Plaid transactions to the system GL entries.
* @param {number} tenantId - Tenant ID.
* @param {number} plaidAccountId - Plaid account ID.
* @param {PlaidTransaction[]} plaidTranasctions - Plaid transactions
*/
public async syncAccountTranactions(
tenantId: number,
plaidAccountId: number,
plaidTranasctions: PlaidTransaction[]
): Promise<void> {
const { Account } = this.tenancy.models(tenantId);
const cashflowAccount = await Account.query()
.findOne({ plaidAccountId })
.throwIfNotFound();
const openingEquityBalance = await Account.query().findOne(
'slug',
'opening-balance-equity'
);
// Transformes the Plaid transactions to cashflow create DTOs.
const transformTransaction = transformPlaidTrxsToCashflowCreate(
cashflowAccount.id,
openingEquityBalance.id
);
const uncategorizedTransDTOs =
R.map(transformTransaction)(plaidTranasctions);
// Creating account transaction queue.
await bluebird.map(
uncategorizedTransDTOs,
(uncategoriedDTO) =>
this.cashflowApp.createUncategorizedTransaction(
tenantId,
uncategoriedDTO
),
{ concurrency: 1 }
);
}
/**
* Syncs the accounts transactions in paraller under controlled concurrency.
* @param {number} tenantId
* @param {PlaidTransaction[]} plaidTransactions
*/
public async syncAccountsTransactions(
tenantId: number,
plaidAccountsTransactions: PlaidTransaction[]
): Promise<void> {
const groupedTrnsxByAccountId = entries(
groupBy(plaidAccountsTransactions, 'account_id')
);
await bluebird.map(
groupedTrnsxByAccountId,
([plaidAccountId, plaidTransactions]: [number, PlaidTransaction[]]) => {
return this.syncAccountTranactions(
tenantId,
plaidAccountId,
plaidTransactions
);
},
{ concurrency: CONCURRENCY_ASYNC }
);
}
/**
* Syncs the removed Plaid transactions ids from the cashflow system transactions.
* @param {string[]} plaidTransactionsIds - Plaid Transactions IDs.
*/
public async syncRemoveTransactions(
tenantId: number,
plaidTransactionsIds: string[]
) {
const { CashflowTransaction } = this.tenancy.models(tenantId);
const cashflowTransactions = await CashflowTransaction.query().whereIn(
'plaidTransactionId',
plaidTransactionsIds
);
const cashflowTransactionsIds = cashflowTransactions.map(
(trans) => trans.id
);
await bluebird.map(
cashflowTransactionsIds,
(transactionId: number) =>
this.deleteCashflowTransactionService.deleteCashflowTransaction(
tenantId,
transactionId
),
{ concurrency: CONCURRENCY_ASYNC }
);
}
/**
* Syncs the Plaid item last transaction cursor.
* @param {number} tenantId - Tenant ID.
* @param {string} itemId - Plaid item ID.
* @param {string} lastCursor - Last transaction cursor.
*/
public async syncTransactionsCursor(
tenantId: number,
plaidItemId: string,
lastCursor: string
) {
const { PlaidItem } = this.tenancy.models(tenantId);
await PlaidItem.query().findOne({ plaidItemId }).patch({ lastCursor });
}
/**
* Updates the last feeds updated at of the given Plaid accounts ids.
* @param {number} tenantId
* @param {string[]} plaidAccountIds
*/
public async updateLastFeedsUpdatedAt(
tenantId: number,
plaidAccountIds: string[]
) {
const { Account } = this.tenancy.models(tenantId);
await Account.query().whereIn('plaid_account_id', plaidAccountIds).patch({
lastFeedsUpdatedAt: new Date(),
});
}
/**
* Updates the accounts feed active status of the given Plaid accounts ids.
* @param {number} tenantId
* @param {number[]} plaidAccountIds
* @param {boolean} isFeedsActive
*/
public async updateAccountsFeedsActive(
tenantId: number,
plaidAccountIds: string[],
isFeedsActive: boolean = true
) {
const { Account } = this.tenancy.models(tenantId);
await Account.query().whereIn('plaid_account_id', plaidAccountIds).patch({
isFeedsActive,
});
}
}

View File

@@ -0,0 +1,119 @@
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Inject, Service } from 'typedi';
import { PlaidClientWrapper } from '@/lib/Plaid/Plaid';
import { PlaidSyncDb } from './PlaidSyncDB';
import { PlaidFetchedTransactionsUpdates } from '@/interfaces';
@Service()
export class PlaidUpdateTransactions {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private plaidSync: PlaidSyncDb;
/**
* Handles the fetching and storing of new, modified, or removed transactions
* @param {number} tenantId Tenant ID.
* @param {string} plaidItemId the Plaid ID for the item.
*/
public async updateTransactions(tenantId: number, plaidItemId: string) {
// Fetch new transactions from plaid api.
const { added, modified, removed, cursor, accessToken } =
await this.fetchTransactionUpdates(tenantId, plaidItemId);
const request = { access_token: accessToken };
const plaidInstance = new PlaidClientWrapper();
const {
data: { accounts, item },
} = await plaidInstance.accountsGet(request);
const plaidAccountsIds = accounts.map((a) => a.account_id);
const {
data: { institution },
} = await plaidInstance.institutionsGetById({
institution_id: item.institution_id,
country_codes: ['US', 'UK'],
});
// Update the DB.
await this.plaidSync.syncBankAccounts(tenantId, accounts, institution);
await this.plaidSync.syncAccountsTransactions(
tenantId,
added.concat(modified)
);
await this.plaidSync.syncRemoveTransactions(tenantId, removed);
await this.plaidSync.syncTransactionsCursor(tenantId, plaidItemId, cursor);
// Update the last feeds updated at of the updated accounts.
await this.plaidSync.updateLastFeedsUpdatedAt(tenantId, plaidAccountsIds);
// Turn on the accounts feeds flag.
await this.plaidSync.updateAccountsFeedsActive(tenantId, plaidAccountsIds);
return {
addedCount: added.length,
modifiedCount: modified.length,
removedCount: removed.length,
};
}
/**
* Fetches transactions from the `Plaid API` for a given item.
* @param {number} tenantId - Tenant ID.
* @param {string} plaidItemId - The Plaid ID for the item.
* @returns {Promise<PlaidFetchedTransactionsUpdates>}
*/
private async fetchTransactionUpdates(
tenantId: number,
plaidItemId: string
): Promise<PlaidFetchedTransactionsUpdates> {
// the transactions endpoint is paginated, so we may need to hit it multiple times to
// retrieve all available transactions.
const { PlaidItem } = this.tenancy.models(tenantId);
const plaidItem = await PlaidItem.query().findOne(
'plaidItemId',
plaidItemId
);
if (!plaidItem) {
throw new Error('The given Plaid item id is not found.');
}
const { plaidAccessToken, lastCursor } = plaidItem;
let cursor = lastCursor;
// New transaction updates since "cursor"
let added = [];
let modified = [];
// Removed transaction ids
let removed = [];
let hasMore = true;
const batchSize = 100;
try {
// Iterate through each page of new transaction updates for item
/* eslint-disable no-await-in-loop */
while (hasMore) {
const request = {
access_token: plaidAccessToken,
cursor: cursor,
count: batchSize,
};
const plaidInstance = new PlaidClientWrapper();
const response = await plaidInstance.transactionsSync(request);
const data = response.data;
// Add this page of results
added = added.concat(data.added);
modified = modified.concat(data.modified);
removed = removed.concat(data.removed);
hasMore = data.has_more;
// Update cursor to the next cursor
cursor = data.next_cursor;
}
} catch (err) {
console.error(`Error fetching transactions: ${err.message}`);
cursor = lastCursor;
}
return { added, modified, removed, cursor, accessToken: plaidAccessToken };
}
}

View File

@@ -0,0 +1,32 @@
import { Request, Response, NextFunction } from 'express';
import { SystemPlaidItem, Tenant } from '@/system/models';
import tenantDependencyInjection from '@/api/middleware/TenantDependencyInjection';
export const PlaidWebhookTenantBootMiddleware = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { item_id: plaidItemId } = req.body;
const plaidItem = await SystemPlaidItem.query().findOne({ plaidItemId });
const notFoundOrganization = () => {
return res.boom.unauthorized('Organization identication not found.', {
errors: [{ type: 'ORGANIZATION.ID.NOT.FOUND', code: 100 }],
});
};
// In case the given organization not found.
if (!plaidItem) {
return notFoundOrganization();
}
const tenant = await Tenant.query()
.findById(plaidItem.tenantId)
.withGraphFetched('metadata');
// When the given organization id not found on the system storage.
if (!tenant) {
return notFoundOrganization();
}
tenantDependencyInjection(req, tenant);
next();
};

View File

@@ -0,0 +1,140 @@
import { Inject, Service } from 'typedi';
import { PlaidUpdateTransactions } from './PlaidUpdateTransactions';
@Service()
export class PlaidWebooks {
@Inject()
private updateTransactionsService: PlaidUpdateTransactions;
/**
* Listens to Plaid webhooks
* @param {number} tenantId - Tenant Id.
* @param {string} webhookType - Webhook type.
* @param {string} plaidItemId - Plaid item Id.
* @param {string} webhookCode - webhook code.
*/
public async webhooks(
tenantId: number,
plaidItemId: string,
webhookType: string,
webhookCode: string
): Promise<void> {
const _webhookType = webhookType.toLowerCase();
// There are five types of webhooks: AUTH, TRANSACTIONS, ITEM, INCOME, and ASSETS.
// @TODO implement handling for remaining webhook types.
const webhookHandlerMap = {
transactions: this.handleTransactionsWebooks.bind(this),
item: this.itemsHandler.bind(this),
};
const webhookHandler =
webhookHandlerMap[_webhookType] || this.unhandledWebhook;
await webhookHandler(tenantId, plaidItemId, webhookCode);
}
/**
* Handles all unhandled/not yet implemented webhook events.
* @param {string} webhookType
* @param {string} webhookCode
* @param {string} plaidItemId
*/
private async unhandledWebhook(
webhookType: string,
webhookCode: string,
plaidItemId: string
): Promise<void> {
console.log(
`UNHANDLED ${webhookType} WEBHOOK: ${webhookCode}: Plaid item id ${plaidItemId}: unhandled webhook type received.`
);
}
/**
* Logs to console and emits to socket
* @param {string} additionalInfo
* @param {string} webhookCode
* @param {string} plaidItemId
*/
private serverLogAndEmitSocket(
additionalInfo: string,
webhookCode: string,
plaidItemId: string
): void {
console.log(
`WEBHOOK: TRANSACTIONS: ${webhookCode}: Plaid_item_id ${plaidItemId}: ${additionalInfo}`
);
}
/**
* Handles all transaction webhook events. The transaction webhook notifies
* you that a single item has new transactions available.
* @param {number} tenantId
* @param {string} plaidItemId
* @param {string} webhookCode
* @returns {Promise<void>}
*/
public async handleTransactionsWebooks(
tenantId: number,
plaidItemId: string,
webhookCode: string
): Promise<void> {
switch (webhookCode) {
case 'SYNC_UPDATES_AVAILABLE': {
// Fired when new transactions data becomes available.
const { addedCount, modifiedCount, removedCount } =
await this.updateTransactionsService.updateTransactions(
tenantId,
plaidItemId
);
this.serverLogAndEmitSocket(
`Transactions: ${addedCount} added, ${modifiedCount} modified, ${removedCount} removed`,
webhookCode,
plaidItemId
);
break;
}
case 'DEFAULT_UPDATE':
case 'INITIAL_UPDATE':
case 'HISTORICAL_UPDATE':
/* ignore - not needed if using sync endpoint + webhook */
break;
default:
this.serverLogAndEmitSocket(
`unhandled webhook type received.`,
webhookCode,
plaidItemId
);
}
}
/**
* Handles all Item webhook events.
* @param {number} tenantId - Tenant ID
* @param {string} webhookCode - The webhook code
* @param {string} plaidItemId - The Plaid ID for the item
* @returns {Promise<void>}
*/
public async itemsHandler(
tenantId: number,
plaidItemId: string,
webhookCode: string
): Promise<void> {
switch (webhookCode) {
case 'WEBHOOK_UPDATE_ACKNOWLEDGED':
this.serverLogAndEmitSocket('is updated', plaidItemId, error);
break;
case 'ERROR': {
break;
}
case 'PENDING_EXPIRATION': {
break;
}
default:
this.serverLogAndEmitSocket(
'unhandled webhook type received.',
webhookCode,
plaidItemId
);
}
}
}

View File

@@ -0,0 +1,34 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import { IPlaidItemCreatedEventPayload } from '@/interfaces/Plaid';
import events from '@/subscribers/events';
@Service()
export class PlaidUpdateTransactionsOnItemCreatedSubscriber extends EventSubscriber {
@Inject('agenda')
private agenda: any;
/**
* Constructor method.
*/
public attach(bus) {
bus.subscribe(
events.plaid.onItemCreated,
this.handleUpdateTransactionsOnItemCreated
);
}
/**
* Updates the Plaid item transactions
* @param {IPlaidItemCreatedEventPayload} payload - Event payload.
*/
private handleUpdateTransactionsOnItemCreated = async ({
tenantId,
plaidItemId,
plaidAccessToken,
plaidInstitutionId,
}: IPlaidItemCreatedEventPayload) => {
const payload = { tenantId, plaidItemId };
await this.agenda.now('plaid-update-account-transactions', payload);
};
}

View File

@@ -0,0 +1,54 @@
import * as R from 'ramda';
import {
CreateUncategorizedTransactionDTO,
IAccountCreateDTO,
PlaidAccount,
PlaidTransaction,
} from '@/interfaces';
/**
* Transformes the Plaid account to create cashflow account DTO.
* @param {PlaidAccount} plaidAccount
* @returns {IAccountCreateDTO}
*/
export const transformPlaidAccountToCreateAccount = R.curry(
(institution: any, plaidAccount: PlaidAccount): IAccountCreateDTO => {
return {
name: `${institution.name} - ${plaidAccount.name}`,
code: '',
description: plaidAccount.official_name,
currencyCode: plaidAccount.balances.iso_currency_code,
accountType: 'cash',
active: true,
plaidAccountId: plaidAccount.account_id,
bankBalance: plaidAccount.balances.current,
accountMask: plaidAccount.mask,
};
}
);
/**
* Transformes the plaid transaction to cashflow create DTO.
* @param {number} cashflowAccountId - Cashflow account ID.
* @param {number} creditAccountId - Credit account ID.
* @param {PlaidTransaction} plaidTranasction - Plaid transaction.
* @returns {CreateUncategorizedTransactionDTO}
*/
export const transformPlaidTrxsToCashflowCreate = R.curry(
(
cashflowAccountId: number,
creditAccountId: number,
plaidTranasction: PlaidTransaction
): CreateUncategorizedTransactionDTO => {
return {
date: plaidTranasction.date,
amount: plaidTranasction.amount,
description: plaidTranasction.name,
payee: plaidTranasction.payment_meta?.payee,
currencyCode: plaidTranasction.iso_currency_code,
accountId: cashflowAccountId,
referenceNo: plaidTranasction.payment_meta?.reference_number,
plaidTransactionId: plaidTranasction.transaction_id,
};
}
);

View File

@@ -0,0 +1,213 @@
import { Inject, Service } from 'typedi';
import { DeleteCashflowTransaction } from './DeleteCashflowTransactionService';
import { UncategorizeCashflowTransaction } from './UncategorizeCashflowTransaction';
import { CategorizeCashflowTransaction } from './CategorizeCashflowTransaction';
import {
CategorizeTransactionAsExpenseDTO,
CreateUncategorizedTransactionDTO,
ICashflowAccountsFilter,
ICashflowNewCommandDTO,
ICategorizeCashflowTransactioDTO,
IGetUncategorizedTransactionsQuery,
} from '@/interfaces';
import { CategorizeTransactionAsExpense } from './CategorizeTransactionAsExpense';
import { GetUncategorizedTransactions } from './GetUncategorizedTransactions';
import { CreateUncategorizedTransaction } from './CreateUncategorizedTransaction';
import { GetUncategorizedTransaction } from './GetUncategorizedTransaction';
import NewCashflowTransactionService from './NewCashflowTransactionService';
import GetCashflowAccountsService from './GetCashflowAccountsService';
import { GetCashflowTransactionService } from './GetCashflowTransactionsService';
@Service()
export class CashflowApplication {
@Inject()
private createTransactionService: NewCashflowTransactionService;
@Inject()
private deleteTransactionService: DeleteCashflowTransaction;
@Inject()
private getCashflowAccountsService: GetCashflowAccountsService;
@Inject()
private getCashflowTransactionService: GetCashflowTransactionService;
@Inject()
private uncategorizeTransactionService: UncategorizeCashflowTransaction;
@Inject()
private categorizeTransactionService: CategorizeCashflowTransaction;
@Inject()
private categorizeAsExpenseService: CategorizeTransactionAsExpense;
@Inject()
private getUncategorizedTransactionsService: GetUncategorizedTransactions;
@Inject()
private getUncategorizedTransactionService: GetUncategorizedTransaction;
@Inject()
private createUncategorizedTransactionService: CreateUncategorizedTransaction;
/**
* Creates a new cashflow transaction.
* @param {number} tenantId
* @param {ICashflowNewCommandDTO} transactionDTO
* @param {number} userId
* @returns
*/
public createTransaction(
tenantId: number,
transactionDTO: ICashflowNewCommandDTO,
userId?: number
) {
return this.createTransactionService.newCashflowTransaction(
tenantId,
transactionDTO,
userId
);
}
/**
* Deletes the given cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @returns
*/
public deleteTransaction(tenantId: number, cashflowTransactionId: number) {
return this.deleteTransactionService.deleteCashflowTransaction(
tenantId,
cashflowTransactionId
);
}
/**
* Retrieves specific cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @returns
*/
public getTransaction(tenantId: number, cashflowTransactionId: number) {
return this.getCashflowTransactionService.getCashflowTransaction(
tenantId,
cashflowTransactionId
);
}
/**
* Retrieves the cashflow accounts.
* @param {number} tenantId
* @param {ICashflowAccountsFilter} filterDTO
* @returns
*/
public getCashflowAccounts(
tenantId: number,
filterDTO: ICashflowAccountsFilter
) {
return this.getCashflowAccountsService.getCashflowAccounts(
tenantId,
filterDTO
);
}
/**
* Creates a new uncategorized cash transaction.
* @param {number} tenantId
* @param {CreateUncategorizedTransactionDTO} createUncategorizedTransactionDTO
* @returns {IUncategorizedCashflowTransaction}
*/
public createUncategorizedTransaction(
tenantId: number,
createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO
) {
return this.createUncategorizedTransactionService.create(
tenantId,
createUncategorizedTransactionDTO
);
}
/**
* Uncategorize the given cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @returns
*/
public uncategorizeTransaction(
tenantId: number,
cashflowTransactionId: number
) {
return this.uncategorizeTransactionService.uncategorize(
tenantId,
cashflowTransactionId
);
}
/**
* Categorize the given cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @param {ICategorizeCashflowTransactioDTO} categorizeDTO
* @returns
*/
public categorizeTransaction(
tenantId: number,
cashflowTransactionId: number,
categorizeDTO: ICategorizeCashflowTransactioDTO
) {
return this.categorizeTransactionService.categorize(
tenantId,
cashflowTransactionId,
categorizeDTO
);
}
/**
* Categorizes the given cashflow transaction as expense transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @param {CategorizeTransactionAsExpenseDTO} transactionDTO
*/
public categorizeAsExpense(
tenantId: number,
cashflowTransactionId: number,
transactionDTO: CategorizeTransactionAsExpenseDTO
) {
return this.categorizeAsExpenseService.categorize(
tenantId,
cashflowTransactionId,
transactionDTO
);
}
/**
* Retrieves the uncategorized cashflow transactions.
* @param {number} tenantId
*/
public getUncategorizedTransactions(
tenantId: number,
accountId: number,
query: IGetUncategorizedTransactionsQuery
) {
return this.getUncategorizedTransactionsService.getTransactions(
tenantId,
accountId,
query
);
}
/**
* Retrieves specific uncategorized transaction.
* @param {number} tenantId
* @param {number} uncategorizedTransactionId
*/
public getUncategorizedTransaction(
tenantId: number,
uncategorizedTransactionId: number
) {
return this.getUncategorizedTransactionService.getTransaction(
tenantId,
uncategorizedTransactionId
);
}
}

View File

@@ -1,11 +1,9 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import * as R from 'ramda';
import {
ILedgerEntry,
ICashflowTransaction,
AccountNormal,
ICashflowTransactionLine,
} from '../../interfaces';
import {
transformCashflowTransactionType,

View File

@@ -0,0 +1,101 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import UnitOfWork from '../UnitOfWork';
import {
ICashflowTransactionCategorizedPayload,
ICashflowTransactionUncategorizingPayload,
ICategorizeCashflowTransactioDTO,
} from '@/interfaces';
import { Knex } from 'knex';
import { transformCategorizeTransToCashflow } from './utils';
import { CommandCashflowValidator } from './CommandCasflowValidator';
import NewCashflowTransactionService from './NewCashflowTransactionService';
import { TransferAuthorizationGuaranteeDecision } from 'plaid';
@Service()
export class CategorizeCashflowTransaction {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private uow: UnitOfWork;
@Inject()
private commandValidators: CommandCashflowValidator;
@Inject()
private createCashflow: NewCashflowTransactionService;
/**
* Categorize the given cashflow transaction.
* @param {number} tenantId
* @param {ICategorizeCashflowTransactioDTO} categorizeDTO
*/
public async categorize(
tenantId: number,
uncategorizedTransactionId: number,
categorizeDTO: ICategorizeCashflowTransactioDTO
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
// Retrieves the uncategorized transaction or throw an error.
const transaction = await UncategorizedCashflowTransaction.query()
.findById(uncategorizedTransactionId)
.throwIfNotFound();
// Validates the transaction shouldn't be categorized before.
this.commandValidators.validateTransactionShouldNotCategorized(transaction);
// Validate the uncateogirzed transaction if it's deposit the transaction direction
// should `IN` and the same thing if it's withdrawal the direction should be OUT.
this.commandValidators.validateUncategorizeTransactionType(
transaction,
categorizeDTO.transactionType
);
// Edits the cashflow transaction under UOW env.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onTransactionCategorizing` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionCategorizing,
{
tenantId,
trx,
} as ICashflowTransactionUncategorizingPayload
);
// Transformes the categorize DTO to the cashflow transaction.
const cashflowTransactionDTO = transformCategorizeTransToCashflow(
transaction,
categorizeDTO
);
// Creates a new cashflow transaction.
const cashflowTransaction =
await this.createCashflow.newCashflowTransaction(
tenantId,
cashflowTransactionDTO
);
// Updates the uncategorized transaction as categorized.
await UncategorizedCashflowTransaction.query(trx).patchAndFetchById(
uncategorizedTransactionId,
{
categorized: true,
categorizeRefType: 'CashflowTransaction',
categorizeRefId: cashflowTransaction.id,
}
);
// Triggers `onCashflowTransactionCategorized` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionCategorized,
{
tenantId,
// cashflowTransaction,
trx,
} as ICashflowTransactionCategorizedPayload
);
});
}
}

View File

@@ -0,0 +1,80 @@
import {
CategorizeTransactionAsExpenseDTO,
ICashflowTransactionCategorizedPayload,
} from '@/interfaces';
import { Inject, Service } from 'typedi';
import UnitOfWork from '../UnitOfWork';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import HasTenancyService from '../Tenancy/TenancyService';
import { Knex } from 'knex';
import { CreateExpense } from '../Expenses/CRUD/CreateExpense';
@Service()
export class CategorizeTransactionAsExpense {
@Inject()
private uow: UnitOfWork;
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private createExpenseService: CreateExpense;
/**
* Categorize the transaction as expense transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @param {CategorizeTransactionAsExpenseDTO} transactionDTO
*/
public async categorize(
tenantId: number,
cashflowTransactionId: number,
transactionDTO: CategorizeTransactionAsExpenseDTO
) {
const { CashflowTransaction } = this.tenancy.models(tenantId);
const transaction = await CashflowTransaction.query()
.findById(cashflowTransactionId)
.throwIfNotFound();
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onTransactionUncategorizing` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionCategorizingAsExpense,
{
tenantId,
trx,
} as ICashflowTransactionCategorizedPayload
);
// Creates a new expense transaction.
const expenseTransaction = await this.createExpenseService.newExpense(
tenantId,
{
},
1
);
// Updates the item on the storage and fetches the updated once.
const cashflowTransaction = await CashflowTransaction.query(
trx
).patchAndFetchById(cashflowTransactionId, {
categorizeRefType: 'Expense',
categorizeRefId: expenseTransaction.id,
uncategorized: true,
});
// Triggers `onTransactionUncategorized` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionCategorizedAsExpense,
{
tenantId,
cashflowTransaction,
trx,
} as ICashflowTransactionUncategorizedPayload
);
});
}
}

View File

@@ -1,9 +1,14 @@
import { Service } from 'typedi';
import { includes, camelCase, upperFirst } from 'lodash';
import { IAccount } from '@/interfaces';
import { IAccount, IUncategorizedCashflowTransaction } from '@/interfaces';
import { getCashflowTransactionType } from './utils';
import { ServiceError } from '@/exceptions';
import { CASHFLOW_TRANSACTION_TYPE, ERRORS } from './constants';
import {
CASHFLOW_DIRECTION,
CASHFLOW_TRANSACTION_TYPE,
ERRORS,
} from './constants';
import CashflowTransaction from '@/models/CashflowTransaction';
@Service()
export class CommandCashflowValidator {
@@ -46,4 +51,52 @@ export class CommandCashflowValidator {
}
return transformedType;
};
/**
* Validate the given transaction should be categorized.
* @param {CashflowTransaction} cashflowTransaction
*/
public validateTransactionShouldCategorized(
cashflowTransaction: CashflowTransaction
) {
if (!cashflowTransaction.uncategorize) {
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_CATEGORIZED);
}
}
/**
* Validate the given transcation shouldn't be categorized.
* @param {CashflowTransaction} cashflowTransaction
*/
public validateTransactionShouldNotCategorized(
cashflowTransaction: CashflowTransaction
) {
if (cashflowTransaction.uncategorize) {
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_CATEGORIZED);
}
}
/**
*
* @param {uncategorizeTransaction}
* @param {string} transactionType
* @throws {ServiceError(ERRORS.UNCATEGORIZED_TRANSACTION_TYPE_INVALID)}
*/
public validateUncategorizeTransactionType(
uncategorizeTransaction: IUncategorizedCashflowTransaction,
transactionType: string
) {
const type = getCashflowTransactionType(
upperFirst(camelCase(transactionType)) as CASHFLOW_TRANSACTION_TYPE
);
if (
(type.direction === CASHFLOW_DIRECTION.IN &&
uncategorizeTransaction.isDepositTransaction) ||
(type.direction === CASHFLOW_DIRECTION.OUT &&
uncategorizeTransaction.isWithdrawalTransaction)
) {
return;
}
throw new ServiceError(ERRORS.UNCATEGORIZED_TRANSACTION_TYPE_INVALID);
}
}

View File

@@ -0,0 +1,40 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork, { IsolationLevel } from '../UnitOfWork';
import { Knex } from 'knex';
import { CreateUncategorizedTransactionDTO } from '@/interfaces';
@Service()
export class CreateUncategorizedTransaction {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private uow: UnitOfWork;
/**
* Creates an uncategorized cashflow transaction.
* @param {number} tenantId
* @param {CreateUncategorizedTransactionDTO} createDTO
*/
public create(
tenantId: number,
createDTO: CreateUncategorizedTransactionDTO
) {
const { UncategorizedCashflowTransaction, Account } =
this.tenancy.models(tenantId);
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
const transaction = await UncategorizedCashflowTransaction.query(
trx
).insertAndFetch({
...createDTO,
});
return transaction;
},
);
}
}

View File

@@ -13,15 +13,15 @@ import UnitOfWork from '@/services/UnitOfWork';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
@Service()
export default class CommandCashflowTransactionService {
export class DeleteCashflowTransaction {
@Inject()
tenancy: HasTenancyService;
private tenancy: HasTenancyService;
@Inject()
eventPublisher: EventPublisher;
private eventPublisher: EventPublisher;
@Inject()
uow: UnitOfWork;
private uow: UnitOfWork;
/**
* Deletes the cashflow transaction with associated journal entries.

View File

@@ -4,17 +4,13 @@ import { CashflowTransactionTransformer } from './CashflowTransactionTransformer
import { ERRORS } from './constants';
import { ICashflowTransaction } from '@/interfaces';
import { ServiceError } from '@/exceptions';
import I18nService from '@/services/I18n/I18nService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
@Service()
export default class GetCashflowTransactionsService {
export class GetCashflowTransactionService {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private i18nService: I18nService;
@Inject()
private transfromer: TransformerInjectable;
@@ -35,6 +31,7 @@ export default class GetCashflowTransactionsService {
.withGraphFetched('entries.cashflowAccount')
.withGraphFetched('entries.creditAccount')
.withGraphFetched('transactions.account')
.orderBy('date', 'DESC')
.throwIfNotFound();
this.throwErrorCashflowTranscationNotFound(cashflowTransaction);

View File

@@ -0,0 +1,36 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { UncategorizedTransactionTransformer } from './UncategorizedTransactionTransformer';
@Service()
export class GetUncategorizedTransaction {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private transformer: TransformerInjectable;
/**
* Retrieves specific uncategorized cashflow transaction.
* @param {number} tenantId - Tenant id.
* @param {number} uncategorizedTransactionId - Uncategorized transaction id.
*/
public async getTransaction(
tenantId: number,
uncategorizedTransactionId: number
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
const transaction = await UncategorizedCashflowTransaction.query()
.findById(uncategorizedTransactionId)
.withGraphFetched('account')
.throwIfNotFound();
return this.transformer.transform(
tenantId,
transaction,
new UncategorizedTransactionTransformer()
);
}
}

View File

@@ -0,0 +1,51 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { UncategorizedTransactionTransformer } from './UncategorizedTransactionTransformer';
import { IGetUncategorizedTransactionsQuery } from '@/interfaces';
@Service()
export class GetUncategorizedTransactions {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private transformer: TransformerInjectable;
/**
* Retrieves the uncategorized cashflow transactions.
* @param {number} tenantId - Tenant id.
* @param {number} accountId - Account Id.
*/
public async getTransactions(
tenantId: number,
accountId: number,
query: IGetUncategorizedTransactionsQuery
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
// Parsed query with default values.
const _query = {
page: 1,
pageSize: 20,
...query,
};
const { results, pagination } =
await UncategorizedCashflowTransaction.query()
.where('accountId', accountId)
.where('categorized', false)
.withGraphFetched('account')
.orderBy('date', 'DESC')
.pagination(_query.page - 1, _query.pageSize);
const data = await this.transformer.transform(
tenantId,
results,
new UncategorizedTransactionTransformer()
);
return {
data,
pagination,
};
}
}

View File

@@ -1,11 +1,10 @@
import { Service, Inject } from 'typedi';
import { isEmpty, pick } from 'lodash';
import { pick } from 'lodash';
import { Knex } from 'knex';
import * as R from 'ramda';
import {
ICashflowNewCommandDTO,
ICashflowTransaction,
ICashflowTransactionLine,
ICommandCashflowCreatedPayload,
ICommandCashflowCreatingPayload,
ICashflowTransactionInput,
@@ -86,6 +85,8 @@ export default class NewCashflowTransactionService {
'cashflowAccountId',
'creditAccountId',
'branchId',
'plaidTransactionId',
'uncategorizedTransactionId',
]);
// Retreive the next invoice number.
const autoNextNumber =
@@ -124,8 +125,8 @@ export default class NewCashflowTransactionService {
public newCashflowTransaction = async (
tenantId: number,
newTransactionDTO: ICashflowNewCommandDTO,
userId: number
): Promise<{ cashflowTransaction: ICashflowTransaction }> => {
userId?: number
): Promise<ICashflowTransaction> => {
const { CashflowTransaction, Account } = this.tenancy.models(tenantId);
// Retrieves the cashflow account or throw not found error.
@@ -174,7 +175,7 @@ export default class NewCashflowTransactionService {
trx,
} as ICommandCashflowCreatedPayload
);
return { cashflowTransaction };
return cashflowTransaction;
});
};
}

View File

@@ -0,0 +1,72 @@
import { Knex } from 'knex';
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork from '../UnitOfWork';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import {
ICashflowTransactionUncategorizedPayload,
ICashflowTransactionUncategorizingPayload,
} from '@/interfaces';
@Service()
export class UncategorizeCashflowTransaction {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private uow: UnitOfWork;
/**
* Uncategorizes the given cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
*/
public async uncategorize(
tenantId: number,
uncategorizedTransactionId: number
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
const oldUncategorizedTransaction =
await UncategorizedCashflowTransaction.query()
.findById(uncategorizedTransactionId)
.throwIfNotFound();
// Updates the transaction under UOW.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onTransactionUncategorizing` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionUncategorizing,
{
tenantId,
trx,
} as ICashflowTransactionUncategorizingPayload
);
// Removes the ref relation with the related transaction.
const uncategorizedTransaction =
await UncategorizedCashflowTransaction.query(trx).updateAndFetchById(
uncategorizedTransactionId,
{
categorized: false,
categorizeRefId: null,
categorizeRefType: null,
}
);
// Triggers `onTransactionUncategorized` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionUncategorized,
{
tenantId,
uncategorizedTransaction,
oldUncategorizedTransaction,
trx,
} as ICashflowTransactionUncategorizedPayload
);
return uncategorizedTransaction;
});
}
}

View File

@@ -0,0 +1,65 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from '@/utils';
export class UncategorizedTransactionTransformer extends Transformer {
/**
* Include these attributes to sale invoice object.
* @returns {string[]}
*/
public includeAttributes = (): string[] => {
return [
'formattedAmount',
'formattedDate',
'formattetDepositAmount',
'formattedWithdrawalAmount',
];
};
/**
* Formattes the transaction date.
* @param transaction
* @returns {string}
*/
public formattedDate(transaction) {
return this.formatDate(transaction.date);
}
/**
* Formatted amount.
* @param transaction
* @returns {string}
*/
public formattedAmount(transaction) {
return formatNumber(transaction.amount, {
currencyCode: transaction.currencyCode,
});
}
/**
* Formatted deposit amount.
* @param transaction
* @returns {string}
*/
protected formattetDepositAmount(transaction) {
if (transaction.isDepositTransaction) {
return formatNumber(transaction.deposit, {
currencyCode: transaction.currencyCode,
});
}
return '';
}
/**
* Formatted withdrawal amount.
* @param transaction
* @returns {string}
*/
protected formattedWithdrawalAmount(transaction) {
if (transaction.isWithdrawalTransaction) {
return formatNumber(transaction.withdrawal, {
currencyCode: transaction.currencyCode,
});
}
return '';
}
}

View File

@@ -8,7 +8,11 @@ export const ERRORS = {
CREDIT_ACCOUNTS_IDS_NOT_FOUND: 'CREDIT_ACCOUNTS_IDS_NOT_FOUND',
CREDIT_ACCOUNTS_HAS_INVALID_TYPE: 'CREDIT_ACCOUNTS_HAS_INVALID_TYPE',
ACCOUNT_ID_HAS_INVALID_TYPE: 'ACCOUNT_ID_HAS_INVALID_TYPE',
ACCOUNT_HAS_ASSOCIATED_TRANSACTIONS: 'account_has_associated_transactions'
ACCOUNT_HAS_ASSOCIATED_TRANSACTIONS: 'account_has_associated_transactions',
TRANSACTION_ALREADY_CATEGORIZED: 'TRANSACTION_ALREADY_CATEGORIZED',
TRANSACTION_ALREADY_UNCATEGORIZED: 'TRANSACTION_ALREADY_UNCATEGORIZED',
UNCATEGORIZED_TRANSACTION_TYPE_INVALID: 'UNCATEGORIZED_TRANSACTION_TYPE_INVALID',
CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED: 'CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED'
};
export enum CASHFLOW_DIRECTION {

View File

@@ -0,0 +1,42 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import { ICashflowTransactionUncategorizedPayload } from '@/interfaces';
import { DeleteCashflowTransaction } from '../DeleteCashflowTransactionService';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service()
export class DeleteCashflowTransactionOnUncategorize {
@Inject()
private deleteCashflowTransactionService: DeleteCashflowTransaction;
/**
* Attaches events with handlers.
*/
public attach = (bus) => {
bus.subscribe(
events.cashflow.onTransactionUncategorized,
this.deleteCashflowTransactionOnUncategorize.bind(this)
);
};
/**
* Deletes the cashflow transaction on uncategorize transaction.
* @param {ICashflowTransactionUncategorizedPayload} payload
*/
public async deleteCashflowTransactionOnUncategorize({
tenantId,
oldUncategorizedTransaction,
trx,
}: ICashflowTransactionUncategorizedPayload) {
// Deletes the cashflow transaction.
if (
oldUncategorizedTransaction.categorizeRefType === 'CashflowTransaction'
) {
await this.deleteCashflowTransactionService.deleteCashflowTransaction(
tenantId,
oldUncategorizedTransaction.categorizeRefId
);
}
}
}

View File

@@ -0,0 +1,52 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import { ICommandCashflowDeletingPayload } from '@/interfaces';
import { ServiceError } from '@/exceptions';
import { ERRORS } from '../constants';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service()
export class PreventDeleteTransactionOnDelete {
@Inject()
private tenancy: HasTenancyService;
/**
* Attaches events with handlers.
*/
public attach = (bus) => {
bus.subscribe(
events.cashflow.onTransactionDeleting,
this.preventDeleteCashflowTransactionHasUncategorizedTransaction.bind(
this
)
);
};
/**
* Prevent delete cashflow transaction has converted from uncategorized transaction.
* @param {ICommandCashflowDeletingPayload} payload
*/
public async preventDeleteCashflowTransactionHasUncategorizedTransaction({
tenantId,
oldCashflowTransaction,
trx,
}: ICommandCashflowDeletingPayload) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
if (oldCashflowTransaction.uncategorizedTransactionId) {
const foundTransactions = await UncategorizedCashflowTransaction.query(
trx
).where({
categorized: true,
categorizeRefId: oldCashflowTransaction.id,
categorizeRefType: 'CashflowTransaction',
});
// Throw the error if the cashflow transaction still linked to uncategorized transaction.
if (foundTransactions.length > 0) {
throw new ServiceError(
ERRORS.CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED,
'Cannot delete cashflow transaction converted from uncategorized transaction.'
);
}
}
}
}

View File

@@ -1,13 +1,19 @@
import { upperFirst, camelCase } from 'lodash';
import { upperFirst, camelCase, omit } from 'lodash';
import {
CASHFLOW_TRANSACTION_TYPE,
CASHFLOW_TRANSACTION_TYPE_META,
ICashflowTransactionTypeMeta,
} from './constants';
import {
ICashflowNewCommandDTO,
ICashflowTransaction,
ICategorizeCashflowTransactioDTO,
IUncategorizedCashflowTransaction,
} from '@/interfaces';
/**
* Ensures the given transaction type to transformed to appropriate format.
* @param {string} type
* @param {string} type
* @returns {string}
*/
export const transformCashflowTransactionType = (type) => {
@@ -32,3 +38,30 @@ export function getCashflowTransactionType(
export const getCashflowAccountTransactionsTypes = () => {
return Object.values(CASHFLOW_TRANSACTION_TYPE_META).map((meta) => meta.type);
};
/**
* Tranasformes the given uncategorized transaction and categorized DTO
* to cashflow create DTO.
* @param {IUncategorizedCashflowTransaction} uncategorizeModel
* @param {ICategorizeCashflowTransactioDTO} categorizeDTO
* @returns {ICashflowNewCommandDTO}
*/
export const transformCategorizeTransToCashflow = (
uncategorizeModel: IUncategorizedCashflowTransaction,
categorizeDTO: ICategorizeCashflowTransactioDTO
): ICashflowNewCommandDTO => {
return {
date: uncategorizeModel.date,
referenceNo: categorizeDTO.referenceNo || uncategorizeModel.referenceNo,
description: categorizeDTO.description || uncategorizeModel.description,
cashflowAccountId: uncategorizeModel.accountId,
creditAccountId: categorizeDTO.creditAccountId,
exchangeRate: categorizeDTO.exchangeRate || 1,
currencyCode: uncategorizeModel.currencyCode,
amount: uncategorizeModel.amount,
transactionNumber: categorizeDTO.transactionNumber,
transactionType: categorizeDTO.transactionType,
uncategorizedTransactionId: uncategorizeModel.id,
publish: true,
};
};

View File

@@ -14,6 +14,7 @@ export class CreditNoteTransformer extends Transformer {
'formattedCreditNoteDate',
'formattedAmount',
'formattedCreditsUsed',
'formattedSubtotal',
'entries',
];
};
@@ -60,6 +61,15 @@ export class CreditNoteTransformer extends Transformer {
});
};
/**
* Retrieves the formatted subtotal.
* @param {ICreditNote} credit
* @returns {string}
*/
protected formattedSubtotal = (credit): string => {
return formatNumber(credit.amount, { money: false });
};
/**
* Retrieves the entries of the credit note.
* @param {ICreditNote} credit

View File

@@ -80,7 +80,7 @@ export default class EditCreditNote extends BaseCreditNotes {
} as ICreditNoteEditingPayload);
// Saves the credit note graph to the storage.
const creditNote = await CreditNote.query(trx).upsertGraph({
const creditNote = await CreditNote.query(trx).upsertGraphAndFetch({
id: creditNoteId,
...creditNoteModel,
});

View File

@@ -1,6 +1,7 @@
import { Inject, Service } from 'typedi';
import { ChromiumlyTenancy } from '../ChromiumlyTenancy/ChromiumlyTenancy';
import { TemplateInjectable } from '../TemplateInjectable/TemplateInjectable';
import GetCreditNote from './GetCreditNote';
@Service()
export default class GetCreditNotePdf {
@@ -10,11 +11,19 @@ export default class GetCreditNotePdf {
@Inject()
private templateInjectable: TemplateInjectable;
@Inject()
private getCreditNoteService: GetCreditNote;
/**
* Retrieve sale invoice pdf content.
* @param {} saleInvoice -
* @param {number} tenantId - Tenant id.
* @param {number} creditNoteId - Credit note id.
*/
public async getCreditNotePdf(tenantId: number, creditNote) {
public async getCreditNotePdf(tenantId: number, creditNoteId: number) {
const creditNote = await this.getCreditNoteService.getCreditNote(
tenantId,
creditNoteId
);
const htmlContent = await this.templateInjectable.render(
tenantId,
'modules/credit-note-standard',

View File

@@ -0,0 +1,21 @@
import { Inject } from 'typedi';
import { ExchangeRatesService } from './ExchangeRatesService';
import { EchangeRateLatestPOJO, ExchangeRateLatestDTO } from '@/interfaces';
export class ExchangeRateApplication {
@Inject()
private exchangeRateService: ExchangeRatesService;
/**
* Gets the latest exchange rate.
* @param {number} tenantId
* @param {ExchangeRateLatestDTO} exchangeRateLatestDTO
* @returns {Promise<EchangeRateLatestPOJO>}
*/
public latest(
tenantId: number,
exchangeRateLatestDTO: ExchangeRateLatestDTO
): Promise<EchangeRateLatestPOJO> {
return this.exchangeRateService.latest(tenantId, exchangeRateLatestDTO);
}
}

View File

@@ -1,193 +1,37 @@
import moment from 'moment';
import { difference } from 'lodash';
import { Service, Inject } from 'typedi';
import { ServiceError } from '@/exceptions';
import DynamicListingService from '@/services/DynamicListing/DynamicListService';
import {
EventDispatcher,
EventDispatcherInterface,
} from 'decorators/eventDispatcher';
import {
IExchangeRateDTO,
IExchangeRate,
IExchangeRatesService,
IExchangeRateEditDTO,
IExchangeRateFilter,
} from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
const ERRORS = {
NOT_FOUND_EXCHANGE_RATES: 'NOT_FOUND_EXCHANGE_RATES',
EXCHANGE_RATE_PERIOD_EXISTS: 'EXCHANGE_RATE_PERIOD_EXISTS',
EXCHANGE_RATE_NOT_FOUND: 'EXCHANGE_RATE_NOT_FOUND',
};
import { Service } from 'typedi';
import { ExchangeRate } from '@/lib/ExchangeRate/ExchangeRate';
import { ExchangeRateServiceType } from '@/lib/ExchangeRate/types';
import { EchangeRateLatestPOJO, ExchangeRateLatestDTO } from '@/interfaces';
import { TenantMetadata } from '@/system/models';
@Service()
export default class ExchangeRatesService implements IExchangeRatesService {
@Inject('logger')
logger: any;
@EventDispatcher()
eventDispatcher: EventDispatcherInterface;
@Inject()
tenancy: TenancyService;
@Inject()
dynamicListService: DynamicListingService;
export class ExchangeRatesService {
/**
* Creates a new exchange rate.
* Gets the latest exchange rate.
* @param {number} tenantId
* @param {IExchangeRateDTO} exchangeRateDTO
* @returns {Promise<IExchangeRate>}
* @param {number} exchangeRateLatestDTO
* @returns {EchangeRateLatestPOJO}
*/
public async newExchangeRate(
public async latest(
tenantId: number,
exchangeRateDTO: IExchangeRateDTO
): Promise<IExchangeRate> {
const { ExchangeRate } = this.tenancy.models(tenantId);
exchangeRateLatestDTO: ExchangeRateLatestDTO
): Promise<EchangeRateLatestPOJO> {
const organization = await TenantMetadata.query().findOne({ tenantId });
this.logger.info('[exchange_rates] trying to insert new exchange rate.', {
tenantId,
exchangeRateDTO,
});
await this.validateExchangeRatePeriodExistance(tenantId, exchangeRateDTO);
// Assign the organization base currency as a default currency
// if no currency is provided
const fromCurrency =
exchangeRateLatestDTO.fromCurrency || organization.baseCurrency;
const toCurrency =
exchangeRateLatestDTO.toCurrency || organization.baseCurrency;
const exchangeRate = await ExchangeRate.query().insertAndFetch({
...exchangeRateDTO,
date: moment(exchangeRateDTO.date).format('YYYY-MM-DD'),
});
this.logger.info('[exchange_rates] inserted successfully.', {
tenantId,
exchangeRateDTO,
});
return exchangeRate;
}
const exchange = new ExchangeRate(ExchangeRateServiceType.OpenExchangeRate);
const exchangeRate = await exchange.latest(fromCurrency, toCurrency);
/**
* Edits the exchange rate details.
* @param {number} tenantId - Tenant id.
* @param {number} exchangeRateId - Exchange rate id.
* @param {IExchangeRateEditDTO} editExRateDTO - Edit exchange rate DTO.
*/
public async editExchangeRate(
tenantId: number,
exchangeRateId: number,
editExRateDTO: IExchangeRateEditDTO
): Promise<void> {
const { ExchangeRate } = this.tenancy.models(tenantId);
this.logger.info('[exchange_rates] trying to edit exchange rate.', {
tenantId,
exchangeRateId,
editExRateDTO,
});
await this.validateExchangeRateExistance(tenantId, exchangeRateId);
await ExchangeRate.query()
.where('id', exchangeRateId)
.update({ ...editExRateDTO });
this.logger.info('[exchange_rates] exchange rate edited successfully.', {
tenantId,
exchangeRateId,
editExRateDTO,
});
}
/**
* Deletes the given exchange rate.
* @param {number} tenantId - Tenant id.
* @param {number} exchangeRateId - Exchange rate id.
*/
public async deleteExchangeRate(
tenantId: number,
exchangeRateId: number
): Promise<void> {
const { ExchangeRate } = this.tenancy.models(tenantId);
await this.validateExchangeRateExistance(tenantId, exchangeRateId);
await ExchangeRate.query().findById(exchangeRateId).delete();
}
/**
* Listing exchange rates details.
* @param {number} tenantId - Tenant id.
* @param {IExchangeRateFilter} exchangeRateFilter - Exchange rates list filter.
*/
public async listExchangeRates(
tenantId: number,
exchangeRateFilter: IExchangeRateFilter
): Promise<void> {
const { ExchangeRate } = this.tenancy.models(tenantId);
const dynamicFilter = await this.dynamicListService.dynamicList(
tenantId,
ExchangeRate,
exchangeRateFilter
);
// Retrieve exchange rates by the given query.
const exchangeRates = await ExchangeRate.query()
.onBuild((query) => {
dynamicFilter.buildQuery()(query);
})
.pagination(exchangeRateFilter.page - 1, exchangeRateFilter.pageSize);
return exchangeRates;
}
/**
* Validates period of the exchange rate existance.
* @param {number} tenantId - Tenant id.
* @param {IExchangeRateDTO} exchangeRateDTO - Exchange rate DTO.
* @return {Promise<void>}
*/
private async validateExchangeRatePeriodExistance(
tenantId: number,
exchangeRateDTO: IExchangeRateDTO
): Promise<void> {
const { ExchangeRate } = this.tenancy.models(tenantId);
this.logger.info('[exchange_rates] trying to validate period existance.', {
tenantId,
});
const foundExchangeRate = await ExchangeRate.query()
.where('currency_code', exchangeRateDTO.currencyCode)
.where('date', exchangeRateDTO.date);
if (foundExchangeRate.length > 0) {
this.logger.info('[exchange_rates] given exchange rate period exists.', {
tenantId,
});
throw new ServiceError(ERRORS.EXCHANGE_RATE_PERIOD_EXISTS);
}
}
/**
* Validate the given echange rate id existance.
* @param {number} tenantId - Tenant id.
* @param {number} exchangeRateId - Exchange rate id.
* @returns {Promise<void>}
*/
private async validateExchangeRateExistance(
tenantId: number,
exchangeRateId: number
) {
const { ExchangeRate } = this.tenancy.models(tenantId);
this.logger.info(
'[exchange_rates] trying to validate exchange rate id existance.',
{ tenantId, exchangeRateId }
);
const foundExchangeRate = await ExchangeRate.query().findById(
exchangeRateId
);
if (!foundExchangeRate) {
this.logger.info('[exchange_rates] exchange rate not found.', {
tenantId,
exchangeRateId,
});
throw new ServiceError(ERRORS.EXCHANGE_RATE_NOT_FOUND);
}
return {
baseCurrency: fromCurrency,
toCurrency: exchangeRateLatestDTO.toCurrency,
exchangeRate,
};
}
}

View File

@@ -136,7 +136,7 @@ export class EditExpense {
} as IExpenseEventEditingPayload);
// Upsert the expense object with expense entries.
const expense: IExpense = await Expense.query(trx).upsertGraph({
const expense: IExpense = await Expense.query(trx).upsertGraphAndFetch({
id: expenseId,
...expenseObj,
});

View File

@@ -0,0 +1,25 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { ExpenseCategory } from '@/models';
import { formatNumber } from '@/utils';
export class ExpenseCategoryTransformer extends Transformer {
/**
* Include these attributes to expense object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return ['amountFormatted'];
};
/**
* Retrieves the formatted amount.
* @param {ExpenseCategory} category
* @returns {string}
*/
protected amountFormatted(category: ExpenseCategory) {
return formatNumber(category.amount, {
currencyCode: this.context.currencyCode,
money: false,
});
}
}

View File

@@ -1,6 +1,7 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from 'utils';
import { IExpense } from '@/interfaces';
import { ExpenseCategoryTransformer } from './ExpenseCategoryTransformer';
export class ExpenseTransfromer extends Transformer {
/**
@@ -12,7 +13,8 @@ export class ExpenseTransfromer extends Transformer {
'formattedAmount',
'formattedLandedCostAmount',
'formattedAllocatedCostAmount',
'formattedDate'
'formattedDate',
'categories',
];
};
@@ -56,5 +58,16 @@ export class ExpenseTransfromer extends Transformer {
*/
protected formattedDate = (expense: IExpense): string => {
return this.formatDate(expense.paymentDate);
}
};
/**
* Retrieves the transformed expense categories.
* @param {IExpense} expense
* @returns {}
*/
protected categories = (expense: IExpense) => {
return this.item(expense.categories, new ExpenseCategoryTransformer(), {
currencyCode: expense.currencyCode,
});
};
}

View File

@@ -1,7 +1,7 @@
import { Knex } from 'knex';
import { Service, Inject } from 'typedi';
import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Service, Inject } from 'typedi';
import { ExpenseGLEntries } from './ExpenseGLEntries';
@Service()

View File

@@ -70,10 +70,10 @@ export class ExpensesWriteGLSubscriber {
authorizedUser,
trx,
}: IExpenseEventEditPayload) => {
// In case expense published, write journal entries.
if (expense.publishedAt) return;
// Cannot continue if the expense is not published.
if (!expense.publishedAt) return;
await this.expenseGLEntries.writeExpenseGLEntries(
await this.expenseGLEntries.rewriteExpenseGLEntries(
tenantId,
expense.id,
trx

View File

@@ -3,6 +3,7 @@ import { APAgingSummaryExportInjectable } from './APAgingSummaryExportInjectable
import { APAgingSummaryTableInjectable } from './APAgingSummaryTableInjectable';
import { IAPAgingSummaryQuery } from '@/interfaces';
import { APAgingSummaryService } from './APAgingSummaryService';
import { APAgingSummaryPdfInjectable } from './APAgingSummaryPdfInjectable';
@Service()
export class APAgingSummaryApplication {
@@ -15,6 +16,9 @@ export class APAgingSummaryApplication {
@Inject()
private APAgingSummarySheet: APAgingSummaryService;
@Inject()
private APAgingSumaryPdf: APAgingSummaryPdfInjectable;
/**
* Retrieve the A/P aging summary in sheet format.
* @param {number} tenantId
@@ -50,4 +54,14 @@ export class APAgingSummaryApplication {
public xlsx(tenantId: number, query: IAPAgingSummaryQuery) {
return this.APAgingSummaryExport.xlsx(tenantId, query);
}
/**
* Retrieves the A/P aging summary in pdf format.
* @param {number} tenantId
* @param {IAPAgingSummaryQuery} query
* @returns {Promise<Buffer>}
*/
public pdf(tenantId: number, query: IAPAgingSummaryQuery) {
return this.APAgingSumaryPdf.pdf(tenantId, query);
}
}

View File

@@ -0,0 +1,26 @@
import { Inject, Service } from 'typedi';
import { IAgingSummaryMeta, IAgingSummaryQuery } from '@/interfaces';
import { AgingSummaryMeta } from './AgingSummaryMeta';
@Service()
export class APAgingSummaryMeta {
@Inject()
private agingSummaryMeta: AgingSummaryMeta;
/**
* Retrieve the aging summary meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
public async meta(
tenantId: number,
query: IAgingSummaryQuery
): Promise<IAgingSummaryMeta> {
const commonMeta = await this.agingSummaryMeta.meta(tenantId, query);
return {
...commonMeta,
sheetName: 'A/P Aging Summary',
};
}
}

View File

@@ -0,0 +1,35 @@
import { Inject, Service } from 'typedi';
import { IAPAgingSummaryQuery } from '@/interfaces';
import { TableSheetPdf } from '../TableSheetPdf';
import { APAgingSummaryTableInjectable } from './APAgingSummaryTableInjectable';
import { HtmlTableCss } from './_constants';
@Service()
export class APAgingSummaryPdfInjectable {
@Inject()
private APAgingSummaryTable: APAgingSummaryTableInjectable;
@Inject()
private tableSheetPdf: TableSheetPdf;
/**
* Converts the given A/P aging summary sheet table to pdf.
* @param {number} tenantId - Tenant ID.
* @param {IAPAgingSummaryQuery} query - Balance sheet query.
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: IAPAgingSummaryQuery
): Promise<Buffer> {
const table = await this.APAgingSummaryTable.table(tenantId, query);
return this.tableSheetPdf.convertToPdf(
tenantId,
table.table,
table.meta.sheetName,
table.meta.formattedAsDate,
HtmlTableCss
);
}
}

View File

@@ -1,18 +1,19 @@
import moment from 'moment';
import { Inject, Service } from 'typedi';
import { isEmpty } from 'lodash';
import { IAPAgingSummaryQuery, IARAgingSummaryMeta } from '@/interfaces';
import { IAPAgingSummaryQuery, IAPAgingSummarySheet } from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
import APAgingSummarySheet from './APAgingSummarySheet';
import { Tenant } from '@/system/models';
import { APAgingSummaryMeta } from './APAgingSummaryMeta';
@Service()
export class APAgingSummaryService {
@Inject()
tenancy: TenancyService;
private tenancy: TenancyService;
@Inject('logger')
logger: any;
@Inject()
private APAgingSummaryMeta: APAgingSummaryMeta;
/**
* Default report query.
@@ -35,35 +36,16 @@ export class APAgingSummaryService {
};
}
/**
* Retrieve the balance sheet meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
reportMetadata(tenantId: number): IARAgingSummaryMeta {
const settings = this.tenancy.settings(tenantId);
const organizationName = settings.get({
group: 'organization',
key: 'name',
});
const baseCurrency = settings.get({
group: 'organization',
key: 'base_currency',
});
return {
organizationName,
baseCurrency,
};
}
/**
* Retrieve A/P aging summary report.
* @param {number} tenantId -
* @param {IAPAgingSummaryQuery} query -
* @returns {Promise<IAPAgingSummarySheet>}
*/
async APAgingSummary(tenantId: number, query: IAPAgingSummaryQuery) {
public async APAgingSummary(
tenantId: number,
query: IAPAgingSummaryQuery
): Promise<IAPAgingSummarySheet> {
const { Bill } = this.tenancy.models(tenantId);
const { vendorRepository } = this.tenancy.repositories(tenantId);
@@ -111,11 +93,14 @@ export class APAgingSummaryService {
const data = APAgingSummaryReport.reportData();
const columns = APAgingSummaryReport.reportColumns();
// Retrieve the aging summary report meta.
const meta = await this.APAgingSummaryMeta.meta(tenantId, filter);
return {
data,
columns,
query: filter,
meta: this.reportMetadata(tenantId),
meta,
};
}
}

View File

@@ -3,6 +3,7 @@ import { IARAgingSummaryQuery } from '@/interfaces';
import { ARAgingSummaryTableInjectable } from './ARAgingSummaryTableInjectable';
import { ARAgingSummaryExportInjectable } from './ARAgingSummaryExportInjectable';
import ARAgingSummaryService from './ARAgingSummaryService';
import { ARAgingSummaryPdfInjectable } from './ARAgingSummaryPdfInjectable';
@Service()
export class ARAgingSummaryApplication {
@@ -15,6 +16,9 @@ export class ARAgingSummaryApplication {
@Inject()
private ARAgingSummarySheet: ARAgingSummaryService;
@Inject()
private ARAgingSummaryPdf: ARAgingSummaryPdfInjectable;
/**
* Retrieve the A/R aging summary sheet.
* @param {number} tenantId
@@ -50,4 +54,14 @@ export class ARAgingSummaryApplication {
public csv(tenantId: number, query: IARAgingSummaryQuery) {
return this.ARAgingSummaryExport.csv(tenantId, query);
}
/**
* Retrieves the A/R aging summary in pdf format.
* @param {number} tenantId
* @param {IARAgingSummaryQuery} query
* @returns {Promise<Buffer>}
*/
public pdf(tenantId: number, query: IARAgingSummaryQuery) {
return this.ARAgingSummaryPdf.pdf(tenantId, query);
}
}

View File

@@ -0,0 +1,26 @@
import { Inject, Service } from 'typedi';
import { IAgingSummaryMeta, IAgingSummaryQuery } from '@/interfaces';
import { AgingSummaryMeta } from './AgingSummaryMeta';
@Service()
export class ARAgingSummaryMeta {
@Inject()
private agingSummaryMeta: AgingSummaryMeta;
/**
* Retrieve the aging summary meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
public async meta(
tenantId: number,
query: IAgingSummaryQuery
): Promise<IAgingSummaryMeta> {
const commonMeta = await this.agingSummaryMeta.meta(tenantId, query);
return {
...commonMeta,
sheetName: 'A/R Aging Summary',
};
}
}

View File

@@ -0,0 +1,35 @@
import { Inject, Service } from 'typedi';
import { IARAgingSummaryQuery } from '@/interfaces';
import { TableSheetPdf } from '../TableSheetPdf';
import { ARAgingSummaryTableInjectable } from './ARAgingSummaryTableInjectable';
import { HtmlTableCss } from './_constants';
@Service()
export class ARAgingSummaryPdfInjectable {
@Inject()
private ARAgingSummaryTable: ARAgingSummaryTableInjectable;
@Inject()
private tableSheetPdf: TableSheetPdf;
/**
* Converts the given balance sheet table to pdf.
* @param {number} tenantId - Tenant ID.
* @param {IBalanceSheetQuery} query - Balance sheet query.
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: IARAgingSummaryQuery
): Promise<Buffer> {
const table = await this.ARAgingSummaryTable.table(tenantId, query);
return this.tableSheetPdf.convertToPdf(
tenantId,
table.table,
table.meta.sheetName,
table.meta.formattedDateRange,
HtmlTableCss
);
}
}

View File

@@ -1,18 +1,19 @@
import moment from 'moment';
import { Inject, Service } from 'typedi';
import { isEmpty } from 'lodash';
import { IARAgingSummaryQuery, IARAgingSummaryMeta } from '@/interfaces';
import { IARAgingSummaryQuery } from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
import ARAgingSummarySheet from './ARAgingSummarySheet';
import { Tenant } from '@/system/models';
import { ARAgingSummaryMeta } from './ARAgingSummaryMeta';
@Service()
export default class ARAgingSummaryService {
@Inject()
tenancy: TenancyService;
private tenancy: TenancyService;
@Inject('logger')
logger: any;
@Inject()
private ARAgingSummaryMeta: ARAgingSummaryMeta;
/**
* Default report query.
@@ -35,29 +36,6 @@ export default class ARAgingSummaryService {
};
}
/**
* Retrieve the balance sheet meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
reportMetadata(tenantId: number): IARAgingSummaryMeta {
const settings = this.tenancy.settings(tenantId);
const organizationName = settings.get({
group: 'organization',
key: 'name',
});
const baseCurrency = settings.get({
group: 'organization',
key: 'base_currency',
});
return {
organizationName,
baseCurrency,
};
}
/**
* Retrieve A/R aging summary report.
* @param {number} tenantId - Tenant id.
@@ -110,11 +88,14 @@ export default class ARAgingSummaryService {
const data = ARAgingSummaryReport.reportData();
const columns = ARAgingSummaryReport.reportColumns();
// Retrieve the aging summary report meta.
const meta = await this.ARAgingSummaryMeta.meta(tenantId, filter);
return {
data,
columns,
query: filter,
meta: this.reportMetadata(tenantId),
meta,
};
}
}

View File

@@ -19,6 +19,7 @@ export class ARAgingSummaryTableInjectable {
query: IARAgingSummaryQuery
): Promise<IARAgingSummaryTable> {
const report = await this.ARAgingSummarySheet.ARAgingSummary(
tenantId,
query
);

View File

@@ -0,0 +1,30 @@
import { Inject } from 'typedi';
import { FinancialSheetMeta } from '../FinancialSheetMeta';
import { IAgingSummaryMeta, IAgingSummaryQuery } from '@/interfaces';
import moment from 'moment';
export class AgingSummaryMeta {
@Inject()
private financialSheetMeta: FinancialSheetMeta;
/**
* Retrieve the aging summary meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
public async meta(
tenantId: number,
query: IAgingSummaryQuery
): Promise<IAgingSummaryMeta> {
const commonMeta = await this.financialSheetMeta.meta(tenantId);
const formattedAsDate = moment(query.asDate).format('YYYY/MM/DD');
const formattedDateRange = `As ${formattedAsDate}`;
return {
...commonMeta,
sheetName: 'A/P Aging Summary',
formattedAsDate,
formattedDateRange,
};
}
}

View File

@@ -56,7 +56,7 @@ export default abstract class AgingSummaryTable extends R.compose(
node: IAgingSummaryContact | IAgingSummaryTotal
): ITableColumnAccessor[] => {
return node.aging.map((aging, index) => ({
key: 'aging',
key: 'aging_period',
accessor: `aging[${index}].total.formattedAmount`,
}));
};

View File

@@ -2,3 +2,20 @@ export enum AgingSummaryRowType {
Contact = 'contact',
Total = 'total',
}
export const HtmlTableCss = `
table tr.row-type--total td{
font-weight: 600;
border-top: 1px solid #bbb;
border-bottom: 3px double #333;
}
table .column--current,
table .column--aging_period,
table .column--total,
table .cell--current,
table .cell--aging_period,
table .cell--total {
text-align: right;
}
`;

View File

@@ -54,4 +54,14 @@ export class BalanceSheetApplication {
public csv(tenantId: number, query: IBalanceSheetQuery): Promise<string> {
return this.balanceSheetExport.csv(tenantId, query);
}
/**
* Retrieves the balance sheet in pdf format.
* @param {number} tenantId
* @param {IBalanceSheetQuery} query
* @returns {Promise<Buffer>}
*/
public pdf(tenantId: number, query: IBalanceSheetQuery) {
return this.balanceSheetExport.pdf(tenantId, query);
}
}

View File

@@ -2,12 +2,16 @@ import { Inject, Service } from 'typedi';
import { BalanceSheetTableInjectable } from './BalanceSheetTableInjectable';
import { TableSheet } from '@/lib/Xlsx/TableSheet';
import { IBalanceSheetQuery } from '@/interfaces';
import { BalanceSheetPdfInjectable } from './BalanceSheetPdfInjectable';
@Service()
export class BalanceSheetExportInjectable {
@Inject()
private balanceSheetTable: BalanceSheetTableInjectable;
@Inject()
private balanceSheetPdf: BalanceSheetPdfInjectable;
/**
* Retrieves the trial balance sheet in XLSX format.
* @param {number} tenantId
@@ -40,4 +44,17 @@ export class BalanceSheetExportInjectable {
return tableCsv;
}
/**
* Retrieves the balance sheet in pdf format.
* @param {number} tenantId
* @param {IBalanceSheetQuery} query
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: IBalanceSheetQuery
): Promise<Buffer> {
return this.balanceSheetPdf.pdf(tenantId, query);
}
}

View File

@@ -4,15 +4,12 @@ import {
IBalanceSheetStatementService,
IBalanceSheetQuery,
IBalanceSheetStatement,
IBalanceSheetMeta,
} from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
import Journal from '@/services/Accounting/JournalPoster';
import BalanceSheetStatement from './BalanceSheet';
import InventoryService from '@/services/Inventory/Inventory';
import { parseBoolean } from 'utils';
import { Tenant } from '@/system/models';
import BalanceSheetRepository from './BalanceSheetRepository';
import { BalanceSheetMetaInjectable } from './BalanceSheetMeta';
@Service()
export default class BalanceSheetStatementService
@@ -22,7 +19,7 @@ export default class BalanceSheetStatementService
private tenancy: TenancyService;
@Inject()
private inventoryService: InventoryService;
private balanceSheetMeta: BalanceSheetMetaInjectable;
/**
* Defaults balance sheet filter query.
@@ -62,33 +59,6 @@ export default class BalanceSheetStatementService
};
}
/**
* Retrieve the balance sheet meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
private reportMetadata(tenantId: number): IBalanceSheetMeta {
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 balance sheet statement.
* @param {number} tenantId
@@ -112,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.
@@ -122,12 +93,15 @@ export default class BalanceSheetStatementService
i18n
);
// Balance sheet data.
const balanceSheetData = balanceSheetInstanace.reportData();
const data = balanceSheetInstanace.reportData();
// Balance sheet meta.
const meta = await this.balanceSheetMeta.meta(tenantId, filter);
return {
data: balanceSheetData,
query: filter,
meta: this.reportMetadata(tenantId),
data,
meta,
};
}
}

View File

@@ -0,0 +1,32 @@
import { Inject, Service } from 'typedi';
import { FinancialSheetMeta } from '../FinancialSheetMeta';
import { IBalanceSheetMeta, IBalanceSheetQuery } from '@/interfaces';
import moment from 'moment';
@Service()
export class BalanceSheetMetaInjectable {
@Inject()
private financialSheetMeta: FinancialSheetMeta;
/**
* Retrieve the balance sheet meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
public async meta(
tenantId: number,
query: IBalanceSheetQuery
): Promise<IBalanceSheetMeta> {
const commonMeta = await this.financialSheetMeta.meta(tenantId);
const formattedAsDate = moment(query.toDate).format('YYYY/MM/DD');
const formattedDateRange = `As ${formattedAsDate}`;
const sheetName = 'Balance Sheet Statement';
return {
...commonMeta,
sheetName,
formattedAsDate,
formattedDateRange,
};
}
}

View File

@@ -0,0 +1,35 @@
import { Inject, Service } from 'typedi';
import { IBalanceSheetQuery } from '@/interfaces';
import { BalanceSheetTableInjectable } from './BalanceSheetTableInjectable';
import { TableSheetPdf } from '../TableSheetPdf';
import { HtmlTableCustomCss } from './constants';
@Service()
export class BalanceSheetPdfInjectable {
@Inject()
private balanceSheetTable: BalanceSheetTableInjectable;
@Inject()
private tableSheetPdf: TableSheetPdf;
/**
* Converts the given balance sheet table to pdf.
* @param {number} tenantId - Tenant ID.
* @param {IBalanceSheetQuery} query - Balance sheet query.
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: IBalanceSheetQuery
): Promise<Buffer> {
const table = await this.balanceSheetTable.table(tenantId, query);
return this.tableSheetPdf.convertToPdf(
tenantId,
table.table,
table.meta.sheetName,
table.meta.formattedDateRange,
HtmlTableCustomCss
);
}
}

View File

@@ -41,15 +41,16 @@ export default class BalanceSheetTable extends R.compose(
BalanceSheetBase
)(FinancialSheet) {
/**
* @param {}
* Balance sheet data.
* @param {IBalanceSheetStatementData}
*/
reportData: IBalanceSheetStatementData;
private reportData: IBalanceSheetStatementData;
/**
* Balance sheet query.
* @parma {}
* @parma {BalanceSheetQuery}
*/
query: BalanceSheetQuery;
private query: BalanceSheetQuery;
/**
* Constructor method.
@@ -215,13 +216,13 @@ export default class BalanceSheetTable extends R.compose(
/**
* Retrieves the total children columns.
* @returns {ITableColumn[]}
* @returns {ITableColumn[]}
*/
private totalColumnChildren = (): ITableColumn[] => {
return R.compose(
R.unless(
R.isEmpty,
R.concat([{ key: 'total', Label: this.i18n.__('balance_sheet.total') }])
R.concat([{ key: 'total', label: this.i18n.__('balance_sheet.total') }])
),
R.concat(this.percentageColumns()),
R.concat(this.getPreviousYearColumns()),

View File

@@ -12,3 +12,53 @@ export enum IROW_TYPE {
NET_INCOME = 'NET_INCOME',
TOTAL = 'TOTAL',
}
export const HtmlTableCustomCss = `
table tr.row-type--total td {
font-weight: 600;
border-top: 1px solid #bbb;
color: #000;
}
table tr.row-type--total.row-id--assets td,
table tr.row-type--total.row-id--liability-equity td {
border-bottom: 3px double #000;
}
table .column--name,
table .cell--name {
width: 400px;
}
table .column--total {
width: 25%;
}
table td.cell--total,
table td.cell--previous_year,
table td.cell--previous_year_change,
table td.cell--previous_year_percentage,
table td.cell--previous_period,
table td.cell--previous_period_change,
table td.cell--previous_period_percentage,
table td.cell--percentage_of_row,
table td.cell--percentage_of_column,
table td[class*="cell--date-range"] {
text-align: right;
}
table .column--total,
table .column--previous_year,
table .column--previous_year_change,
table .column--previous_year_percentage,
table .column--previous_period,
table .column--previous_period_change,
table .column--previous_period_percentage,
table .column--percentage_of_row,
table .column--percentage_of_column,
table [class*="column--date-range"] {
text-align: right;
}
`;

View File

@@ -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
};
}
}

View File

@@ -76,7 +76,7 @@ export default class CashFlowTable implements ICashFlowTable {
*/
private commonColumns = () => {
return R.compose(
R.concat([{ key: 'label', accessor: 'label' }]),
R.concat([{ key: 'name', accessor: 'label' }]),
R.when(
R.always(this.isDisplayColumnsBy(DISPLAY_COLUMNS_BY.DATE_PERIODS)),
R.concat(this.datePeriodsColumnsAccessors())

View File

@@ -3,6 +3,7 @@ import { CashflowExportInjectable } from './CashflowExportInjectable';
import { ICashFlowStatementQuery } from '@/interfaces';
import CashFlowStatementService from './CashFlowService';
import { CashflowTableInjectable } from './CashflowTableInjectable';
import { CashflowTablePdfInjectable } from './CashflowTablePdfInjectable';
@Service()
export class CashflowSheetApplication {
@@ -15,6 +16,9 @@ export class CashflowSheetApplication {
@Inject()
private cashflowTable: CashflowTableInjectable;
@Inject()
private cashflowPdf: CashflowTablePdfInjectable;
/**
* Retrieves the cashflow sheet
* @param {number} tenantId
@@ -55,4 +59,17 @@ export class CashflowSheetApplication {
): Promise<string> {
return this.cashflowExport.csv(tenantId, query);
}
/**
* Retrieves the cashflow sheet in pdf format.
* @param {number} tenantId
* @param {ICashFlowStatementQuery} query
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: ICashFlowStatementQuery
): Promise<Buffer> {
return this.cashflowPdf.pdf(tenantId, query);
}
}

View File

@@ -0,0 +1,36 @@
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<ICashFlowStatementMeta>}
*/
public async meta(
tenantId: number,
query: ICashFlowStatementQuery
): Promise<ICashFlowStatementMeta> {
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}`;
const sheetName = 'Statement of Cash Flow';
return {
...meta,
sheetName,
formattedToDate,
formattedFromDate,
formattedDateRange,
};
}
}

View File

@@ -0,0 +1,34 @@
import { Inject } from 'typedi';
import { CashflowTableInjectable } from './CashflowTableInjectable';
import { TableSheetPdf } from '../TableSheetPdf';
import { ICashFlowStatementQuery } from '@/interfaces';
import { HtmlTableCustomCss } from './constants';
export class CashflowTablePdfInjectable {
@Inject()
private cashflowTable: CashflowTableInjectable;
@Inject()
private tableSheetPdf: TableSheetPdf;
/**
* Converts the given cashflow sheet table to pdf.
* @param {number} tenantId - Tenant ID.
* @param {IBalanceSheetQuery} query - Balance sheet query.
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: ICashFlowStatementQuery
): Promise<Buffer> {
const table = await this.cashflowTable.table(tenantId, query);
return this.tableSheetPdf.convertToPdf(
tenantId,
table.table,
table.meta.sheetName,
table.meta.formattedDateRange,
HtmlTableCustomCss
);
}
}

View File

@@ -1,8 +1,33 @@
export const DISPLAY_COLUMNS_BY = {
DATE_PERIODS: 'date_periods',
TOTAL: 'total',
};
export const MAP_CONFIG = { childrenPath: 'children', pathFormat: 'array' };
export const MAP_CONFIG = { childrenPath: 'children', pathFormat: 'array' };
export const HtmlTableCustomCss = `
table tr.row-type--accounts td {
border-top: 1px solid #bbb;
}
table tr.row-id--cash-end-period td {
border-bottom: 3px double #333;
}
table tr.row-type--total {
font-weight: 600;
}
table tr.row-type--total td {
color: #000;
}
table tr.row-type--total:not(:first-child) td {
border-top: 1px solid #bbb;
}
table .column--name,
table .cell--name {
width: 400px;
}
table .column--total,
table .cell--total,
table [class*="column--date-range"],
table [class*="cell--date-range"] {
text-align: right;
}
`;

View File

@@ -3,6 +3,7 @@ import { CustomerBalanceSummaryExportInjectable } from './CustomerBalanceSummary
import { CustomerBalanceSummaryTableInjectable } from './CustomerBalanceSummaryTableInjectable';
import { ICustomerBalanceSummaryQuery } from '@/interfaces';
import { CustomerBalanceSummaryService } from './CustomerBalanceSummaryService';
import { CustomerBalanceSummaryPdf } from './CustomerBalanceSummaryPdf';
@Service()
export class CustomerBalanceSummaryApplication {
@@ -14,6 +15,9 @@ export class CustomerBalanceSummaryApplication {
@Inject()
private customerBalanceSummarySheet: CustomerBalanceSummaryService;
@Inject()
private customerBalanceSummaryPdf: CustomerBalanceSummaryPdf;
/**
* Retrieves the customer balance sheet in json format.
@@ -57,4 +61,14 @@ export class CustomerBalanceSummaryApplication {
public csv(tenantId: number, query: ICustomerBalanceSummaryQuery) {
return this.customerBalanceSummaryExport.csv(tenantId, query);
}
/**
* Retrieves the customer balance sheet in PDF format.
* @param {number} tenantId
* @param {ICustomerBalanceSummaryQuery} query
* @returns {Promise<Buffer>}
*/
public pdf(tenantId: number, query: ICustomerBalanceSummaryQuery) {
return this.customerBalanceSummaryPdf.pdf(tenantId, query);
}
}

View File

@@ -0,0 +1,35 @@
import moment from 'moment';
import { Inject, Service } from 'typedi';
import {
ICustomerBalanceSummaryMeta,
ICustomerBalanceSummaryQuery,
} from '@/interfaces';
import { FinancialSheetMeta } from '../FinancialSheetMeta';
@Service()
export class CustomerBalanceSummaryMeta {
@Inject()
private financialSheetMeta: FinancialSheetMeta;
/**
* Retrieves the customer balance summary meta.
* @param {number} tenantId
* @param {ICustomerBalanceSummaryQuery} query
* @returns {Promise<ICustomerBalanceSummaryMeta>}
*/
async meta(
tenantId: number,
query: ICustomerBalanceSummaryQuery
): Promise<ICustomerBalanceSummaryMeta> {
const commonMeta = await this.financialSheetMeta.meta(tenantId);
const formattedAsDate = moment(query.asDate).format('YYYY/MM/DD');
const formattedDateRange = `As ${formattedAsDate}`;
return {
...commonMeta,
sheetName: 'Customer Balance Summary',
formattedAsDate,
formattedDateRange,
};
}
}

View File

@@ -0,0 +1,36 @@
import { Inject, Service } from 'typedi';
import { ICustomerBalanceSummaryQuery } from '@/interfaces';
import { TableSheetPdf } from '../TableSheetPdf';
import { CustomerBalanceSummaryTableInjectable } from './CustomerBalanceSummaryTableInjectable';
import { HtmlTableCustomCss } from './constants';
@Service()
export class CustomerBalanceSummaryPdf {
@Inject()
private customerBalanceSummaryTable: CustomerBalanceSummaryTableInjectable;
@Inject()
private tableSheetPdf: TableSheetPdf;
/**
* Converts the given customer balance summary sheet table to pdf.
* @param {number} tenantId - Tenant ID.
* @param {IAPAgingSummaryQuery} query - Balance sheet query.
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: ICustomerBalanceSummaryQuery
): Promise<Buffer> {
const table = await this.customerBalanceSummaryTable.table(tenantId, query);
return this.tableSheetPdf.convertToPdf(
tenantId,
table.table,
table.meta.sheetName,
table.meta.formattedDateRange,
HtmlTableCustomCss
);
}
}

View File

@@ -1,4 +1,4 @@
import { Inject } from 'typedi';
import { Inject, Service } from 'typedi';
import moment from 'moment';
import * as R from 'ramda';
import {
@@ -12,13 +12,18 @@ import { CustomerBalanceSummaryReport } from './CustomerBalanceSummary';
import Ledger from '@/services/Accounting/Ledger';
import CustomerBalanceSummaryRepository from './CustomerBalanceSummaryRepository';
import { Tenant } from '@/system/models';
import { CustomerBalanceSummaryMeta } from './CustomerBalanceSummaryMeta';
@Service()
export class CustomerBalanceSummaryService
implements ICustomerBalanceSummaryService
{
@Inject()
private reportRepository: CustomerBalanceSummaryRepository;
@Inject()
private customerBalanceSummaryMeta: CustomerBalanceSummaryMeta;
/**
* Defaults balance sheet filter query.
* @return {ICustomerBalanceSummaryQuery}
@@ -96,10 +101,13 @@ export class CustomerBalanceSummaryService
filter,
tenant.metadata.baseCurrency
);
// Retrieve the customer balance summary meta.
const meta = await this.customerBalanceSummaryMeta.meta(tenantId, filter);
return {
data: report.reportData(),
query: filter,
meta,
};
}
}

View File

@@ -26,19 +26,20 @@ export class CustomerBalanceSummaryTableInjectable {
filter: ICustomerBalanceSummaryQuery
): Promise<ICustomerBalanceSummaryTable> {
const i18n = this.tenancy.i18n(tenantId);
const { data, query } =
const { data, query, meta } =
await this.customerBalanceSummaryService.customerBalanceSummary(
tenantId,
filter
);
const tableRows = new CustomerBalanceSummaryTable(data, filter, i18n);
const table = new CustomerBalanceSummaryTable(data, filter, i18n);
return {
table: {
columns: tableRows.tableColumns(),
rows: tableRows.tableRows(),
columns: table.tableColumns(),
rows: table.tableRows(),
},
query,
meta,
};
}
}

View File

@@ -52,7 +52,7 @@ export class CustomerBalanceSummaryTable {
*/
private getCustomerColumnsAccessor = (): IColumnMapperMeta[] => {
const columns = [
{ key: 'customerName', accessor: 'customerName' },
{ key: 'name', accessor: 'customerName' },
{ key: 'total', accessor: 'total.formattedAmount' },
];
return R.compose(
@@ -85,7 +85,7 @@ export class CustomerBalanceSummaryTable {
*/
private getTotalColumnsAccessor = (): IColumnMapperMeta[] => {
const columns = [
{ key: 'total', value: this.i18n.__('Total') },
{ key: 'name', value: this.i18n.__('Total') },
{ key: 'total', accessor: 'total.formattedAmount' },
];
return R.compose(

View File

@@ -0,0 +1,14 @@
export const HtmlTableCustomCss = `
table tr.row-type--total td {
font-weight: 600;
border-top: 1px solid #bbb;
border-bottom: 3px double #333;
}
table .column--name {
width: 65%;
}
table .column--total,
table .cell--total {
text-align: right;
}
`;

View File

@@ -0,0 +1,34 @@
import { TenantMetadata } from '@/system/models';
import { Inject, Service } from 'typedi';
import InventoryService from '../Inventory/Inventory';
import { IFinancialSheetCommonMeta } from '@/interfaces';
@Service()
export class FinancialSheetMeta {
@Inject()
private inventoryService: InventoryService;
/**
* Retrieves the common meta data of the financial sheet.
* @param {number} tenantId
* @returns {Promise<IFinancialSheetCommonMeta>}
*/
async meta(tenantId: number): Promise<IFinancialSheetCommonMeta> {
const tenantMetadata = await TenantMetadata.query().findOne({ tenantId });
const organizationName = tenantMetadata.name;
const baseCurrency = tenantMetadata.baseCurrency;
const dateFormat = tenantMetadata.dateFormat;
const isCostComputeRunning =
this.inventoryService.isItemsCostComputeRunning(tenantId);
return {
organizationName,
baseCurrency,
dateFormat,
isCostComputeRunning,
sheetName: '',
};
}
}

View File

@@ -61,14 +61,14 @@ export const FinancialSheetStructure = (Base: Class) =>
});
};
findNodeDeep = (nodes, callback) => {
public findNodeDeep = (nodes, callback) => {
return findValueDeep(nodes, callback, {
childrenPath: 'children',
pathFormat: 'array',
});
};
mapAccNodesDeep = (nodes, callback) => {
public mapAccNodesDeep = (nodes, callback) => {
return reduceDeep(
nodes,
(acc, value, key, parentValue, context) => {
@@ -97,11 +97,11 @@ export const FinancialSheetStructure = (Base: Class) =>
});
};
getTotalOfChildrenNodes = (node) => {
public getTotalOfChildrenNodes = (node) => {
return this.getTotalOfNodes(node.children);
};
getTotalOfNodes = (nodes) => {
public getTotalOfNodes = (nodes) => {
return sumBy(nodes, 'total.amount');
};
};

View File

@@ -10,6 +10,7 @@ import {
IContact,
} from '@/interfaces';
import FinancialSheet from '../FinancialSheet';
import moment from 'moment';
/**
* General ledger sheet.
@@ -88,8 +89,10 @@ export default class GeneralLedgerSheet extends FinancialSheet {
const newEntry = {
date: entry.date,
dateFormatted: moment(entry.date).format('YYYY MMM DD'),
entryId: entry.id,
transactionNumber: entry.transactionNumber,
referenceType: entry.referenceType,
referenceId: entry.referenceId,
referenceTypeFormatted: this.i18n.__(entry.referenceTypeFormatted),

View File

@@ -0,0 +1,83 @@
import { Inject } from 'typedi';
import {
IGeneralLedgerSheetQuery,
IGeneralLedgerTableData,
} from '@/interfaces';
import { GeneralLedgerTableInjectable } from './GeneralLedgerTableInjectable';
import { GeneralLedgerExportInjectable } from './GeneralLedgerExport';
import { GeneralLedgerService } from './GeneralLedgerService';
import { GeneralLedgerPdf } from './GeneralLedgerPdf';
export class GeneralLedgerApplication {
@Inject()
private GLTable: GeneralLedgerTableInjectable;
@Inject()
private GLExport: GeneralLedgerExportInjectable;
@Inject()
private GLSheet: GeneralLedgerService;
@Inject()
private GLPdf: GeneralLedgerPdf;
/**
* Retrieves the G/L sheet in json format.
* @param {number} tenantId
* @param {IGeneralLedgerSheetQuery} query
*/
public sheet(tenantId: number, query: IGeneralLedgerSheetQuery) {
return this.GLSheet.generalLedger(tenantId, query);
}
/**
* Retrieves the G/L sheet in table format.
* @param {number} tenantId
* @param {IGeneralLedgerSheetQuery} query
* @returns {Promise<IGeneralLedgerTableData>}
*/
public table(
tenantId: number,
query: IGeneralLedgerSheetQuery
): Promise<IGeneralLedgerTableData> {
return this.GLTable.table(tenantId, query);
}
/**
* Retrieves the G/L sheet in xlsx format.
* @param {number} tenantId
* @param {IGeneralLedgerSheetQuery} query
* @returns {}
*/
public xlsx(
tenantId: number,
query: IGeneralLedgerSheetQuery
): Promise<Buffer> {
return this.GLExport.xlsx(tenantId, query);
}
/**
* Retrieves the G/L sheet in csv format.
* @param {number} tenantId -
* @param {IGeneralLedgerSheetQuery} query -
*/
public csv(
tenantId: number,
query: IGeneralLedgerSheetQuery
): Promise<string> {
return this.GLExport.csv(tenantId, query);
}
/**
* Retrieves the G/L sheet in pdf format.
* @param {number} tenantId
* @param {IGeneralLedgerSheetQuery} query
* @returns {Promise<Buffer>}
*/
public pdf(
tenantId: number,
query: IGeneralLedgerSheetQuery
): Promise<Buffer> {
return this.GLPdf.pdf(tenantId, query);
}
}

View File

@@ -0,0 +1,43 @@
import { IGeneralLedgerSheetQuery } from '@/interfaces';
import { TableSheet } from '@/lib/Xlsx/TableSheet';
import { Inject, Service } from 'typedi';
import { GeneralLedgerTableInjectable } from './GeneralLedgerTableInjectable';
@Service()
export class GeneralLedgerExportInjectable {
@Inject()
private generalLedgerTable: GeneralLedgerTableInjectable;
/**
* Retrieves the general ledger sheet in XLSX format.
* @param {number} tenantId
* @param {IGeneralLedgerSheetQuery} query
* @returns {Promise<Buffer>}
*/
public async xlsx(tenantId: number, query: IGeneralLedgerSheetQuery) {
const table = await this.generalLedgerTable.table(tenantId, query);
const tableSheet = new TableSheet(table.table);
const tableCsv = tableSheet.convertToXLSX();
return tableSheet.convertToBuffer(tableCsv, 'xlsx');
}
/**
* Retrieves the general ledger sheet in CSV format.
* @param {number} tenantId
* @param {IGeneralLedgerSheetQuery} query
* @returns {Promise<Buffer>}
*/
public async csv(
tenantId: number,
query: IGeneralLedgerSheetQuery
): Promise<string> {
const table = await this.generalLedgerTable.table(tenantId, query);
const tableSheet = new TableSheet(table.table);
const tableCsv = tableSheet.convertToCSV();
return tableCsv;
}
}

View File

@@ -0,0 +1,33 @@
import { IGeneralLedgerMeta, IGeneralLedgerSheetQuery } from '@/interfaces';
import moment from 'moment';
import { Inject, Service } from 'typedi';
import { FinancialSheetMeta } from '../FinancialSheetMeta';
@Service()
export class GeneralLedgerMeta {
@Inject()
private financialSheetMeta: FinancialSheetMeta;
/**
* Retrieve the balance sheet meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
public async meta(
tenantId: number,
query: IGeneralLedgerSheetQuery
): Promise<IGeneralLedgerMeta> {
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,
};
}
}

View File

@@ -0,0 +1,35 @@
import { Inject, Service } from 'typedi';
import { TableSheetPdf } from '../TableSheetPdf';
import { GeneralLedgerTableInjectable } from './GeneralLedgerTableInjectable';
import { IGeneralLedgerSheetQuery } from '@/interfaces';
import { HtmlTableCustomCss } from './constants';
@Service()
export class GeneralLedgerPdf {
@Inject()
private generalLedgerTable: GeneralLedgerTableInjectable;
@Inject()
private tableSheetPdf: TableSheetPdf;
/**
* Converts the general ledger sheet table to pdf.
* @param {number} tenantId - Tenant ID.
* @param {IGeneralLedgerSheetQuery} query -
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: IGeneralLedgerSheetQuery
): Promise<Buffer> {
const table = await this.generalLedgerTable.table(tenantId, query);
return this.tableSheetPdf.convertToPdf(
tenantId,
table.table,
table.meta.sheetName,
table.meta.formattedDateRange,
HtmlTableCustomCss
);
}
}

View File

@@ -6,24 +6,21 @@ import { IGeneralLedgerSheetQuery, IGeneralLedgerMeta } from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
import Journal from '@/services/Accounting/JournalPoster';
import GeneralLedgerSheet from '@/services/FinancialStatements/GeneralLedger/GeneralLedger';
import InventoryService from '@/services/Inventory/Inventory';
import { transformToMap, parseBoolean } from 'utils';
import { transformToMap } from 'utils';
import { Tenant } from '@/system/models';
import { GeneralLedgerMeta } from './GeneralLedgerMeta';
const ERRORS = {
ACCOUNTS_NOT_FOUND: 'ACCOUNTS_NOT_FOUND',
};
@Service()
export default class GeneralLedgerService {
export class GeneralLedgerService {
@Inject()
tenancy: TenancyService;
private tenancy: TenancyService;
@Inject()
inventoryService: InventoryService;
@Inject('logger')
logger: any;
private generalLedgerMeta: GeneralLedgerMeta;
/**
* Defaults general ledger report filter query.
@@ -59,36 +56,8 @@ export default class GeneralLedgerService {
}
}
/**
* Retrieve the balance sheet meta.
* @param {number} tenantId -
* @returns {IGeneralLedgerMeta}
*/
reportMetadata(tenantId: number): IGeneralLedgerMeta {
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 general ledger report statement.
* ----------
* @param {number} tenantId
* @param {IGeneralLedgerSheetQuery} query
* @return {IGeneralLedgerStatement}
@@ -99,13 +68,10 @@ export default class GeneralLedgerService {
): Promise<{
data: any;
query: IGeneralLedgerSheetQuery;
meta: IGeneralLedgerMeta
meta: IGeneralLedgerMeta;
}> {
const {
accountRepository,
transactionsRepository,
contactRepository
} = this.tenancy.repositories(tenantId);
const { accountRepository, transactionsRepository, contactRepository } =
this.tenancy.repositories(tenantId);
const i18n = this.tenancy.i18n(tenantId);
@@ -129,13 +95,13 @@ export default class GeneralLedgerService {
const transactions = await transactionsRepository.journal({
fromDate: filter.fromDate,
toDate: filter.toDate,
branchesIds: filter.branchesIds
branchesIds: filter.branchesIds,
});
// Retreive opening balance credit/debit sumation.
const openingBalanceTrans = await transactionsRepository.journal({
toDate: moment(filter.fromDate).subtract(1, 'day'),
sumationCreditDebit: true,
branchesIds: filter.branchesIds
branchesIds: filter.branchesIds,
});
// Transform array transactions to journal collection.
const transactionsJournal = Journal.fromTransactions(
@@ -143,7 +109,7 @@ export default class GeneralLedgerService {
tenantId,
accountsGraph
);
// Accounts opening transactions.
// Accounts opening transactions.
const openingTransJournal = Journal.fromTransactions(
openingBalanceTrans,
tenantId,
@@ -163,10 +129,13 @@ export default class GeneralLedgerService {
// Retrieve general ledger report data.
const reportData = generalLedgerInstance.reportData();
// Retrieve general ledger report metadata.
const meta = await this.generalLedgerMeta.meta(tenantId, filter);
return {
data: reportData,
query: filter,
meta: this.reportMetadata(tenantId),
meta,
};
}
}

View File

@@ -0,0 +1,256 @@
import * as R from 'ramda';
import {
IColumnMapperMeta,
IGeneralLedgerMeta,
IGeneralLedgerSheetAccount,
IGeneralLedgerSheetAccountTransaction,
IGeneralLedgerSheetData,
IGeneralLedgerSheetQuery,
ITableColumn,
ITableColumnAccessor,
ITableRow,
} from '@/interfaces';
import FinancialSheet from '../FinancialSheet';
import { FinancialSheetStructure } from '../FinancialSheetStructure';
import { FinancialTable } from '../FinancialTable';
import { tableRowMapper } from '@/utils';
import { ROW_TYPE } from './utils';
export class GeneralLedgerTable extends R.compose(
FinancialTable,
FinancialSheetStructure
)(FinancialSheet) {
private data: IGeneralLedgerSheetData;
private query: IGeneralLedgerSheetQuery;
private meta: IGeneralLedgerMeta;
/**
* Creates an instance of `GeneralLedgerTable`.
* @param {IGeneralLedgerSheetData} data
* @param {IGeneralLedgerSheetQuery} query
*/
constructor(
data: IGeneralLedgerSheetData,
query: IGeneralLedgerSheetQuery,
meta: IGeneralLedgerMeta
) {
super();
this.data = data;
this.query = query;
this.meta = meta;
}
/**
* Retrieves the common table accessors.
* @returns {ITableColumnAccessor[]}
*/
private accountColumnsAccessors(): ITableColumnAccessor[] {
return [
{ key: 'date', accessor: 'name' },
{ key: 'account_name', accessor: '_empty_' },
{ key: 'reference_type', accessor: '_empty_' },
{ key: 'reference_number', accessor: '_empty_' },
{ key: 'description', accessor: 'description' },
{ key: 'credit', accessor: '_empty_' },
{ key: 'debit', accessor: '_empty_' },
{ key: 'amount', accessor: 'amount.formattedAmount' },
{ key: 'running_balance', accessor: 'closingBalance.formattedAmount' },
];
}
/**
* Retrieves the transaction column accessors.
* @returns {ITableColumnAccessor[]}
*/
private transactionColumnAccessors(): ITableColumnAccessor[] {
return [
{ key: 'date', accessor: 'dateFormatted' },
{ key: 'account_name', accessor: 'account.name' },
{ key: 'reference_type', accessor: 'referenceTypeFormatted' },
{ key: 'reference_number', accessor: 'transactionNumber' },
{ key: 'description', accessor: 'note' },
{ key: 'credit', accessor: 'formattedCredit' },
{ key: 'debit', accessor: 'formattedDebit' },
{ key: 'amount', accessor: 'formattedAmount' },
{ key: 'running_balance', accessor: 'formattedRunningBalance' },
];
}
/**
* Retrieves the opening row column accessors.
* @returns {ITableRowIColumnMapperMeta[]}
*/
private openingBalanceColumnsAccessors(): IColumnMapperMeta[] {
return [
{ key: 'date', value: this.meta.fromDate },
{ key: 'account_name', value: 'Opening Balance' },
{ key: 'reference_type', accessor: '_empty_' },
{ key: 'reference_number', accessor: '_empty_' },
{ key: 'description', accessor: 'description' },
{ key: 'credit', accessor: '_empty_' },
{ key: 'debit', accessor: '_empty_' },
{ key: 'amount', accessor: 'openingBalance.formattedAmount' },
{ key: 'running_balance', accessor: 'openingBalance.formattedAmount' },
];
}
/**
* Closing balance row column accessors.
* @returns {ITableColumnAccessor[]}
*/
private closingBalanceColumnAccessors(): IColumnMapperMeta[] {
return [
{ key: 'date', value: this.meta.toDate },
{ key: 'account_name', value: 'Closing Balance' },
{ key: 'reference_type', accessor: '_empty_' },
{ key: 'reference_number', accessor: '_empty_' },
{ key: 'description', accessor: '_empty_' },
{ key: 'credit', accessor: '_empty_' },
{ key: 'debit', accessor: '_empty_' },
{ key: 'amount', accessor: 'closingBalance.formattedAmount' },
{ key: 'running_balance', accessor: 'closingBalance.formattedAmount' },
];
}
/**
* Retrieves the common table columns.
* @returns {ITableColumn[]}
*/
private commonColumns(): ITableColumn[] {
return [
{ key: 'date', label: 'Date' },
{ key: 'account_name', label: 'Account Name' },
{ key: 'reference_type', label: 'Transaction Type' },
{ key: 'reference_number', label: 'Transaction #' },
{ key: 'description', label: 'Description' },
{ key: 'credit', label: 'Credit' },
{ key: 'debit', label: 'Debit' },
{ key: 'amount', label: 'Amount' },
{ key: 'running_balance', label: 'Running Balance' },
];
}
/**
* Maps the given transaction node to table row.
* @param {IGeneralLedgerSheetAccountTransaction} transaction
* @returns {ITableRow}
*/
private transactionMapper = R.curry(
(
account: IGeneralLedgerSheetAccount,
transaction: IGeneralLedgerSheetAccountTransaction
): ITableRow => {
const columns = this.transactionColumnAccessors();
const data = { ...transaction, account };
const meta = {
rowTypes: [ROW_TYPE.TRANSACTION],
};
return tableRowMapper(data, columns, meta);
}
);
/**
* Maps the given transactions nodes to table rows.
* @param {IGeneralLedgerSheetAccountTransaction[]} transactions
* @returns {ITableRow[]}
*/
private transactionsMapper = (
account: IGeneralLedgerSheetAccount
): ITableRow[] => {
const transactionMapper = this.transactionMapper(account);
return R.map(transactionMapper)(account.transactions);
};
/**
* Maps the given account node to opening balance table row.
* @param {IGeneralLedgerSheetAccount} account
* @returns {ITableRow}
*/
private openingBalanceMapper = (
account: IGeneralLedgerSheetAccount
): ITableRow => {
const columns = this.openingBalanceColumnsAccessors();
const meta = {
rowTypes: [ROW_TYPE.OPENING_BALANCE],
};
return tableRowMapper(account, columns, meta);
};
/**
* Maps the given account node to closing balance table row.
* @param {IGeneralLedgerSheetAccount} account
* @returns {ITableRow}
*/
private closingBalanceMapper = (account: IGeneralLedgerSheetAccount) => {
const columns = this.closingBalanceColumnAccessors();
const meta = {
rowTypes: [ROW_TYPE.CLOSING_BALANCE],
};
return tableRowMapper(account, columns, meta);
};
/**
* Maps the given account node to transactions table rows.
* @param {IGeneralLedgerSheetAccount} account
* @returns {ITableRow[]}
*/
private transactionsNode = (
account: IGeneralLedgerSheetAccount
): ITableRow[] => {
const openingBalance = this.openingBalanceMapper(account);
const transactions = this.transactionsMapper(account);
const closingBalance = this.closingBalanceMapper(account);
return R.when(
R.always(R.not(R.isEmpty(transactions))),
R.prepend(openingBalance)
)([...transactions, closingBalance]) as ITableRow[];
};
/**
* Maps the given account node to the table rows.
* @param {IGeneralLedgerSheetAccount} account
* @returns {ITableRow}
*/
private accountMapper = (account: IGeneralLedgerSheetAccount): ITableRow => {
const columns = this.accountColumnsAccessors();
const transactions = this.transactionsNode(account);
const meta = {
rowTypes: [ROW_TYPE.ACCOUNT],
};
const row = tableRowMapper(account, columns, meta);
return R.assoc('children', transactions)(row);
};
/**
* Maps the given account node to table rows.
* @param {IGeneralLedgerSheetAccount[]} accounts
* @returns {ITableRow[]}
*/
private accountsMapper = (
accounts: IGeneralLedgerSheetAccount[]
): ITableRow[] => {
return this.mapNodesDeep(accounts, this.accountMapper);
};
/**
* Retrieves the table rows.
* @returns {ITableRow[]}
*/
public tableRows(): ITableRow[] {
return R.compose(this.accountsMapper)(this.data);
}
/**
* Retrieves the table columns.
* @returns {ITableColumn[]}
*/
public tableColumns(): ITableColumn[] {
const columns = this.commonColumns();
return R.compose(this.tableColumnsCellIndexing)(columns);
}
}

View File

@@ -0,0 +1,45 @@
import {
IGeneralLedgerSheetQuery,
IGeneralLedgerTableData,
} from '@/interfaces';
import { Inject, Service } from 'typedi';
import { GeneralLedgerService } from './GeneralLedgerService';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { GeneralLedgerTable } from './GeneralLedgerTable';
@Service()
export class GeneralLedgerTableInjectable {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private GLSheet: GeneralLedgerService;
/**
* Retrieves the G/L table.
* @param {number} tenantId
* @param {IGeneralLedgerSheetQuery} query
* @returns {Promise<IGeneralLedgerTableData>}
*/
public async table(
tenantId: number,
query: IGeneralLedgerSheetQuery
): Promise<IGeneralLedgerTableData> {
const {
data: sheetData,
query: sheetQuery,
meta: sheetMeta,
} = await this.GLSheet.generalLedger(tenantId, query);
const table = new GeneralLedgerTable(sheetData, sheetQuery, sheetMeta);
return {
table: {
columns: table.tableColumns(),
rows: table.tableRows(),
},
query: sheetQuery,
meta: sheetMeta,
};
}
}

View File

@@ -0,0 +1,29 @@
export const HtmlTableCustomCss = `
table tr:last-child td {
border-bottom: 1px solid #ececec;
}
table tr.row-type--account td,
table tr.row-type--opening-balance td,
table tr.row-type--closing-balance td{
font-weight: 600;
}
table tr.row-type--closing-balance td {
border-bottom: 1px solid #ececec;
}
table .column--debit,
table .column--credit,
table .column--amount,
table .column--running_balance,
table .cell--debit,
table .cell--credit,
table .cell--amount,
table .cell--running_balance{
text-align: right;
}
table tr.row-type--account .cell--date span,
table tr.row-type--opening-balance .cell--account_name span,
table tr.row-type--closing-balance .cell--account_name span{
white-space: nowrap;
}
`;

View File

@@ -0,0 +1,6 @@
export enum ROW_TYPE {
ACCOUNT = 'ACCOUNT',
OPENING_BALANCE = 'OPENING_BALANCE',
TRANSACTION = 'TRANSACTION',
CLOSING_BALANCE = 'CLOSING_BALANCE',
}

View File

@@ -6,6 +6,7 @@ import { Inject, Service } from 'typedi';
import { InventoryDetailsExportInjectable } from './InventoryDetailsExportInjectable';
import { InventoryDetailsTableInjectable } from './InventoryDetailsTableInjectable';
import { InventoryDetailsService } from './InventoryDetailsService';
import { InventoryDetailsTablePdf } from './InventoryDetailsTablePdf';
@Service()
export class InventortyDetailsApplication {
@@ -18,6 +19,9 @@ export class InventortyDetailsApplication {
@Inject()
private inventoryDetails: InventoryDetailsService;
@Inject()
private inventoryDetailsPdf: InventoryDetailsTablePdf;
/**
* Retrieves the inventory details report in sheet format.
* @param {number} tenantId
@@ -63,4 +67,14 @@ export class InventortyDetailsApplication {
public csv(tenantId: number, query: IInventoryDetailsQuery): Promise<string> {
return this.inventoryDetailsExport.csv(tenantId, query);
}
/**
* Retrieves the inventory details report in PDF format.
* @param {number} tenantId
* @param {IInventoryDetailsQuery} query
* @returns {Promise<Buffer>}
*/
public pdf(tenantId: number, query: IInventoryDetailsQuery) {
return this.inventoryDetailsPdf.pdf(tenantId, query);
}
}

View File

@@ -0,0 +1,35 @@
import moment from 'moment';
import { Inject, Service } from 'typedi';
import { FinancialSheetMeta } from '../FinancialSheetMeta';
import { IInventoryDetailsQuery, IInventoryItemDetailMeta } from '@/interfaces';
@Service()
export class InventoryDetailsMetaInjectable {
@Inject()
private financialSheetMeta: FinancialSheetMeta;
/**
* Retrieve the inventoy details meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
public async meta(
tenantId: number,
query: IInventoryDetailsQuery
): Promise<IInventoryItemDetailMeta> {
const commonMeta = await this.financialSheetMeta.meta(tenantId);
const formattedFromDate = moment(query.fromDate).format('YYYY/MM/DD');
const formattedToDay = moment(query.toDate).format('YYYY/MM/DD');
const formattedDateRange = `From ${formattedFromDate} | To ${formattedToDay}`;
const sheetName = 'Inventory Item Details';
return {
...commonMeta,
sheetName,
formattedFromDate,
formattedToDay,
formattedDateRange,
};
}
}

View File

@@ -1,17 +1,12 @@
import moment from 'moment';
import { Service, Inject } from 'typedi';
import {
IInventoryDetailsQuery,
IInvetoryItemDetailDOO,
IInventoryItemDetailMeta,
} from '@/interfaces';
import { IInventoryDetailsQuery, IInvetoryItemDetailDOO } from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
import { InventoryDetails } from './InventoryDetails';
import FinancialSheet from '../FinancialSheet';
import InventoryDetailsRepository from './InventoryDetailsRepository';
import InventoryService from '@/services/Inventory/Inventory';
import { parseBoolean } from 'utils';
import { Tenant } from '@/system/models';
import { InventoryDetailsMetaInjectable } from './InventoryDetailsMeta';
@Service()
export class InventoryDetailsService extends FinancialSheet {
@@ -22,7 +17,7 @@ export class InventoryDetailsService extends FinancialSheet {
private reportRepo: InventoryDetailsRepository;
@Inject()
private inventoryService: InventoryService;
private inventoryDetailsMeta: InventoryDetailsMetaInjectable;
/**
* Defaults balance sheet filter query.
@@ -46,33 +41,6 @@ export class InventoryDetailsService extends FinancialSheet {
};
}
/**
* Retrieve the balance sheet meta.
* @param {number} tenantId -
* @returns {IInventoryItemDetailMeta}
*/
private reportMetadata(tenantId: number): IInventoryItemDetailMeta {
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 the inventory details report data.
* @param {number} tenantId -
@@ -115,11 +83,12 @@ export class InventoryDetailsService extends FinancialSheet {
tenant.metadata.baseCurrency,
i18n
);
const meta = await this.inventoryDetailsMeta.meta(tenantId, query);
return {
data: inventoryDetailsInstance.reportData(),
query: filter,
meta: this.reportMetadata(tenantId),
meta,
};
}
}

View File

@@ -0,0 +1,35 @@
import { Inject, Service } from 'typedi';
import { InventoryDetailsTableInjectable } from './InventoryDetailsTableInjectable';
import { TableSheetPdf } from '../TableSheetPdf';
import { IInventoryDetailsQuery } from '@/interfaces';
import { HtmlTableCustomCss } from './constant';
@Service()
export class InventoryDetailsTablePdf {
@Inject()
private inventoryDetailsTable: InventoryDetailsTableInjectable;
@Inject()
private tableSheetPdf: TableSheetPdf;
/**
* Converts the given inventory details sheet table to pdf.
* @param {number} tenantId - Tenant ID.
* @param {IBalanceSheetQuery} query - Balance sheet query.
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: IInventoryDetailsQuery
): Promise<Buffer> {
const table = await this.inventoryDetailsTable.table(tenantId, query);
return this.tableSheetPdf.convertToPdf(
tenantId,
table.table,
table.meta.sheetName,
table.meta.formattedDateRange,
HtmlTableCustomCss
);
}
}

View File

@@ -0,0 +1,7 @@
export const HtmlTableCustomCss = `
table tr.row-type--item td,
table tr.row-type--opening-entry td,
table tr.row-type--closing-entry td{
font-weight: 500;
}
`;

View File

@@ -11,7 +11,7 @@ import {
} from '@/interfaces';
import { allPassedConditionsPass, transformToMap } from 'utils';
export default class InventoryValuationSheet extends FinancialSheet {
export class InventoryValuationSheet extends FinancialSheet {
readonly query: IInventoryValuationReportQuery;
readonly items: IItem[];
readonly INInventoryCostLots: Map<number, InventoryCostLotTracker>;
@@ -259,6 +259,6 @@ export default class InventoryValuationSheet extends FinancialSheet {
const items = this.itemsSection();
const total = this.totalSection(items);
return items.length > 0 ? { items, total } : {};
return { items, total };
}
}

View File

@@ -0,0 +1,93 @@
import {
IInventoryValuationReportQuery,
IInventoryValuationSheet,
IInventoryValuationTable,
} from '@/interfaces';
import { Inject, Service } from 'typedi';
import { InventoryValuationSheetService } from './InventoryValuationSheetService';
import { InventoryValuationSheetTableInjectable } from './InventoryValuationSheetTableInjectable';
import { InventoryValuationSheetExportable } from './InventoryValuationSheetExportable';
import { InventoryValuationSheetPdf } from './InventoryValuationSheetPdf';
@Service()
export class InventoryValuationSheetApplication {
@Inject()
private inventoryValuationSheet: InventoryValuationSheetService;
@Inject()
private inventoryValuationTable: InventoryValuationSheetTableInjectable;
@Inject()
private inventoryValuationExport: InventoryValuationSheetExportable;
@Inject()
private inventoryValuationPdf: InventoryValuationSheetPdf;
/**
* Retrieves the inventory valuation json format.
* @param {number} tenantId
* @param {IInventoryValuationReportQuery} query
* @returns
*/
public sheet(
tenantId: number,
query: IInventoryValuationReportQuery
): Promise<IInventoryValuationSheet> {
return this.inventoryValuationSheet.inventoryValuationSheet(
tenantId,
query
);
}
/**
* Retrieves the inventory valuation json table format.
* @param {number} tenantId
* @param {IInventoryValuationReportQuery} query
* @returns {Promise<IInventoryValuationTable>}
*/
public table(
tenantId: number,
query: IInventoryValuationReportQuery
): Promise<IInventoryValuationTable> {
return this.inventoryValuationTable.table(tenantId, query);
}
/**
* Retrieves the inventory valuation xlsx format.
* @param {number} tenantId
* @param {IInventoryValuationReportQuery} query
* @returns
*/
public xlsx(
tenantId: number,
query: IInventoryValuationReportQuery
): Promise<Buffer> {
return this.inventoryValuationExport.xlsx(tenantId, query);
}
/**
* Retrieves the inventory valuation csv format.
* @param {number} tenantId
* @param {IInventoryValuationReportQuery} query
* @returns
*/
public csv(
tenantId: number,
query: IInventoryValuationReportQuery
): Promise<string> {
return this.inventoryValuationExport.csv(tenantId, query);
}
/**
* Retrieves the inventory valuation pdf format.
* @param {number} tenantId
* @param {IInventoryValuationReportQuery} query
* @returns {Promise<Buffer>}
*/
public pdf(
tenantId: number,
query: IInventoryValuationReportQuery
): Promise<Buffer> {
return this.inventoryValuationPdf.pdf(tenantId, query);
}
}

View File

@@ -0,0 +1,46 @@
import { Inject, Service } from 'typedi';
import { IInventoryValuationReportQuery } from '@/interfaces';
import { InventoryValuationSheetTableInjectable } from './InventoryValuationSheetTableInjectable';
import { TableSheet } from '@/lib/Xlsx/TableSheet';
@Service()
export class InventoryValuationSheetExportable {
@Inject()
private inventoryValuationTable: InventoryValuationSheetTableInjectable;
/**
* Retrieves the trial balance sheet in XLSX format.
* @param {number} tenantId
* @param {IInventoryValuationReportQuery} query
* @returns {Promise<Buffer>}
*/
public async xlsx(
tenantId: number,
query: IInventoryValuationReportQuery
): Promise<Buffer> {
const table = await this.inventoryValuationTable.table(tenantId, query);
const tableSheet = new TableSheet(table.table);
const tableCsv = tableSheet.convertToXLSX();
return tableSheet.convertToBuffer(tableCsv, 'xlsx');
}
/**
* Retrieves the trial balance sheet in CSV format.
* @param {number} tenantId
* @param {IInventoryValuationReportQuery} query
* @returns {Promise<Buffer>}
*/
public async csv(
tenantId: number,
query: IInventoryValuationReportQuery
): Promise<string> {
const table = await this.inventoryValuationTable.table(tenantId, query);
const tableSheet = new TableSheet(table.table);
const tableCsv = tableSheet.convertToCSV();
return tableCsv;
}
}

View File

@@ -0,0 +1,33 @@
import { Inject, Service } from 'typedi';
import { FinancialSheetMeta } from '../FinancialSheetMeta';
import { IBalanceSheetMeta, IBalanceSheetQuery, IInventoryValuationReportQuery } from '@/interfaces';
import moment from 'moment';
@Service()
export class InventoryValuationMetaInjectable {
@Inject()
private financialSheetMeta: FinancialSheetMeta;
/**
* Retrieve the balance sheet meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
public async meta(
tenantId: number,
query: IInventoryValuationReportQuery
): Promise<IBalanceSheetMeta> {
const commonMeta = await this.financialSheetMeta.meta(tenantId);
const formattedAsDate = moment(query.asDate).format('YYYY/MM/DD');
const formattedDateRange = `As ${formattedAsDate}`;
return {
...commonMeta,
sheetName: 'Inventory Valuation Sheet',
formattedAsDate,
formattedDateRange,
};
}
}

View File

@@ -0,0 +1,36 @@
import { Inject, Service } from "typedi";
import { InventoryValuationSheetTableInjectable } from "./InventoryValuationSheetTableInjectable";
import { TableSheetPdf } from "../TableSheetPdf";
import { IInventoryValuationReportQuery } from "@/interfaces";
import { HtmlTableCustomCss } from "./_constants";
@Service()
export class InventoryValuationSheetPdf {
@Inject()
private inventoryValuationTable: InventoryValuationSheetTableInjectable;
@Inject()
private tableSheetPdf: TableSheetPdf;
/**
* Converts the given balance sheet table to pdf.
* @param {number} tenantId - Tenant ID.
* @param {IBalanceSheetQuery} query - Balance sheet query.
* @returns {Promise<Buffer>}
*/
public async pdf(
tenantId: number,
query: IInventoryValuationReportQuery
): Promise<Buffer> {
const table = await this.inventoryValuationTable.table(tenantId, query);
return this.tableSheetPdf.convertToPdf(
tenantId,
table.table,
table.meta.sheetName,
table.meta.formattedDateRange,
HtmlTableCustomCss
);
}
}

View File

@@ -3,15 +3,17 @@ import moment from 'moment';
import { isEmpty } from 'lodash';
import {
IInventoryValuationReportQuery,
IInventoryValuationSheet,
IInventoryValuationSheetMeta,
} from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
import InventoryValuationSheet from './InventoryValuationSheet';
import { InventoryValuationSheet } from './InventoryValuationSheet';
import InventoryService from '@/services/Inventory/Inventory';
import { Tenant } from '@/system/models';
import { InventoryValuationMetaInjectable } from './InventoryValuationSheetMeta';
@Service()
export default class InventoryValuationSheetService {
export class InventoryValuationSheetService {
@Inject()
tenancy: TenancyService;
@@ -21,6 +23,9 @@ export default class InventoryValuationSheetService {
@Inject()
inventoryService: InventoryService;
@Inject()
private inventoryValuationMeta: InventoryValuationMetaInjectable;
/**
* Defaults balance sheet filter query.
* @return {IBalanceSheetQuery}
@@ -45,33 +50,6 @@ export default class InventoryValuationSheetService {
};
}
/**
* Retrieve the balance sheet meta.
* @param {number} tenantId -
* @returns {IBalanceSheetMeta}
*/
reportMetadata(tenantId: number): IInventoryValuationSheetMeta {
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 {
organizationName,
baseCurrency,
isCostComputeRunning,
};
}
/**
* Inventory valuation sheet.
* @param {number} tenantId - Tenant id.
@@ -80,7 +58,7 @@ export default class InventoryValuationSheetService {
public async inventoryValuationSheet(
tenantId: number,
query: IInventoryValuationReportQuery
) {
): Promise<IInventoryValuationSheet> {
const { Item, InventoryCostLotTracker } = this.tenancy.models(tenantId);
const tenant = await Tenant.query()
@@ -135,10 +113,13 @@ export default class InventoryValuationSheetService {
// Retrieve the inventory valuation report data.
const inventoryValuationData = inventoryValuationInstance.reportData();
// Retrieves the inventorty valuation meta.
const meta = await this.inventoryValuationMeta.meta(tenantId, filter);
return {
data: inventoryValuationData,
query: filter,
meta: this.reportMetadata(tenantId),
meta,
};
}
}

View File

@@ -0,0 +1,105 @@
import * as R from 'ramda';
import {
IInventoryValuationItem,
IInventoryValuationSheetData,
IInventoryValuationTotal,
ITableColumn,
ITableColumnAccessor,
ITableRow,
} from '@/interfaces';
import { tableRowMapper } from '@/utils';
import FinancialSheet from '../FinancialSheet';
import { FinancialSheetStructure } from '../FinancialSheetStructure';
import { FinancialTable } from '../FinancialTable';
import { ROW_TYPE } from './_constants';
export class InventoryValuationSheetTable extends R.compose(
FinancialTable,
FinancialSheetStructure
)(FinancialSheet) {
private readonly data: IInventoryValuationSheetData;
/**
* Constructor method.
* @param {IInventoryValuationSheetData} data
*/
constructor(data: IInventoryValuationSheetData) {
super();
this.data = data;
}
/**
* Retrieves the common columns accessors.
* @returns {ITableColumnAccessor}
*/
private commonColumnsAccessors(): ITableColumnAccessor[] {
return [
{ key: 'item_name', accessor: 'name' },
{ key: 'quantity', accessor: 'quantityFormatted' },
{ key: 'valuation', accessor: 'valuationFormatted' },
{ key: 'average', accessor: 'averageFormatted' },
];
}
/**
* Maps the given total node to table row.
* @param {IInventoryValuationTotal} total
* @returns {ITableRow}
*/
private totalRowMapper = (total: IInventoryValuationTotal): ITableRow => {
const accessors = this.commonColumnsAccessors();
const meta = {
rowTypes: [ROW_TYPE.TOTAL],
};
return tableRowMapper(total, accessors, meta);
};
/**
* Maps the given item node to table row.
* @param {IInventoryValuationItem} item
* @returns {ITableRow}
*/
private itemRowMapper = (item: IInventoryValuationItem): ITableRow => {
const accessors = this.commonColumnsAccessors();
const meta = {
rowTypes: [ROW_TYPE.ITEM],
};
return tableRowMapper(item, accessors, meta);
};
/**
* Maps the given items nodes to table rowes.
* @param {IInventoryValuationItem[]} items
* @returns {ITableRow[]}
*/
private itemsRowsMapper = (items: IInventoryValuationItem[]): ITableRow[] => {
return R.map(this.itemRowMapper)(items);
};
/**
* Retrieves the table rows.
* @returns {ITableRow[]}
*/
public tableRows(): ITableRow[] {
const itemsRows = this.itemsRowsMapper(this.data.items);
const totalRow = this.totalRowMapper(this.data.total);
return R.compose(
R.when(R.always(R.not(R.isEmpty(itemsRows))), R.append(totalRow))
)([...itemsRows]) as ITableRow[];
}
/**
* Retrieves the table columns.
* @returns {ITableColumn[]}
*/
public tableColumns(): ITableColumn[] {
const columns = [
{ key: 'item_name', label: 'Item Name' },
{ key: 'quantity', label: 'Quantity' },
{ key: 'valuation', label: 'Valuation' },
{ key: 'average', label: 'Average' },
];
return R.compose(this.tableColumnsCellIndexing)(columns);
}
}

View File

@@ -0,0 +1,39 @@
import { Inject, Service } from 'typedi';
import { InventoryValuationSheetService } from './InventoryValuationSheetService';
import {
IInventoryValuationReportQuery,
IInventoryValuationTable,
} from '@/interfaces';
import { InventoryValuationSheetTable } from './InventoryValuationSheetTable';
@Service()
export class InventoryValuationSheetTableInjectable {
@Inject()
private sheet: InventoryValuationSheetService;
/**
* Retrieves the inventory valuation json table format.
* @param {number} tenantId -
* @param {IInventoryValuationReportQuery} filter -
* @returns {Promise<IInventoryValuationTable>}
*/
public async table(
tenantId: number,
filter: IInventoryValuationReportQuery
): Promise<IInventoryValuationTable> {
const { data, query, meta } = await this.sheet.inventoryValuationSheet(
tenantId,
filter
);
const table = new InventoryValuationSheetTable(data);
return {
table: {
columns: table.tableColumns(),
rows: table.tableRows(),
},
query,
meta,
};
}
}

View File

@@ -0,0 +1,12 @@
export enum ROW_TYPE {
ITEM = 'ITEM',
TOTAL = 'TOTAL',
}
export const HtmlTableCustomCss = `
table tr.row-type--total td {
border-top: 1px solid #bbb;
font-weight: 600;
border-bottom: 3px double #000;
}
`;

View File

@@ -6,8 +6,10 @@ import {
IJournalReportQuery,
IJournalReport,
IContact,
IJournalTableData,
} from '@/interfaces';
import FinancialSheet from '../FinancialSheet';
import moment from 'moment';
export default class JournalSheet extends FinancialSheet {
readonly tenantId: number;
@@ -96,6 +98,8 @@ export default class JournalSheet extends FinancialSheet {
return {
date: groupEntry.date,
dateFormatted: moment(groupEntry.date).format('YYYY MMM DD'),
referenceType: groupEntry.referenceType,
referenceId: groupEntry.referenceId,
referenceTypeFormatted: this.i18n.__(groupEntry.referenceTypeFormatted),
@@ -131,7 +135,7 @@ export default class JournalSheet extends FinancialSheet {
* Retrieve journal report.
* @return {IJournalReport}
*/
reportData(): IJournalReport {
reportData(): IJournalTableData {
return this.entriesWalker(this.journal.entries);
}
}

Some files were not shown because too many files have changed in this diff Show More