WIP transactions by vendors report.

This commit is contained in:
a.bouhuolia
2021-05-06 04:53:33 +02:00
parent c57f2de970
commit 9b5ccf9248
13 changed files with 620 additions and 202 deletions

View File

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