mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
WIP transactions by vendors report.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import * as R from 'ramda';
|
||||
import { sumBy } from 'lodash';
|
||||
import FinancialSheet from '../FinancialSheet';
|
||||
import {
|
||||
ITransactionsByCustomersTransaction,
|
||||
ITransactionsByCustomersFilter,
|
||||
@@ -11,8 +10,9 @@ import {
|
||||
IAccountTransaction,
|
||||
ICustomer,
|
||||
} from 'interfaces';
|
||||
import TransactionsByContact from '../TransactionsByContact/TransactionsByContact';
|
||||
|
||||
export default class TransactionsByCustomers extends FinancialSheet {
|
||||
export default class TransactionsByCustomers extends TransactionsByContact {
|
||||
readonly customers: ICustomer[];
|
||||
readonly transactionsByContact: any;
|
||||
readonly filter: ITransactionsByCustomersFilter;
|
||||
@@ -40,53 +40,6 @@ export default class TransactionsByCustomers extends FinancialSheet {
|
||||
this.numberFormat = this.filter.numberFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Customer transaction mapper.
|
||||
* @param {any} transaction -
|
||||
* @return {Omit<ITransactionsByCustomersTransaction, 'runningBalance'>}
|
||||
*/
|
||||
private customerTransactionMapper(
|
||||
transaction
|
||||
): Omit<ITransactionsByCustomersTransaction, 'runningBalance'> {
|
||||
const currencyCode = 'USD';
|
||||
|
||||
return {
|
||||
credit: this.getCustomerAmount(transaction.credit, currencyCode),
|
||||
debit: this.getCustomerAmount(transaction.debit, currencyCode),
|
||||
currencyCode: 'USD',
|
||||
transactionNumber: transaction.transactionNumber,
|
||||
referenceNumber: transaction.referenceNumber,
|
||||
date: transaction.date,
|
||||
createdAt: transaction.createdAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Customer transactions mapper with running balance.
|
||||
* @param {number} openingBalance
|
||||
* @param {ITransactionsByCustomersTransaction[]} transactions
|
||||
* @returns {ITransactionsByCustomersTransaction[]}
|
||||
*/
|
||||
private customerTransactionRunningBalance(
|
||||
openingBalance: number,
|
||||
transactions: Omit<ITransactionsByCustomersTransaction, 'runningBalance'>[]
|
||||
): 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 };
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the customer transactions from the given customer id and opening balance.
|
||||
* @param {number} customerId - Customer id.
|
||||
@@ -100,38 +53,11 @@ export default class TransactionsByCustomers extends FinancialSheet {
|
||||
const transactions = this.transactionsByContact.get(customerId + '') || [];
|
||||
|
||||
return R.compose(
|
||||
R.curry(this.customerTransactionRunningBalance)(openingBalance),
|
||||
R.map(this.customerTransactionMapper.bind(this))
|
||||
R.curry(this.contactTransactionRunningBalance)(openingBalance),
|
||||
R.map(this.contactTransactionMapper.bind(this))
|
||||
).bind(this)(transactions);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -140,17 +66,17 @@ export default class TransactionsByCustomers extends FinancialSheet {
|
||||
private customerMapper(
|
||||
customer: ICustomer
|
||||
): ITransactionsByCustomersCustomer {
|
||||
const openingBalance = this.getCustomerOpeningBalance(1);
|
||||
const openingBalance = this.getContactOpeningBalance(1);
|
||||
const transactions = this.customerTransactions(customer.id, openingBalance);
|
||||
const closingBalance = this.getCustomerClosingBalance(transactions, 0);
|
||||
const closingBalance = this.getContactClosingBalance(transactions, 0);
|
||||
|
||||
return {
|
||||
customerName: customer.displayName,
|
||||
openingBalance: this.getCustomerAmount(
|
||||
openingBalance: this.getContactAmount(
|
||||
openingBalance,
|
||||
customer.currencyCode
|
||||
),
|
||||
closingBalance: this.getCustomerAmount(
|
||||
closingBalance: this.getContactAmount(
|
||||
closingBalance,
|
||||
customer.currencyCode
|
||||
),
|
||||
@@ -158,23 +84,6 @@ export default class TransactionsByCustomers extends FinancialSheet {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the customers sections of the report.
|
||||
* @param {ICustomer[]} customers
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as R from 'ramda';
|
||||
import { tableRowMapper, tableMapper } from 'utils';
|
||||
import { ITransactionsByCustomersCustomer, ITableRow } from 'interfaces';
|
||||
import TransactionsByContactsTableRows from '../TransactionsByContact/TransactionsByContactTableRows';
|
||||
|
||||
enum ROW_TYPE {
|
||||
OPENING_BALANCE = 'OPENING_BALANCE',
|
||||
@@ -9,102 +10,37 @@ enum ROW_TYPE {
|
||||
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]
|
||||
});
|
||||
}
|
||||
|
||||
export default class TransactionsByCustomersTableRows extends TransactionsByContactsTableRows {
|
||||
/**
|
||||
* Retrieve the table row of customer details.
|
||||
* @param {ITransactionsByCustomersCustomer} customer -
|
||||
* @param {ITransactionsByCustomersCustomer} customer -
|
||||
* @returns {ITableRow[]}
|
||||
*/
|
||||
private customerDetails(
|
||||
customer: ITransactionsByCustomersCustomer
|
||||
) {
|
||||
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)),
|
||||
R.append(this.contactOpeningBalance(customer)),
|
||||
R.concat(this.contactTransactions(customer)),
|
||||
R.append(this.contactClosingBalance(customer))
|
||||
)([]),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the table rows of the customer section.
|
||||
* @param {ITransactionsByCustomersCustomer} customer
|
||||
* @param {ITransactionsByCustomersCustomer} customer
|
||||
* @returns {ITableRow[]}
|
||||
*/
|
||||
private customerRowsMapper(customer: ITransactionsByCustomersCustomer) {
|
||||
return R.pipe(
|
||||
R.append(this.customerDetails(customer)),
|
||||
).bind(this)([]);
|
||||
return R.pipe(R.append(this.customerDetails(customer))).bind(this)([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the table rows of transactions by customers report.
|
||||
* @param {ITransactionsByCustomersCustomer[]} customers
|
||||
* @param {ITransactionsByCustomersCustomer[]} customers
|
||||
* @returns {ITableRow[]}
|
||||
*/
|
||||
public tableRows(customers: ITransactionsByCustomersCustomer[]): ITableRow[] {
|
||||
|
||||
Reference in New Issue
Block a user