2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00
books/models/baseModels/Party/PartyServer.js

38 lines
1.0 KiB
JavaScript

import frappe from 'frappe';
import Document from 'frappe/model/document';
export default class PartyServer extends Document {
beforeInsert() {
if (this.customer && this.supplier) {
throw new Error('Select a single party type.');
}
if (!this.customer && !this.supplier) {
this.supplier = 1;
}
if (this.gstin && ['Unregistered', 'Consumer'].includes(this.gstType)) {
this.gstin = '';
}
}
async updateOutstandingAmount() {
let isCustomer = this.customer;
let doctype = isCustomer ? 'SalesInvoice' : 'PurchaseInvoice';
const outstandingAmounts = (
await frappe.db.getAllRaw(doctype, {
fields: ['outstandingAmount', 'party'],
filters: { submitted: true },
})
).filter(({ party }) => party === this.name);
const totalOutstanding = outstandingAmounts
.map(({ outstandingAmount }) => frappe.pesa(outstandingAmount))
.reduce((a, b) => a.add(b), frappe.pesa(0));
await this.set('outstandingAmount', totalOutstanding);
await this.update();
}
}