From 5ad858ede74a5a35de12be1ef4294f214b42d473 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Fri, 31 Dec 2021 12:51:08 +0530 Subject: [PATCH] fix: knex queries --- models/doctype/Party/PartyServer.js | 22 +++++++++++++--------- src/pages/Dashboard/Expenses.vue | 3 ++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/models/doctype/Party/PartyServer.js b/models/doctype/Party/PartyServer.js index 93b9d070..bd53c5f4 100644 --- a/models/doctype/Party/PartyServer.js +++ b/models/doctype/Party/PartyServer.js @@ -1,5 +1,5 @@ -import BaseDocument from 'frappejs/model/document'; import frappe from 'frappejs'; +import BaseDocument from 'frappejs/model/document'; export default class PartyServer extends BaseDocument { beforeInsert() { @@ -8,8 +8,8 @@ export default class PartyServer extends BaseDocument { method: 'show-dialog', args: { title: 'Invalid Entry', - message: 'Select a single party type.' - } + message: 'Select a single party type.', + }, }); throw new Error(); } @@ -23,14 +23,18 @@ export default class PartyServer extends BaseDocument { let isCustomer = this.customer; let doctype = isCustomer ? 'SalesInvoice' : 'PurchaseInvoice'; let partyField = isCustomer ? 'customer' : 'supplier'; - let { totalOutstanding } = await frappe.db.knex - .sum({ totalOutstanding: 'outstandingAmount' }) + + const outstandingAmounts = await frappe.db.knex + .select('outstandingAmount') .from(doctype) .where('submitted', 1) - .andWhere(partyField, this.name) - .first(); + .andWhere(partyField, this.name); - await this.set('outstandingAmount', this.round(totalOutstanding)); + 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(); } -}; +} diff --git a/src/pages/Dashboard/Expenses.vue b/src/pages/Dashboard/Expenses.vue index 0e67bca9..efc823e5 100644 --- a/src/pages/Dashboard/Expenses.vue +++ b/src/pages/Dashboard/Expenses.vue @@ -84,9 +84,10 @@ export default { .select('name') .from('Account') .where('rootType', 'Expense'); + let topExpenses = await frappe.db.knex .select({ - total: frappe.db.knex.raw('sum(??) - sum(??)', ['debit', 'credit']), + total: frappe.db.knex.raw('sum(cast(?? as real)) - sum(cast(?? as real))', ['debit', 'credit']), }) .select('account') .from('AccountingLedgerEntry')