2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00

eslint fixes

This commit is contained in:
Faris Ansari 2018-07-12 17:05:24 +05:30
parent bb5dc896c8
commit 1a5d01ffeb
2 changed files with 95 additions and 94 deletions

View File

@ -1,83 +1,86 @@
const frappe = require('frappejs');
module.exports = class LedgerPosting {
constructor({reference, party, date, description}) {
Object.assign(this, arguments[0]);
this.entries = [];
this.entryMap = {};
constructor({ reference, party, date, description }) {
Object.assign(this, arguments[0]);
this.entries = [];
this.entryMap = {};
}
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;
}
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;
}
debit(account, amount, referenceType, referenceName) {
const entry = this.getEntry(account, referenceType, referenceName);
entry.debit += amount;
return this.entryMap[account];
}
async post() {
this.validateEntries();
await this.insertEntries();
}
async postReverse() {
this.validateEntries();
let temp;
for (let entry of this.entries) {
temp = entry.debit;
entry.debit = entry.credit;
entry.credit = temp;
}
await this.insertEntries();
}
validateEntries() {
let debit = 0;
let credit = 0;
let debitAccounts = [];
let creditAccounts = [];
for (let entry of this.entries) {
debit += entry.debit;
credit += entry.credit;
if (debit) {
debitAccounts.push(entry.account);
} else {
creditAccounts.push(entry.account);
}
}
credit(account, amount, referenceType, referenceName) {
const entry = this.getEntry(account, referenceType, referenceName);
entry.credit += amount;
if (debit !== credit) {
throw frappe.errors.ValidationError(frappe._('Debit {0} must be equal to Credit {1}', [debit, credit]));
}
}
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;
}
return this.entryMap[account];
async insertEntries() {
for (let entry of this.entries) {
let entryDoc = frappe.newDoc({
doctype: 'AccountingLedgerEntry'
});
Object.assign(entryDoc, entry);
await entryDoc.insert();
}
async post() {
this.validateEntries();
await this.insertEntries();
}
async postReverse() {
this.validateEntries();
let temp;
for (let entry of this.entries) {
temp = entry.debit;
entry.debit = entry.credit;
entry.credit = temp;
}
await this.insertEntries();
}
validateEntries() {
let debit = 0, credit = 0;
let debitAccounts = [], creditAccounts = [];
for (let entry of this.entries) {
debit += entry.debit;
credit += entry.credit;
if (debit) {
debitAccounts.push(entry.account);
} else {
creditAccounts.push(entry.account);
}
}
if (debit !== credit) {
throw frappe.errors.ValidationError(frappe._("Debit {0} must be equal to Credit {1}", [debit, credit]));
}
}
async insertEntries() {
for (let entry of this.entries) {
let entryDoc = frappe.newDoc({
doctype: 'AccountingLedgerEntry'
});
Object.assign(entryDoc, entry);
await entryDoc.insert();
}
}
}
}
};

View File

@ -1,30 +1,28 @@
const Invoice = require('./InvoiceDocument');
const frappe = require('frappejs');
const LedgerPosting = require('../../../accounting/ledgerPosting');
module.exports = class InvoiceServer extends Invoice {
getPosting() {
let entries = new LedgerPosting({reference: this, party: this.customer});
entries.debit(this.account, this.grandTotal);
for (let item of this.items) {
entries.credit(item.account, item.amount);
}
if (this.taxes) {
for (let tax of this.taxes) {
entries.credit(tax.account, tax.amount);
}
}
return entries;
getPosting() {
let entries = new LedgerPosting({ reference: this, party: this.customer });
entries.debit(this.account, this.grandTotal);
for (let item of this.items) {
entries.credit(item.account, item.amount);
}
async afterSubmit() {
await this.getPosting().post();
if (this.taxes) {
for (let tax of this.taxes) {
entries.credit(tax.account, tax.amount);
}
}
return entries;
}
async afterRevert() {
await this.getPosting().postReverse();
}
}
async afterSubmit() {
await this.getPosting().post();
}
async afterRevert() {
await this.getPosting().postReverse();
}
};