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

fix: Update party outstanding amount

This commit is contained in:
Faris Ansari 2019-12-26 19:15:25 +05:30
parent 061c405212
commit dc160a4232
3 changed files with 25 additions and 1 deletions

View File

@ -18,4 +18,19 @@ module.exports = class PartyServer extends BaseDocument {
this.gstin = ''; this.gstin = '';
} }
} }
async updateOutstandingAmount() {
let isCustomer = this.customer;
let doctype = isCustomer ? 'SalesInvoice' : 'PurchaseInvoice';
let partyField = isCustomer ? 'customer' : 'supplier';
let { totalOutstanding } = await frappe.db.knex
.sum({ totalOutstanding: 'outstandingAmount' })
.from(doctype)
.where('submitted', 1)
.andWhere(partyField, this.name)
.first();
await this.set('outstandingAmount', this.round(totalOutstanding));
await this.update();
}
}; };

View File

@ -42,9 +42,12 @@ module.exports = class PaymentServer extends BaseDocument {
`Payment amount (${this.amount}) should be greater than 0 and less than Outstanding amount (${outstandingAmount})` `Payment amount (${this.amount}) should be greater than 0 and less than Outstanding amount (${outstandingAmount})`
); );
} else { } else {
// update outstanding amounts in invoice and party
let newOutstanding = outstandingAmount - this.amount; let newOutstanding = outstandingAmount - this.amount;
await referenceDoc.set('outstandingAmount', newOutstanding); await referenceDoc.set('outstandingAmount', newOutstanding);
await referenceDoc.update(); await referenceDoc.update();
let party = await frappe.getDoc('Party', this.party);
await party.updateOutstandingAmount();
} }
} }
} }

View File

@ -24,15 +24,21 @@ module.exports = {
await entries.validateEntries(); await entries.validateEntries();
}, },
async beforeSubmit() { async afterSubmit() {
// post ledger entries
const entries = await this.getPosting(); const entries = await this.getPosting();
await entries.post(); await entries.post();
// update outstanding amounts
await frappe.db.setValue( await frappe.db.setValue(
this.doctype, this.doctype,
this.name, this.name,
'outstandingAmount', 'outstandingAmount',
this.baseGrandTotal this.baseGrandTotal
); );
let party = await frappe.getDoc('Party', this.customer || this.supplier);
await party.updateOutstandingAmount();
}, },
async afterRevert() { async afterRevert() {