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

@@ -51,18 +51,9 @@ export const useCustomersTransactionsColumns = () => {
width: 120, width: 120,
textOverview: true, textOverview: true,
}, },
{
Header: formatMessage({ id: 'transaction_number' }),
accessor: 'cells[4].value',
width: getColumnWidth(tableRows, 'cells[4].value', {
minWidth: 140,
magicSpacing: 10,
}),
textOverview: true,
},
{ {
Header: formatMessage({ id: 'credit' }), Header: formatMessage({ id: 'credit' }),
accessor: 'cells[5].value', accessor: 'cells[4].value',
className: 'credit', className: 'credit',
textOverview: true, textOverview: true,
width: getColumnWidth(tableRows, 'cells[5].value', { width: getColumnWidth(tableRows, 'cells[5].value', {
@@ -72,7 +63,7 @@ export const useCustomersTransactionsColumns = () => {
}, },
{ {
Header: formatMessage({ id: 'debit' }), Header: formatMessage({ id: 'debit' }),
accessor: 'cells[6].value', accessor: 'cells[5].value',
className: 'debit', className: 'debit',
textOverview: true, textOverview: true,
width: getColumnWidth(tableRows, 'cells[6].value', { width: getColumnWidth(tableRows, 'cells[6].value', {
@@ -82,7 +73,7 @@ export const useCustomersTransactionsColumns = () => {
}, },
{ {
Header: formatMessage({ id: 'running_balance' }), Header: formatMessage({ id: 'running_balance' }),
accessor: 'cells[7].value', accessor: 'cells[6].value',
className: 'running_balance', className: 'running_balance',
textOverview: true, textOverview: true,
width: getColumnWidth(tableRows, 'cells[7].value', { width: getColumnWidth(tableRows, 'cells[7].value', {

View File

@@ -51,18 +51,9 @@ export const useVendorsTransactionsColumns = () => {
textOverview: true, textOverview: true,
width: 120, width: 120,
}, },
{
Header: formatMessage({ id: 'transaction_number' }),
accessor: 'cells[4].value',
width: getColumnWidth(tableRows, 'cells[4].value', {
minWidth: 140,
magicSpacing: 10,
}),
textOverview: true,
},
{ {
Header: formatMessage({ id: 'credit' }), Header: formatMessage({ id: 'credit' }),
accessor: 'cells[5].value', accessor: 'cells[4].value',
className: 'credit', className: 'credit',
textOverview: true, textOverview: true,
width: getColumnWidth(tableRows, 'cells[5].value', { width: getColumnWidth(tableRows, 'cells[5].value', {
@@ -72,7 +63,7 @@ export const useVendorsTransactionsColumns = () => {
}, },
{ {
Header: formatMessage({ id: 'debit' }), Header: formatMessage({ id: 'debit' }),
accessor: 'cells[6].value', accessor: 'cells[5].value',
className: 'debit', className: 'debit',
textOverview: true, textOverview: true,
width: getColumnWidth(tableRows, 'cells[6].value', { width: getColumnWidth(tableRows, 'cells[6].value', {
@@ -82,7 +73,7 @@ export const useVendorsTransactionsColumns = () => {
}, },
{ {
Header: formatMessage({ id: 'running_balance' }), Header: formatMessage({ id: 'running_balance' }),
accessor: 'cells[7].value', accessor: 'cells[6].value',
className: 'running_balance', className: 'running_balance',
textOverview: true, textOverview: true,
width: getColumnWidth(tableRows, 'cells[7].value', { width: getColumnWidth(tableRows, 'cells[7].value', {

View File

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

View File

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

View File

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

View File

@@ -32,8 +32,8 @@ export default class TransactionsByVendorsTableRows extends TransactionsByContac
R.when( R.when(
R.always(vendor.transactions.length > 0), R.always(vendor.transactions.length > 0),
R.pipe( 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)) R.append(this.contactClosingBalance(vendor))