mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 05:40:31 +00:00
feat: Add transactions by given reference report.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import {
|
||||
IAccount,
|
||||
IAccountTransaction,
|
||||
INumberFormatQuery,
|
||||
ITransactionsByReferenceQuery,
|
||||
ITransactionsByReferenceTransaction,
|
||||
} from 'interfaces';
|
||||
import FinancialSheet from '../FinancialSheet';
|
||||
|
||||
export default class TransactionsByReference extends FinancialSheet {
|
||||
readonly transactions: IAccountTransaction[];
|
||||
readonly query: ITransactionsByReferenceQuery;
|
||||
readonly baseCurrency: string;
|
||||
readonly numberFormat: INumberFormatQuery;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {IAccountTransaction[]} transactions
|
||||
* @param {ITransactionsByReferenceQuery} query
|
||||
* @param {string} baseCurrency
|
||||
*/
|
||||
constructor(
|
||||
transactions: (IAccountTransaction & { account: IAccount }) [],
|
||||
query: ITransactionsByReferenceQuery,
|
||||
baseCurrency: string
|
||||
) {
|
||||
super();
|
||||
|
||||
this.transactions = transactions;
|
||||
this.query = query;
|
||||
this.baseCurrency = baseCurrency;
|
||||
this.numberFormat = this.query.numberFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mappes the given account transaction to report transaction.
|
||||
* @param {IAccountTransaction} transaction
|
||||
* @returns {ITransactionsByReferenceTransaction}
|
||||
*/
|
||||
private transactionMapper = (
|
||||
transaction: IAccountTransaction
|
||||
): ITransactionsByReferenceTransaction => {
|
||||
return {
|
||||
credit: this.getAmountMeta(transaction.credit, { money: true }),
|
||||
debit: this.getAmountMeta(transaction.debit, { money: true }),
|
||||
|
||||
referenceTypeFormatted: transaction.referenceTypeFormatted,
|
||||
referenceType: transaction.referenceType,
|
||||
referenceId: transaction.referenceId,
|
||||
|
||||
contactId: transaction.contactId,
|
||||
contactType: transaction.contactType,
|
||||
contactTypeFormatted: transaction.contactType,
|
||||
|
||||
accountName: transaction.account.name,
|
||||
accountCode: transaction.account.code,
|
||||
accountId: transaction.accountId,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Mappes the given accounts transactions to report transactions.
|
||||
* @param {IAccountTransaction} transaction
|
||||
* @returns {ITransactionsByReferenceTransaction}
|
||||
*/
|
||||
private transactionsMapper = (
|
||||
transactions: IAccountTransaction[]
|
||||
): ITransactionsByReferenceTransaction[] => {
|
||||
return transactions.map(this.transactionMapper);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the report data.
|
||||
* @returns {ITransactionsByReferenceTransaction}
|
||||
*/
|
||||
public reportData(): ITransactionsByReferenceTransaction[] {
|
||||
return this.transactionsMapper(this.transactions);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import HasTenancyService from 'services/Tenancy/TenancyService';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { IAccount, IAccountTransaction, ITransactionsByReferenceQuery } from 'interfaces';
|
||||
|
||||
@Service()
|
||||
export default class TransactionsByReferenceRepository {
|
||||
@Inject()
|
||||
tenancy: HasTenancyService;
|
||||
|
||||
/**
|
||||
* Retrieve the accounts transactions of the givne reference id and type.
|
||||
* @param {number} tenantId -
|
||||
* @param {number} referenceId - Reference id.
|
||||
* @param {string} referenceType - Reference type.
|
||||
* @return {Promise<IAccountTransaction[]>}
|
||||
*/
|
||||
public getTransactions(
|
||||
tenantId: number,
|
||||
referenceId: number,
|
||||
referenceType: string,
|
||||
): Promise<(IAccountTransaction & { account: IAccount }) []> {
|
||||
const { AccountTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
return AccountTransaction.query()
|
||||
.where('reference_id', referenceId)
|
||||
.where('reference_type', referenceType)
|
||||
.withGraphFetched('account');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import HasTenancyService from 'services/Tenancy/TenancyService';
|
||||
import {
|
||||
ITransactionsByReferenceQuery,
|
||||
ITransactionsByReferenceTransaction,
|
||||
} from 'interfaces';
|
||||
import TransactionsByReferenceRepository from './TransactionsByReferenceRepository';
|
||||
import TransactionsByReferenceReport from './TransactionsByReferenceReport';
|
||||
|
||||
@Service()
|
||||
export default class TransactionsByReferenceService {
|
||||
@Inject()
|
||||
tenancy: HasTenancyService;
|
||||
|
||||
@Inject('logger')
|
||||
logger: any;
|
||||
|
||||
@Inject()
|
||||
reportRepository: TransactionsByReferenceRepository;
|
||||
|
||||
/**
|
||||
* Default query of transactions by reference report.
|
||||
*/
|
||||
get defaultQuery(): ITransactionsByReferenceQuery {
|
||||
return {
|
||||
numberFormat: {
|
||||
precision: 2,
|
||||
divideOn1000: false,
|
||||
showZero: false,
|
||||
formatMoney: 'total',
|
||||
negativeFormat: 'mines',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve accounts transactions by given reference id and type.
|
||||
* @param {number} tenantId
|
||||
* @param {ITransactionsByReferenceQuery} filter
|
||||
*/
|
||||
public async getTransactionsByReference(
|
||||
tenantId: number,
|
||||
query: ITransactionsByReferenceQuery
|
||||
): Promise<{
|
||||
data: ITransactionsByReferenceTransaction[];
|
||||
}> {
|
||||
const filter = {
|
||||
...this.defaultQuery,
|
||||
...query,
|
||||
};
|
||||
|
||||
// Retrieve the accounts transactions of the given reference.
|
||||
const transactions = await this.reportRepository.getTransactions(
|
||||
tenantId,
|
||||
filter.referenceId,
|
||||
filter.referenceType
|
||||
);
|
||||
|
||||
// Settings tenant service.
|
||||
const settings = this.tenancy.settings(tenantId);
|
||||
const baseCurrency = settings.get({
|
||||
group: 'organization',
|
||||
key: 'base_currency',
|
||||
});
|
||||
|
||||
// Transactions by reference report.
|
||||
const report = new TransactionsByReferenceReport(
|
||||
transactions,
|
||||
filter,
|
||||
baseCurrency
|
||||
);
|
||||
|
||||
return {
|
||||
data: report.reportData(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user