2018-03-05 16:45:40 +00:00
|
|
|
const frappe = require('frappejs');
|
|
|
|
|
|
|
|
module.exports = class LedgerPosting {
|
2018-07-12 11:35:24 +00:00
|
|
|
constructor({ reference, party, date, description }) {
|
|
|
|
Object.assign(this, arguments[0]);
|
|
|
|
this.entries = [];
|
|
|
|
this.entryMap = {};
|
|
|
|
}
|
2018-03-05 16:45:40 +00:00
|
|
|
|
2018-07-12 11:35:24 +00:00
|
|
|
debit(account, amount, referenceType, referenceName) {
|
|
|
|
const entry = this.getEntry(account, referenceType, referenceName);
|
|
|
|
entry.debit += amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
credit(account, amount, referenceType, referenceName) {
|
|
|
|
const entry = this.getEntry(account, referenceType, referenceName);
|
|
|
|
entry.credit += amount;
|
|
|
|
}
|
2018-03-05 16:45:40 +00:00
|
|
|
|
2018-07-12 11:35:24 +00:00
|
|
|
getEntry(account, referenceType, referenceName) {
|
|
|
|
if (!this.entryMap[account]) {
|
|
|
|
const entry = {
|
|
|
|
account: account,
|
|
|
|
party: this.party || '',
|
|
|
|
date: this.date || this.reference.date,
|
|
|
|
referenceType: referenceType || this.reference.doctype,
|
|
|
|
referenceName: referenceName || this.reference.name,
|
|
|
|
description: this.description,
|
|
|
|
debit: 0,
|
|
|
|
credit: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
this.entries.push(entry);
|
|
|
|
this.entryMap[account] = entry;
|
2018-03-05 16:45:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 11:35:24 +00:00
|
|
|
return this.entryMap[account];
|
|
|
|
}
|
2018-03-05 16:45:40 +00:00
|
|
|
|
2018-07-12 11:35:24 +00:00
|
|
|
async post() {
|
|
|
|
this.validateEntries();
|
|
|
|
await this.insertEntries();
|
|
|
|
}
|
2018-03-05 16:45:40 +00:00
|
|
|
|
2018-07-12 11:35:24 +00:00
|
|
|
async postReverse() {
|
|
|
|
this.validateEntries();
|
|
|
|
let temp;
|
|
|
|
for (let entry of this.entries) {
|
|
|
|
temp = entry.debit;
|
|
|
|
entry.debit = entry.credit;
|
|
|
|
entry.credit = temp;
|
2018-03-05 16:45:40 +00:00
|
|
|
}
|
2018-07-12 11:35:24 +00:00
|
|
|
await this.insertEntries();
|
|
|
|
}
|
2018-03-05 16:45:40 +00:00
|
|
|
|
2018-07-12 11:35:24 +00:00
|
|
|
validateEntries() {
|
|
|
|
let debit = 0;
|
|
|
|
let credit = 0;
|
|
|
|
let debitAccounts = [];
|
|
|
|
let creditAccounts = [];
|
2018-03-05 16:45:40 +00:00
|
|
|
|
2018-07-12 11:35:24 +00:00
|
|
|
for (let entry of this.entries) {
|
|
|
|
debit += entry.debit;
|
|
|
|
credit += entry.credit;
|
|
|
|
if (debit) {
|
|
|
|
debitAccounts.push(entry.account);
|
|
|
|
} else {
|
|
|
|
creditAccounts.push(entry.account);
|
|
|
|
}
|
2018-03-05 16:45:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 11:35:24 +00:00
|
|
|
if (debit !== credit) {
|
2018-09-26 14:23:53 +00:00
|
|
|
throw new frappe.errors.ValidationError(frappe._('Debit {0} must be equal to Credit {1}', [debit, credit]));
|
2018-03-05 16:45:40 +00:00
|
|
|
}
|
2018-07-12 11:35:24 +00:00
|
|
|
}
|
2018-03-05 16:45:40 +00:00
|
|
|
|
2018-07-12 11:35:24 +00:00
|
|
|
async insertEntries() {
|
|
|
|
for (let entry of this.entries) {
|
|
|
|
let entryDoc = frappe.newDoc({
|
|
|
|
doctype: 'AccountingLedgerEntry'
|
|
|
|
});
|
|
|
|
Object.assign(entryDoc, entry);
|
|
|
|
await entryDoc.insert();
|
2018-03-05 16:45:40 +00:00
|
|
|
}
|
2018-07-12 11:35:24 +00:00
|
|
|
}
|
|
|
|
};
|