fix: customer/vendor balance summary and transaction report.

This commit is contained in:
a.bouhuolia
2021-05-08 23:50:47 +02:00
parent 8b2f8d1007
commit 32ad5aa25d
6 changed files with 83 additions and 52 deletions

View File

@@ -45,16 +45,24 @@ export default class TransactionsByContact extends FinancialSheet {
*/
protected contactTransactionRunningBalance(
openingBalance: number,
accountNormal: 'credit' | 'debit',
transactions: Omit<ITransactionsByContactsTransaction, 'runningBalance'>[]
): any {
let _openingBalance = openingBalance;
return transactions.map(
(transaction: ITransactionsByContactsTransaction) => {
_openingBalance += transaction.debit.amount;
_openingBalance -= transaction.credit.amount;
_openingBalance +=
accountNormal === 'debit'
? transaction.debit.amount
: -1 * transaction.debit.amount;
const runningBalance = this.getContactAmount(
_openingBalance +=
accountNormal === 'credit'
? transaction.credit.amount
: -1 * transaction.credit.amount;
const runningBalance = this.getTotalAmountMeta(
_openingBalance,
transaction.currencyCode
);
@@ -71,6 +79,7 @@ export default class TransactionsByContact extends FinancialSheet {
*/
protected getContactClosingBalance(
customerTransactions: ITransactionsByContactsTransaction[],
contactNormal: 'credit' | 'debit',
openingBalance: number
): number {
const closingBalance = openingBalance;
@@ -78,7 +87,11 @@ export default class TransactionsByContact extends FinancialSheet {
const totalCredit = sumBy(customerTransactions, 'credit.amount');
const totalDebit = sumBy(customerTransactions, 'debit.amount');
return closingBalance + (totalDebit - totalCredit);
const total = contactNormal === 'debit' ?
totalDebit - totalCredit :
totalCredit - totalDebit;
return closingBalance + total;
}
/**
@@ -120,10 +133,7 @@ export default class TransactionsByContact extends FinancialSheet {
* @param {string} currencyCode - Currency code./
* @returns {ITransactionsByContactsAmount}
*/
protected getTotalAmountMeta(
amount: number,
currencyCode: string
) {
protected getTotalAmountMeta(amount: number, currencyCode: string) {
return {
amount,
formattedAmount: this.formatTotalNumber(amount, { currencyCode }),

View File

@@ -10,6 +10,8 @@ import {
import TransactionsByContact from '../TransactionsByContact/TransactionsByContact';
import Ledger from 'services/Accounting/Ledger';
const CUSTOMER_NORMAL = 'debit';
export default class TransactionsByCustomers extends TransactionsByContact {
readonly customers: ICustomer[];
readonly ledger: Ledger;
@@ -58,8 +60,8 @@ export default class TransactionsByCustomers extends TransactionsByContact {
const ledgerEntries = ledger.getEntries();
return R.compose(
R.curry(this.contactTransactionRunningBalance)(openingBalance),
return R.compose(
R.curry(this.contactTransactionRunningBalance)(openingBalance, 'debit'),
R.map(this.contactTransactionMapper.bind(this))
).bind(this)(ledgerEntries);
}
@@ -74,7 +76,10 @@ export default class TransactionsByCustomers extends TransactionsByContact {
): ITransactionsByCustomersCustomer {
const openingBalance = this.getContactOpeningBalance(customer.id);
const transactions = this.customerTransactions(customer.id, openingBalance);
const closingBalance = this.getContactClosingBalance(transactions, openingBalance);
const closingBalance = this.getCustomerClosingBalance(
transactions,
openingBalance
);
return {
customerName: customer.displayName,
@@ -90,6 +95,23 @@ export default class TransactionsByCustomers extends TransactionsByContact {
};
}
/**
* Retrieve the vendor closing balance from the given customer transactions.
* @param {ITransactionsByContactsTransaction[]} customerTransactions
* @param {number} openingBalance
* @returns
*/
private getCustomerClosingBalance(
customerTransactions: ITransactionsByCustomersTransaction[],
openingBalance: number
): number {
return this.getContactClosingBalance(
customerTransactions,
CUSTOMER_NORMAL,
openingBalance
);
}
/**
* Retrieve the customers sections of the report.
* @param {ICustomer[]} customers

View File

@@ -1,17 +1,20 @@
import * as R from 'ramda';
import { sumBy } from 'lodash';
import {
ITransactionsByContactsTransaction,
ITransactionsByVendorsFilter,
ITransactionsByVendorsTransaction,
ITransactionsByVendorsVendor,
ITransactionsByVendorsData,
ILedger,
INumberFormatQuery,
IVendor
IVendor,
} from 'interfaces';
import TransactionsByContact from "../TransactionsByContact/TransactionsByContact";
import TransactionsByContact from '../TransactionsByContact/TransactionsByContact';
export default class TransactionsByVendors extends TransactionsByContact{
const VENDOR_NORMAL = 'credit';
export default class TransactionsByVendors extends TransactionsByContact {
readonly contacts: IVendor[];
readonly transactionsByContact: any;
readonly filter: ITransactionsByVendorsFilter;
@@ -61,7 +64,7 @@ export default class TransactionsByVendors extends TransactionsByContact{
const openingEntries = openingBalanceLedger.getEntries();
return R.compose(
R.curry(this.contactTransactionRunningBalance)(openingBalance),
R.curry(this.contactTransactionRunningBalance)(openingBalance, 'credit'),
R.map(this.contactTransactionMapper.bind(this))
).bind(this)(openingEntries);
}
@@ -71,12 +74,13 @@ export default class TransactionsByVendors extends TransactionsByContact{
* @param {IVendor} vendor
* @returns {ITransactionsByVendorsVendor}
*/
private vendorMapper(
vendor: IVendor
): ITransactionsByVendorsVendor {
private vendorMapper(vendor: IVendor): ITransactionsByVendorsVendor {
const openingBalance = this.getContactOpeningBalance(vendor.id);
const transactions = this.vendorTransactions(vendor.id, openingBalance);
const closingBalance = this.getContactClosingBalance(transactions, openingBalance);
const closingBalance = this.getVendorClosingBalance(
transactions,
openingBalance
);
return {
vendorName: vendor.displayName,
@@ -92,17 +96,30 @@ export default class TransactionsByVendors extends TransactionsByContact{
};
}
/**
* Retrieve the vendor closing balance from the given customer transactions.
* @param {ITransactionsByContactsTransaction[]} customerTransactions
* @param {number} openingBalance
* @returns
*/
private getVendorClosingBalance(
customerTransactions: ITransactionsByContactsTransaction[],
openingBalance: number
) {
return this.getContactClosingBalance(
customerTransactions,
VENDOR_NORMAL,
openingBalance
);
}
/**
* Retrieve the vendors sections of the report.
* @param {IVendor[]} vendors
* @returns {ITransactionsByVendorsVendor[]}
*/
private vendorsMapper(
vendors: IVendor[]
): ITransactionsByVendorsVendor[] {
return R.compose(R.map(this.vendorMapper.bind(this))).bind(this)(
vendors
);
private vendorsMapper(vendors: IVendor[]): ITransactionsByVendorsVendor[] {
return R.compose(R.map(this.vendorMapper.bind(this))).bind(this)(vendors);
}
/**
@@ -119,4 +136,4 @@ export default class TransactionsByVendors extends TransactionsByContact{
public reportColumns() {
return [];
}
}
}

View File

@@ -32,8 +32,8 @@ export default class TransactionsByVendorsTableRows extends TransactionsByContac
R.when(
R.always(vendor.transactions.length > 0),
R.pipe(
R.append(this.contactOpeningBalance(vendor)),
R.concat(this.contactTransactions(vendor))
R.concat(this.contactTransactions(vendor)),
R.prepend(this.contactOpeningBalance(vendor)),
)
),
R.append(this.contactClosingBalance(vendor))