WIP: transactions by customers.

This commit is contained in:
a.bouhuolia
2021-05-07 23:49:38 +02:00
parent 5f2e90b234
commit b5ed7af7eb
16 changed files with 425 additions and 76 deletions

View File

@@ -694,4 +694,51 @@ export default class JournalPoster implements IJournalPoster {
getAccountEntries(accountId: number) {
return this.entries.filter((entry) => entry.account === accountId);
}
/**
* Retrieve total balnace of the given customer/vendor contact.
* @param {Number} accountId
* @param {Number} contactId
* @param {String} contactType
* @param {Date} closingDate
*/
getEntriesBalance(
entries
) {
let balance = 0;
entries.forEach((entry) => {
if (entry.credit) {
balance -= entry.credit;
}
if (entry.debit) {
balance += entry.debit;
}
});
return balance;
}
getContactEntries(
contactId: number,
openingDate: Date,
closingDate?: Date,
) {
const momentClosingDate = moment(closingDate);
const momentOpeningDate = moment(openingDate);
return this.entries.filter((entry) => {
if (
(closingDate &&
!momentClosingDate.isAfter(entry.date, 'day') &&
!momentClosingDate.isSame(entry.date, 'day')) ||
(openingDate &&
!momentOpeningDate.isBefore(entry.date, 'day') &&
!momentOpeningDate.isSame(entry.date)) ||
(entry.contactId === contactId)
) {
return true;
}
return false;
});
}
}

View File

@@ -0,0 +1,92 @@
import moment from 'moment';
import {
ILedger,
ILedgerEntry
} from 'interfaces';
export default class Ledger implements ILedger {
readonly entries: ILedgerEntry[];
/**
* Constructor method.
* @param {ILedgerEntry[]} entries
*/
constructor(entries: ILedgerEntry[]) {
this.entries = Ledger.mappingEntries(entries);
}
/**
* Filters the ledegr entries.
* @param callback
* @returns
*/
filter(callback) {
const entries = this.entries.filter(callback);
return new Ledger(entries);
}
getEntries(): ILedgerEntry[] {
return this.entries;
}
whereContactId(contactId: number): ILedger {
return this.filter((entry) => entry.contactId === contactId);
}
whereAccountId(accountId: number): ILedger {
return this.filter((entry) => entry.accountId === accountId);
}
whereFromDate(fromDate: Date | string): ILedger {
const fromDateParsed = moment(fromDate);
return this.filter(
(entry) =>
fromDateParsed.isBefore(entry.date) || fromDateParsed.isSame(entry.date)
);
}
whereToDate(toDate: Date | string): ILedger {
const toDateParsed = moment(toDate);
return this.filter(
(entry) =>
toDateParsed.isAfter(entry.date) || toDateParsed.isSame(entry.date)
);
}
/**
* Retrieve the closing balance of the entries.
* @returns {number}
*/
getClosingBalance() {
let closingBalance = 0;
this.entries.forEach((entry) => {
if (entry.accountNormal === 'credit') {
closingBalance += entry.credit ? entry.credit : -1 * entry.debit;
} else if (entry.accountNormal === 'debit') {
closingBalance += entry.debit ? entry.debit : -1 * entry.credit;
}
});
return closingBalance;
}
static mappingEntries(entries): ILedgerEntry[] {
return entries.map(this.mapEntry);
}
static mapEntry(entry): ILedgerEntry {
return {
credit: entry.credit,
debit: entry.debit,
accountNormal: entry.accountNormal,
accountId: entry.accountId,
contactId: entry.contactId,
date: entry.date,
transactionNumber: entry.transactionNumber,
referenceNumber: entry.referenceNumber,
}
}
}