mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
WIP: transactions by customers report.
This commit is contained in:
@@ -71,8 +71,7 @@ export default class FinancialStatementsService {
|
|||||||
router.use(
|
router.use(
|
||||||
'/transactions-by-customers',
|
'/transactions-by-customers',
|
||||||
Container.get(TransactionsByCustomers).router(),
|
Container.get(TransactionsByCustomers).router(),
|
||||||
)
|
);
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,15 @@ import { Inject } from 'typedi';
|
|||||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||||
import BaseFinancialReportController from '../BaseFinancialReportController';
|
import BaseFinancialReportController from '../BaseFinancialReportController';
|
||||||
import TransactionsByCustomersService from 'services/FinancialStatements/TransactionsByCustomer/TransactionsByCustomersService';
|
import TransactionsByCustomersService from 'services/FinancialStatements/TransactionsByCustomer/TransactionsByCustomersService';
|
||||||
|
import TransactionsByCustomersTableRows from 'services/FinancialStatements/TransactionsByCustomer/TransactionsByCustomersTableRows';
|
||||||
|
|
||||||
export default class TransactionsByCustomersReportController extends BaseFinancialReportController {
|
export default class TransactionsByCustomersReportController extends BaseFinancialReportController {
|
||||||
@Inject()
|
@Inject()
|
||||||
transactionsByCustomersService: TransactionsByCustomersService;
|
transactionsByCustomersService: TransactionsByCustomersService;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
transactionsByCustomersTableRows: TransactionsByCustomersTableRows;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Router constructor.
|
* Router constructor.
|
||||||
*/
|
*/
|
||||||
@@ -60,6 +64,12 @@ export default class TransactionsByCustomersReportController extends BaseFinanci
|
|||||||
filter
|
filter
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return res.status(200).send({
|
||||||
|
table: {
|
||||||
|
rows: this.transactionsByCustomersTableRows.tableRows(data),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return res.status(200).send({
|
return res.status(200).send({
|
||||||
data: this.transfromToResponse(data),
|
data: this.transfromToResponse(data),
|
||||||
columns: this.transfromToResponse(columns),
|
columns: this.transfromToResponse(columns),
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { INumberFormatQuery } from './FinancialStatements';
|
|||||||
export interface ITransactionsByCustomersAmount {
|
export interface ITransactionsByCustomersAmount {
|
||||||
amount: number;
|
amount: number;
|
||||||
formattedAmount: string;
|
formattedAmount: string;
|
||||||
|
currencyCode: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ITransactionsByCustomersTransaction {
|
export interface ITransactionsByCustomersTransaction {
|
||||||
@@ -10,9 +11,11 @@ export interface ITransactionsByCustomersTransaction {
|
|||||||
credit: ITransactionsByCustomersAmount;
|
credit: ITransactionsByCustomersAmount;
|
||||||
debit: ITransactionsByCustomersAmount;
|
debit: ITransactionsByCustomersAmount;
|
||||||
runningBalance: ITransactionsByCustomersAmount;
|
runningBalance: ITransactionsByCustomersAmount;
|
||||||
|
currencyCode: string;
|
||||||
referenceNumber: string;
|
referenceNumber: string;
|
||||||
transactionNumber: string;
|
transactionNumber: string;
|
||||||
}
|
createdAt: string|Date,
|
||||||
|
};
|
||||||
|
|
||||||
export interface ITransactionsByCustomersCustomer {
|
export interface ITransactionsByCustomersCustomer {
|
||||||
customerName: string;
|
customerName: string;
|
||||||
@@ -29,9 +32,7 @@ export interface ITransactionsByCustomersFilter {
|
|||||||
noneZero: boolean;
|
noneZero: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ITransactionsByCustomersData {
|
export type ITransactionsByCustomersData = ITransactionsByCustomersCustomer[];
|
||||||
customers: ITransactionsByCustomersCustomer[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ITransactionsByCustomersStatement {
|
export interface ITransactionsByCustomersStatement {
|
||||||
data: ITransactionsByCustomersData;
|
data: ITransactionsByCustomersData;
|
||||||
|
|||||||
@@ -1,22 +1,34 @@
|
|||||||
import * as R from 'ramda';
|
import * as R from 'ramda';
|
||||||
|
import { sumBy } from 'lodash';
|
||||||
import FinancialSheet from '../FinancialSheet';
|
import FinancialSheet from '../FinancialSheet';
|
||||||
import { IAccountTransaction, ICustomer } from 'interfaces';
|
import {
|
||||||
import { transaction } from 'objection';
|
ITransactionsByCustomersTransaction,
|
||||||
|
ITransactionsByCustomersFilter,
|
||||||
|
ITransactionsByCustomersCustomer,
|
||||||
|
ITransactionsByCustomersAmount,
|
||||||
|
ITransactionsByCustomersData,
|
||||||
|
INumberFormatQuery,
|
||||||
|
IAccountTransaction,
|
||||||
|
ICustomer,
|
||||||
|
} from 'interfaces';
|
||||||
|
|
||||||
export default class TransactionsByCustomers extends FinancialSheet {
|
export default class TransactionsByCustomers extends FinancialSheet {
|
||||||
customers: ICustomer[];
|
readonly customers: ICustomer[];
|
||||||
transactionsByContact: any;
|
readonly transactionsByContact: any;
|
||||||
baseCurrency: string;
|
readonly filter: ITransactionsByCustomersFilter;
|
||||||
|
readonly baseCurrency: string;
|
||||||
|
readonly numberFormat: INumberFormatQuery;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor method.
|
* Constructor method.
|
||||||
* @param {ICustomer} customers
|
* @param {ICustomer} customers
|
||||||
* @param transactionsByContact
|
* @param {Map<number, IAccountTransaction[]>} transactionsByContact
|
||||||
* @param {string} baseCurrency
|
* @param {string} baseCurrency
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
customers: ICustomer[],
|
customers: ICustomer[],
|
||||||
transactionsByContact: Map<number, IAccountTransaction[]>,
|
transactionsByContact: Map<number, IAccountTransaction[]>,
|
||||||
|
filter: ITransactionsByCustomersFilter,
|
||||||
baseCurrency: string
|
baseCurrency: string
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
@@ -24,17 +36,24 @@ export default class TransactionsByCustomers extends FinancialSheet {
|
|||||||
this.customers = customers;
|
this.customers = customers;
|
||||||
this.transactionsByContact = transactionsByContact;
|
this.transactionsByContact = transactionsByContact;
|
||||||
this.baseCurrency = baseCurrency;
|
this.baseCurrency = baseCurrency;
|
||||||
|
this.filter = filter;
|
||||||
|
this.numberFormat = this.filter.numberFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Customer transaction mapper.
|
||||||
|
* @param {any} transaction -
|
||||||
|
* @return {Omit<ITransactionsByCustomersTransaction, 'runningBalance'>}
|
||||||
*/
|
*/
|
||||||
private customerTransactionMapper(
|
private customerTransactionMapper(
|
||||||
transaction
|
transaction
|
||||||
): ITransactionsByCustomersTransaction {
|
): Omit<ITransactionsByCustomersTransaction, 'runningBalance'> {
|
||||||
|
const currencyCode = 'USD';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
credit: transaction.credit,
|
credit: this.getCustomerAmount(transaction.credit, currencyCode),
|
||||||
debit: transaction.debit,
|
debit: this.getCustomerAmount(transaction.debit, currencyCode),
|
||||||
|
currencyCode: 'USD',
|
||||||
transactionNumber: transaction.transactionNumber,
|
transactionNumber: transaction.transactionNumber,
|
||||||
referenceNumber: transaction.referenceNumber,
|
referenceNumber: transaction.referenceNumber,
|
||||||
date: transaction.date,
|
date: transaction.date,
|
||||||
@@ -42,39 +61,144 @@ export default class TransactionsByCustomers extends FinancialSheet {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customer transactions mapper with running balance.
|
||||||
|
* @param {number} openingBalance
|
||||||
|
* @param {ITransactionsByCustomersTransaction[]} transactions
|
||||||
|
* @returns {ITransactionsByCustomersTransaction[]}
|
||||||
|
*/
|
||||||
private customerTransactionRunningBalance(
|
private customerTransactionRunningBalance(
|
||||||
openingBalance: number,
|
openingBalance: number,
|
||||||
transaction: ITransactionsByCustomersTransaction
|
transactions: Omit<ITransactionsByCustomersTransaction, 'runningBalance'>[]
|
||||||
): ITransactionsByCustomersTransaction {
|
): any {
|
||||||
|
let _openingBalance = openingBalance;
|
||||||
|
|
||||||
|
return transactions.map(
|
||||||
|
(transaction: ITransactionsByCustomersTransaction) => {
|
||||||
|
_openingBalance += transaction.debit.amount;
|
||||||
|
_openingBalance -= transaction.credit.amount;
|
||||||
|
|
||||||
|
const runningBalance = this.getCustomerAmount(
|
||||||
|
_openingBalance,
|
||||||
|
transaction.currencyCode
|
||||||
|
);
|
||||||
|
return { ...transaction, runningBalance };
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private customerTransactions(customerId: number) {
|
/**
|
||||||
|
* Retrieve the customer transactions from the given customer id and opening balance.
|
||||||
|
* @param {number} customerId - Customer id.
|
||||||
|
* @param {number} openingBalance - Opening balance amount.
|
||||||
|
* @returns {ITransactionsByCustomersTransaction[]}
|
||||||
|
*/
|
||||||
|
private customerTransactions(
|
||||||
|
customerId: number,
|
||||||
|
openingBalance: number
|
||||||
|
): ITransactionsByCustomersTransaction[] {
|
||||||
const transactions = this.transactionsByContact.get(customerId + '') || [];
|
const transactions = this.transactionsByContact.get(customerId + '') || [];
|
||||||
|
|
||||||
return R.compose(
|
return R.compose(
|
||||||
R.map(this.customerTransactionMapper),
|
R.curry(this.customerTransactionRunningBalance)(openingBalance),
|
||||||
R.map(R.curry(this.customerTransactionRunningBalance(0)))
|
R.map(this.customerTransactionMapper.bind(this))
|
||||||
).bind(this)(transactions);
|
).bind(this)(transactions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private customerMapper(customer: ICustomer) {
|
/**
|
||||||
|
* Retrieve the customer closing balance from the given transactions and opening balance.
|
||||||
|
* @param {number} customerTransactions
|
||||||
|
* @param {number} openingBalance
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
private getCustomerClosingBalance(
|
||||||
|
customerTransactions: ITransactionsByCustomersTransaction[],
|
||||||
|
openingBalance: number
|
||||||
|
): number {
|
||||||
|
const closingBalance = openingBalance;
|
||||||
|
|
||||||
|
const totalCredit = sumBy(customerTransactions, 'credit');
|
||||||
|
const totalDebit = sumBy(customerTransactions, 'debit');
|
||||||
|
|
||||||
|
return closingBalance + (totalDebit - totalCredit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the given customer opening balance from the given customer id.
|
||||||
|
* @param {number} customerId
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
private getCustomerOpeningBalance(customerId: number): number {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customer section mapper.
|
||||||
|
* @param {ICustomer} customer
|
||||||
|
* @returns {ITransactionsByCustomersCustomer}
|
||||||
|
*/
|
||||||
|
private customerMapper(
|
||||||
|
customer: ICustomer
|
||||||
|
): ITransactionsByCustomersCustomer {
|
||||||
|
const openingBalance = this.getCustomerOpeningBalance(1);
|
||||||
|
const transactions = this.customerTransactions(customer.id, openingBalance);
|
||||||
|
const closingBalance = this.getCustomerClosingBalance(transactions, 0);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
customerName: customer.displayName,
|
customerName: customer.displayName,
|
||||||
openingBalance: {},
|
openingBalance: this.getCustomerAmount(
|
||||||
closingBalance: {},
|
openingBalance,
|
||||||
transactions: this.customerTransactions(customer.id),
|
customer.currencyCode
|
||||||
|
),
|
||||||
|
closingBalance: this.getCustomerAmount(
|
||||||
|
closingBalance,
|
||||||
|
customer.currencyCode
|
||||||
|
),
|
||||||
|
transactions,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private customersMapper(customers: ICustomer[]) {
|
/**
|
||||||
return customers.map(this.customerMapper.bind(this));
|
* Retrieve the customer amount format meta.
|
||||||
|
* @param {number} amount
|
||||||
|
* @param {string} currencyCode
|
||||||
|
* @returns {ITransactionsByCustomersAmount}
|
||||||
|
*/
|
||||||
|
private getCustomerAmount(
|
||||||
|
amount: number,
|
||||||
|
currencyCode: string
|
||||||
|
): ITransactionsByCustomersAmount {
|
||||||
|
return {
|
||||||
|
amount,
|
||||||
|
formattedAmount: this.formatNumber(amount, { currencyCode }),
|
||||||
|
currencyCode,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public reportData() {
|
/**
|
||||||
|
* Retrieve the customers sections of the report.
|
||||||
|
* @param {ICustomer[]} customers
|
||||||
|
* @returns {ITransactionsByCustomersCustomer[]}
|
||||||
|
*/
|
||||||
|
private customersMapper(
|
||||||
|
customers: ICustomer[]
|
||||||
|
): ITransactionsByCustomersCustomer[] {
|
||||||
|
return R.compose(R.map(this.customerMapper.bind(this))).bind(this)(
|
||||||
|
customers
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the report data.
|
||||||
|
* @returns {ITransactionsByCustomersData}
|
||||||
|
*/
|
||||||
|
public reportData(): ITransactionsByCustomersData {
|
||||||
return this.customersMapper(this.customers);
|
return this.customersMapper(this.customers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the report columns.
|
||||||
|
*/
|
||||||
public reportColumns() {
|
public reportColumns() {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ import {
|
|||||||
} from 'interfaces';
|
} from 'interfaces';
|
||||||
import TransactionsByCustomers from './TransactionsByCustomers';
|
import TransactionsByCustomers from './TransactionsByCustomers';
|
||||||
|
|
||||||
export default class TransactionsByCustomersService implements ITransactionsByCustomersService {
|
export default class TransactionsByCustomersService
|
||||||
|
implements ITransactionsByCustomersService {
|
||||||
@Inject()
|
@Inject()
|
||||||
tenancy: TenancyService;
|
tenancy: TenancyService;
|
||||||
|
|
||||||
@@ -59,6 +60,10 @@ export default class TransactionsByCustomersService implements ITransactionsByCu
|
|||||||
key: 'base_currency',
|
key: 'base_currency',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const filter = {
|
||||||
|
...this.defaultQuery,
|
||||||
|
...query,
|
||||||
|
};
|
||||||
const customers = await Customer.query().orderBy('displayName');
|
const customers = await Customer.query().orderBy('displayName');
|
||||||
|
|
||||||
// Retrieve all journal transactions based on the given query.
|
// Retrieve all journal transactions based on the given query.
|
||||||
@@ -66,10 +71,15 @@ export default class TransactionsByCustomersService implements ITransactionsByCu
|
|||||||
fromDate: query.fromDate,
|
fromDate: query.fromDate,
|
||||||
toDate: query.toDate,
|
toDate: query.toDate,
|
||||||
});
|
});
|
||||||
|
// Transactions map by contact id.
|
||||||
|
const transactionsMap = new Map(
|
||||||
|
Object.entries(groupBy(transactions, 'contactId'))
|
||||||
|
);
|
||||||
// Transactions by customers data mapper.
|
// Transactions by customers data mapper.
|
||||||
const reportInstance = new TransactionsByCustomers(
|
const reportInstance = new TransactionsByCustomers(
|
||||||
customers,
|
customers,
|
||||||
new Map(Object.entries(groupBy(transactions, 'contactId'))),
|
transactionsMap,
|
||||||
|
filter,
|
||||||
baseCurrency
|
baseCurrency
|
||||||
);
|
);
|
||||||
const reportData = reportInstance.reportData();
|
const reportData = reportInstance.reportData();
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import * as R from 'ramda';
|
||||||
|
import { tableRowMapper, tableMapper } from 'utils';
|
||||||
|
import { ITransactionsByCustomersCustomer, ITableRow } from 'interfaces';
|
||||||
|
|
||||||
|
enum ROW_TYPE {
|
||||||
|
OPENING_BALANCE = 'OPENING_BALANCE',
|
||||||
|
CLOSING_BALANCE = 'CLOSING_BALANCE',
|
||||||
|
TRANSACTION = 'TRANSACTION',
|
||||||
|
CUSTOMER = 'CUSTOMER',
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class TransactionsByCustomersTableRows {
|
||||||
|
/**
|
||||||
|
* Retrieve the table rows of customer transactions.
|
||||||
|
* @param {ITransactionsByCustomersCustomer} customer
|
||||||
|
* @returns {ITableRow[]}
|
||||||
|
*/
|
||||||
|
private customerTransactions(
|
||||||
|
customer: ITransactionsByCustomersCustomer
|
||||||
|
): ITableRow[] {
|
||||||
|
const columns = [
|
||||||
|
{ key: 'date', accessor: 'date' },
|
||||||
|
{ key: 'account', accessor: 'account.name' },
|
||||||
|
{ key: 'referenceType', accessor: 'referenceType' },
|
||||||
|
{ key: 'transactionType', accessor: 'transactionType' },
|
||||||
|
{ key: 'credit', accessor: 'credit.formattedAmount' },
|
||||||
|
{ key: 'debit', accessor: 'debit.formattedAmount' },
|
||||||
|
];
|
||||||
|
return tableMapper(customer.transactions, columns, {
|
||||||
|
rowTypes: [ROW_TYPE.TRANSACTION]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the table row of customer opening balance.
|
||||||
|
* @param {ITransactionsByCustomersCustomer} customer
|
||||||
|
* @returns {ITableRow}
|
||||||
|
*/
|
||||||
|
private customerOpeningBalance(
|
||||||
|
customer: ITransactionsByCustomersCustomer
|
||||||
|
): ITableRow {
|
||||||
|
const columns = [
|
||||||
|
{ key: 'openingBalanceLabel', value: 'Opening balance' },
|
||||||
|
{
|
||||||
|
key: 'openingBalanceValue',
|
||||||
|
accessor: 'openingBalance.formattedAmount',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
return tableRowMapper(customer, columns, {
|
||||||
|
rowTypes: [ROW_TYPE.OPENING_BALANCE]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the table row of customer closing balance.
|
||||||
|
* @param {ITransactionsByCustomersCustomer} customer -
|
||||||
|
* @returns {ITableRow}
|
||||||
|
*/
|
||||||
|
private customerClosingBalance(
|
||||||
|
customer: ITransactionsByCustomersCustomer
|
||||||
|
): ITableRow {
|
||||||
|
const columns = [
|
||||||
|
{ key: 'openingBalanceLabel', value: 'Closing balance' },
|
||||||
|
{
|
||||||
|
key: 'openingBalanceValue',
|
||||||
|
accessor: 'closingBalance.formattedAmount',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
return tableRowMapper(customer, columns, {
|
||||||
|
rowTypes: [ROW_TYPE.CLOSING_BALANCE]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the table row of customer details.
|
||||||
|
* @param {ITransactionsByCustomersCustomer} customer -
|
||||||
|
* @returns {ITableRow[]}
|
||||||
|
*/
|
||||||
|
private customerDetails(
|
||||||
|
customer: ITransactionsByCustomersCustomer
|
||||||
|
) {
|
||||||
|
const columns = [{ key: 'customerName', accessor: 'customerName' }];
|
||||||
|
|
||||||
|
return {
|
||||||
|
...tableRowMapper(customer, columns, { rowTypes: [ROW_TYPE.CUSTOMER] }),
|
||||||
|
children: R.pipe(
|
||||||
|
R.append(this.customerOpeningBalance(customer)),
|
||||||
|
R.concat(this.customerTransactions(customer)),
|
||||||
|
R.append(this.customerClosingBalance(customer)),
|
||||||
|
)([]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the table rows of the customer section.
|
||||||
|
* @param {ITransactionsByCustomersCustomer} customer
|
||||||
|
* @returns {ITableRow[]}
|
||||||
|
*/
|
||||||
|
private customerRowsMapper(customer: ITransactionsByCustomersCustomer) {
|
||||||
|
return R.pipe(
|
||||||
|
R.append(this.customerDetails(customer)),
|
||||||
|
).bind(this)([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the table rows of transactions by customers report.
|
||||||
|
* @param {ITransactionsByCustomersCustomer[]} customers
|
||||||
|
* @returns {ITableRow[]}
|
||||||
|
*/
|
||||||
|
public tableRows(customers: ITransactionsByCustomersCustomer[]): ITableRow[] {
|
||||||
|
return R.map(this.customerRowsMapper.bind(this))(customers);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user